Getting Uid/Gid for a folder

93 views
Skip to first unread message

Serguei Bezverkhi (sbezverk)

unread,
Jul 31, 2017, 10:23:22 PM7/31/17
to golang-nuts
Hello,

I am having trouble getting GID for a folder. I checked os.Stat api but it returns only permissions, there is no Uid or Gid.

stat /mnt/disks/vol2
File: ‘/mnt/disks/vol2’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: fc12h/64530d Inode: 64 Links: 2


Access: (2775/drwxrwsr-x) Uid: ( 0/ root) Gid: ( 2323/ UNKNOWN) < ------ I need to get this..

Greatly appreciate some pointers.
Thanks a lot
Serguei


Ian Lance Taylor

unread,
Jul 31, 2017, 10:46:44 PM7/31/17
to Serguei Bezverkhi (sbezverk), golang-nuts
UID and GID are system dependent. Here is sample code for GNU/Linux.

Ian

package main

import (
"fmt"
"os"
"syscall"
)

func main() {
fi, err := os.Stat(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
sys := fi.Sys().(*syscall.Stat_t)
fmt.Println("uid:", sys.Uid)
fmt.Println("gid:", sys.Gid)
}

charle...@gmail.com

unread,
Aug 8, 2017, 5:46:21 PM8/8/17
to golang-nuts, sbez...@cisco.com
WOW this is exactly what I was looking for - Thanks sbez
Reply all
Reply to author
Forward
0 new messages