X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm%2Ffixperms.go;h=36d618f7fe1b12a0b2197f9186a0673f71c483ba;hb=9269fa3c94e700afc0be823f58ea473a2db8f3dc;hp=6770934f14d176f7ab15f413ea1d9ce8f0accbd3;hpb=f2f2bc47e8729548f3c10117f7f008b547c4afc5;p=safcm%2Fsafcm.git diff --git a/cmd/safcm/fixperms.go b/cmd/safcm/fixperms.go index 6770934..36d618f 100644 --- a/cmd/safcm/fixperms.go +++ b/cmd/safcm/fixperms.go @@ -25,6 +25,7 @@ import ( "path/filepath" "ruderich.org/simon/safcm/cmd/safcm/config" + "ruderich.org/simon/safcm/remote/sync" ) func MainFixperms() error { @@ -78,7 +79,7 @@ func fixpermsWalkDirFunc(path string, d fs.DirEntry, err error) error { // This is safe because perm does not include // setuid/setgid/sticky which use different values in // FileMode. - err := os.Chmod(path, fs.FileMode(perm)) + err := chmodNoFollow(path, fs.FileMode(perm)) if err != nil { return err } @@ -87,7 +88,7 @@ func fixpermsWalkDirFunc(path string, d fs.DirEntry, err error) error { if perm != 0755 { perm = 0755 log.Printf("chmodding %q to %#o", path, perm) - err := os.Chmod(path, fs.FileMode(perm)) + err := chmodNoFollow(path, fs.FileMode(perm)) if err != nil { return err } @@ -97,3 +98,19 @@ func fixpermsWalkDirFunc(path string, d fs.DirEntry, err error) error { return nil } + +// chmodNoFollow works like os.Chmod but doesn't follow symlinks. +func chmodNoFollow(path string, mode fs.FileMode) error { + x, err := sync.OpenFileNoFollow(path) + if err != nil { + return err + } + defer x.Close() + + err = x.Chmod(mode) + if err != nil { + return err + } + + return nil +}