Skip to content

Commit

Permalink
Removes leading slash from paths, to normalize expressions (#32)
Browse files Browse the repository at this point in the history
Paketo images have a mix of layers where some are prefixed with '/' and
others aren't. This normalizes to the default ( no prefix ).

Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Feb 22, 2024
1 parent 30fdb91 commit 099c540
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions internal/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (c *car) do(ctx context.Context, readFile api.ReadFile, ref api.Reference,
}
pm := patternmatcher.New(c.filePatterns, c.fastRead)
rf := func(name string, size int64, mode os.FileMode, modTime time.Time, reader io.Reader) error {
name = stripLeadingSlash(name)
if !pm.MatchesPattern(name) {
return nil
}
Expand Down Expand Up @@ -187,3 +188,13 @@ func (c *car) getFilesystemLayers(ctx context.Context, ref api.Reference, platfo
}
return filteredLayers, nil
}

// stripLeadingSlash removes any leading slash from the input file name, to
// normalize pattern matching. For example, paketo images have a combination of
// relative and absolute paths in their squashed image.
func stripLeadingSlash(name string) string {
if len(name) > 0 && name[0] == '/' {
return name[1:]
}
return name
}
3 changes: 2 additions & 1 deletion internal/registry/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ type fakeFile struct {
// The fake data intentionally overlaps on "usr/local" for testing. Even if weird, it adds windows paths.
var fakeFiles = [][]*fakeFile{
{
{"bin/apple.txt", 10, 0o640 & os.ModePerm, "2020-06-07T06:28:15Z"},
// intentionally leading slash
{"/bin/apple.txt", 10, 0o640 & os.ModePerm, "2020-06-07T06:28:15Z"},
{"usr/local/bin/boat", 20, 0o755 & os.ModePerm, "2021-04-16T22:53:09Z"},
},
{
Expand Down

0 comments on commit 099c540

Please sign in to comment.