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")
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
40 files := make(map[string]*safcm.File)
41 err := filepath.WalkDir(basePath, func(path string, d fs.DirEntry,
52 typ := info.Mode().Type()
53 perm := FileModeToFullPerm(info.Mode())
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 {
62 "%q: invalid permissions %#o%s",
65 data, err = os.ReadFile(path)
69 } else if typ == fs.ModeDir {
72 "%q: invalid permissions %#o%s",
75 } else if typ == fs.ModeSymlink {
76 x, err := os.Readlink(path)
82 return fmt.Errorf("%q: file type not supported", path)
85 // Convert to absolute path as used on the target host
86 x, err := filepath.Rel(basePath, path)
90 x = filepath.Join("/", x)
92 files[x] = &safcm.File{
94 Mode: typ | (fs.FileMode(perm) & fs.ModePerm),
102 if os.IsNotExist(err) {
105 return nil, fmt.Errorf("%s: %v", group, err)