check: fix cachedir creation when using default location

This commit is contained in:
Michael Eischer 2024-06-30 12:03:09 +02:00
parent 132965fd5c
commit ce0cbc7a36
3 changed files with 24 additions and 6 deletions

View file

@ -7,3 +7,4 @@ The `check` command now attempts to create the cache directory before initializ
https://github.com/restic/restic/issues/4437 https://github.com/restic/restic/issues/4437
https://github.com/restic/restic/pull/4805 https://github.com/restic/restic/pull/4805
https://github.com/restic/restic/pull/4883

View file

@ -176,6 +176,7 @@ func prepareCheckCache(opts CheckOptions, gopts *GlobalOptions, printer progress
cachedir = cache.EnvDir() cachedir = cache.EnvDir()
} }
if cachedir != "" {
// use a cache in a temporary directory // use a cache in a temporary directory
err := os.MkdirAll(cachedir, 0755) err := os.MkdirAll(cachedir, 0755)
if err != nil { if err != nil {
@ -183,6 +184,7 @@ func prepareCheckCache(opts CheckOptions, gopts *GlobalOptions, printer progress
gopts.NoCache = true gopts.NoCache = true
return cleanup return cleanup
} }
}
tempdir, err := os.MkdirTemp(cachedir, "restic-check-cache-") tempdir, err := os.MkdirTemp(cachedir, "restic-check-cache-")
if err != nil { if err != nil {
// if an error occurs, don't use any cache // if an error occurs, don't use any cache

View file

@ -8,6 +8,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui/progress" "github.com/restic/restic/internal/ui/progress"
@ -229,3 +230,17 @@ func TestPrepareCheckCache(t *testing.T) {
}) })
} }
} }
func TestPrepareDefaultCheckCache(t *testing.T) {
gopts := GlobalOptions{CacheDir: ""}
cleanup := prepareCheckCache(CheckOptions{}, &gopts, &progress.NoopPrinter{})
_, err := os.ReadDir(gopts.CacheDir)
rtest.OK(t, err)
// Call the cleanup function to remove the temporary cache directory
cleanup()
// Verify that the cache directory has been removed
_, err = os.ReadDir(gopts.CacheDir)
rtest.Assert(t, errors.Is(err, os.ErrNotExist), "Expected cache directory to be removed, but it still exists")
}