From 65b13c31dc44545440dad10923466666eba10b09 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Wed, 3 Apr 2019 15:58:12 +0200 Subject: [PATCH] Fix duplicate tool bug When you attempt to download the same tool twice it can fail if there are symlinks inside the archive. By removing the folder we make sure we start from scratch every time Also indexes were behaving badly attempting to parse extraneous files --- v2/pkgs/indexes.go | 11 ++++++++--- v2/pkgs/indexes_test.go | 4 ++++ v2/pkgs/tools.go | 7 +++++++ v2/pkgs/tools_test.go | 10 ++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/v2/pkgs/indexes.go b/v2/pkgs/indexes.go index 032d16614..ff0d9dcff 100644 --- a/v2/pkgs/indexes.go +++ b/v2/pkgs/indexes.go @@ -7,6 +7,7 @@ import ( "net/url" "os" "path/filepath" + "strings" "github.com/arduino/arduino-create-agent/gen/indexes" "github.com/sirupsen/logrus" @@ -77,13 +78,17 @@ func (c *Indexes) List(context.Context) ([]string, error) { return nil, err } - res := make([]string, len(files)) - for i, file := range files { + res := []string{} + for _, file := range files { + // Select only files that begin with http + if !strings.HasPrefix(file.Name(), "http") { + continue + } path, err := url.PathUnescape(file.Name()) if err != nil { c.Log.Warn(err) } - res[i] = path + res = append(res, path) } return res, nil diff --git a/v2/pkgs/indexes_test.go b/v2/pkgs/indexes_test.go index ee7625216..278fc4116 100644 --- a/v2/pkgs/indexes_test.go +++ b/v2/pkgs/indexes_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "testing" @@ -28,6 +29,9 @@ func TestIndexes(t *testing.T) { } defer os.RemoveAll(tmp) + // Create extraneous folder in temp folder + os.MkdirAll(filepath.Join(tmp, "arduino"), 0755) + service := pkgs.Indexes{ Folder: tmp, } diff --git a/v2/pkgs/tools.go b/v2/pkgs/tools.go index 7598e0e7b..777007943 100644 --- a/v2/pkgs/tools.go +++ b/v2/pkgs/tools.go @@ -169,8 +169,15 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools var buffer bytes.Buffer reader := io.TeeReader(res.Body, &buffer) + // Cleanup + err = os.RemoveAll(filepath.Join(c.Folder, path)) + if err != nil { + return nil, err + } + err = extract.Archive(ctx, reader, c.Folder, rename(path)) if err != nil { + os.RemoveAll(path) return nil, err } diff --git a/v2/pkgs/tools_test.go b/v2/pkgs/tools_test.go index f45bd7c1d..30fdb8cd0 100644 --- a/v2/pkgs/tools_test.go +++ b/v2/pkgs/tools_test.go @@ -89,6 +89,16 @@ func TestTools(t *testing.T) { t.Fatalf("expected %d == %d (%s)", len(installed), 1, "len(installed)") } + // Install the tool again + _, err = service.Install(ctx, &tools.ToolPayload{ + Packager: "arduino", + Name: "avrdude", + Version: "6.0.1-arduino2", + }) + if err != nil { + t.Fatal(err) + } + // Remove tool _, err = service.Remove(ctx, &tools.ToolPayload{ Packager: "arduino",