]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/files.go
8f637701a60735c25ebb63969d075b8e1526edd9
[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
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
38 via "safcm fixperms".
39 `
40
41         files := make(map[string]*safcm.File)
42         err := filepath.WalkDir(basePath, func(path string, d fs.DirEntry,
43                 err error) error {
44
45                 if err != nil {
46                         return err
47                 }
48
49                 info, err := d.Info()
50                 if err != nil {
51                         return err
52                 }
53                 typ := info.Mode().Type()
54                 perm := FileModeToFullPerm(info.Mode())
55
56                 var data []byte
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 {
62                                 return fmt.Errorf(
63                                         "%q: invalid permissions %#o%s",
64                                         path, perm, errMsg)
65                         }
66                         data, err = os.ReadFile(path)
67                         if err != nil {
68                                 return err
69                         }
70                 } else if typ == fs.ModeDir {
71                         if perm != 0755 {
72                                 return fmt.Errorf(
73                                         "%q: invalid permissions %#o%s",
74                                         path, perm, errMsg)
75                         }
76                 } else if typ == fs.ModeSymlink {
77                         x, err := os.Readlink(path)
78                         if err != nil {
79                                 return err
80                         }
81                         data = []byte(x)
82                         perm |= 0777 // see cmd/safcm-remote/sync/files.go
83                 } else {
84                         return fmt.Errorf("%q: file type not supported", path)
85                 }
86
87                 // Convert to absolute path as used on the target host
88                 x, err := filepath.Rel(basePath, path)
89                 if err != nil {
90                         return err
91                 }
92                 x = filepath.Join("/", x)
93
94                 files[x] = &safcm.File{
95                         Path: x,
96                         Mode: typ | (fs.FileMode(perm) & fs.ModePerm),
97                         Uid:  -1,
98                         Gid:  -1,
99                         Data: data,
100                 }
101                 return nil
102         })
103         if err != nil {
104                 if os.IsNotExist(err) {
105                         return nil, nil
106                 }
107                 return nil, fmt.Errorf("%s: %v", group, err)
108         }
109         return files, nil
110 }