Artifact 14124be0092edea45928971b6226179aa4e57d109f64a5b4a750c93e8dc006b7:
- File
internal/manifest/files.go
— part of check-in
[670ca0e75f]
at
2026-07-19 16:00:09
on branch trunk
— fix: separate blob existence from blob availability (#43) (#52)
* fix: separate blob existence from blob availability (#43)
A phantom is a real blob row with size = -1 and NULL content, so blob.Exists — "SELECT rid FROM blob WHERE uuid=?" — answers true for it. Every guard built on Exists therefore admitted artifacts whose content cannot be read, and content.Expand aborted with "rid N is a phantom".
Canonical Fossil keeps two predicates and never substitutes one for the other: rid_from_uuid (existence, xfer.c:70) and content_is_available (availability, content.c:163). The latter is transitive — it follows delta.srcid up the chain, so a delta whose base is a phantom is unavailable even though every row in the chain exists.
Add content.IsAvailable / content.AvailableByUUID, ported from content_is_available including the chain walk, with an explicit depth bound so a cyclic delta table terminates instead of hanging. The walk lives in exactly one place; no caller touches the delta table.
blob.Exists keeps its behavior and gains a doc comment stating that it answers existence only, returns true for phantoms, and naming the availability predicate as the alternative.
manifest.blobPresent is deleted. It was a local patch at one call site that tested size >= 0 without the chain walk, so it still reported a delta-on-phantom as present — the same class of bug it was written to work around.
Every blob.Exists call site was classified as existence or availability and converted where it guards a content read. Sites that want rid identity — plink/mlink/tagxref population, cluster phantom creation, private-status bookkeeping, the post-store sanity check in handleFile, and phantom filling in storeReceivedFile — keep existence, since delta-before-base is a normal steady state during clone.
Verified against a loopback fossil 2.28 server: the 23-artifact repro that previously failed roughly half the time now clones 6 of 6.
* fix: IsAvailable must fail closed when the delta lookup errors
Only sql.ErrNoRows means the chain is grounded. Any other failure — cancelled context, closed connection, NULL srcid — says nothing about groundedness, so returning true there claims readable content that was never verified. That is the same class of bug this predicate exists to prevent, and it runs on data arriving over the wire during clone.
The blob-size lookup above already fails closed on the same pattern; this restores the symmetry. (user: [email protected], size: 3120) [annotate] [blame] [check-ins using]
package manifest import ( "fmt" "sort" libfossil "github.com/danmestas/libfossil/internal/fsltype" "github.com/danmestas/libfossil/internal/content" "github.com/danmestas/libfossil/internal/deck" "github.com/danmestas/libfossil/internal/repo" ) type FileEntry struct { Name string UUID string Perm string } func ListFiles(r *repo.Repo, rid libfossil.FslID) ([]FileEntry, error) { d, err := GetManifest(r, rid) if err != nil { return nil, fmt.Errorf("manifest.ListFiles: %w", err) } if d.B == "" { return fCardsToEntries(d.F), nil } baseRid, ok := content.AvailableByUUID(r.DB(), d.B) if !ok { return nil, fmt.Errorf("manifest.ListFiles: baseline %s not found", d.B) } baseData, err := content.Expand(r.DB(), baseRid) if err != nil { return nil, fmt.Errorf("manifest.ListFiles: expand baseline: %w", err) } baseDeck, err := deck.Parse(baseData) if err != nil { return nil, fmt.Errorf("manifest.ListFiles: parse baseline: %w", err) } fileMap := make(map[string]FileEntry) for _, f := range baseDeck.F { fileMap[f.Name] = FileEntry{Name: f.Name, UUID: f.UUID, Perm: f.Perm} } for _, f := range d.F { if f.UUID == "" { delete(fileMap, f.Name) } else { fileMap[f.Name] = FileEntry{Name: f.Name, UUID: f.UUID, Perm: f.Perm} } } entries := make([]FileEntry, 0, len(fileMap)) for _, e := range fileMap { entries = append(entries, e) } sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name }) return entries, nil } func fCardsToEntries(fCards []deck.FileCard) []FileEntry { entries := make([]FileEntry, len(fCards)) for i, f := range fCards { entries[i] = FileEntry{Name: f.Name, UUID: f.UUID, Perm: f.Perm} } sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name }) return entries } // MergeParentFiles returns supplied with every file tracked at parentRID // that is not already in supplied appended. Names in supplied win over the // parent's entry; the parent's untouched files have their content expanded // from the repo so the resulting slice is a full-tree set ready for Checkin. // // parentRID == 0 returns supplied unchanged. func MergeParentFiles(r *repo.Repo, parentRID libfossil.FslID, supplied []File) ([]File, error) { if parentRID == 0 { return supplied, nil } parentFiles, err := ListFiles(r, parentRID) if err != nil { return nil, fmt.Errorf("manifest.MergeParentFiles: %w", err) } suppliedNames := make(map[string]struct{}, len(supplied)) for _, f := range supplied { suppliedNames[f.Name] = struct{}{} } merged := supplied for _, pf := range parentFiles { if _, ok := suppliedNames[pf.Name]; ok { continue } baseRid, ok := content.AvailableByUUID(r.DB(), pf.UUID) if !ok { return nil, fmt.Errorf("manifest.MergeParentFiles: blob %s for %s not found", pf.UUID, pf.Name) } data, err := content.Expand(r.DB(), baseRid) if err != nil { return nil, fmt.Errorf("manifest.MergeParentFiles: expand %s: %w", pf.Name, err) } merged = append(merged, File{ Name: pf.Name, Content: data, Perm: pf.Perm, }) } return merged, nil }