Skip to content

Commit

Permalink
Not public (will remove this line once cl chain is submitted)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 720557909
  • Loading branch information
Mario Leyva authored and copybara-github committed Feb 5, 2025
1 parent 34e66e8 commit 42a6827
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
9 changes: 9 additions & 0 deletions artifact/image/layerscanning/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func FromV1Image(v1Image v1.Image, config *Config) (*Image, error) {
// v1.Layers found in the image from the tarball.
func initializeChainLayers(v1Layers []v1.Layer, configFile *v1.ConfigFile) ([]*chainLayer, error) {
layerIndex := 0

if configFile == nil {
return nil, fmt.Errorf("config file is nil")
}

chainLayers := make([]*chainLayer, 0, len(configFile.History))
for _, entry := range configFile.History {
if entry.EmptyLayer {
Expand Down Expand Up @@ -419,6 +424,10 @@ func (img *Image) handleDir(realFilePath, virtualPath, originLayerID string, tar
// handleFile creates the file specified by path, and then copies the contents of the tarReader into
// the file.
func (img *Image) handleFile(realFilePath, virtualPath, originLayerID string, tarReader *tar.Reader, header *tar.Header, isWhiteout bool) (*fileNode, error) {
parentDirectory := filepath.Dir(realFilePath)
if err := os.MkdirAll(parentDirectory, dirPermission); err != nil {
return nil, fmt.Errorf("failed to create parent directory %s: %w", parentDirectory, err)
}
// Write all files as read/writable by the current user, inaccessible by anyone else
// Actual permission bits are stored in FileNode
f, err := os.OpenFile(realFilePath, os.O_CREATE|os.O_RDWR, filePermission)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ type FakeChainLayer struct {
}

// New creates a new FakeChainLayer.
func New(testDir string, index int, diffID digest.Digest, command string, layer image.Layer, files map[string]string) (*FakeChainLayer, error) {
for name, contents := range files {
filename := filepath.Join(testDir, name)
file, err := os.Create(filename)
if err != nil {
return nil, err
func New(testDir string, index int, diffID digest.Digest, command string, layer image.Layer, files map[string]string, filesAlreadyExist bool) (*FakeChainLayer, error) {
if !filesAlreadyExist {
for name, contents := range files {
filename := filepath.Join(testDir, name)
if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
return nil, err
}

if err := os.WriteFile(filename, []byte(contents), 0600); err != nil {
return nil, err
}
}
file.WriteString(contents)
file.Close()
}
return &FakeChainLayer{
index: index,
Expand Down
2 changes: 1 addition & 1 deletion artifact/image/layerscanning/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func setupFakeChainLayer(t *testing.T, testDir string, index int, diffID digest.
t.Helper()

layer := fakelayer.New(diffID, command)
chainLayer, err := fakechainlayer.New(testDir, index, diffID, command, layer, fileContents)
chainLayer, err := fakechainlayer.New(testDir, index, diffID, command, layer, fileContents, false)
if err != nil {
t.Fatalf("fakechainlayer.New(%d, %q, %q, %v, %v) failed: %v", index, diffID, command, layer, fileContents, err)
}
Expand Down

0 comments on commit 42a6827

Please sign in to comment.