I need to use Go to get the file permissions of a given file (specifically the SUID bit). Here are the permissions of an example file.
$ touch example_file.test
$ chmod 7777 example_file.test
$ ls -ltra example_file.test
-rwsrwsrwt 1 luke users 0 Feb 25 21:53 example_file.test
$ stat -c "%a %n" example_file.test
7777 example_file.test
Here is a tiny program illustrating the issue.
func main() {
info, _ := os.Stat("example_file.test")
fmt.Println(info.Mode().String()) // ugtrwxrwxrwx
fmt.Println(info.Mode().Perm().String()) // -rwxrwxrwx
fmt.Printf("permissions: %#o\n", info.Mode().Perm()) // permissions: 0777
}
The Go results are inconsistent because ugtrwxrwxrwx != 0777.
The following quote from the documentation indicates that this might be a cross-platform compatibility issue.
A FileMode represents a file's mode and permission bits. The bits have the same definition on all systems, so that information about files can be moved from one system to another portably. Not all bits apply to all systems. The only required bit is ModeDir for directories.
type FileMode uint32 The defined file mode bits are the most significant bits of the FileMode. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
Is this a limitation of Go?