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"
30 func MainFixperms() error {
31 _, _, _, err := LoadBaseFiles()
33 return fmt.Errorf("not in a safcm directory: %v", err)
36 xs, err := os.ReadDir(".")
40 for _, x := range xs {
44 path := filepath.Join(x.Name(), "files")
46 err := filepath.WalkDir(path, fixpermsWalkDirFunc)
48 if os.IsNotExist(err) {
51 return fmt.Errorf("%s: %v", path, err)
58 func fixpermsWalkDirFunc(path string, d fs.DirEntry, err error) error {
67 typ := info.Mode().Type()
68 perm := config.FileModeToFullPerm(info.Mode())
70 if typ == 0 /* regular file */ {
71 if perm != 0644 && perm != 0755 {
72 if perm&0111 != 0 /* executable */ {
77 log.Printf("chmodding %q to %#o", path, perm)
78 // This is safe because perm does not include
79 // setuid/setgid/sticky which use different values in
81 err := os.Chmod(path, fs.FileMode(perm))
86 } else if typ == fs.ModeDir {
89 log.Printf("chmodding %q to %#o", path, perm)
90 err := os.Chmod(path, fs.FileMode(perm))
96 // Other file types are caught by regular "sync"