1 // Config: load files/ directory tree
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/>.
26 "ruderich.org/simon/safcm"
29 func LoadFiles(group string) (map[string]*safcm.File, error) {
30 basePath := filepath.Join(group, "files")
34 The actual permissions and user/group of files and directories are not used
35 (except for +x on files). 0644/0755 and current remote user/group is used per
36 default. Apply different file permissions via permissions.yaml. To prevent
37 confusion files must be manually chmodded 0644/0755 and directories 0755 or
41 files := make(map[string]*safcm.File)
42 err := filepath.WalkDir(basePath, func(path string, d fs.DirEntry,
53 typ := info.Mode().Type()
54 perm := FileModeToFullPerm(info.Mode())
57 // See errMsg above. If a user stores a file with stricter
58 // permissions they could assume that these permissions are
59 // respected. This is not the case.
60 if typ == 0 /* regular file */ {
61 if perm != 0644 && perm != 0755 {
63 "%q: invalid permissions %#o%s",
66 data, err = os.ReadFile(path)
70 } else if typ == fs.ModeDir {
73 "%q: invalid permissions %#o%s",
76 } else if typ == fs.ModeSymlink {
77 x, err := os.Readlink(path)
83 return fmt.Errorf("%q: file type not supported", path)
86 // Convert to absolute path as used on the target host
87 x, err := filepath.Rel(basePath, path)
91 x = filepath.Join("/", x)
93 files[x] = &safcm.File{
95 Mode: typ | (fs.FileMode(perm) & fs.ModePerm),
103 if os.IsNotExist(err) {
106 return nil, fmt.Errorf("%s: %v", group, err)