1 // Download and write files atomically to the file system
3 // Copyright (C) 2019 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero 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 Affero General Public License for more details.
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program. If not, see <https://www.gnu.org/licenses/>.
30 "github.com/pkg/errors"
33 func handleFiles(cfg *Config, state *State) error {
34 for i, f := range cfg.Files {
35 err := fetchFile(&cfg.Files[i], state)
37 return errors.Wrapf(err, "%q (%s)", f.Url, f.Type)
41 for i, f := range cfg.Files {
47 err := deployFile(&cfg.Files[i])
49 return errors.Wrapf(err, "%q (%s)", f.Url, f.Type)
56 func fetchFile(file *File, state *State) error {
57 t := state.LastModified[file.Url]
58 status, body, err := fetchIfModified(file.Url, &t)
62 if status == http.StatusNotModified {
63 log.Printf("%q -> %q: not modified", file.Url, file.Path)
66 if status != http.StatusOK {
67 return fmt.Errorf("status code %v", status)
69 state.LastModified[file.Url] = t
71 if file.Type == FileTypePlain {
73 return fmt.Errorf("refusing to use empty response")
77 } else if file.Type == FileTypePasswd {
78 pws, err := ParsePasswds(bytes.NewReader(body))
82 // Safety check: having no users can be very dangerous, don't
85 return fmt.Errorf("refusing to use empty passwd file")
89 err = SerializePasswds(&x, pws)
95 } else if file.Type == FileTypeGroup {
96 grs, err := ParseGroups(bytes.NewReader(body))
101 return fmt.Errorf("refusing to use empty group file")
105 err = SerializeGroups(&x, grs)
109 file.body = x.Bytes()
112 return fmt.Errorf("unsupported file type %v", file.Type)
117 func deployFile(file *File) error {
118 log.Printf("%q -> %q: updating file", file.Url, file.Path)
121 if len(file.body) == 0 {
122 return fmt.Errorf("refusing to write empty file")
125 // Write the file in an atomic fashion by creating a temporary file
126 // and renaming it over the target file
128 dir := filepath.Dir(file.Path)
129 name := filepath.Base(file.Path)
131 f, err := ioutil.TempFile(dir, "tmp-"+name+"-")
135 defer os.Remove(f.Name())
138 // Apply permissions/user/group from the target file
139 stat, err := os.Stat(file.Path)
141 // We do not create the path if it doesn't exist, because we
142 // do not know the proper permissions
143 return errors.Wrapf(err, "file.path %q must exist", file.Path)
145 err = f.Chmod(stat.Mode())
149 // TODO: support more systems
150 sys, ok := stat.Sys().(*syscall.Stat_t)
152 return fmt.Errorf("unsupported FileInfo.Sys()")
154 err = f.Chown(int(sys.Uid), int(sys.Gid))
159 _, err = f.Write(file.body)
167 return os.Rename(f.Name(), file.Path)