[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[suggestion] File.chown to take user and group *names*

Brian Candler

2/28/2005 5:58:00 PM

Just a simple suggestion, but it would be really nice if File.chown could
take user and group *names* as well as numeric IDs.

It would simplify this:

File.chown(Etc.getpwnam("foo").uid, Etc.getgrnam("bar").gid, filename)

to this:

File.chown("foo", "bar", filename)

Seem reasonable?? There doesn't seem to be a chown in fileutils.rb either.

Regards,

Brian.


2 Answers

djberg96

3/2/2005 5:04:00 AM

0

Brian Candler wrote:
> Just a simple suggestion, but it would be really nice if File.chown
could
> take user and group *names* as well as numeric IDs.
>
> It would simplify this:
>
> File.chown(Etc.getpwnam("foo").uid, Etc.getgrnam("bar").gid,
filename)
>
> to this:
>
> File.chown("foo", "bar", filename)
>
> Seem reasonable?? There doesn't seem to be a chown in fileutils.rb
either.
>
> Regards,
>
> Brian.

Generally speaking I like this idea, although it means doing type
checking in the underlying C code, something I'm not generally a big
fan of. It will also fail miserably on Win32, unless you mandate the
win32-etc package.

Regards,

Dan

E F van de Laar

3/2/2005 9:25:00 AM

0

Brian Candler wrote:
> Just a simple suggestion, but it would be really nice if File.chown could
> take user and group *names* as well as numeric IDs.
>
> It would simplify this:
>
> File.chown(Etc.getpwnam("foo").uid, Etc.getgrnam("bar").gid, filename)
>
> to this:
>
> File.chown("foo", "bar", filename)
>
> Seem reasonable?? There doesn't seem to be a chown in fileutils.rb either.

I've written some code like this before too and your suggestion is a
good one IMHO. The relevant code is implemented in file.c. I'm not sure
if it would be a good idea to pollute this code with these kind of
lookups though. Another thing to consider is how other platforms handle
this, i.e. Windows.

Does this work on win32? Etc.getpwnam("Administrator").uid

In the mean time maybe something like this...

def File.my_chown(o, g, file)
case o
when Numeric
uid = o
when String
require 'Etc'
uid = Etc.getpwnam(o).uid
end

case g
when Numeric
gid = g
when String
require 'Etc'
gid = Etc.getgrnam(g).gid
end

File.chown(uid, gid, file)
end

Cheers,

Emiel
--
Emiel van de Laar