1
0
mirror of https://github.com/rancher/os.git synced 2025-04-27 11:10:56 +00:00

Fix update cache bug

This commit is contained in:
Jason-ZW 2019-07-01 10:31:34 +08:00 committed by niusmallnan
parent 34afa7824e
commit a37efde319
2 changed files with 26 additions and 5 deletions

View File

@ -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"))
}

View File

@ -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
}