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

B

Brian Candler

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.
 
D

Daniel Berger

Brian said:
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

Emiel van de Laar

Brian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top