mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-10 18:10:49 +09:00
GO-4701 Add tmp cleanup (#2276)
This commit is contained in:
parent
bd74a6218a
commit
5c3474860e
2 changed files with 79 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/anyproto/any-sync/app"
|
||||
|
||||
|
@ -54,8 +55,36 @@ func (s *TempDirService) TempDir() string {
|
|||
s.tempDir = os.TempDir()
|
||||
} else {
|
||||
s.tempDir = path
|
||||
go s.cleanUp()
|
||||
}
|
||||
})
|
||||
|
||||
return s.tempDir
|
||||
}
|
||||
|
||||
func (s *TempDirService) cleanUp() {
|
||||
cutoff := time.Now().Add(-72 * time.Hour)
|
||||
recursiveCleanup(s.tempDir, cutoff)
|
||||
}
|
||||
|
||||
func recursiveCleanup(path string, cutoff time.Time) {
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
log.Warnf("tmp cleanup readdir: %v", err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
fullEntryPath := filepath.Join(path, entry.Name())
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Warnf("tmp cleanup entry: %v", err)
|
||||
}
|
||||
if entry.IsDir() {
|
||||
recursiveCleanup(fullEntryPath, cutoff)
|
||||
} else if info.ModTime().IsZero() || info.ModTime().Before(cutoff) {
|
||||
err = os.RemoveAll(fullEntryPath)
|
||||
if err != nil {
|
||||
log.Warnf("tmp cleanup delete: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
50
pkg/lib/core/tmp_dir_test.go
Normal file
50
pkg/lib/core/tmp_dir_test.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTempDirService(t *testing.T) {
|
||||
t.Run("cleanup", func(t *testing.T) {
|
||||
// given
|
||||
s := NewTempDirService()
|
||||
s.tempDir = t.TempDir()
|
||||
|
||||
oldFile := filepath.Join(s.tempDir, "old.txt")
|
||||
require.NoError(t, os.WriteFile(oldFile, []byte("old"), 0600))
|
||||
oldTime := time.Now().Add(-100 * time.Hour)
|
||||
require.NoError(t, os.Chtimes(oldFile, oldTime, oldTime))
|
||||
|
||||
newFile := filepath.Join(s.tempDir, "new.txt")
|
||||
require.NoError(t, os.WriteFile(newFile, []byte("new"), 0600))
|
||||
|
||||
nestedDir := filepath.Join(s.tempDir, "nested")
|
||||
require.NoError(t, os.MkdirAll(nestedDir, 0755))
|
||||
nestedOldFile := filepath.Join(nestedDir, "nested_old.txt")
|
||||
require.NoError(t, os.WriteFile(nestedOldFile, []byte("nested old"), 0600))
|
||||
require.NoError(t, os.Chtimes(nestedOldFile, oldTime, oldTime))
|
||||
nestedNewFile := filepath.Join(nestedDir, "nested_new.txt")
|
||||
require.NoError(t, os.WriteFile(nestedNewFile, []byte("nested new"), 0600))
|
||||
|
||||
// when
|
||||
s.cleanUp()
|
||||
|
||||
// then
|
||||
_, err := os.Stat(oldFile)
|
||||
require.True(t, os.IsNotExist(err), "old file should be deleted")
|
||||
|
||||
_, err = os.Stat(newFile)
|
||||
require.NoError(t, err, "new file should remain")
|
||||
|
||||
_, err = os.Stat(nestedOldFile)
|
||||
require.True(t, os.IsNotExist(err), "nested old file should be deleted")
|
||||
|
||||
_, err = os.Stat(nestedNewFile)
|
||||
require.NoError(t, err, "nested new file should remain")
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue