1
0
mirror of https://github.com/rancher/os.git synced 2025-07-19 17:39:04 +00:00
os/pkg/util/network/cache.go

60 lines
1.2 KiB
Go
Raw Normal View History

package network
import (
"crypto/md5"
"encoding/hex"
"io/ioutil"
"os"
2019-07-01 02:31:34 +00:00
"strings"
2018-09-16 04:55:26 +00:00
"github.com/rancher/os/pkg/log"
)
2017-03-14 02:11:24 +00:00
const (
cacheDirectory = "/var/lib/rancher/cache/"
)
func locationHash(location string) string {
sum := md5.Sum([]byte(location))
return hex.EncodeToString(sum[:])
}
2017-03-14 02:11:24 +00:00
func cacheLookup(location string) []byte {
cacheFile := cacheDirectory + locationHash(location)
bytes, err := ioutil.ReadFile(cacheFile)
if err == nil {
log.Debugf("Using cached file: %s", cacheFile)
2017-03-14 02:11:24 +00:00
return bytes
}
2017-03-14 02:11:24 +00:00
return nil
}
2017-03-14 02:11:24 +00:00
func cacheAdd(location string, data []byte) {
tempFile, err := ioutil.TempFile(cacheDirectory, "")
if err != nil {
2017-03-14 02:11:24 +00:00
return
}
defer os.Remove(tempFile.Name())
_, err = tempFile.Write(data)
if err != nil {
2017-03-14 02:11:24 +00:00
return
}
2017-03-14 02:11:24 +00:00
cacheFile := cacheDirectory + locationHash(location)
os.Rename(tempFile.Name(), cacheFile)
}
2019-02-25 09:48:31 +00:00
2019-07-01 02:31:34 +00:00
func cacheMove(location string) (string, error) {
2019-02-25 09:48:31 +00:00
cacheFile := cacheDirectory + locationHash(location)
2019-07-01 02:31:34 +00:00
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"))
2019-02-25 09:48:31 +00:00
}