]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/files.go
First working version
[safcm/safcm.git] / cmd / safcm / config / files.go
1 // Config: load files/ directory tree
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 config
19
20 import (
21         "fmt"
22         "io/fs"
23         "os"
24         "path/filepath"
25
26         "ruderich.org/simon/safcm"
27 )
28
29 func LoadFiles(group string) (map[string]*safcm.File, error) {
30         basePath := filepath.Join(group, "files")
31
32         const errMsg = `
33 The actual permissions and user/group of files and directories are not used
34 (except for +x on files). 0644/0755 and current remote user/group is used per
35 default. Apply different file permissions via permissions.yaml. To prevent
36 confusion files must be manually chmodded 0644/0755 and directories 0755 or
37 via "safcm fixperms".
38 `
39
40         files := make(map[string]*safcm.File)
41         err := filepath.WalkDir(basePath, func(path string, d fs.DirEntry,
42                 err error) error {
43
44                 if err != nil {
45                         return err
46                 }
47
48                 info, err := d.Info()
49                 if err != nil {
50                         return err
51                 }
52                 typ := info.Mode().Type()
53                 perm := FileModeToFullPerm(info.Mode())
54
55                 var data []byte
56                 // See errMsg above. If a user stores a file with stricter
57                 // permissions they could assume that these permissions are
58                 // respected. This is not the case.
59                 if typ == 0 /* regular file */ {
60                         if perm != 0644 && perm != 0755 {
61                                 return fmt.Errorf(
62                                         "%q: invalid permissions %#o%s",
63                                         path, perm, errMsg)
64                         }
65                         data, err = os.ReadFile(path)
66                         if err != nil {
67                                 return err
68                         }
69                 } else if typ == fs.ModeDir {
70                         if perm != 0755 {
71                                 return fmt.Errorf(
72                                         "%q: invalid permissions %#o%s",
73                                         path, perm, errMsg)
74                         }
75                 } else if typ == fs.ModeSymlink {
76                         x, err := os.Readlink(path)
77                         if err != nil {
78                                 return err
79                         }
80                         data = []byte(x)
81                 } else {
82                         return fmt.Errorf("%q: file type not supported", path)
83                 }
84
85                 // Convert to absolute path as used on the target host
86                 x, err := filepath.Rel(basePath, path)
87                 if err != nil {
88                         return err
89                 }
90                 x = filepath.Join("/", x)
91
92                 files[x] = &safcm.File{
93                         Path: x,
94                         Mode: typ | (fs.FileMode(perm) & fs.ModePerm),
95                         Uid:  -1,
96                         Gid:  -1,
97                         Data: data,
98                 }
99                 return nil
100         })
101         if err != nil {
102                 if os.IsNotExist(err) {
103                         return nil, nil
104                 }
105                 return nil, fmt.Errorf("%s: %v", group, err)
106         }
107         return files, nil
108 }