Skip to content

refactor: share the tar entry lookup in the package parsers - #94

Merged
bketelsen merged 3 commits into
mainfrom
groom/parser-tar-lookup
Nov 11, 2026
Merged

bketelsen merged 3 commits into
mainfrom
groom/parser-tar-lookup

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 11, 2026 •

Copy link
Copy Markdown

What changed and why

The deb, apk and pacman parsers had 4 copies of the same loop: walk a tar stream with Next() until an entry with a given name appears, then io.ReadAll it. This PR moves that loop into 1 helper and simplifies 1 nested search.

  • New utils.ReadTarEntry(tr, names...) (data, found, err) in internal/utils/tar.go. It matches on header.Name only, returns found=false at io.EOF, and returns the Next() and ReadAll errors unchanged.
  • Callers: deb.extractControlFromTar (./control, control), apk.extractPKGINFO (.PKGINFO), apk.parseAPKINDEX (APKINDEX), pacman.extractPKGINFO (.PKGINFO). Each keeps its own not-found error text. parseAPKINDEX keeps its len(apkindexData) == 0 check, so an empty APKINDEX entry still gives "APKINDEX not found in tar". The decompressor setup and the deferred closes are unchanged.
  • pacman.ParseExistingMetadata: the 4-level nested filepath.Glob cascade becomes a loop over *.db.tar.zst, *.db.tar.xz, *.db.tar.gz, *.db, in the same order, with the same "first pattern with no error and at least 1 match" rule.

Production code: 4 files, about 50 lines removed net. The rest of the diff is tests.

No change in behavior

Same results, same error values and texts, same order of checks. One difference is on an error path only: when io.ReadAll fails, the old code returned the partial data together with the error, and the helper returns nil data with the error. Every caller drops the data when the error is not nil (ParsePackage in deb, apk and pacman; parseAPKINDEX returns the error), so no caller can see it.

Evidence

  • Commit 1 (test: characterize the package parsers' archive lookups) adds characterization tests on the base code: TestExtractControlFromTar (plain, gz, xz, zst; ./control and control; first match wins; not found), TestExtractControlFromPackage (an ar archive with an odd-size member for the 2-byte padding), TestExtractControlWithoutControlTar, the apk TestExtractPKGINFO* and TestParseAPKINDEX* (found, absent, empty entry, missing file), the pacman TestExtractPKGINFO* (zst, xz, gz, plain tar, unsupported, absent) and TestParseExistingMetadata{PrefersZstDatabase,ReadsGzipDatabase,ReadsUncompressedDatabase,SkipsArchWithoutDatabase,NothingFound}.
  • Base run: at 193d90d (base code + those tests), go test -count=1 ./internal/generator/apk/ ./internal/generator/deb/ ./internal/generator/pacman/ passes, and all the new tests pass.
  • Base 86a7607: make check exit 0 (the full gate, run before any change).
  • Head b0c771c: make build exit 0 and make check exit 0 (15 packages ok, test cache cleaned first). Existing tests are unchanged.
  • Commit 2 also adds TestReadTarEntry and TestReadTarEntryReadError for the helper.

Docs: docs/design/overview.md lists the new internal/utils/tar.go.

Left out on purpose

  • The decompressor selection is also similar in deb and pacman, but the suffix rules differ (pacman's .db.tar zstd fallback), so it stays per parser.
  • The checksum fields set in each ParsePackage stay as they are.
  • No risk path is touched (pacman/generator.go, apk/generator.go and deb/release.go are risk paths and are not in this diff).

@github-actions github-actions Bot added the level:L3 Skim label Nov 11, 2026
@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.27273% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/generator/apk/parser.go 55.55% 2 Missing and 2 partials ⚠️
internal/generator/deb/parser.go 66.66% 1 Missing and 1 partial ⚠️
internal/generator/pacman/parser.go 86.66% 1 Missing and 1 partial ⚠️
internal/utils/tar.go 85.71% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@frostyard-machines

Copy link
Copy Markdown
Author

Cross-review (grooming, no change in behavior): Ready, Blocking: none.

  • Reviewer: openai/gpt-6.1-sol. Coder: deepseek/deepseek-flash.
  • Base 86a7607b44b462853560e89c52b9d88497662ea4, reviewed head b0c771cf5816bdc5a3a0c2c40b329397e7732ff9 (initial review at dcd5c5e + 1 re-review).
  • Initial findings: (1) the docs tree did not list the new internal/utils/tar.go; fixed in b0c771c. (2) the PR body did not record the base make check and the head make build; both runs exist (exit 0) and are now in the body.
  • The reviewer confirmed: same error texts and check order in every caller; an empty or absent APKINDEX still gives "APKINDEX not found in tar"; the pacman database order zst, xz, gz, db is kept; the characterization tests have real assertions and pass on the base code (193d90d); no risk path is touched.
  • Revert test: exempt (pure refactor; the proof is the characterization tests and the unchanged tests green on base and head).
  • Checks: make check exit 0 at base and head; CI make ci green at b0c771c.

@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 11, 2026
@bketelsen
bketelsen added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit 0f1f070 Nov 11, 2026
5 checks passed
@frostyard-machines
frostyard-machines deleted the groom/parser-tar-lookup branch November 13, 2026 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants