Skip to content
Permalink
Browse files
fix(local-cache-cleanup): more correct GC for ~/.local_cache/git_* data
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 e93bb73d046a4642005f0d3d306b837ed32e68fd
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 27 deletions.
@@ -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)
}
}
}

@@ -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 {
@@ -6,6 +6,7 @@ type GitDataEntry interface {
GetPaths() []string
GetSize() uint64
GetLastAccessAt() time.Time
GetCacheBasePath() string
}

type GitDataLruSort []GitDataEntry
@@ -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

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

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

0 comments on commit e93bb73

Please sign in to comment.