-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpermission_control.go
59 lines (48 loc) · 1.74 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
50
51
52
53
54
55
56
57
58
59
package aferocopy
import (
"os"
"github.com/spf13/afero"
)
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(0o755)
)
// PermissionControlFunc is a function that can be used to control the permission of a file or directory while copying.
type PermissionControlFunc func(srcInfo os.FileInfo, destFs afero.Fs, dest string) (chmodFunc func(*error), err error)
var (
// AddPermission controls the permission of the destination file.
AddPermission = func(perm os.FileMode) PermissionControlFunc {
return func(srcInfo os.FileInfo, destFs afero.Fs, dest string) (func(*error), error) {
orig := srcInfo.Mode()
if srcInfo.IsDir() {
if err := destFs.MkdirAll(dest, tmpPermissionForDirectory); err != nil {
return func(*error) {}, err
}
}
return func(err *error) {
chmod(destFs, dest, orig|perm, err)
}, nil
}
}
// PreservePermission preserves the original permission.
PreservePermission = AddPermission(0)
// DoNothing do not touch the permission.
DoNothing = PermissionControlFunc(func(srcInfo os.FileInfo, destFs afero.Fs, dest string) (func(*error), error) {
if srcInfo.IsDir() {
if err := destFs.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(fs afero.Fs, dir string, mode os.FileMode, reported *error) {
if err := fs.Chmod(dir, mode); *reported == nil {
*reported = err
}
}