]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/fixperms.go
changes: hide commands if quiet and dry-run is active
[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-remote/sync"
28         "ruderich.org/simon/safcm/cmd/safcm/config"
29 )
30
31 func MainFixperms() error {
32         _, _, _, err := LoadBaseFiles()
33         if err != nil {
34                 return fmt.Errorf("not in a safcm directory: %v", err)
35         }
36
37         xs, err := os.ReadDir(".")
38         if err != nil {
39                 return err
40         }
41         for _, x := range xs {
42                 if !x.IsDir() {
43                         continue
44                 }
45                 path := filepath.Join(x.Name(), "files")
46
47                 err := filepath.WalkDir(path, fixpermsWalkDirFunc)
48                 if err != nil {
49                         if os.IsNotExist(err) {
50                                 continue
51                         }
52                         return fmt.Errorf("%s: %v", path, err)
53                 }
54         }
55
56         return nil
57 }
58
59 func fixpermsWalkDirFunc(path string, d fs.DirEntry, err error) error {
60         if err != nil {
61                 return err
62         }
63
64         info, err := d.Info()
65         if err != nil {
66                 return err
67         }
68         typ := info.Mode().Type()
69         perm := config.FileModeToFullPerm(info.Mode())
70
71         if typ == 0 /* regular file */ {
72                 if perm != 0644 && perm != 0755 {
73                         if perm&0111 != 0 /* executable */ {
74                                 perm = 0755
75                         } else {
76                                 perm = 0644
77                         }
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
81                         // FileMode.
82                         err := chmodNoFollow(path, fs.FileMode(perm))
83                         if err != nil {
84                                 return err
85                         }
86                 }
87         } else if typ == fs.ModeDir {
88                 if perm != 0755 {
89                         perm = 0755
90                         log.Printf("chmodding %q to %#o", path, perm)
91                         err := chmodNoFollow(path, fs.FileMode(perm))
92                         if err != nil {
93                                 return err
94                         }
95                 }
96         }
97         // Other file types are caught by regular "sync"
98
99         return nil
100 }
101
102 // chmodNoFollow works like os.Chmod but doesn't follow symlinks.
103 func chmodNoFollow(path string, mode fs.FileMode) error {
104         x, err := sync.OpenFileNoFollow(path)
105         if err != nil {
106                 return err
107         }
108         defer x.Close()
109
110         err = x.Chmod(mode)
111         if err != nil {
112                 return err
113         }
114
115         return nil
116 }