diff --git a/pkg/util/network/cache.go b/pkg/util/network/cache.go index 51ba457b..e02e90b3 100644 --- a/pkg/util/network/cache.go +++ b/pkg/util/network/cache.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "io/ioutil" "os" + "strings" "github.com/rancher/os/pkg/log" ) @@ -44,7 +45,15 @@ func cacheAdd(location string, data []byte) { os.Rename(tempFile.Name(), cacheFile) } -func cacheRemove(location string) error { +func cacheMove(location string) (string, error) { cacheFile := cacheDirectory + locationHash(location) - return os.Remove(cacheFile) + tempFile := cacheFile + "_temp" + if err := os.Rename(cacheFile, tempFile); err != nil { + return "", err + } + return tempFile, nil +} + +func cacheMoveBack(name string) error { + return os.Rename(name, strings.TrimRight(name, "_temp")) } diff --git a/pkg/util/network/network.go b/pkg/util/network/network.go index 402b77b1..74007d2e 100644 --- a/pkg/util/network/network.go +++ b/pkg/util/network/network.go @@ -230,12 +230,24 @@ func UpdateCaches(urls []string, key string) error { } func UpdateCache(location string) ([]byte, error) { - if err := cacheRemove(location); err != nil { - return []byte{}, err + // move cache file to temp directory + tempFile, err := cacheMove(location) + if err != nil { + return nil, err } + content, err := LoadResource(location, true) if err != nil { - return []byte{}, err + // move back old cache file + if err := cacheMoveBack(tempFile); err != nil { + return nil, err + } + return ioutil.ReadFile(location) } + // remove old cache file + if err := os.Remove(tempFile); err != nil { + return nil, err + } + return content, nil }