1 // "fixperms" sub-command: apply proper permissions in files/ directories
3 // Copyright (C) 2021 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "ruderich.org/simon/safcm/cmd/safcm/config"
28 "ruderich.org/simon/safcm/remote/sync"
31 func MainFixperms() error {
32 _, _, _, err := LoadBaseFiles()
34 return fmt.Errorf("not in a safcm directory: %v", err)
37 xs, err := os.ReadDir(".")
41 for _, x := range xs {
45 path := filepath.Join(x.Name(), "files")
47 err := filepath.WalkDir(path, fixpermsWalkDirFunc)
49 if os.IsNotExist(err) {
52 return fmt.Errorf("%s: %v", path, err)
59 func fixpermsWalkDirFunc(path string, d fs.DirEntry, err error) error {
68 typ := info.Mode().Type()
69 perm := config.FileModeToFullPerm(info.Mode())
71 if typ == 0 /* regular file */ {
72 if perm != 0644 && perm != 0755 {
73 if perm&0111 != 0 /* executable */ {
78 log.Printf("chmodding %q to %#o", path, perm)
79 // This is safe because perm does not include
80 // setuid/setgid/sticky which use different values in
82 err := chmodNoFollow(path, fs.FileMode(perm))
87 } else if typ == fs.ModeDir {
90 log.Printf("chmodding %q to %#o", path, perm)
91 err := chmodNoFollow(path, fs.FileMode(perm))
97 // Other file types are caught by regular "sync"
102 // chmodNoFollow works like os.Chmod but doesn't follow symlinks.
103 func chmodNoFollow(path string, mode fs.FileMode) error {
104 x, err := sync.OpenFileNoSymlinks(path)