diff options
author | Jon Bratseth <bratseth@oath.com> | 2022-02-02 12:23:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-02 12:23:24 +0100 |
commit | 6c1d5b3cc3002c832ea1474d80a1b9e2001f1516 (patch) | |
tree | 005341de479d048f8268ee061a44a3e6af471003 | |
parent | fe37932e6418d3d7a8bbea2f4ca5f727b67e8d00 (diff) | |
parent | ed0c38cccd5e54e7a4959510d203337d64ae28ec (diff) |
Merge pull request #21020 from vespa-engine/mpolden/preserve-permissions
Preserve permissions of extracted files
-rw-r--r-- | client/go/cmd/clone.go | 62 | ||||
-rw-r--r-- | client/go/cmd/clone_test.go | 7 | ||||
-rw-r--r-- | client/go/cmd/testdata/sample-apps-master.zip | bin | 4253469 -> 4653209 bytes |
3 files changed, 37 insertions, 32 deletions
diff --git a/client/go/cmd/clone.go b/client/go/cmd/clone.go index 6fe3c0d5a29..1ba95668d29 100644 --- a/client/go/cmd/clone.go +++ b/client/go/cmd/clone.go @@ -65,41 +65,40 @@ variable.`, }, } -func cloneApplication(source string, name string) { +func cloneApplication(applicationName string, applicationDir string) { zipFile := getSampleAppsZip() defer zipFile.Close() - zipReader, zipOpenError := zip.OpenReader(zipFile.Name()) - if zipOpenError != nil { - fatalErr(zipOpenError, "Could not open sample apps zip '", color.Cyan(zipFile.Name()), "'") + r, err := zip.OpenReader(zipFile.Name()) + if err != nil { + fatalErr(err, "Could not open sample apps zip '", color.Cyan(zipFile.Name()), "'") return } - defer zipReader.Close() + defer r.Close() found := false - for _, f := range zipReader.File { - zipEntryPrefix := "sample-apps-master/" + source + "/" - if strings.HasPrefix(f.Name, zipEntryPrefix) { + for _, f := range r.File { + dirPrefix := "sample-apps-master/" + applicationName + "/" + if strings.HasPrefix(f.Name, dirPrefix) { if !found { // Create destination directory lazily when source is found - createErr := os.Mkdir(name, 0755) + createErr := os.Mkdir(applicationDir, 0755) if createErr != nil { - fatalErr(createErr, "Could not create directory '", color.Cyan(name), "'") + fatalErr(createErr, "Could not create directory '", color.Cyan(applicationDir), "'") return } } found = true - copyError := copy(f, name, zipEntryPrefix) - if copyError != nil { - fatalErr(copyError, "Could not copy zip entry '", color.Cyan(f.Name), "' to ", color.Cyan(name)) + if err := copy(f, applicationDir, dirPrefix); err != nil { + fatalErr(err, "Could not copy zip entry '", color.Cyan(f.Name), "' to ", color.Cyan(applicationDir)) return } } } if !found { - fatalErrHint(fmt.Errorf("Could not find source application '%s'", color.Cyan(source)), "Use -f to ignore the cache") + fatalErrHint(fmt.Errorf("Could not find source application '%s'", color.Cyan(applicationName)), "Use -f to ignore the cache") } else { - log.Print("Created ", color.Cyan(name)) + log.Print("Created ", color.Cyan(applicationDir)) } } @@ -158,6 +157,10 @@ func getSampleAppsZip() *os.File { fatalErr(nil, "Could not download sample apps from GitHub: ", response.StatusCode) return nil } + if err := f.Truncate(0); err != nil { + fatalErr(err, "Could not truncate sample apps file: ", f.Name()) + return nil + } if _, err := io.Copy(f, response.Body); err != nil { fatalErr(err, "Could not write sample apps to file: ", f.Name()) return nil @@ -172,26 +175,25 @@ func copy(f *zip.File, destinationDir string, zipEntryPrefix string) error { destinationPath := filepath.Join(destinationDir, filepath.FromSlash(strings.TrimPrefix(f.Name, zipEntryPrefix))) if strings.HasSuffix(f.Name, "/") { if f.Name != zipEntryPrefix { // root is already created - createError := os.Mkdir(destinationPath, 0755) - if createError != nil { - return createError + if err := os.Mkdir(destinationPath, 0755); err != nil { + return err } } } else { - zipEntry, zipEntryOpenError := f.Open() - if zipEntryOpenError != nil { - return zipEntryOpenError + r, err := f.Open() + if err != nil { + return err } - defer zipEntry.Close() - - destination, createError := os.Create(destinationPath) - if createError != nil { - return createError + defer r.Close() + destination, err := os.Create(destinationPath) + if err != nil { + return err } - - _, copyError := io.Copy(destination, zipEntry) - if copyError != nil { - return copyError + if _, err := io.Copy(destination, r); err != nil { + return err + } + if err := os.Chmod(destinationPath, f.Mode()); err != nil { + return err } } return nil diff --git a/client/go/cmd/clone_test.go b/client/go/cmd/clone_test.go index 054dc7b21fb..af8b686b111 100644 --- a/client/go/cmd/clone_test.go +++ b/client/go/cmd/clone_test.go @@ -15,7 +15,7 @@ import ( ) func TestClone(t *testing.T) { - assertCreated("album-recommendation-selfhosted", "mytestapp", t) + assertCreated("text-search", "mytestapp", t) } func assertCreated(sampleAppName string, app string, t *testing.T) { @@ -32,6 +32,9 @@ func assertCreated(sampleAppName string, app string, t *testing.T) { assert.True(t, util.IsDirectory(filepath.Join(app, "src", "main", "application"))) servicesStat, _ := os.Stat(filepath.Join(app, "src", "main", "application", "services.xml")) - servicesSize := int64(2474) + servicesSize := int64(1772) assert.Equal(t, servicesSize, servicesStat.Size()) + + scriptStat, _ := os.Stat(filepath.Join(app, "bin", "convert-msmarco.sh")) + assert.Equal(t, os.FileMode(0755), scriptStat.Mode()) } diff --git a/client/go/cmd/testdata/sample-apps-master.zip b/client/go/cmd/testdata/sample-apps-master.zip Binary files differindex 6ad49361072..c8fb40af713 100644 --- a/client/go/cmd/testdata/sample-apps-master.zip +++ b/client/go/cmd/testdata/sample-apps-master.zip |