Skip to content

Commit 80edb72

Browse files
author
Luca Bianconi
committed
refactor: just simple
1 parent d7cacff commit 80edb72

File tree

5 files changed

+41
-110
lines changed

5 files changed

+41
-110
lines changed

Diff for: buildcache/build_cache.go

+38-7
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,51 @@ import (
1919
"time"
2020

2121
"github.com/arduino/go-paths-helper"
22+
"github.com/pkg/errors"
23+
"github.com/sirupsen/logrus"
2224
)
2325

24-
// GetOrCreate retrieves or creates the cache directory for the given key inside basePath.
26+
const lastUsedFileName = ".last-used"
27+
28+
// GetOrCreate retrieves or creates the cache directory at the given path
2529
// If the cache already exists the lifetime of the cache is extended.
26-
func GetOrCreate(dir *paths.Path, key string) error {
27-
unusedTTL := time.Hour
28-
_, err := newDirectoryCache(dir, unusedTTL).
29-
GetOrCreate(key, time.Now())
30-
return err
30+
func GetOrCreate(dir *paths.Path) error {
31+
if !dir.Exist() {
32+
if err := dir.MkdirAll(); err != nil {
33+
return err
34+
}
35+
}
36+
37+
return dir.Join(lastUsedFileName).WriteFile([]byte{})
3138
}
3239

3340
// Purge removes all cache directories within baseDir that have expired
3441
// To know how long ago a directory has been last used
3542
// it checks into the .last-used file.
3643
func Purge(baseDir *paths.Path, ttl time.Duration) {
37-
newDirectoryCache(baseDir, ttl).Purge()
44+
files, err := baseDir.ReadDir()
45+
if err != nil {
46+
return
47+
}
48+
for _, file := range files {
49+
if file.IsDir() {
50+
removeIfExpired(file, ttl)
51+
}
52+
}
53+
}
54+
55+
func removeIfExpired(dir *paths.Path, ttl time.Duration) {
56+
fileInfo, err := dir.Join().Stat()
57+
if err != nil {
58+
return
59+
}
60+
lifeExpectancy := ttl - time.Since(fileInfo.ModTime())
61+
if lifeExpectancy > 0 {
62+
return
63+
}
64+
logrus.Tracef(`Purging cache directory "%s". Expired by %s`, dir, lifeExpectancy.Abs())
65+
err = dir.RemoveAll()
66+
if err != nil {
67+
logrus.Tracef(`Error while pruning cache directory "%s": %s`, dir, errors.WithStack(err))
68+
}
3869
}

Diff for: buildcache/build_cache_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Test_UpdateLastUsedFileExisting(t *testing.T) {
4343
}
4444

4545
func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) {
46-
require.NoError(t, GetOrCreate(dir.Parent(), dir.Base()))
46+
require.NoError(t, GetOrCreate(dir))
4747
expectedFile := dir.Join(lastUsedFileName)
4848
fileInfo, err := expectedFile.Stat()
4949
require.Nil(t, err)

Diff for: buildcache/directory_cache.go

-100
This file was deleted.

Diff for: commands/compile/compile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
138138
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
139139
}
140140

141-
buildcache.GetOrCreate(builderCtx.BuildPath.Parent(), builderCtx.BuildPath.Base())
141+
buildcache.GetOrCreate(builderCtx.BuildPath)
142142
// cache is purged after compilation to not remove entries that might be required
143143
defer maybePurgeBuildCache()
144144

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
9595
if buildCachePath != nil {
9696
archivedCoreName := GetCachedCoreArchiveDirName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN),
9797
buildProperties.Get("compiler.optimization_flags"), realCoreFolder)
98-
buildcache.GetOrCreate(buildCachePath, archivedCoreName)
98+
buildcache.GetOrCreate(buildCachePath.Join(archivedCoreName))
9999
targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a")
100100
canUseArchivedCore := !ctx.OnlyUpdateCompilationDatabase &&
101101
!ctx.Clean &&

0 commit comments

Comments
 (0)