-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileperm.go
99 lines (88 loc) · 2.61 KB
/
fileperm.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
package check
import (
"fmt"
"os"
)
// FilePerm is the type of a check function which takes an os.FileMode
// parameter and returns an error or nil if the check passes
type FilePerm func(d os.FileMode) error
// FilePermEQ returns a function that will check that the file permission is
// set to the value of the perms parameter
func FilePermEQ(perms os.FileMode) FilePerm {
return func(fm os.FileMode) error {
if fm.Perm() == perms {
return nil
}
return fmt.Errorf("the permissions (%04o) should be equal to %04o",
fm.Perm(), perms)
}
}
// FilePermHasAll returns a function that will check that the file permission
// has all of the permissions set in the perms parameter
func FilePermHasAll(perms os.FileMode) FilePerm {
return func(fm os.FileMode) error {
if (fm.Perm() & perms) == perms {
return nil
}
return fmt.Errorf(
"the permissions (%04o) should have all of the permissions in %04o",
fm.Perm(), perms)
}
}
// FilePermHasNone returns a function that will check that the file permission
// has none of the permissions set in the perms parameter
func FilePermHasNone(perms os.FileMode) FilePerm {
return func(fm os.FileMode) error {
if (fm.Perm() & perms) == 0 {
return nil
}
return fmt.Errorf(
"the permissions (%04o)"+
" should have none of the permissions in %04o",
fm.Perm(), perms)
}
}
// FilePermOr returns a function that will check that the value, when
// passed to each of the check funcs in turn, passes at least one of them
func FilePermOr(chkFuncs ...FilePerm) FilePerm {
return func(v os.FileMode) error {
compositeErr := ""
sep := "("
for _, cf := range chkFuncs {
err := cf(v)
if err == nil {
return nil
}
compositeErr += sep + err.Error()
sep = _Or
}
return fmt.Errorf("%s)", compositeErr)
}
}
// FilePermAnd returns a function that will check that the value, when
// passed to each of the check funcs in turn, passes all of them
func FilePermAnd(chkFuncs ...FilePerm) FilePerm {
return func(v os.FileMode) error {
for _, cf := range chkFuncs {
err := cf(v)
if err != nil {
return err
}
}
return nil
}
}
// FilePermNot returns a function that will check that the value, when passed
// to the check func, does not pass it. You must also supply the error text
// to appear after the value that fails. This error text should be a string
// that describes the quality that the permissions should not have.
func FilePermNot(c FilePerm, errMsg string) FilePerm {
return func(v os.FileMode) error {
err := c(v)
if err != nil {
return nil
}
return fmt.Errorf("the permissions (%04o) should not %s",
v.Perm(), errMsg)
}
}