diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-09-28 16:06:43 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-09-29 12:25:19 +0300 |
commit | e527850187ba1083798fa7e4810f6952c33f0119 (patch) | |
tree | 93049a1d3931900b11d2b241cbc064f61d313f64 | |
parent | 2ee852d39dc6fa8940564f93734913e5fa81b6af (diff) |
cache: Convert file cleanup to use `os.ReadDir()`pks-refactor-ioutil-readdir-usages
Go 1.15 has deprecated the `ioutil` package and made replacements
available in the `io` or `os` packages. While we have already migrated
most callsites away, one notable omission was `ioutil.ReadDir()` as this
function doesn't have an exact replacement: the new `os.ReadDir()`
function does not stat all directory entries anymore.
Refactor the cleanup of stale cached files in the `cache` package to use
the new function.
-rw-r--r-- | internal/cache/walker.go | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/internal/cache/walker.go b/internal/cache/walker.go index 6a0b065df..b14c8adf6 100644 --- a/internal/cache/walker.go +++ b/internal/cache/walker.go @@ -5,10 +5,10 @@ // worker will walk the cache directory every ten minutes. package cache -//nolint:depguard import ( + "errors" "fmt" - "io/ioutil" + "io/fs" "os" "path/filepath" "time" @@ -31,7 +31,7 @@ func (c *DiskCache) cleanWalk(path string) error { defer time.Sleep(100 * time.Microsecond) // relieve pressure c.walkerCheckTotal.Inc() - entries, err := ioutil.ReadDir(path) + entries, err := os.ReadDir(path) if err != nil { if os.IsNotExist(err) { return nil @@ -50,8 +50,19 @@ func (c *DiskCache) cleanWalk(path string) error { continue } + info, err := e.Info() + if err != nil { + // The file may have been cleaned up already, so we just ignore it as we + // wanted to remove it anyway. + if errors.Is(err, fs.ErrNotExist) { + continue + } + + return fmt.Errorf("statting cached file: %w", err) + } + c.walkerCheckTotal.Inc() - if time.Since(e.ModTime()) < staleAge { + if time.Since(info.ModTime()) < staleAge { continue // still fresh } |