Allow checking a read-only repo with --no-lock

This commit is contained in:
Alexander Neumann 2015-11-10 21:41:22 +01:00
parent acba82c8f7
commit c79dcbd7c4
2 changed files with 37 additions and 7 deletions

View File

@ -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)

View File

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