]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/fixperms.go
First working version
[safcm/safcm.git] / cmd / safcm / fixperms.go
1 // "fixperms" sub-command: apply proper permissions in files/ directories
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
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.
9 //
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.
14 //
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/>.
17
18 package main
19
20 import (
21         "fmt"
22         "io/fs"
23         "log"
24         "os"
25         "path/filepath"
26
27         "ruderich.org/simon/safcm/cmd/safcm/config"
28 )
29
30 func MainFixperms() error {
31         _, _, _, err := LoadBaseFiles()
32         if err != nil {
33                 return fmt.Errorf("not in a safcm directory: %v", err)
34         }
35
36         xs, err := os.ReadDir(".")
37         if err != nil {
38                 return err
39         }
40         for _, x := range xs {
41                 if !x.IsDir() {
42                         continue
43                 }
44                 path := filepath.Join(x.Name(), "files")
45
46                 err := filepath.WalkDir(path, fixpermsWalkDirFunc)
47                 if err != nil {
48                         if os.IsNotExist(err) {
49                                 continue
50                         }
51                         return fmt.Errorf("%s: %v", path, err)
52                 }
53         }
54
55         return nil
56 }
57
58 func fixpermsWalkDirFunc(path string, d fs.DirEntry, err error) error {
59         if err != nil {
60                 return err
61         }
62
63         info, err := d.Info()
64         if err != nil {
65                 return err
66         }
67         typ := info.Mode().Type()
68         perm := config.FileModeToFullPerm(info.Mode())
69
70         if typ == 0 /* regular file */ {
71                 if perm != 0644 && perm != 0755 {
72                         if perm&0111 != 0 /* executable */ {
73                                 perm = 0755
74                         } else {
75                                 perm = 0644
76                         }
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
80                         // FileMode.
81                         err := os.Chmod(path, fs.FileMode(perm))
82                         if err != nil {
83                                 return err
84                         }
85                 }
86         } else if typ == fs.ModeDir {
87                 if perm != 0755 {
88                         perm = 0755
89                         log.Printf("chmodding %q to %#o", path, perm)
90                         err := os.Chmod(path, fs.FileMode(perm))
91                         if err != nil {
92                                 return err
93                         }
94                 }
95         }
96         // Other file types are caught by regular "sync"
97
98         return nil
99 }