Skip to content

Commit 15183f9

Browse files
authored
Merge pull request #56 from vedala/source-path-no-download-unwanted
Fix to ensure specifying source-path does not download unwanted files
2 parents 1ca7fcb + 69e310f commit 15183f9

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

file.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string, instan
6161
return zipFilePath, nil
6262
}
6363

64+
func shouldExtractPathInZip(pathPrefix string, zipPath *zip.File) bool {
65+
//
66+
// We need to return true (i.e extract file) based on the following conditions:
67+
//
68+
// The current archive item is a directory.
69+
// Archive item's path name will always be appended with a "/", so we use
70+
// this fact to ensure we are working with a full directory name.
71+
// Extract the file if (pathPrefix + "/") is a prefix in path name
72+
//
73+
// The current archive item is a file.
74+
// There are two things possible here:
75+
// 1 User specified a filename that is an exact match for the current archive file,
76+
// we need to extract this file.
77+
// 2 The current archive filename is not a exact match to the user supplied filename.
78+
// Check if (pathPrefix + "/") is a prefix in f.Name, if yes, we extract this file.
79+
80+
zipPathIsFile := !zipPath.FileInfo().IsDir()
81+
return (zipPathIsFile && zipPath.Name == pathPrefix) || strings.Index(zipPath.Name, pathPrefix + "/") == 0
82+
}
83+
6484
// Decompress the file at zipFileAbsPath and move only those files under filesToExtractFromZipPath to localPath
6585
func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) error {
6686

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

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

92112
if f.FileInfo().IsDir() {
93113
// Create a directory
@@ -149,4 +169,4 @@ func MakeGitHubZipFileRequest(gitHubCommit GitHubCommit, gitHubToken string, ins
149169
}
150170

151171
return request, nil
152-
}
172+
}

file_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ func TestExtractFiles(t *testing.T) {
296296
{publicGitHub, "test-fixtures/fetch-test-public-0.0.2.zip", "/", 2, nil},
297297
{publicGitHub, "test-fixtures/fetch-test-public-0.0.3.zip", "/", 4, []string{"/README.md"} },
298298
{publicGitHub, "test-fixtures/fetch-test-public-0.0.3.zip", "/folder", 2, nil},
299+
{publicGitHub, "test-fixtures/fetch-test-public-0.0.4.zip", "/aaa", 2, []string{"/hello.txt", "/subaaa/subhello.txt"} },
299300
}
300301

301302
for _, tc := range cases {
@@ -339,6 +340,35 @@ func TestExtractFiles(t *testing.T) {
339340
}
340341
}
341342

343+
func TestExtractFilesExtractFile(t *testing.T) {
344+
// Create a temp directory
345+
tempDir, err := ioutil.TempDir("", "")
346+
if err != nil {
347+
t.Fatalf("Failed to create temp directory: %s", err)
348+
}
349+
defer os.RemoveAll(tempDir)
350+
351+
zipFilePath := "test-fixtures/fetch-test-public-0.0.4.zip"
352+
filePathToExtract := "zzz.txt"
353+
localFileName := "/localzzz.txt"
354+
localPathName := filepath.Join(tempDir, localFileName)
355+
err = extractFiles(zipFilePath, filePathToExtract, localPathName)
356+
if err != nil {
357+
t.Fatalf("Failed to extract files: %s", err)
358+
}
359+
360+
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
361+
relativeFilename := strings.TrimPrefix(path, tempDir)
362+
363+
if ! info.IsDir() {
364+
if relativeFilename != localFileName {
365+
t.Fatalf("Expected local file %s to be created, but not found.\n", localFileName)
366+
}
367+
}
368+
return nil
369+
})
370+
}
371+
342372
// Return ture if the given slice contains the given string
343373
func stringInSlice(s string, slice []string) bool {
344374
for _, val := range slice {
1.85 KB
Binary file not shown.

0 commit comments

Comments
 (0)