Golang File Permission Bit Operators.
Anywhere you can specify a file mode, you can use the permbits to add together the permissions you want.
(This test works because it's likely the umask is 022 which will mean the first MkdirAll will actually create a directory with 0750 permissions).
testPath := "./test"
mode := permbits.UserAll+permbits.GroupAll
if err := os.MkdirAll(testPath, mode); err != nil {
log.Fatal(err)
}
if st, err := os.Stat(testPath); err == nil {
if !permbits.Is(st.Mode(), mode) {
log.Printf("Updating mode for %s (was %o)", testPath, st.Mode())
os.Chmod(testPath, mode)
} else {
log.Printf("Test path mode is correct: %s (%o)", testPath, st.Mode())
}
}$ go run main.go
2021/07/09 13:12:53 Updating mode for ./test (was 20000000750)Checks whether mode contains all the bits set in is.
Takes a base mode and ensures all wanted permission bits are set.
mode := permbits.Force(0o111, permbits.UserReadWriteExecute)
// mode is now 0o711Parses symbolic mode strings (a subset of chmod syntax) into an os.FileMode.
Supports references ugoa, operators +-=, and modes rwx.
mode, err := permbits.FromString("u=rw,g=r,o=")
// mode is 0o640Wrapper around FromString that panics on invalid input.
| Mode | Constant |
|---|---|
| u+r | permbits.UserRead |
| u+w | permbits.UserWrite |
| u+x | permbits.UserExecute |
| g+r | permbits.GroupRead |
| g+w | permbits.GroupWrite |
| g+x | permbits.GroupExecute |
| o+r | permbits.OtherRead |
| o+w | permbits.OtherWrite |
| o+x | permbits.OtherExecute |
| Mode | Constant |
|---|---|
| u+r+w | permbits.UserReadWrite |
| u+r+w+x | permbits.UserReadWriteExecute or permbits.UserAll |
| g+r+w | permbits.GroupReadWrite |
| g+r+w+x | permbits.GroupReadWriteExecute or permbits.GroupAll |
| o+r+w | permbits.OtherReadWrite |
| o+r+w+x | permbits.OtherReadWriteExecute or permbits.OtherAll |