Skip to content

Commit 6fb6e12

Browse files
author
Luca Bianconi
committed
fix: do not purge if file missing
1 parent 298e69f commit 6fb6e12

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

Diff for: buildcache/directory_cache.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ func (dc *directoryCache) basePath() *paths.Path {
3636
return paths.New(dc.baseDir)
3737
}
3838

39-
func (dc *directoryCache) isExpired(key string, ttl time.Duration) bool {
39+
func (dc *directoryCache) isExpired(key string) time.Duration {
4040
modTime, _ := dc.modTime(key)
41-
return time.Since(modTime) >= ttl
41+
return dc.ttl - time.Since(modTime)
4242
}
4343

4444
func (dc *directoryCache) modTime(key string) (time.Time, error) {
4545
fileInfo, err := dc.basePath().Join(key, lastUsedFileName).Stat()
4646
if err != nil {
47-
return time.Unix(0, 0), err
47+
// folders with a missing last used file are not purged
48+
return time.Now().Add(time.Minute), err
4849
}
4950

5051
return fileInfo.ModTime(), nil
@@ -75,20 +76,27 @@ func (dc *directoryCache) Purge() error {
7576
return err
7677
}
7778
for _, file := range files {
78-
if file.IsDir() && dc.isExpired(file.Base(), dc.ttl) {
79-
subDir := dc.basePath().Join(file.Base())
80-
modTime, _ := dc.modTime(file.Base())
81-
logrus.Tracef(`Purging cache directory "%s". Expired by %s\n`, subDir, (time.Since(modTime) - dc.ttl))
82-
err := subDir.RemoveAll()
83-
84-
if err != nil {
85-
logrus.Tracef(`Error while pruning cache directory "%s".\n%s\n`, subDir, errors.WithStack(err))
86-
}
79+
if file.IsDir() {
80+
dc.removeIfExpired(file)
8781
}
8882
}
8983
return nil
9084
}
9185

86+
func (dc *directoryCache) removeIfExpired(dir *paths.Path) {
87+
lifeExpectancy := dc.isExpired(dir.Base())
88+
if lifeExpectancy > 0 {
89+
return
90+
}
91+
subDir := dc.basePath().Join(dir.Base())
92+
logrus.Tracef(`Purging cache directory "%s". Expired by %s\n`, subDir, lifeExpectancy)
93+
err := subDir.RemoveAll()
94+
95+
if err != nil {
96+
logrus.Tracef(`Error while pruning cache directory "%s".\n%s\n`, subDir, errors.WithStack(err))
97+
}
98+
}
99+
92100
func newDirectoryCache(baseDir string, ttl time.Duration) *directoryCache {
93101
return &directoryCache{
94102
baseDir: baseDir,

Diff for: internal/integrationtest/compile_1/compile_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,25 @@ func compileWithCachePurgeNeeded(t *testing.T, env *integrationtest.Environment,
122122
// create directories that must be purged
123123
baseDir := paths.TempDir().Join("arduino", "sketches")
124124

125-
// purge case: hold file too old
125+
// purge case: last used file too old
126126
oldDir1 := baseDir.Join("test_old_sketch_1")
127127
require.NoError(t, os.MkdirAll(oldDir1.String(), 0770))
128128
require.NoError(t, oldDir1.Join(".last-used").WriteFile([]byte{}))
129129
require.NoError(t, os.Chtimes(oldDir1.Join(".last-used").String(), time.Now(), time.Unix(0, 0)))
130-
// purge case: hold file not existing
131-
oldDir2 := baseDir.Join("test_old_sketch_2")
132-
require.NoError(t, os.MkdirAll(oldDir2.String(), 0770))
130+
// no purge case: last used file not existing
131+
missingFileDir := baseDir.Join("test_sketch_2")
132+
require.NoError(t, os.MkdirAll(missingFileDir.String(), 0770))
133133

134-
defer os.RemoveAll(oldDir1.String())
135-
defer os.RemoveAll(oldDir2.String())
134+
defer oldDir1.RemoveAll()
135+
defer missingFileDir.RemoveAll()
136136

137137
customEnv := cli.GetDefaultEnv()
138138
customEnv["ARDUINO_BUILD_CACHE_COMPILATIONS_BEFORE_PURGE"] = "1"
139139
compileWithSimpleSketchCustomEnv(t, env, cli, customEnv)
140140

141141
// check that purge has been run
142142
require.NoFileExists(t, oldDir1.String())
143-
require.NoFileExists(t, oldDir2.String())
143+
require.DirExists(t, missingFileDir.String())
144144
}
145145

146146
func compileWithSimpleSketchCustomEnv(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI, customEnv map[string]string) {

Diff for: legacy/builder/phases/core_builder.go

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
121121

122122
// archive core.a
123123
if targetArchivedCore != nil && !ctx.OnlyUpdateCompilationDatabase {
124-
targetArchivedCore.Parent().MkdirAll()
125124
err := archiveFile.CopyTo(targetArchivedCore)
126125
buildcache.Used(targetArchivedCore.Parent())
127126
if ctx.Verbose {

0 commit comments

Comments
 (0)