-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermbits.go
93 lines (81 loc) · 2.8 KB
/
permbits.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package permbits
import (
"math/bits"
"os"
)
const (
// UserRead is read permissions for the user.
UserRead os.FileMode = 0o400
// UserWrite is write permissions for the user.
UserWrite os.FileMode = 0o200
// UserExecute is execute permissions for the user.
UserExecute os.FileMode = 0o100
)
const (
// UserReadWrite is a helper combination of read and write permissions for the user.
UserReadWrite os.FileMode = UserRead + UserWrite
// UserReadWriteExecute is a helper combination of read, write and execute permissions for the user.
UserReadWriteExecute os.FileMode = UserRead + UserWrite + UserExecute
// UserAll is all permissions for user.
UserAll os.FileMode = UserReadWriteExecute
)
const (
// GroupRead is read permissions for the group.
GroupRead os.FileMode = 0o040
// GroupWrite is write permissions for the group.
GroupWrite os.FileMode = 0o020
// GroupExecute is execute permissions for the group.
GroupExecute os.FileMode = 0o010
)
const (
// GroupReadWrite is a helper combination of read and write permissions for the group.
GroupReadWrite os.FileMode = GroupRead + GroupWrite
// GroupReadWriteExecute is a helper combination of read, write and execute permissions for the group.
GroupReadWriteExecute os.FileMode = GroupRead + GroupWrite + GroupExecute
// GroupAll is all permissions for groups.
GroupAll os.FileMode = GroupReadWriteExecute
)
const (
// OtherRead is read permissions for others.
OtherRead os.FileMode = 0o004
// OtherWrite is write permissions for others.
OtherWrite os.FileMode = 0o002
// OtherExecute is execute permissions for others.
OtherExecute os.FileMode = 0o001
)
const (
// OtherReadWrite is a helper combination of read and write permissions for the others.
OtherReadWrite os.FileMode = OtherRead + OtherWrite
// OtherReadWriteExecute is a helper combination of read, write and execute permissions for the others.
OtherReadWriteExecute os.FileMode = OtherRead + OtherWrite + OtherExecute
// OtherAll is all permissions for others.
OtherAll os.FileMode = OtherReadWriteExecute
)
// Is compares a supplied os.FileMode and returns true if it contains a reference mode.
func Is(mode os.FileMode, is os.FileMode) bool {
return (mode & is) == is
}
// Force takes a base mode and then a list of wanted modes, and makes sure the result
// is true for all the wanted modes.
//
// It is more efficient to supply a list of individual modes than a combination mode.
func Force(mode os.FileMode, want ...os.FileMode) os.FileMode {
for _, item := range want {
if item&(item-1) != 0 {
// multiple bits set
for i := range bits.Len(uint(item)) {
if v := item & (1 << i); v != 0 {
if !Is(mode, v) {
mode += v
}
}
}
continue
}
// single bit set, so just check it and add it if needed.
if !Is(mode, item) {
mode += item
}
}
return mode
}