// "fixperms" sub-command: apply proper permissions in files/ directories
-// Copyright (C) 2021 Simon Ruderich
+// Copyright (C) 2021-2023 Simon Ruderich
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
"path/filepath"
"ruderich.org/simon/safcm/cmd/safcm/config"
+ "ruderich.org/simon/safcm/remote/sync"
)
func MainFixperms() 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
}
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
}
return nil
}
+
+// chmodNoFollow works like os.Chmod but doesn't follow symlinks.
+func chmodNoFollow(path string, mode fs.FileMode) error {
+ x, err := sync.OpenFileNoSymlinks(path)
+ if err != nil {
+ return err
+ }
+ defer x.Close()
+
+ err = x.Chmod(mode)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}