forked from homedepot/flop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permlinux.go
44 lines (38 loc) · 1.25 KB
/
permlinux.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
// +build linux darwin
package flop
import (
"github.com/pkg/errors"
"os"
)
// setPermissions will set file level permissions on dst based on options and other criteria.
func setPermissions(dstFile *File, srcMode os.FileMode, opts Options) error {
var mode os.FileMode
fi, err := os.Stat(dstFile.Path)
if err != nil {
return err
}
mode = fi.Mode()
if dstFile.existOnInit {
if mode == dstFile.fileInfoOnInit.Mode() {
opts.logDebug("existing dst %s permissions %s are unchanged", dstFile.Path, mode)
return nil
}
// make sure dst perms are set to their original value
opts.logDebug("changing dst %s permissions to %s", dstFile.Path, dstFile.fileInfoOnInit.Mode())
err := os.Chmod(dstFile.Path, dstFile.fileInfoOnInit.Mode())
if err != nil {
return errors.Wrapf(ErrCannotChmodFile, "destination file %s: %s", dstFile.Path, err)
}
} else {
if mode == srcMode {
opts.logDebug("dst %s permissions %s already match src perms", dstFile.Path, mode)
}
// make sure dst perms are set to that of src
opts.logDebug("changing dst %s permissions to %s", dstFile.Path, srcMode)
err := os.Chmod(dstFile.Path, srcMode)
if err != nil {
return errors.Wrapf(ErrCannotChmodFile, "destination file %s: %s", dstFile.Path, err)
}
}
return nil
}