2015-06-27 13:05:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2015-07-12 20:10:01 +00:00
|
|
|
"sync"
|
2015-06-27 13:05:20 +00:00
|
|
|
|
2024-02-24 14:19:02 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2015-06-27 13:05:20 +00:00
|
|
|
)
|
|
|
|
|
2015-07-12 20:10:01 +00:00
|
|
|
var globalLocks struct {
|
2020-08-09 11:54:39 +00:00
|
|
|
sync.Once
|
2015-07-12 20:10:01 +00:00
|
|
|
}
|
2015-06-27 13:05:20 +00:00
|
|
|
|
2024-02-24 14:19:02 +00: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 15:26:29 +00: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)
|
2024-02-24 14:19:02 +00:00
|
|
|
unlock = func() {
|
2024-02-24 15:26:29 +00:00
|
|
|
repository.Unlock(lock)
|
2024-02-24 14:19:02 +00:00
|
|
|
}
|
|
|
|
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)
|
2015-06-27 13:05:20 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 14:19:02 +00:00
|
|
|
func openWithExclusiveLock(ctx context.Context, gopts GlobalOptions, dryRun bool) (context.Context, *repository.Repository, func(), error) {
|
|
|
|
return internalOpenWithLocked(ctx, gopts, dryRun, true)
|
2023-02-16 15:58:36 +00:00
|
|
|
}
|