@@ -29,6 +29,7 @@ import (
29
29
"path/filepath"
30
30
"runtime"
31
31
"strings"
32
+ "sync"
32
33
33
34
"github.com/arduino/arduino-create-agent/gen/tools"
34
35
"github.com/arduino/arduino-create-agent/index"
@@ -60,6 +61,7 @@ type Tools struct {
60
61
index * index.Resource
61
62
folder string
62
63
behaviour string
64
+ mutex sync.RWMutex
63
65
}
64
66
65
67
// New will return a Tool object, allowing the caller to execute operations on it.
@@ -70,6 +72,7 @@ func New(index *index.Resource, folder, behaviour string) *Tools {
70
72
index : index ,
71
73
folder : folder ,
72
74
behaviour : behaviour ,
75
+ mutex : sync.RWMutex {},
73
76
}
74
77
}
75
78
@@ -187,7 +190,7 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
187
190
}
188
191
if ok && pathExists (location ) {
189
192
// overwrite the default tool with this one
190
- err := writeInstalled ( t . folder , path )
193
+ err := t . writeInstalled ( path )
191
194
if err != nil {
192
195
return nil , err
193
196
}
@@ -245,7 +248,7 @@ func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools
245
248
}
246
249
247
250
// Write installed.json for retrocompatibility with v1
248
- err = writeInstalled ( t . folder , path )
251
+ err = t . writeInstalled ( path )
249
252
if err != nil {
250
253
return nil , err
251
254
}
@@ -307,9 +310,11 @@ func checkInstalled(folder, key string) (string, bool, error) {
307
310
return location , ok , err
308
311
}
309
312
310
- func writeInstalled (folder , path string ) error {
313
+ func (t * Tools ) writeInstalled (path string ) error {
314
+ t .mutex .RLock ()
315
+ defer t .mutex .RUnlock ()
311
316
// read installed.json
312
- installedFile , err := utilities .SafeJoin (folder , "installed.json" )
317
+ installedFile , err := utilities .SafeJoin (t . folder , "installed.json" )
313
318
if err != nil {
314
319
return err
315
320
}
@@ -321,7 +326,7 @@ func writeInstalled(folder, path string) error {
321
326
parts := strings .Split (path , string (filepath .Separator ))
322
327
tool := parts [len (parts )- 2 ]
323
328
toolWithVersion := fmt .Sprint (tool , "-" , parts [len (parts )- 1 ])
324
- toolFile , err := utilities .SafeJoin (folder , path )
329
+ toolFile , err := utilities .SafeJoin (t . folder , path )
325
330
if err != nil {
326
331
return err
327
332
}
0 commit comments