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/>.
30 LastModified map[string]time.Time
31 Checksum map[string]string // SHA512 in hex
34 func LoadState(path string) (*State, error) {
37 x, err := ioutil.ReadFile(path)
39 // It's fine if the state file does not exist yet, we'll
40 // create it later when writing the state
41 if !os.IsNotExist(err) {
45 err := json.Unmarshal(x, &state)
51 if state.LastModified == nil {
52 state.LastModified = make(map[string]time.Time)
54 if state.Checksum == nil {
55 state.Checksum = make(map[string]string)
61 func WriteState(path string, state *State) error {
62 // Update the state file even if nothing has changed to provide a
63 // simple way to check if nsscash ran successfully (the state is only
64 // updated if there were no errors)
66 x, err := json.Marshal(state)
71 // Write the file in an atomic fashion by creating a temporary file
72 // and renaming it over the target file
74 dir := filepath.Dir(path)
75 name := filepath.Base(path)
77 f, err := ioutil.TempFile(dir, "tmp-"+name+"-")
81 defer os.Remove(f.Name())
92 return os.Rename(f.Name(), path)