restic/cmd/restic/lock.go

62 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
2017-06-04 11:16:55 +02:00
"context"
2015-07-12 22:10:01 +02:00
"sync"
"github.com/restic/restic/internal/repository"
2017-07-24 17:42:25 +02:00
"github.com/restic/restic/internal/restic"
)
2015-07-12 22:10:01 +02:00
var globalLocks struct {
sync.Once
2015-07-12 22:10:01 +02:00
}
func internalOpenWithLocked(ctx context.Context, gopts GlobalOptions, dryRun bool, exclusive bool) (context.Context, *repository.Repository, func(), error) {
repo, err := OpenRepository(ctx, gopts)
if err != nil {
return nil, nil, nil, err
}
unlock := func() {}
if !dryRun {
var lock *restic.Lock
2024-02-24 16:26:29 +01:00
// make sure that a repository is unlocked properly and after cancel() was
// called by the cleanup handler in global.go
globalLocks.Do(func() {
AddCleanupHandler(repository.UnlockAll)
})
lock, ctx, err = repository.Lock(ctx, repo, exclusive, gopts.RetryLock, func(msg string) {
if !gopts.JSON {
Verbosef("%s", msg)
}
}, Warnf)
unlock = func() {
2024-02-24 16:26:29 +01:00
repository.Unlock(lock)
}
if err != nil {
return nil, nil, nil, err
}
} else {
repo.SetDryRun()
}
return ctx, repo, unlock, nil
}
func openWithReadLock(ctx context.Context, gopts GlobalOptions, noLock bool) (context.Context, *repository.Repository, func(), error) {
// TODO enfore read-only operations once the locking code has moved to the repository
return internalOpenWithLocked(ctx, gopts, noLock, false)
}
func openWithAppendLock(ctx context.Context, gopts GlobalOptions, dryRun bool) (context.Context, *repository.Repository, func(), error) {
// TODO enfore non-exclusive operations once the locking code has moved to the repository
return internalOpenWithLocked(ctx, gopts, dryRun, false)
}
func openWithExclusiveLock(ctx context.Context, gopts GlobalOptions, dryRun bool) (context.Context, *repository.Repository, func(), error) {
return internalOpenWithLocked(ctx, gopts, dryRun, true)
}