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,12 +176,14 @@ func prepareCheckCache(opts CheckOptions, gopts *GlobalOptions, printer progress
cachedir = cache.EnvDir() cachedir = cache.EnvDir()
} }
// use a cache in a temporary directory if cachedir != "" {
err := os.MkdirAll(cachedir, 0755) // use a cache in a temporary directory
if err != nil { err := os.MkdirAll(cachedir, 0755)
Warnf("unable to create cache directory %s, disabling cache: %v\n", cachedir, err) if err != nil {
gopts.NoCache = true Warnf("unable to create cache directory %s, disabling cache: %v\n", cachedir, err)
return cleanup gopts.NoCache = true
return cleanup
}
} }
tempdir, err := os.MkdirTemp(cachedir, "restic-check-cache-") tempdir, err := os.MkdirTemp(cachedir, "restic-check-cache-")
if err != nil { if err != nil {

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")
}