diff --git a/pkg/git_repo/gitdata/gc.go b/pkg/git_repo/gitdata/gc.go index da77662aac..f71612c6a1 100644 --- a/pkg/git_repo/gitdata/gc.go +++ b/pkg/git_repo/gitdata/gc.go @@ -207,9 +207,8 @@ func RunGC(ctx context.Context, allowedVolumeUsagePercentage, allowedVolumeUsage var freedBytes uint64 for _, entry := range gitDataEntries { for _, path := range entry.GetPaths() { - logboek.Context(ctx).LogF("Removing %s\n", path) - - if err := RemovePathWithEmptyParentDirsInsideScope(werf.GetLocalCacheDir(), path); err != nil { + logboek.Context(ctx).LogF("Removing %q inside scope %q\n", path, entry.GetCacheBasePath()) + if err := RemovePathWithEmptyParentDirsInsideScope(entry.GetCacheBasePath(), path); err != nil { return fmt.Errorf("unable to remove %q: %w", path, err) } } @@ -260,6 +259,8 @@ func RemovePathWithEmptyParentDirsInsideScope(scopeDir, path string) error { } func wipeCacheDirs(ctx context.Context, cacheRootDir string, keepCacheVersions []string) error { + logboek.Context(ctx).Debug().LogF("wipeCacheDirs %q\n", cacheRootDir) + if _, err := os.Stat(cacheRootDir); os.IsNotExist(err) { return nil } else if err != nil { @@ -275,13 +276,24 @@ WipeCacheDirs: for _, finfo := range dirs { for _, keepCacheVersion := range keepCacheVersions { if finfo.Name() == keepCacheVersion { + logboek.Context(ctx).Debug().LogF("wipeCacheDirs in %q: keep cache version %q\n", cacheRootDir, keepCacheVersion) continue WipeCacheDirs } } - path := filepath.Join(cacheRootDir, finfo.Name()) - if err := os.RemoveAll(path); err != nil { - return fmt.Errorf("unable to remove %q: %w", path, err) + versionedCacheDir := filepath.Join(cacheRootDir, finfo.Name()) + subdirs, err := ioutil.ReadDir(versionedCacheDir) + if err != nil { + return fmt.Errorf("error reading dir %q: %w", versionedCacheDir, err) + } + + for _, cacheFile := range subdirs { + path := filepath.Join(versionedCacheDir, cacheFile.Name()) + + logboek.Context(ctx).Debug().LogF("wipeCacheDirs in %q: removing %q\n", cacheRootDir, path) + if err := os.RemoveAll(path); err != nil { + return fmt.Errorf("unable to remove %q: %w", path, err) + } } } diff --git a/pkg/git_repo/gitdata/git_archive.go b/pkg/git_repo/gitdata/git_archive.go index 88a9288377..499ad13444 100644 --- a/pkg/git_repo/gitdata/git_archive.go +++ b/pkg/git_repo/gitdata/git_archive.go @@ -11,10 +11,11 @@ import ( ) type GitArchiveDesc struct { - MetadataPath string - ArchivePath string - Metadata *ArchiveMetadata - Size uint64 + MetadataPath string + ArchivePath string + Metadata *ArchiveMetadata + Size uint64 + CacheBasePath string } func (entry *GitArchiveDesc) GetPaths() []string { @@ -29,6 +30,10 @@ func (entry *GitArchiveDesc) GetLastAccessAt() time.Time { return time.Unix(entry.Metadata.LastAccessTimestamp, 0) } +func (entry *GitArchiveDesc) GetCacheBasePath() string { + return entry.CacheBasePath +} + func GetExistingGitArchives(cacheVersionRoot string) ([]*GitArchiveDesc, error) { var res []*GitArchiveDesc @@ -84,7 +89,7 @@ func GetExistingGitArchives(cacheVersionRoot string) ([]*GitArchiveDesc, error) continue } - desc := &GitArchiveDesc{MetadataPath: path} + desc := &GitArchiveDesc{MetadataPath: path, CacheBasePath: cacheVersionRoot} res = append(res, desc) if data, err := ioutil.ReadFile(path); err != nil { diff --git a/pkg/git_repo/gitdata/git_data_entry.go b/pkg/git_repo/gitdata/git_data_entry.go index fcfe7c7bc7..e950b39e7a 100644 --- a/pkg/git_repo/gitdata/git_data_entry.go +++ b/pkg/git_repo/gitdata/git_data_entry.go @@ -6,6 +6,7 @@ type GitDataEntry interface { GetPaths() []string GetSize() uint64 GetLastAccessAt() time.Time + GetCacheBasePath() string } type GitDataLruSort []GitDataEntry diff --git a/pkg/git_repo/gitdata/git_patch.go b/pkg/git_repo/gitdata/git_patch.go index 7224df9d2f..0aec3f9e5e 100644 --- a/pkg/git_repo/gitdata/git_patch.go +++ b/pkg/git_repo/gitdata/git_patch.go @@ -11,10 +11,11 @@ import ( ) type GitPatchDesc struct { - MetadataPath string - PatchPath string - Metadata *PatchMetadata - Size uint64 + MetadataPath string + PatchPath string + Metadata *PatchMetadata + Size uint64 + CacheBasePath string } func (entry *GitPatchDesc) GetPaths() []string { @@ -29,6 +30,10 @@ func (entry *GitPatchDesc) GetLastAccessAt() time.Time { return time.Unix(entry.Metadata.LastAccessTimestamp, 0) } +func (entry *GitPatchDesc) GetCacheBasePath() string { + return entry.CacheBasePath +} + func GetExistingGitPatches(cacheVersionRoot string) ([]*GitPatchDesc, error) { var res []*GitPatchDesc diff --git a/pkg/git_repo/gitdata/git_repo.go b/pkg/git_repo/gitdata/git_repo.go index 391fbd5b3f..20ed711fea 100644 --- a/pkg/git_repo/gitdata/git_repo.go +++ b/pkg/git_repo/gitdata/git_repo.go @@ -12,9 +12,10 @@ import ( ) type GitRepoDesc struct { - Path string - LastAccessAt time.Time - Size uint64 + Path string + LastAccessAt time.Time + Size uint64 + CacheBasePath string } func (entry *GitRepoDesc) GetPaths() []string { @@ -29,6 +30,10 @@ func (entry *GitRepoDesc) GetLastAccessAt() time.Time { return entry.LastAccessAt } +func (entry *GitRepoDesc) GetCacheBasePath() string { + return entry.CacheBasePath +} + func GetExistingGitRepos(cacheVersionRoot string) ([]*GitRepoDesc, error) { var res []*GitRepoDesc @@ -64,9 +69,10 @@ func GetExistingGitRepos(cacheVersionRoot string) ([]*GitRepoDesc, error) { } res = append(res, &GitRepoDesc{ - Path: repoPath, - Size: size, - LastAccessAt: lastAccessAt, + Path: repoPath, + Size: size, + LastAccessAt: lastAccessAt, + CacheBasePath: cacheVersionRoot, }) } diff --git a/pkg/git_repo/gitdata/git_worktree.go b/pkg/git_repo/gitdata/git_worktree.go index 46a1008f18..ef9fedf37a 100644 --- a/pkg/git_repo/gitdata/git_worktree.go +++ b/pkg/git_repo/gitdata/git_worktree.go @@ -12,9 +12,10 @@ import ( ) type GitWorktreeDesc struct { - Path string - LastAccessAt time.Time - Size uint64 + Path string + LastAccessAt time.Time + Size uint64 + CacheBasePath string } func (entry *GitWorktreeDesc) GetPaths() []string { @@ -29,6 +30,10 @@ func (entry *GitWorktreeDesc) GetLastAccessAt() time.Time { return entry.LastAccessAt } +func (entry *GitWorktreeDesc) GetCacheBasePath() string { + return entry.CacheBasePath +} + func GetExistingGitWorktrees(cacheVersionRoot string) ([]*GitWorktreeDesc, error) { var res []*GitWorktreeDesc @@ -65,9 +70,10 @@ func GetExistingGitWorktrees(cacheVersionRoot string) ([]*GitWorktreeDesc, error } res = append(res, &GitWorktreeDesc{ - Path: worktreePath, - Size: size, - LastAccessAt: lastAccessAt, + Path: worktreePath, + Size: size, + LastAccessAt: lastAccessAt, + CacheBasePath: dir, }) } }