From c79dcbd7c47a86d0b37bd50b7f755ffd67467c91 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 10 Nov 2015 21:41:22 +0100 Subject: [PATCH] Allow checking a read-only repo with --no-lock --- cmd/restic/cmd_check.go | 17 ++++++++++------- cmd/restic/integration_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index a94ad138b..3e437b315 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -9,8 +9,9 @@ import ( ) type CmdCheck struct { - ReadData bool `long:"read-data" description:"Read data blobs" default:"false"` - CheckUnused bool `long:"check-unused" description:"Check for unused blobs" default:"false"` + ReadData bool `long:"read-data" default:"false" description:"Read data blobs"` + CheckUnused bool `long:"check-unused" default:"false" description:"Check for unused blobs"` + NoLock bool `long:"no-lock" default:"false" description:"Do not lock repository, this allows checking read-only"` global *GlobalOptions } @@ -39,11 +40,13 @@ func (cmd CmdCheck) Execute(args []string) error { return err } - cmd.global.Verbosef("Create exclusive lock for repository\n") - lock, err := lockRepoExclusive(repo) - defer unlockRepo(lock) - if err != nil { - return err + if !cmd.NoLock { + cmd.global.Verbosef("Create exclusive lock for repository\n") + lock, err := lockRepoExclusive(repo) + defer unlockRepo(lock) + if err != nil { + return err + } } chkr := checker.New(repo) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 8d01849eb..3cbfde18a 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -103,6 +103,16 @@ func cmdCheckOutput(t testing.TB, global GlobalOptions) string { return string(buf.Bytes()) } +func cmdCheckNoLock(t testing.TB, global GlobalOptions) { + cmd := &CmdCheck{ + global: &global, + ReadData: true, + CheckUnused: true, + NoLock: true, + } + OK(t, cmd.Execute(nil)) +} + func cmdRebuildIndex(t testing.TB, global GlobalOptions) { global.stdout = ioutil.Discard cmd := &CmdRebuildIndex{global: &global} @@ -788,3 +798,20 @@ func TestOptimizeRemoveUnusedBlobs(t *testing.T) { }) } } + +func TestCheckRestoreNoLock(t *testing.T) { + withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { + datafile := filepath.Join("testdata", "repo-restore-permissions-test.tar.gz") + SetupTarTestFixture(t, env.base, datafile) + + err := filepath.Walk(env.repo, func(p string, fi os.FileInfo, e error) error { + if e != nil { + return e + } + return os.Chmod(p, fi.Mode() & ^(os.FileMode(0222))) + }) + OK(t, err) + + cmdCheckNoLock(t, global) + }) +}