Skip to content

Commit

Permalink
Don't remove file with epoch atime.
Browse files Browse the repository at this point in the history
Files initially created on some filesystems (ZFS) may have an access
time of the Unix epoch.

Addresses anatol#119
  • Loading branch information
chennin committed Nov 18, 2024
1 parent 2c24be4 commit 156dc1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func purgeStaleFiles(cacheDir string, purgeFilesAfter int, repoName string) {

t := times.Get(info)
atime := t.AccessTime()
if atime.Before(removeIfOlder) {
if atime.Before(removeIfOlder) && atime.After(time.Unix(0, 0).UTC()) {
log.Printf("Remove stale file %v as its access time (%v) is too old", path, atime)
if err := os.Remove(path); err != nil {
log.Print(err)
Expand Down
13 changes: 11 additions & 2 deletions purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestPurge(t *testing.T) {
fileToRemove := path.Join(testRepo, "toremove")
fileToStay := path.Join(testRepo, "tostay")
fileToBePurgedLater := path.Join(testRepo, "tobepurgedlater")
fileWithZeroAccessTime := path.Join(testRepo, "zeroaccesstime")
fileOutsideRepo := path.Join(testPacolocoDir, "outsiderepo")

thresholdTime := time.Now().Add(time.Duration(-purgeFilesAfter) * time.Second)
Expand All @@ -52,13 +53,19 @@ func TestPurge(t *testing.T) {
require.NoError(t, err)
os.Chtimes(fileToBePurgedLater, thresholdTime.Add(time.Hour), thresholdTime.Add(-time.Hour))

os.Create(fileWithZeroAccessTime)
pkgFileContent = "leave me too"
require.NoError(t, os.WriteFile(fileWithZeroAccessTime, []byte(pkgFileContent), os.ModePerm))
infoZeroAccessTime, err := os.Stat(fileWithZeroAccessTime)
os.Chtimes(fileWithZeroAccessTime, time.Unix(0, 0).UTC(), thresholdTime.Add(-time.Hour))

os.Create(fileOutsideRepo)
pkgFileContent = "don't touch me"
require.NoError(t, os.WriteFile(fileOutsideRepo, []byte(pkgFileContent), os.ModePerm))
os.Chtimes(fileOutsideRepo, thresholdTime.Add(-time.Hour), thresholdTime.Add(-time.Hour))

expectedPackageNum := float64(2)
expectedSize := float64(infoToStay.Size() + infoToBePurgedLater.Size())
expectedPackageNum := float64(3)
expectedSize := float64(infoToStay.Size() + infoToBePurgedLater.Size() + infoZeroAccessTime.Size())

purgeStaleFiles(testPacolocoDir, purgeFilesAfter, "purgerepo")

Expand All @@ -69,6 +76,8 @@ func TestPurge(t *testing.T) {
require.NoError(t, err)
_, err = os.Stat(fileToBePurgedLater)
require.NoError(t, err)
_, err = os.Stat(fileWithZeroAccessTime)
require.NoError(t, err)
// files outside of the pkgs cache should not be touched
_, err = os.Stat(fileOutsideRepo)
require.NoError(t, err)
Expand Down

0 comments on commit 156dc1d

Please sign in to comment.