-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.go
103 lines (83 loc) · 3.88 KB
/
permissions.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
94
95
96
97
98
99
100
101
102
103
package fs
import (
iofs "io/fs"
"os"
)
var (
NoPermissions Permissions = 0
UserExecute Permissions = 0100
UserWrite Permissions = 0200
UserRead Permissions = 0400
UserReadWrite Permissions = UserRead | UserWrite
UserReadWriteExecute Permissions = UserRead | UserWrite | UserExecute
GroupExecute Permissions = 0010
GroupWrite Permissions = 0020
GroupRead Permissions = 0040
GroupReadWrite Permissions = GroupRead | GroupWrite
GroupReadWriteExecute Permissions = GroupRead | GroupWrite | GroupExecute
UserAndGroupRead Permissions = UserRead | GroupRead
UserAndGroupReadWrite Permissions = UserReadWrite | GroupReadWrite
UserAndGroupReadWriteExecute Permissions = UserReadWriteExecute | GroupReadWriteExecute
OthersExecute Permissions = 0001
OthersWrite Permissions = 0002
OthersRead Permissions = 0004
OthersReadWrite Permissions = OthersRead | OthersWrite
OthersReadWriteExecute Permissions = OthersRead | OthersWrite | OthersExecute
AllRead = UserRead | GroupRead | OthersRead
AllWrite = UserWrite | GroupWrite | OthersWrite
AllExecute = UserExecute | GroupExecute | OthersExecute
AllReadWrite = UserReadWrite | GroupReadWrite | OthersReadWrite
)
// Permissions for a file, follows the Unix/os.FileMode bit schema.
type Permissions int
// FileMode returns an os.FileMode for the given permissions
// together with the information if the file is a directory.
func (perm Permissions) FileMode(isDir bool) os.FileMode {
m := os.FileMode(perm)
if isDir {
m |= os.ModeDir
}
return m
}
func (perm Permissions) Readable() (user, group, others bool) {
return perm&UserRead != 0, perm&GroupRead != 0, perm&OthersRead != 0
}
func (perm Permissions) Writable() (user, group, others bool) {
return perm&UserWrite != 0, perm&GroupWrite != 0, perm&OthersWrite != 0
}
func (perm Permissions) Executable() (user, group, others bool) {
return perm&UserExecute != 0, perm&GroupExecute != 0, perm&OthersExecute != 0
}
func (perm Permissions) Can(p Permissions) bool {
return perm&p == p
}
func (perm Permissions) CanUserExecute() bool { return perm.Can(UserExecute) }
func (perm Permissions) CanUserWrite() bool { return perm.Can(UserWrite) }
func (perm Permissions) CanUserRead() bool { return perm.Can(UserRead) }
func (perm Permissions) CanUserReadWrite() bool { return perm.Can(UserReadWrite) }
func (perm Permissions) CanGroupExecute() bool { return perm.Can(GroupExecute) }
func (perm Permissions) CanGroupWrite() bool { return perm.Can(GroupWrite) }
func (perm Permissions) CanGroupRead() bool { return perm.Can(GroupRead) }
func (perm Permissions) CanGroupReadWrite() bool { return perm.Can(GroupReadWrite) }
func (perm Permissions) CanUserAndGroupRead() bool { return perm.Can(UserAndGroupRead) }
func (perm Permissions) CanUserAndGroupReadWrite() bool { return perm.Can(UserAndGroupReadWrite) }
func (perm Permissions) CanOthersExecute() bool { return perm.Can(OthersExecute) }
func (perm Permissions) CanOthersWrite() bool { return perm.Can(OthersWrite) }
func (perm Permissions) CanOthersRead() bool { return perm.Can(OthersRead) }
func (perm Permissions) CanOthersReadWrite() bool { return perm.Can(OthersReadWrite) }
func (perm Permissions) CanAllRead() bool { return perm.Can(AllRead) }
func (perm Permissions) CanAllWrite() bool { return perm.Can(AllWrite) }
func (perm Permissions) CanAllExecute() bool { return perm.Can(AllExecute) }
func (perm Permissions) CanAllReadWrite() bool { return perm.Can(AllReadWrite) }
func JoinPermissions(perms []Permissions, defaultPerm Permissions) (result Permissions) {
if len(perms) == 0 {
return defaultPerm
}
for _, p := range perms {
result |= p
}
return result
}
func PermissionsFromStdFileInfo(info iofs.FileInfo) Permissions {
return Permissions(info.Mode().Perm())
}