1 // Read and write the state file used to keep data over multiple runs
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/>.
29 LastModified map[string]time.Time
32 func LoadState(path string) (*State, error) {
35 x, err := ioutil.ReadFile(path)
37 // It's fine if the state file does not exist yet, we'll
38 // create it later when writing the state
39 if !os.IsNotExist(err) {
43 err := json.Unmarshal(x, &state)
49 if state.LastModified == nil {
50 state.LastModified = make(map[string]time.Time)
56 func WriteState(path string, state *State) error {
57 // Update the state file even if nothing has changed to provide a
58 // simple way to check if nsscash ran successfully (the state is only
59 // updated if there were no errors)
61 x, err := json.Marshal(state)
66 // Write the file in an atomic fashion by creating a temporary file
67 // and renaming it over the target file
69 dir := filepath.Dir(path)
70 name := filepath.Base(path)
72 f, err := ioutil.TempFile(dir, "tmp-"+name+"-")
76 defer os.Remove(f.Name())
87 return os.Rename(f.Name(), path)