1
0
mirror of https://github.com/rancher/os.git synced 2025-08-31 22:32:14 +00:00

Revert "WIP New ros cli"

This commit is contained in:
Sven Dowideit
2017-03-14 12:11:24 +10:00
committed by GitHub
parent 4cd73c111e
commit 2d92956c82
13 changed files with 134 additions and 622 deletions

30
util/network/cache.go Executable file → Normal file
View File

@@ -5,45 +5,41 @@ import (
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"github.com/rancher/os/config"
"github.com/rancher/os/log"
)
const (
cacheDirectory = "/var/lib/rancher/cache/"
)
func locationHash(location string) string {
sum := md5.Sum([]byte(location))
return hex.EncodeToString(sum[:])
}
func CacheLookup(location string) ([]byte, error) {
cacheFile := filepath.Join(config.CacheDirectory, location)
func cacheLookup(location string) []byte {
cacheFile := cacheDirectory + locationHash(location)
bytes, err := ioutil.ReadFile(cacheFile)
if err == nil {
log.Debugf("Using cached file: %s", cacheFile)
return bytes, nil
return bytes
}
log.Debugf("Cached file not found: %s", cacheFile)
return nil, err
return nil
}
func cacheAdd(location string, data []byte) error {
os.MkdirAll(config.CacheDirectory, 0755)
tempFile, err := ioutil.TempFile(config.CacheDirectory, "")
func cacheAdd(location string, data []byte) {
tempFile, err := ioutil.TempFile(cacheDirectory, "")
if err != nil {
return err
return
}
defer os.Remove(tempFile.Name())
_, err = tempFile.Write(data)
if err != nil {
return err
return
}
cacheFile := filepath.Join(config.CacheDirectory, location)
cacheDir := filepath.Dir(cacheFile)
log.Debugf("writing %s to %s", cacheFile, cacheDir)
os.MkdirAll(cacheDir, 0755)
cacheFile := cacheDirectory + locationHash(location)
os.Rename(tempFile.Name(), cacheFile)
return nil
}