feat: release the target claim in cleanup

cleanup now deletes the claim file under .cephmig/claims as its last
step, if it holds the job's own ID, and removes the claim directories
when they are empty. A cleaned-up job is finished: cleanup and finalize
do nothing and do not claim the target again, and sync is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-12-03 20:37:20 +01:00
1 parent 5f29328932
commit a6e7bbb17e
10 files changed
+218 -14

No files matched your search

+41
View File
@@ -2,10 +2,14 @@ package migration
import (
"context"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"syscall"
)
func nativePath(p string) (string, error) {
@@ -24,6 +28,9 @@ type Access interface {
Prepare(context.Context, string, string, string) error
Apply(context.Context, string, string) error
Verify(context.Context, string, string, string) error
// Release deletes the claim of job id, and the claim directories if
// they are empty. A missing claim is not an error.
Release(context.Context, Config, string, string) error
}
type mountedAccess struct{}
@@ -38,6 +45,40 @@ func (mountedAccess) Claim(_ context.Context, c Config, fsid, id string) (io.Clo
return claimTarget(c.Target.Mount, fsid, c.Target, id)
}
func (mountedAccess) Release(_ context.Context, c Config, fsid, id string) error {
dir := filepath.Join(c.Target.Mount, ".cephmig", "claims")
p := filepath.Join(dir, claimName(fsid, c.Target))
f, err := os.OpenFile(p, os.O_RDONLY|syscall.O_NOFOLLOW, 0)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}
b, err := io.ReadAll(io.LimitReader(f, 256))
f.Close()
if err != nil {
return err
}
if string(b) != id+"\n" {
return errors.New("target claim belongs to a different job; not removed")
}
if err = os.Remove(p); err != nil {
return err
}
for _, d := range []string{dir, filepath.Dir(dir)} {
if err := os.Remove(d); err != nil && !errors.Is(err, os.ErrNotExist) && !errors.Is(err, syscall.ENOTEMPTY) && !errors.Is(err, syscall.EEXIST) {
return err
}
}
return nil
}
func (mountedAccess) Prepare(ctx context.Context, src, dst, scratch string) error {
if err := clearQuotas(ctx, dst); err != nil {
return err