-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve symlinks when fetching file contents (#782)
- Loading branch information
Showing
41 changed files
with
947 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package file | ||
|
||
import ( | ||
"github.com/anchore/syft/internal/log" | ||
"github.com/anchore/syft/syft/source" | ||
) | ||
|
||
func allRegularFiles(resolver source.FileResolver) (locations []source.Location) { | ||
for location := range resolver.AllLocations() { | ||
resolvedLocations, err := resolver.FilesByPath(location.RealPath) | ||
if err != nil { | ||
log.Warnf("unable to resolve %+v: %+v", location, err) | ||
continue | ||
} | ||
|
||
for _, resolvedLocation := range resolvedLocations { | ||
metadata, err := resolver.FileMetadataByLocation(resolvedLocation) | ||
if err != nil { | ||
log.Warnf("unable to get metadata for %+v: %+v", location, err) | ||
continue | ||
} | ||
|
||
if metadata.Type != source.RegularFile { | ||
continue | ||
} | ||
locations = append(locations, resolvedLocation) | ||
} | ||
} | ||
return locations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package file | ||
|
||
import ( | ||
"github.com/anchore/stereoscope/pkg/imagetest" | ||
"github.com/anchore/syft/syft/source" | ||
"github.com/scylladb/go-set/strset" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func Test_allRegularFiles(t *testing.T) { | ||
type access struct { | ||
realPath string | ||
virtualPath string | ||
} | ||
tests := []struct { | ||
name string | ||
setup func() source.FileResolver | ||
wantRealPaths *strset.Set | ||
wantVirtualPaths *strset.Set | ||
}{ | ||
{ | ||
name: "image", | ||
setup: func() source.FileResolver { | ||
testImage := "image-file-type-mix" | ||
|
||
if *updateImageGoldenFiles { | ||
imagetest.UpdateGoldenFixtureImage(t, testImage) | ||
} | ||
|
||
img := imagetest.GetGoldenFixtureImage(t, testImage) | ||
|
||
s, err := source.NewFromImage(img, "---") | ||
require.NoError(t, err) | ||
|
||
r, err := s.FileResolver(source.SquashedScope) | ||
require.NoError(t, err) | ||
|
||
return r | ||
}, | ||
wantRealPaths: strset.New("/file-1.txt"), | ||
wantVirtualPaths: strset.New("/file-1.txt", "/symlink-1", "/hardlink-1"), | ||
}, | ||
{ | ||
name: "directory", | ||
setup: func() source.FileResolver { | ||
s, err := source.NewFromDirectory("test-fixtures/symlinked-root/nested/link-root") | ||
require.NoError(t, err) | ||
r, err := s.FileResolver(source.SquashedScope) | ||
require.NoError(t, err) | ||
return r | ||
}, | ||
wantRealPaths: strset.New("file1.txt", "nested/file2.txt"), | ||
wantVirtualPaths: strset.New("nested/linked-file1.txt"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
resolver := tt.setup() | ||
locations := allRegularFiles(resolver) | ||
realLocations := strset.New() | ||
virtualLocations := strset.New() | ||
for _, l := range locations { | ||
realLocations.Add(l.RealPath) | ||
if l.VirtualPath != "" { | ||
virtualLocations.Add(l.VirtualPath) | ||
} | ||
} | ||
assert.ElementsMatch(t, tt.wantRealPaths.List(), realLocations.List(), "mismatched real paths") | ||
assert.ElementsMatch(t, tt.wantVirtualPaths.List(), virtualLocations.List(), "mismatched virtual paths") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.