@@ -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
}
@@ -311,9 +314,11 @@ func checkInstalled(folder, key string) (string, bool, error) {
311
314
return location , ok , err
312
315
}
313
316
314
- func writeInstalled (folder , path string ) error {
317
+ func (t * Tools ) writeInstalled (path string ) error {
318
+ t .mutex .RLock ()
319
+ defer t .mutex .RUnlock ()
315
320
// read installed.json
316
- installedFile , err := utilities .SafeJoin (folder , "installed.json" )
321
+ installedFile , err := utilities .SafeJoin (t . folder , "installed.json" )
317
322
if err != nil {
318
323
return err
319
324
}
@@ -325,7 +330,7 @@ func writeInstalled(folder, path string) error {
325
330
parts := strings .Split (path , string (filepath .Separator ))
326
331
tool := parts [len (parts )- 2 ]
327
332
toolWithVersion := fmt .Sprint (tool , "-" , parts [len (parts )- 1 ])
328
- toolFile , err := utilities .SafeJoin (folder , path )
333
+ toolFile , err := utilities .SafeJoin (t . folder , path )
329
334
if err != nil {
330
335
return err
331
336
}
0 commit comments