summaryrefslogtreecommitdiff
path: root/vendor/github.com/otiai10/copy/permission_control.go
blob: 97ae12d8e0999d164bb93c4589fee9b1fddd8933 (plain)
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
package copy

import (
	"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 fileInfo, dest string) (chmodfunc func(*error), err error)

var (
	AddPermission = func(perm os.FileMode) PermissionControlFunc {
		return func(srcinfo 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 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 asiging 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
	}
}