github.com/saymoon/flop@v0.1.6-0.20201205092451-00912199cc96/permlinux.go (about) 1 // +build linux darwin 2 3 package flop 4 5 import ( 6 "github.com/pkg/errors" 7 "os" 8 ) 9 10 // setPermissions will set file level permissions on dst based on options and other criteria. 11 func SetPermissions(dstFile *File, srcMode os.FileMode, opts Options) error { 12 var mode os.FileMode 13 fi, err := os.Stat(dstFile.Path) 14 if err != nil { 15 return err 16 } 17 mode = fi.Mode() 18 19 if dstFile.existOnInit { 20 if mode == dstFile.fileInfoOnInit.Mode() { 21 opts.logDebug("existing dst %s permissions %s are unchanged", dstFile.Path, mode) 22 return nil 23 } 24 25 // make sure dst perms are set to their original value 26 opts.logDebug("changing dst %s permissions to %s", dstFile.Path, dstFile.fileInfoOnInit.Mode()) 27 err := os.Chmod(dstFile.Path, dstFile.fileInfoOnInit.Mode()) 28 if err != nil { 29 return errors.Wrapf(ErrCannotChmodFile, "destination file %s: %s", dstFile.Path, err) 30 } 31 } else { 32 if mode == srcMode { 33 opts.logDebug("dst %s permissions %s already match src perms", dstFile.Path, mode) 34 } 35 36 // make sure dst perms are set to that of src 37 opts.logDebug("changing dst %s permissions to %s", dstFile.Path, srcMode) 38 err := os.Chmod(dstFile.Path, srcMode) 39 if err != nil { 40 return errors.Wrapf(ErrCannotChmodFile, "destination file %s: %s", dstFile.Path, err) 41 } 42 } 43 return nil 44 }