Skip to content

Commit 65b13c3

Browse files
committed
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
1 parent 94c79b3 commit 65b13c3

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

v2/pkgs/indexes.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/url"
88
"os"
99
"path/filepath"
10+
"strings"
1011

1112
"github.com/arduino/arduino-create-agent/gen/indexes"
1213
"github.com/sirupsen/logrus"
@@ -77,13 +78,17 @@ func (c *Indexes) List(context.Context) ([]string, error) {
7778
return nil, err
7879
}
7980

80-
res := make([]string, len(files))
81-
for i, file := range files {
81+
res := []string{}
82+
for _, file := range files {
83+
// Select only files that begin with http
84+
if !strings.HasPrefix(file.Name(), "http") {
85+
continue
86+
}
8287
path, err := url.PathUnescape(file.Name())
8388
if err != nil {
8489
c.Log.Warn(err)
8590
}
86-
res[i] = path
91+
res = append(res, path)
8792
}
8893

8994
return res, nil

v2/pkgs/indexes_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"os"
9+
"path/filepath"
910
"strings"
1011
"testing"
1112

@@ -28,6 +29,9 @@ func TestIndexes(t *testing.T) {
2829
}
2930
defer os.RemoveAll(tmp)
3031

32+
// Create extraneous folder in temp folder
33+
os.MkdirAll(filepath.Join(tmp, "arduino"), 0755)
34+
3135
service := pkgs.Indexes{
3236
Folder: tmp,
3337
}

v2/pkgs/tools.go

+7
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,15 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) (*tools
169169
var buffer bytes.Buffer
170170
reader := io.TeeReader(res.Body, &buffer)
171171

172+
// Cleanup
173+
err = os.RemoveAll(filepath.Join(c.Folder, path))
174+
if err != nil {
175+
return nil, err
176+
}
177+
172178
err = extract.Archive(ctx, reader, c.Folder, rename(path))
173179
if err != nil {
180+
os.RemoveAll(path)
174181
return nil, err
175182
}
176183

v2/pkgs/tools_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ func TestTools(t *testing.T) {
8989
t.Fatalf("expected %d == %d (%s)", len(installed), 1, "len(installed)")
9090
}
9191

92+
// Install the tool again
93+
_, err = service.Install(ctx, &tools.ToolPayload{
94+
Packager: "arduino",
95+
Name: "avrdude",
96+
Version: "6.0.1-arduino2",
97+
})
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
92102
// Remove tool
93103
_, err = service.Remove(ctx, &tools.ToolPayload{
94104
Packager: "arduino",

0 commit comments

Comments
 (0)