]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - state.go
.github: update upstream actions to latest version
[nsscash/nsscash.git] / state.go
index b0dec1c2d47673fffa5eb81ee8e3d3113fce689f..59c885ae9cf17c9a15a2e980702926a24cf26339 100644 (file)
--- a/state.go
+++ b/state.go
@@ -1,6 +1,6 @@
 // Read and write the state file used to keep data over multiple runs
 
-// Copyright (C) 2019  Simon Ruderich
+// Copyright (C) 2019-2021  Simon Ruderich
 //
 // This program is free software: you can redistribute it and/or modify
 // it under the terms of the GNU Affero General Public License as published by
@@ -22,15 +22,15 @@ import (
        "io/ioutil"
        "os"
        "path/filepath"
-       "reflect"
        "time"
+
+       "github.com/google/renameio"
 )
 
 type State struct {
+       // Key is File.Url
        LastModified map[string]time.Time
-
-       // Copy of LastModified to write the state file only on changes
-       origLastModified map[string]time.Time
+       Checksum     map[string]string // SHA512 in hex
 }
 
 func LoadState(path string) (*State, error) {
@@ -53,46 +53,36 @@ func LoadState(path string) (*State, error) {
        if state.LastModified == nil {
                state.LastModified = make(map[string]time.Time)
        }
-
-       state.origLastModified = make(map[string]time.Time)
-       for k, v := range state.LastModified {
-               state.origLastModified[k] = v
+       if state.Checksum == nil {
+               state.Checksum = make(map[string]string)
        }
 
        return &state, nil
 }
 
-func WriteStateIfChanged(path string, state *State) error {
-       // State hasn't changed, nothing to do
-       if reflect.DeepEqual(state.LastModified, state.origLastModified) {
-               return nil
-       }
+func WriteState(path string, state *State) error {
+       // Update the state file even if nothing has changed to provide a
+       // simple way to check if nsscash ran successfully (the state is only
+       // updated if there were no errors)
 
        x, err := json.Marshal(state)
        if err != nil {
                return err
        }
 
-       // Write the file in an atomic fashion by creating a temporary file
-       // and renaming it over the target file
-
-       dir := filepath.Dir(path)
-       name := filepath.Base(path)
-
-       f, err := ioutil.TempFile(dir, "tmp-"+name+"-")
+       f, err := renameio.TempFile(filepath.Dir(path), path)
        if err != nil {
                return err
        }
-       defer os.Remove(f.Name())
-       defer f.Close()
+       defer f.Cleanup()
 
        _, err = f.Write(x)
        if err != nil {
                return err
        }
-       err = f.Sync()
+       err = f.CloseAtomicallyReplace()
        if err != nil {
                return err
        }
-       return os.Rename(f.Name(), path)
+       return syncPath(filepath.Dir(path))
 }