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:
parent
34afa7824e
commit
a37efde319
@ -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"))
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user