From afeccac93d6cace9986dd0f8896cfdf3240bf894 Mon Sep 17 00:00:00 2001 From: zwwhdls Date: Sun, 12 Jan 2020 20:03:09 +0800 Subject: [PATCH] fix conflict subdirectory when untardir is the clashing directory Signed-off-by: zwwhdls --- cmd/helm/fetch.go | 15 +++++++++++---- cmd/helm/fetch_test.go | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/helm/fetch.go b/cmd/helm/fetch.go index 7fe0ab015..9407b8f1f 100644 --- a/cmd/helm/fetch.go +++ b/cmd/helm/fetch.go @@ -166,13 +166,20 @@ func (f *fetchCmd) run() error { if !filepath.IsAbs(ud) { ud = filepath.Join(f.destdir, ud) } - if _, err := os.Stat(ud); err != nil { - if err := os.MkdirAll(ud, 0755); err != nil { + // Let udCheck to check conflict file/dir without replacing ud when untarDir is the current directory(.). + udCheck := ud + if udCheck == "." { + _, udCheck = filepath.Split(f.chartRef) + } else { + _, chartName := filepath.Split(f.chartRef) + udCheck = filepath.Join(udCheck, chartName) + } + if _, err := os.Stat(udCheck); err != nil { + if err := os.MkdirAll(udCheck, 0755); err != nil { return fmt.Errorf("Failed to untar (mkdir): %s", err) } - } else { - return fmt.Errorf("failed to untar: a file or directory with the name %s already exists", ud) + return fmt.Errorf("Failed to untar: a file or directory with the name %s already exists", udCheck) } return chartutil.ExpandFile(ud, saved) diff --git a/cmd/helm/fetch_test.go b/cmd/helm/fetch_test.go index f92a4f827..de796bab2 100644 --- a/cmd/helm/fetch_test.go +++ b/cmd/helm/fetch_test.go @@ -188,7 +188,7 @@ func TestFetchCmd(t *testing.T) { if err := cmd.RunE(cmd, []string{tt.chart}); err != nil { if tt.fail { if tt.wantErrorMsg != "" && strings.Contains(tt.wantErrorMsg, err.Error()) { - t.Fatalf("%q reported error not equel wantErr, reported: %s, wanted: %s", tt.name, err, tt.wantErrorMsg) + t.Fatalf("Actual error %s, not equal to expected error %s", err, tt.wantErrorMsg) } return }