Skip to content

Golang File Permission Bit Operators

License

Notifications You must be signed in to change notification settings

na4ma4/go-permbits

Repository files navigation

go-permbits

Golang File Permission Bit Operators.

CI GoDoc

Usage

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)

Constants

Mode Constant
u+a permbits.UserAll
u+r permbits.UserRead
u+w permbits.UserWrite
u+x permbits.UserExecute
g+a permbits.GroupAll
g+r permbits.GroupRead
g+w permbits.GroupWrite
g+x permbits.GroupExecute
o+a permbits.OtherAll
o+r permbits.OtherRead
o+w permbits.OtherWrite
o+x permbits.OtherExecute