Skip to content

Commit

Permalink
fix(local-cache-cleanup): more correct GC for ~/.local_cache/git_* data
Browse files Browse the repository at this point in the history
Do not remove base cache dirs, which could result in recreation of these base dirs with root-permissions.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jul 27, 2022
1 parent b20ccd7 commit e93bb73
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 27 deletions.
24 changes: 18 additions & 6 deletions pkg/git_repo/gitdata/gc.go
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions pkg/git_repo/gitdata/git_archive.go
Expand Up @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/git_repo/gitdata/git_data_entry.go
Expand Up @@ -6,6 +6,7 @@ type GitDataEntry interface {
GetPaths() []string
GetSize() uint64
GetLastAccessAt() time.Time
GetCacheBasePath() string
}

type GitDataLruSort []GitDataEntry
Expand Down
13 changes: 9 additions & 4 deletions pkg/git_repo/gitdata/git_patch.go
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
18 changes: 12 additions & 6 deletions pkg/git_repo/gitdata/git_repo.go
Expand Up @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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,
})
}

Expand Down
18 changes: 12 additions & 6 deletions pkg/git_repo/gitdata/git_worktree.go
Expand Up @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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,
})
}
}
Expand Down

0 comments on commit e93bb73

Please sign in to comment.