From e2d88b846a9bd3aade3340cfaf725ce0b7844fa8 Mon Sep 17 00:00:00 2001 From: Tim Ramlot <42113979+inteon@users.noreply.github.com> Date: Fri, 3 May 2024 09:38:43 +0200 Subject: [PATCH] also use cache dir as temporary directory, solving cross-disk copy errors Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> --- pkg/cache/clone.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pkg/cache/clone.go b/pkg/cache/clone.go index 88bf54c..6e8effa 100644 --- a/pkg/cache/clone.go +++ b/pkg/cache/clone.go @@ -12,7 +12,7 @@ import ( ) func calculateCacheKey(src mod.KloneSource) string { - return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s-%s-%s", src.RepoURL, src.RepoHash, src.RepoPath))))[:30] + return fmt.Sprintf("cache-%x", sha256.Sum256([]byte(fmt.Sprintf("%s-%s-%s", src.RepoURL, src.RepoHash, src.RepoPath))))[:30] } func getCacheDir() (string, error) { @@ -28,14 +28,6 @@ func getCacheDir() (string, error) { return filepath.Abs(filepath.Clean(filepath.Join(home, ".cache", "klone"))) } -func getTempDir() (string, error) { - if tempDir := os.Getenv("KLONE_TEMP_DIR"); tempDir != "" { - return filepath.Abs(filepath.Clean(tempDir)) - } - - return os.TempDir(), nil -} - func CloneWithCache( destPath string, src mod.KloneSource, @@ -46,17 +38,16 @@ func CloneWithCache( return err } + if err := os.MkdirAll(cacheDir, 0755); err != nil { + return err + } + cachePath := filepath.Join(cacheDir, calculateCacheKey(src)) if _, err := os.Stat(cachePath); err != nil && !os.IsNotExist(err) { return err } else if err != nil { - tmpParentDir, err := getTempDir() - if err != nil { - return err - } - - tempDir, err := os.MkdirTemp(tmpParentDir, "klone-*") + tempDir, err := os.MkdirTemp(cacheDir, "temp-*") if err != nil { return err }