restic: list pack header via ListPackHandles

Replace ListPack with ListPackHandles so callers only receive blob
handles from pack headers, not layout fields.
This commit is contained in:
Michael Eischer
2026-06-04 21:58:23 +02:00
parent be43815d5d
commit 781c6a12ae
8 changed files with 38 additions and 26 deletions
+4 -4
View File
@@ -103,17 +103,17 @@ func testPackAndBlobCounts(t testing.TB, gopts global.Options) (countTreePacks i
defer unlock()
rtest.OK(t, repo.List(context.TODO(), restic.PackFile, func(id restic.ID, size int64) error {
blobs, err := repo.ListPack(context.TODO(), id, size)
handles, err := repo.ListPackHandles(context.TODO(), id, size)
rtest.OK(t, err)
rtest.Assert(t, len(blobs) > 0, "a packfile should contain at least one blob")
rtest.Assert(t, len(handles) > 0, "a packfile should contain at least one blob")
switch blobs[0].Type {
switch handles[0].Type {
case restic.TreeBlob:
countTreePacks++
case restic.DataBlob:
countDataPacks++
}
countBlobs += len(blobs)
countBlobs += len(handles)
return nil
}))
return nil
+6 -6
View File
@@ -472,18 +472,18 @@ func (f *Finder) packsToBlobs(ctx context.Context, packs []string) error {
delete(packIDs, idStr)
}
debug.Log("Found pack %s", idStr)
blobs, err := f.repo.ListPack(ctx, id, size)
handles, err := f.repo.ListPackHandles(ctx, id, size)
if err != nil {
return err
}
for _, b := range blobs {
switch b.Type {
for _, h := range handles {
switch h.Type {
case restic.DataBlob:
f.blobIDs[b.ID.String()] = struct{}{}
f.blobIDs[h.ID.String()] = struct{}{}
case restic.TreeBlob:
f.treeIDs[b.ID.String()] = struct{}{}
f.treeIDs[h.ID.String()] = struct{}{}
default:
panic(fmt.Sprintf("unknown type %v in blob list", b.Type.String()))
panic(fmt.Sprintf("unknown type %v in blob list", h.Type.String()))
}
}
// Stop searching when all packs have been found
+2 -2
View File
@@ -49,7 +49,7 @@ func writePackDumpJSON(wr io.Writer, item any) error {
func DumpPacks(ctx context.Context, repo *Repository, wr io.Writer, printer progress.Printer) error {
var m sync.Mutex
return restic.ParallelList(ctx, repo, restic.PackFile, repo.Connections(), func(ctx context.Context, id restic.ID, size int64) error {
blobs, err := repo.ListPack(ctx, id, size)
blobs, err := repo.listPack(ctx, id, size)
if err != nil {
printer.E("error for pack %v: %v", id.Str(), err)
return nil
@@ -135,7 +135,7 @@ func ExaminePack(ctx context.Context, repo *Repository, id restic.ID, opts Exami
printer.S(" ========================================")
printer.S(" inspect the pack itself")
blobs, err := repo.ListPack(ctx, id, int64(len(buf)))
blobs, err := repo.listPack(ctx, id, int64(len(buf)))
if err != nil {
return fmt.Errorf("pack %v: %v", id.Str(), err)
}
+4 -5
View File
@@ -86,13 +86,12 @@ func selectBlobs(t *testing.T, random *rand.Rand, repo restic.Repository, p floa
blobs := restic.NewBlobSet()
err := repo.List(context.TODO(), restic.PackFile, func(id restic.ID, size int64) error {
entries, err := repo.ListPack(context.TODO(), id, size)
handles, err := repo.ListPackHandles(context.TODO(), id, size)
if err != nil {
t.Fatalf("error listing pack %v: %v", id, err)
}
for _, entry := range entries {
h := restic.BlobHandle{ID: entry.ID, Type: entry.Type}
for _, h := range handles {
if blobs.Has(h) {
t.Errorf("ignoring duplicate blob %v", h)
return nil
@@ -100,9 +99,9 @@ func selectBlobs(t *testing.T, random *rand.Rand, repo restic.Repository, p floa
blobs.Insert(h)
if random.Float32() <= p {
list1.Insert(restic.BlobHandle{ID: entry.ID, Type: entry.Type})
list1.Insert(h)
} else {
list2.Insert(restic.BlobHandle{ID: entry.ID, Type: entry.Type})
list2.Insert(h)
}
}
return nil
+1 -1
View File
@@ -76,7 +76,7 @@ func resolveBlobsForPacks(ctx context.Context, repo *Repository, ids restic.IDSe
err := repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
if ids.Has(id) {
blobs, err := repo.ListPack(ctx, id, size)
blobs, err := repo.listPack(ctx, id, size)
if err != nil {
return nil
}
+16 -3
View File
@@ -781,7 +781,7 @@ func (r *Repository) createIndexFromPacks(ctx context.Context, packsize map[rest
// a worker receives an pack ID from ch, reads the pack contents, and adds them to idx
worker := func() error {
for fi := range ch {
entries, err := r.ListPack(wgCtx, fi.ID, fi.Size)
entries, err := r.listPack(wgCtx, fi.ID, fi.Size)
if err != nil {
debug.Log("unable to list pack file %v", fi.ID.Str())
m.Lock()
@@ -957,8 +957,8 @@ func (r *Repository) List(ctx context.Context, t restic.FileType, fn func(restic
})
}
// ListPack returns the list of blobs saved in the pack id.
func (r *Repository) ListPack(ctx context.Context, id restic.ID, size int64) (restic.Blobs, error) {
// listPack returns blob entries from the pack file header including offsets.
func (r *Repository) listPack(ctx context.Context, id restic.ID, size int64) (restic.Blobs, error) {
h := backend.Handle{Type: restic.PackFile, Name: id.String()}
entries, _, err := pack.List(r.Key(), backend.ReaderAt(ctx, r.be, h), size)
@@ -974,6 +974,19 @@ func (r *Repository) ListPack(ctx context.Context, id restic.ID, size int64) (re
return entries, err
}
// ListPackHandles returns the blob handles stored in the pack file header.
func (r *Repository) ListPackHandles(ctx context.Context, id restic.ID, size int64) ([]restic.BlobHandle, error) {
blobs, err := r.listPack(ctx, id, size)
if err != nil {
return nil, err
}
handles := make([]restic.BlobHandle, len(blobs))
for i, blob := range blobs {
handles[i] = blob.BlobHandle
}
return handles, nil
}
// Delete calls backend.Delete() if implemented, and returns an error
// otherwise.
func (r *Repository) Delete(ctx context.Context) error {
+3 -3
View File
@@ -457,11 +457,11 @@ func TestListPack(t *testing.T) {
return nil
}))
blobs, err := repo.ListPack(context.TODO(), packID, size)
handles, err := repo.ListPackHandles(context.TODO(), packID, size)
rtest.OK(t, err)
rtest.Assert(t, len(blobs) == 1 && blobs[0].ID == id, "unexpected blobs in pack: %v", blobs)
rtest.Assert(t, len(handles) == 1 && handles[0].ID == id, "unexpected blobs in pack: %v", handles)
rtest.Assert(t, !c.Has(backend.Handle{Type: restic.PackFile, Name: packID.String()}), "tree pack should no longer be cached as ListPack does not set IsMetadata in the backend.Handle")
rtest.Assert(t, !c.Has(backend.Handle{Type: restic.PackFile, Name: packID.String()}), "tree pack should no longer be cached as listPack does not set IsMetadata in the backend.Handle")
}
func TestNoDoubleInit(t *testing.T) {
+2 -2
View File
@@ -32,8 +32,8 @@ type Repository interface {
// the index iteration returns immediately with ctx.Err(). This blocks any modification of the index.
ListBlobs(ctx context.Context, fn func(PackedBlob)) error
ListPacksFromIndex(ctx context.Context, packs IDSet) <-chan PackBlobs
// ListPack returns the list of blobs saved in the pack id.
ListPack(ctx context.Context, id ID, packSize int64) (entries Blobs, err error)
// ListPackHandles returns the blob handles stored in the pack file header.
ListPackHandles(ctx context.Context, id ID, packSize int64) ([]BlobHandle, error)
LoadBlob(ctx context.Context, t BlobType, id ID, buf []byte) ([]byte, error)
LoadBlobsFromPack(ctx context.Context, packID ID, blobs []BlobHandle, handleBlobFn func(blob BlobHandle, buf []byte, err error) error) error