Created
March 2, 2018 12:26
-
-
Save mrclmr/a76400ac00db4be9585be72855ca5dce to your computer and use it in GitHub Desktop.
File modes in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| ) | |
| type NamedFileMode struct { | |
| name string | |
| fm os.FileMode | |
| } | |
| func main() { | |
| fileModes := []NamedFileMode{ | |
| {"ModeDir // d: is a directory", os.ModeDir}, | |
| {"ModeAppend // a: append-only", os.ModeAppend}, | |
| {"ModeExclusive // l: exclusive use", os.ModeExclusive}, | |
| {"ModeTemporary // T: temporary file; Plan 9 only", os.ModeTemporary}, | |
| {"ModeSymlink // L: symbolic link", os.ModeSymlink}, | |
| {"ModeDevice // D: device file", os.ModeDevice}, | |
| {"ModeNamedPipe // p: named pipe (FIFO)", os.ModeNamedPipe}, | |
| {"ModeSocket // S: Unix domain socket", os.ModeSocket}, | |
| {"ModeSetuid // u: setuid", os.ModeSetuid}, | |
| {"ModeSetgid // g: setgid", os.ModeSetgid}, | |
| {"ModeCharDevice // c: Unix character device, when ModeDevice is set", os.ModeCharDevice}, | |
| {"ModeSticky // t: sticky", os.ModeSticky}, | |
| } | |
| info, err := os.Stdin.Stat() | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Printf("%02d %032b %s\n", 0, info.Mode(), "ModeInput") | |
| fmt.Println() | |
| for pos, nfm := range fileModes { | |
| fmt.Printf("%02d %032b %s\n", pos+1, nfm.fm, nfm.name) | |
| } | |
| fmt.Println() | |
| for pos, nfm := range fileModes { | |
| if info.Mode()&nfm.fm == nfm.fm { | |
| fmt.Printf("%02d %s\n", pos+1, nfm.name) | |
| } | |
| } | |
| fmt.Println() | |
| fmt.Println("Args") | |
| for pos, arg := range os.Args { | |
| fmt.Printf("%02d %s\n", pos, arg) | |
| } | |
| fmt.Println() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment