-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
permission_control.go
49 lines (44 loc) · 1.38 KB
/
permission_control.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
package copy
import (
"io/fs"
"os"
)
const (
// tmpPermissionForDirectory makes the destination directory writable,
// so that stuff can be copied recursively even if any original directory is NOT writable.
// See https://github.com/otiai10/copy/pull/9 for more information.
tmpPermissionForDirectory = os.FileMode(0755)
)
type PermissionControlFunc func(srcinfo fs.FileInfo, dest string) (chmodfunc func(*error), err error)
var (
AddPermission = func(perm os.FileMode) PermissionControlFunc {
return func(srcinfo fs.FileInfo, dest string) (func(*error), error) {
orig := srcinfo.Mode()
if srcinfo.IsDir() {
if err := os.MkdirAll(dest, tmpPermissionForDirectory); err != nil {
return func(*error) {}, err
}
}
return func(err *error) {
chmod(dest, orig|perm, err)
}, nil
}
}
PerservePermission PermissionControlFunc = AddPermission(0)
DoNothing PermissionControlFunc = func(srcinfo fs.FileInfo, dest string) (func(*error), error) {
if srcinfo.IsDir() {
if err := os.MkdirAll(dest, srcinfo.Mode()); err != nil {
return func(*error) {}, err
}
}
return func(*error) {}, nil
}
)
// chmod ANYHOW changes file mode,
// with assigning error raised during Chmod,
// BUT respecting the error already reported.
func chmod(dir string, mode os.FileMode, reported *error) {
if err := os.Chmod(dir, mode); *reported == nil {
*reported = err
}
}