Skip to content

Commit

Permalink
Add specific files from tar archives to list of snapshotted filesa
Browse files Browse the repository at this point in the history
I changed UnpackLocalTarArchive to return a list of files that were
extracted, so that the list of snapshotted files for ADD is more
accurate. Previously, we used to add all files in the extracted dir to
be snapshotted, but this could result in preexisting files being
snapshotted again.
  • Loading branch information
Priya Wadhwa committed Aug 27, 2018
1 parent 9a93f5b commit b93b48d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
10 changes: 3 additions & 7 deletions pkg/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,12 @@ func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
a.snapshotFiles = append(a.snapshotFiles, urlDest)
} else if util.IsFileLocalTarArchive(fullPath) {
logrus.Infof("Unpacking local tar archive %s to %s", src, dest)
if err := util.UnpackLocalTarArchive(fullPath, dest); err != nil {
return err
}
// Add the unpacked files to the snapshotter
filesAdded, err := util.Files(dest)
extractedFiles, err := util.UnpackLocalTarArchive(fullPath, dest)
if err != nil {
return err
}
logrus.Debugf("Added %v from local tar archive %s", filesAdded, src)
a.snapshotFiles = append(a.snapshotFiles, filesAdded...)
logrus.Debugf("Added %v from local tar archive %s", extractedFiles, src)
a.snapshotFiles = append(a.snapshotFiles, extractedFiles...)
} else {
unresolvedSrcs = append(unresolvedSrcs, src)
}
Expand Down
29 changes: 7 additions & 22 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,24 @@ func ChildDirInWhitelist(path, directory string) bool {
return false
}

func unTar(r io.Reader, dest string) error {
// unTar returns a list of files extracted from the tar archive
func unTar(r io.Reader, dest string) ([]string, error) {
var extractedFiles []string
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
return nil, err
}
if err := extractFile(dest, hdr, tr); err != nil {
return err
return nil, err
}
extractedFiles = append(extractedFiles, dest)
}
return nil
return extractedFiles, nil
}

func extractFile(dest string, hdr *tar.Header, tr io.Reader) error {
Expand Down Expand Up @@ -349,24 +352,6 @@ func RelativeFiles(fp string, root string) ([]string, error) {
return files, err
}

// Files returns a list of all files rooted at root
func Files(root string) ([]string, error) {
var files []string
logrus.Debugf("Getting files and contents at root %s", root)
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
whitelisted, err := CheckWhitelist(path)
if err != nil {
return err
}
if whitelisted {
return nil
}
files = append(files, path)
return err
})
return files, err
}

// ParentDirectories returns a list of paths to all parent directories
// Ex. /some/temp/dir -> [/, /some, /some/temp, /some/temp/dir]
func ParentDirectories(path string) []string {
Expand Down
15 changes: 8 additions & 7 deletions pkg/util/tar_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ func (t *Tar) checkHardlink(p string, i os.FileInfo) (bool, string) {
}

// UnpackLocalTarArchive unpacks the tar archive at path to the directory dest
// Returns true if the path was actually unpacked
func UnpackLocalTarArchive(path, dest string) error {
// Returns the files extracted from the tar archive
func UnpackLocalTarArchive(path, dest string) ([]string, error) {
// First, we need to check if the path is a local tar archive
if compressed, compressionLevel := fileIsCompressedTar(path); compressed {
file, err := os.Open(path)
if err != nil {
return err
return nil, err
}
defer file.Close()
if compressionLevel == archive.Gzip {
return UnpackCompressedTar(path, dest)
return nil, UnpackCompressedTar(path, dest)
} else if compressionLevel == archive.Bzip2 {
bzr := bzip2.NewReader(file)
return unTar(bzr, dest)
Expand All @@ -156,12 +156,12 @@ func UnpackLocalTarArchive(path, dest string) error {
if fileIsUncompressedTar(path) {
file, err := os.Open(path)
if err != nil {
return err
return nil, err
}
defer file.Close()
return unTar(file, dest)
}
return errors.New("path does not lead to local tar archive")
return nil, errors.New("path does not lead to local tar archive")
}

//IsFileLocalTarArchive returns true if the file is a local tar archive
Expand Down Expand Up @@ -223,5 +223,6 @@ func UnpackCompressedTar(path, dir string) error {
return err
}
defer gzr.Close()
return unTar(gzr, dir)
_, err = unTar(gzr, dir)
return err
}

0 comments on commit b93b48d

Please sign in to comment.