Skip to content

Commit

Permalink
Merge pull request #56 from vedala/source-path-no-download-unwanted
Browse files Browse the repository at this point in the history
Fix to ensure specifying source-path does not download unwanted files
  • Loading branch information
brikis98 authored Sep 10, 2019
2 parents 1ca7fcb + 69e310f commit 15183f9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
26 changes: 23 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string, instan
return zipFilePath, nil
}

func shouldExtractPathInZip(pathPrefix string, zipPath *zip.File) bool {
//
// We need to return true (i.e extract file) based on the following conditions:
//
// The current archive item is a directory.
// Archive item's path name will always be appended with a "/", so we use
// this fact to ensure we are working with a full directory name.
// Extract the file if (pathPrefix + "/") is a prefix in path name
//
// The current archive item is a file.
// There are two things possible here:
// 1 User specified a filename that is an exact match for the current archive file,
// we need to extract this file.
// 2 The current archive filename is not a exact match to the user supplied filename.
// Check if (pathPrefix + "/") is a prefix in f.Name, if yes, we extract this file.

zipPathIsFile := !zipPath.FileInfo().IsDir()
return (zipPathIsFile && zipPath.Name == pathPrefix) || strings.Index(zipPath.Name, pathPrefix + "/") == 0
}

// Decompress the file at zipFileAbsPath and move only those files under filesToExtractFromZipPath to localPath
func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) error {

Expand All @@ -86,8 +106,8 @@ func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) erro
// printing some of their contents.
for _, f := range r.File {

// If the given file is in the filesToExtractFromZipPath, proceed
if strings.Index(f.Name, pathPrefix) == 0 {
// check if current archive file needs to be extracted
if shouldExtractPathInZip(pathPrefix, f) {

if f.FileInfo().IsDir() {
// Create a directory
Expand Down Expand Up @@ -149,4 +169,4 @@ func MakeGitHubZipFileRequest(gitHubCommit GitHubCommit, gitHubToken string, ins
}

return request, nil
}
}
30 changes: 30 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func TestExtractFiles(t *testing.T) {
{publicGitHub, "test-fixtures/fetch-test-public-0.0.2.zip", "/", 2, nil},
{publicGitHub, "test-fixtures/fetch-test-public-0.0.3.zip", "/", 4, []string{"/README.md"} },
{publicGitHub, "test-fixtures/fetch-test-public-0.0.3.zip", "/folder", 2, nil},
{publicGitHub, "test-fixtures/fetch-test-public-0.0.4.zip", "/aaa", 2, []string{"/hello.txt", "/subaaa/subhello.txt"} },
}

for _, tc := range cases {
Expand Down Expand Up @@ -339,6 +340,35 @@ func TestExtractFiles(t *testing.T) {
}
}

func TestExtractFilesExtractFile(t *testing.T) {
// Create a temp directory
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}
defer os.RemoveAll(tempDir)

zipFilePath := "test-fixtures/fetch-test-public-0.0.4.zip"
filePathToExtract := "zzz.txt"
localFileName := "/localzzz.txt"
localPathName := filepath.Join(tempDir, localFileName)
err = extractFiles(zipFilePath, filePathToExtract, localPathName)
if err != nil {
t.Fatalf("Failed to extract files: %s", err)
}

filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
relativeFilename := strings.TrimPrefix(path, tempDir)

if ! info.IsDir() {
if relativeFilename != localFileName {
t.Fatalf("Expected local file %s to be created, but not found.\n", localFileName)
}
}
return nil
})
}

// Return ture if the given slice contains the given string
func stringInSlice(s string, slice []string) bool {
for _, val := range slice {
Expand Down
Binary file added test-fixtures/fetch-test-public-0.0.4.zip
Binary file not shown.

0 comments on commit 15183f9

Please sign in to comment.