Get file owner/group

3,292 views
Skip to first unread message

Torrance Hodgson

unread,
Feb 19, 2014, 6:47:14 PM2/19/14
to golan...@googlegroups.com
Hi all, I must be missing something simple here! :)

How do I find the owner and group of a file? Either as a integer or name?

I can see all sorts of methods in package os that allow me to set both the owner and group, but not to read it. Is it buried somewhere in os.FileInfo or os.FileMode and I'm simply missing it?

And as an aside, is there a nice way to translate a user or group name to its integer value?

Thanks!
Torrance


Caleb Spare

unread,
Feb 19, 2014, 6:53:45 PM2/19/14
to Torrance Hodgson, golang-nuts
On Wed, Feb 19, 2014 at 3:47 PM, Torrance Hodgson <torra...@gmail.com> wrote:
Hi all, I must be missing something simple here! :)

How do I find the owner and group of a file? Either as a integer or name?

I can see all sorts of methods in package os that allow me to set both the owner and group, but not to read it. Is it buried somewhere in os.FileInfo

or os.FileMode and I'm simply missing it?

And as an aside, is there a nice way to translate a user or group name to its integer value?

Thanks!
Torrance


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Torrance Hodgson

unread,
Feb 19, 2014, 7:01:08 PM2/19/14
to Caleb Spare, golang-nuts

On 20/02/2014, at 12:53 pm, Caleb Spare <ces...@gmail.com> wrote:

Thanks, i saw that reply earlier but thought there must be a cross-platform way to do it.

Why can we change file ownership/group safely across platforms, but not read ownership/group information?

Michael Hofmann

unread,
Feb 21, 2014, 1:55:54 PM2/21/14
to golan...@googlegroups.com, Caleb Spare
On Thursday, February 20, 2014 1:01:08 AM UTC+1, Torrance Hodgson wrote:
Why can we change file ownership/group safely across platforms [...]

You can't, actually. os.Chown doesn't work on Windows (and neither on Plan9 I believe).
Which leads to the question why this function is part of package os. Shouldn't it rather be in syscall? 

Gyepi SAM

unread,
Feb 22, 2014, 2:22:07 AM2/22/14
to Michael Hofmann, golan...@googlegroups.com, Caleb Spare
chown is part of the posix standard.

I know some Windows versions were posix compliant, but I don't know about
more recent versions; I stopped at NT and 2K, which were.

You're right about Plan9.

-Gyepi

Michael Hofmann

unread,
Feb 22, 2014, 8:40:01 AM2/22/14
to golan...@googlegroups.com

On Saturday, February 22, 2014 8:22:07 AM UTC+1, Gyepi SAM wrote:
chown is part of the posix standard.

I know some Windows versions were posix compliant, but I don't know about
more recent versions; I stopped at NT and 2K, which were.

Anyway, package os is meant to be platform independent and thus shouldn't depend on POSIX functionality. To quote the package documentation:
"The os interface is intended to be uniform across all operating systems. Features not generally available appear in the system-specific package syscall."

Chmod isn't generally available and neither are Getuid and friends and maybe some other functions that I've overlooked. I know this isn't terribly important and couldn't be changed before Go 2.0 anyway. I think it's just important to know that package os is not as platform independent as it's documentation suggests.
Reply all
Reply to author
Forward
0 new messages