From 81b6414c559de5a779a71ec49d9801049740118a Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 7 Jun 2026 13:06:40 +0200 Subject: [PATCH] repository: hide Lock methods Unexport lock file methods except String. Lock file operations are only used within the repository package. --- internal/repository/lock.go | 8 +++---- internal/repository/lock_file.go | 18 +++++++------- internal/repository/lock_file_test.go | 34 +++++++++++++-------------- internal/repository/lock_test.go | 2 +- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/internal/repository/lock.go b/internal/repository/lock.go index a91341f42..303ef082e 100644 --- a/internal/repository/lock.go +++ b/internal/repository/lock.go @@ -129,7 +129,7 @@ func (l *locker) refreshLocks(ctx context.Context, backend backend.Backend, lock // remove the lock from the repo debug.Log("unlocking repository with lock %v", lock) - if err := lock.Unlock(ctx); err != nil { + if err := lock.unlock(ctx); err != nil { debug.Log("error while unlocking: %v", err) logger("error while unlocking: %v", err) } @@ -165,7 +165,7 @@ func (l *locker) refreshLocks(ctx context.Context, backend backend.Backend, lock } debug.Log("refreshing locks") - err := lock.Refresh(context.TODO()) + err := lock.refresh(context.TODO()) if err != nil { logger("unable to refresh lock: %v\n", err) } else { @@ -250,7 +250,7 @@ func tryRefreshStaleLock(ctx context.Context, be backend.Backend, lock *Lock, ca defer freeze.Unfreeze() } - err := lock.RefreshStaleLock(ctx) + err := lock.refreshStaleLock(ctx) if err != nil { logger("failed to refresh stale lock: %v\n", err) // cancel context while the backend is still frozen to prevent accidental modifications @@ -284,7 +284,7 @@ func RemoveStaleLocks(ctx context.Context, repo *Repository) (uint, error) { return nil } - if lock.Stale() { + if lock.stale() { err = (&internalRepository{repo}).RemoveUnpacked(ctx, restic.LockFile, id) if err == nil { processed++ diff --git a/internal/repository/lock_file.go b/internal/repository/lock_file.go index 78d60979f..b2e28435c 100644 --- a/internal/repository/lock_file.go +++ b/internal/repository/lock_file.go @@ -132,7 +132,7 @@ func newLock(ctx context.Context, repo restic.Unpacked[restic.FileType], exclusi time.Sleep(waitBeforeLockCheck) if err = lock.checkForOtherLocks(ctx); err != nil { - _ = lock.Unlock(ctx) + _ = lock.unlock(ctx) return nil, err } @@ -236,8 +236,8 @@ func (l *Lock) createLock(ctx context.Context) (restic.ID, error) { return id, nil } -// Unlock removes the lock from the repository. -func (l *Lock) Unlock(ctx context.Context) error { +// unlock removes the lock from the repository. +func (l *Lock) unlock(ctx context.Context) error { if l == nil || l.lockID == nil { return nil } @@ -250,10 +250,10 @@ func (l *Lock) Unlock(ctx context.Context) error { var staleLockTimeout = 30 * time.Minute -// Stale returns true if the lock is stale. A lock is stale if the timestamp is +// stale returns true if the lock is stale. A lock is stale if the timestamp is // older than 30 minutes or if it was created on the current machine and the // process isn't alive any more. -func (l *Lock) Stale() bool { +func (l *Lock) stale() bool { l.lock.Lock() defer l.lock.Unlock() debug.Log("testing if lock %v for process %d is stale", l.lockID, l.PID) @@ -303,9 +303,9 @@ func delayedCancelContext(parentCtx context.Context, delay time.Duration) (conte return ctx, cancel } -// Refresh refreshes the lock by creating a new file in the backend with a new +// refresh refreshes the lock by creating a new file in the backend with a new // timestamp. Afterwards the old lock is removed. -func (l *Lock) Refresh(ctx context.Context) error { +func (l *Lock) refresh(ctx context.Context) error { debug.Log("refreshing lock %v", l.lockID) l.lock.Lock() l.Time = time.Now() @@ -328,8 +328,8 @@ func (l *Lock) Refresh(ctx context.Context) error { return l.repo.RemoveUnpacked(ctx, restic.LockFile, *oldLockID) } -// RefreshStaleLock is an extended variant of Refresh that can also refresh stale lock files. -func (l *Lock) RefreshStaleLock(ctx context.Context) error { +// refreshStaleLock is an extended variant of refresh that can also refresh stale lock files. +func (l *Lock) refreshStaleLock(ctx context.Context) error { debug.Log("refreshing stale lock %v", l.lockID) // refreshing a stale lock is possible if it still exists and continues to do // so until after creating a new lock. The initial check avoids creating a new diff --git a/internal/repository/lock_file_test.go b/internal/repository/lock_file_test.go index 1e2256860..af3cbc689 100644 --- a/internal/repository/lock_file_test.go +++ b/internal/repository/lock_file_test.go @@ -21,7 +21,7 @@ func TestLockFile(t *testing.T) { lock, err := newLock(context.TODO(), &internalRepository{repo}, false) rtest.OK(t, err) - rtest.OK(t, lock.Unlock(context.TODO())) + rtest.OK(t, lock.unlock(context.TODO())) } func TestDoubleUnlock(t *testing.T) { @@ -31,9 +31,9 @@ func TestDoubleUnlock(t *testing.T) { lock, err := newLock(context.TODO(), &internalRepository{repo}, false) rtest.OK(t, err) - rtest.OK(t, lock.Unlock(context.TODO())) + rtest.OK(t, lock.unlock(context.TODO())) - err = lock.Unlock(context.TODO()) + err = lock.unlock(context.TODO()) rtest.Assert(t, err != nil, "double unlock didn't return an error, got %v", err) } @@ -48,8 +48,8 @@ func TestMultipleLock(t *testing.T) { lock2, err := newLock(context.TODO(), &internalRepository{repo}, false) rtest.OK(t, err) - rtest.OK(t, lock1.Unlock(context.TODO())) - rtest.OK(t, lock2.Unlock(context.TODO())) + rtest.OK(t, lock1.unlock(context.TODO())) + rtest.OK(t, lock2.unlock(context.TODO())) } type failLockLoadingBackend struct { @@ -74,7 +74,7 @@ func TestMultipleLockFailure(t *testing.T) { _, err = newLock(context.TODO(), &internalRepository{repo}, false) rtest.Assert(t, err != nil, "unreadable lock file did not result in an error") - rtest.OK(t, lock1.Unlock(context.TODO())) + rtest.OK(t, lock1.unlock(context.TODO())) } func TestLockExclusive(t *testing.T) { @@ -82,7 +82,7 @@ func TestLockExclusive(t *testing.T) { elock, err := newLock(context.TODO(), &internalRepository{repo}, true) rtest.OK(t, err) - rtest.OK(t, elock.Unlock(context.TODO())) + rtest.OK(t, elock.unlock(context.TODO())) } func TestLockOnExclusiveLockedRepo(t *testing.T) { @@ -98,8 +98,8 @@ func TestLockOnExclusiveLockedRepo(t *testing.T) { rtest.Assert(t, IsAlreadyLocked(err), "create normal lock with exclusively locked repo didn't return the correct error") - rtest.OK(t, lock.Unlock(context.TODO())) - rtest.OK(t, elock.Unlock(context.TODO())) + rtest.OK(t, lock.unlock(context.TODO())) + rtest.OK(t, elock.unlock(context.TODO())) } func TestExclusiveLockOnLockedRepo(t *testing.T) { @@ -115,8 +115,8 @@ func TestExclusiveLockOnLockedRepo(t *testing.T) { rtest.Assert(t, IsAlreadyLocked(err), "create normal lock with exclusively locked repo didn't return the correct error") - rtest.OK(t, lock.Unlock(context.TODO())) - rtest.OK(t, elock.Unlock(context.TODO())) + rtest.OK(t, lock.unlock(context.TODO())) + rtest.OK(t, elock.unlock(context.TODO())) } var staleLockTests = []struct { @@ -164,12 +164,12 @@ func TestLockStale(t *testing.T) { Hostname: hostname, } - rtest.Assert(t, lock.Stale() == test.stale, + rtest.Assert(t, lock.stale() == test.stale, "TestStaleLock: test %d failed: expected stale: %v, got %v", i, test.stale, !test.stale) lock.Hostname = otherHostname - rtest.Assert(t, lock.Stale() == test.staleOnOtherHost, + rtest.Assert(t, lock.stale() == test.staleOnOtherHost, "TestStaleLock: test %d failed: expected staleOnOtherHost: %v, got %v", i, test.staleOnOtherHost, !test.staleOnOtherHost) } @@ -215,18 +215,18 @@ func testLockRefresh(t *testing.T, refresh func(lock *Lock) error) { rtest.OK(t, err) rtest.Assert(t, lock2.Time.After(time0), "expected a later timestamp after lock refresh") - rtest.OK(t, lock.Unlock(context.TODO())) + rtest.OK(t, lock.unlock(context.TODO())) } func TestLockRefresh(t *testing.T) { testLockRefresh(t, func(lock *Lock) error { - return lock.Refresh(context.TODO()) + return lock.refresh(context.TODO()) }) } func TestLockRefreshStale(t *testing.T) { testLockRefresh(t, func(lock *Lock) error { - return lock.RefreshStaleLock(context.TODO()) + return lock.refreshStaleLock(context.TODO()) }) } @@ -241,6 +241,6 @@ func TestLockRefreshStaleMissing(t *testing.T) { // refresh must fail if lock was removed rtest.OK(t, be.Remove(context.TODO(), backend.Handle{Type: restic.LockFile, Name: lockID.String()})) time.Sleep(time.Millisecond) - err = lock.RefreshStaleLock(context.TODO()) + err = lock.refreshStaleLock(context.TODO()) rtest.Assert(t, err == errRemovedLock, "unexpected error, expected %v, got %v", errRemovedLock, err) } diff --git a/internal/repository/lock_test.go b/internal/repository/lock_test.go index a9c269738..2fb9025a1 100644 --- a/internal/repository/lock_test.go +++ b/internal/repository/lock_test.go @@ -38,7 +38,7 @@ func checkedLockRepo(ctx context.Context, t *testing.T, repo *Repository, locker lock, wrappedCtx, err := lockerInst.Lock(ctx, repo, false, retryLock, func(msg string) {}, func(format string, args ...interface{}) {}) rtest.OK(t, err) rtest.OK(t, wrappedCtx.Err()) - if lock.info.lock.Stale() { + if lock.info.lock.stale() { t.Fatal("lock returned stale lock") } return lock, wrappedCtx