2015-06-27 15:05:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-04 11:16:55 +02:00
|
|
|
"context"
|
2015-06-27 15:05:20 +02:00
|
|
|
|
2024-02-24 15:19:02 +01:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2015-06-27 15:05:20 +02:00
|
|
|
)
|
|
|
|
|
2024-02-24 15:19:02 +01: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 {
|
2024-02-24 16:45:57 +01:00
|
|
|
var lock *repository.Unlocker
|
2024-02-24 16:26:29 +01:00
|
|
|
|
|
|
|
lock, ctx, err = repository.Lock(ctx, repo, exclusive, gopts.RetryLock, func(msg string) {
|
|
|
|
if !gopts.JSON {
|
|
|
|
Verbosef("%s", msg)
|
|
|
|
}
|
|
|
|
}, Warnf)
|
2024-02-24 15:19:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2024-03-29 23:29:49 +01:00
|
|
|
|
|
|
|
unlock = lock.Unlock
|
2024-02-24 15:19:02 +01:00
|
|
|
} 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 15:05:20 +02:00
|
|
|
}
|
|
|
|
|
2024-02-24 15:19:02 +01: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 16:58:36 +01:00
|
|
|
}
|