2023-12-06 04:47:41 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-12-16 14:19:53 +00:00
|
|
|
"fmt"
|
2023-12-06 04:47:41 +00:00
|
|
|
"io"
|
2023-12-16 14:19:53 +00:00
|
|
|
"log/slog"
|
2023-12-06 04:47:41 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2023-12-06 04:47:41 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type hashInfo struct {
|
|
|
|
Modified time.Time
|
|
|
|
Hash string
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
var hashCache *xsync.MapOf[string, hashInfo]
|
2023-12-06 04:47:41 +00:00
|
|
|
|
|
|
|
var integrityFilename = ".integrity"
|
|
|
|
|
|
|
|
func getFileHash(file string) (string, error) {
|
|
|
|
if hashCache == nil {
|
|
|
|
loadHashCache()
|
|
|
|
}
|
2023-12-07 16:57:31 +00:00
|
|
|
cachedHash, ok := hashCache.Load(file)
|
2023-12-06 04:47:41 +00:00
|
|
|
if !ok {
|
|
|
|
return cacheFileHash(file)
|
|
|
|
}
|
|
|
|
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
|
|
|
|
stat, err := os.Stat(filepath.Join(downloadCache, file))
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return "", fmt.Errorf("failed to stat file: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
if stat.Size() != cachedHash.Size || stat.ModTime() != cachedHash.Modified {
|
|
|
|
return cacheFileHash(file)
|
|
|
|
}
|
|
|
|
return cachedHash.Hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cacheFileHash(file string) (string, error) {
|
|
|
|
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
|
|
|
|
stat, err := os.Stat(filepath.Join(downloadCache, file))
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return "", fmt.Errorf("failed to stat file: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
f, err := os.Open(filepath.Join(downloadCache, file))
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return "", fmt.Errorf("failed to open file: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
hash, err := utils.SHA256Data(f)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return "", fmt.Errorf("failed to hash file: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
2023-12-07 16:57:31 +00:00
|
|
|
hashCache.Store(file, hashInfo{
|
2023-12-06 04:47:41 +00:00
|
|
|
Hash: hash,
|
|
|
|
Size: stat.Size(),
|
|
|
|
Modified: stat.ModTime(),
|
2023-12-07 16:57:31 +00:00
|
|
|
})
|
2023-12-06 04:47:41 +00:00
|
|
|
saveHashCache()
|
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadHashCache() {
|
2023-12-07 16:57:31 +00:00
|
|
|
hashCache = xsync.NewMapOf[string, hashInfo]()
|
2023-12-06 04:47:41 +00:00
|
|
|
cacheFile := filepath.Join(viper.GetString("cache-dir"), "downloadCache", integrityFilename)
|
|
|
|
if _, err := os.Stat(cacheFile); os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err := os.Open(cacheFile)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
slog.Warn("failed to open hash cache, recreating", slog.Any("err", err))
|
2023-12-06 04:47:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
hashCacheJSON, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
slog.Warn("failed to read hash cache, recreating", slog.Any("err", err))
|
2023-12-06 04:47:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(hashCacheJSON, &hashCache); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
slog.Warn("failed to unmarshal hash cache, recreating", slog.Any("err", err))
|
2023-12-06 04:47:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveHashCache() {
|
|
|
|
cacheFile := filepath.Join(viper.GetString("cache-dir"), "downloadCache", integrityFilename)
|
2023-12-07 16:57:31 +00:00
|
|
|
plainCache := make(map[string]hashInfo, hashCache.Size())
|
|
|
|
hashCache.Range(func(k string, v hashInfo) bool {
|
|
|
|
plainCache[k] = v
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
hashCacheJSON, err := json.Marshal(plainCache)
|
2023-12-06 04:47:41 +00:00
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
slog.Warn("failed to marshal hash cache", slog.Any("err", err))
|
2023-12-06 04:47:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(cacheFile, hashCacheJSON, 0o755); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
slog.Warn("failed to write hash cache", slog.Any("err", err))
|
2023-12-06 04:47:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|