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:23:28 +0300 |
commit | 15fb0dcbc902e88589084577896fd59ddb61b058 (patch) | |
tree | f21486aa609fca60814381c9a0efea447e093b42 | |
parent | 5cba52f4acb04ddbe27d8b7cb2e936ea0be45ae1 (diff) |
tempdir: Convert cleaning to use `os.ReadDir()`
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 `tempdir` package's clean walker to use the new function
and manually stat the directory entries as required.
-rw-r--r-- | internal/tempdir/clean.go | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/internal/tempdir/clean.go b/internal/tempdir/clean.go index 2edfcc929..7009a443a 100644 --- a/internal/tempdir/clean.go +++ b/internal/tempdir/clean.go @@ -1,10 +1,10 @@ package tempdir -//nolint:depguard import ( "context" + "errors" "fmt" - "io/ioutil" + "io/fs" "os" "path/filepath" "strings" @@ -71,7 +71,7 @@ func clean(locator storage.Locator, storage config.Storage) error { panic(invalidCleanRoot("invalid tempdir clean root: panicking to prevent data loss")) } - entries, err := ioutil.ReadDir(dir) + entries, err := os.ReadDir(dir) if os.IsNotExist(err) { return nil } @@ -79,7 +79,18 @@ func clean(locator storage.Locator, storage config.Storage) error { return err } - for _, info := range entries { + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + // It's fine if the entry has disappeared meanwhile, we wanted to remove it + // anyway. + if errors.Is(err, fs.ErrNotExist) { + continue + } + + return fmt.Errorf("statting tempdir entry: %w", err) + } + if time.Since(info.ModTime()) < maxAge { continue } |