Skip to content

fileutils: fix wildcard negation directory descent in archive.TarWithOptions#1006

Open
Honny1 wants to merge 1 commit into
podman-container-tools:mainfrom
Honny1:fix-archive-filtering
Open

fileutils: fix wildcard negation directory descent in archive.TarWithOptions#1006
Honny1 wants to merge 1 commit into
podman-container-tools:mainfrom
Honny1:fix-archive-filtering

Conversation

@Honny1

@Honny1 Honny1 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

The strings.HasPrefix check in tarWithOptionsTo only worked for literal negation patterns (e.g. !cmd/main.go) but failed for wildcard negations like !**/*.go, !*/*.go, or !cmd/image*. This caused excluded directories to be skipped entirely even when negation patterns should have re-included files within them.

Add ShouldDescendExcludedDir to pkg/fileutils which extracts the literal prefix before the first wildcard character and checks whether the directory is at or under that prefix. When no literal prefix exists (e.g. !**/*.go), always descend. Replace the inline loop in tarWithOptionsTo with a call to the new function.

@github-actions github-actions Bot added the storage Related to "storage" package label Jul 21, 2026
@Honny1
Honny1 marked this pull request as ready for review July 21, 2026 15:25
@Honny1
Honny1 force-pushed the fix-archive-filtering branch from c1bc58d to 785ad11 Compare July 21, 2026 15:28
…hOptions`

The `strings.HasPrefix` check in `tarWithOptionsTo` only worked for literal
negation patterns (e.g. `!cmd/main.go`) but failed for wildcard negations
like `!**/*.go`, `!*/*.go`, or `!cmd/image*`. This caused excluded directories
to be skipped entirely even when negation patterns should have re-included
files within them.

Add `ShouldDescendExcludedDir` to `pkg/fileutils` which extracts the literal
prefix before the first wildcard character and checks whether the directory
is at or under that prefix. When no literal prefix exists (e.g. `!**/*.go`),
always descend. Replace the inline loop in tarWithOptionsTo with a call to
the new function.

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
@Honny1
Honny1 force-pushed the fix-archive-filtering branch from 785ad11 to 2e515ea Compare July 21, 2026 15:43

@mtrmac mtrmac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

// overmatch (descend into directories that won't ultimately contain matches),
// which is safe because actual file-level matching happens later.
func ShouldDescendExcludedDir(dirPath string, pm *PatternMatcher) bool {
if pm == nil || !pm.Exclusions() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: Do we need the pm == nil here? I think this is a caller error and not silently accepting it might make it easier to discover such errors.

// the literal prefix before the first wildcard and may intentionally
// overmatch (descend into directories that won't ultimately contain matches),
// which is safe because actual file-level matching happens later.
func ShouldDescendExcludedDir(dirPath string, pm *PatternMatcher) bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider making this a method of PatternMatcher? Any reason not to do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

storage Related to "storage" package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants