@@ -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,17 +61,23 @@ type Tools struct {
60
61
index * index.Resource
61
62
folder string
62
63
behaviour string
64
+ installed map [string ]string
65
+ mutex sync.RWMutex
63
66
}
64
67
65
68
// New will return a Tool object, allowing the caller to execute operations on it.
66
69
// The New function will accept an index as parameter (used to download the indexes)
67
70
// and a folder used to download the indexes
68
71
func New (index * index.Resource , folder , behaviour string ) * Tools {
69
- return & Tools {
72
+ t := & Tools {
70
73
index : index ,
71
74
folder : folder ,
72
75
behaviour : behaviour ,
76
+ installed : map [string ]string {},
77
+ mutex : sync.RWMutex {},
73
78
}
79
+ t .readInstalled ()
80
+ return t
74
81
}
75
82
76
83
// Installedhead is here only because it was required by the front-end.
@@ -181,13 +188,10 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
181
188
key := correctTool .Name + "-" + correctTool .Version
182
189
// Check if it already exists
183
190
if t .behaviour == "keep" && pathExists (t .folder ) {
184
- location , ok , err := checkInstalled (t .folder , key )
185
- if err != nil {
186
- return nil , err
187
- }
191
+ location , ok := t .installed [key ]
188
192
if ok && pathExists (location ) {
189
193
// overwrite the default tool with this one
190
- err := writeInstalled ( t . folder , path )
194
+ err := t . writeInstalled ( path )
191
195
if err != nil {
192
196
return nil , err
193
197
}
@@ -245,7 +249,7 @@ func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools
245
249
}
246
250
247
251
// Write installed.json for retrocompatibility with v1
248
- err = writeInstalled ( t . folder , path )
252
+ err = t . writeInstalled ( path )
249
253
if err != nil {
250
254
return nil , err
251
255
}
@@ -285,61 +289,53 @@ func rename(base string) extract.Renamer {
285
289
}
286
290
}
287
291
288
- func readInstalled (installedFile string ) (map [string ]string , error ) {
292
+ func (t * Tools ) readInstalled () error {
293
+ t .mutex .RLock ()
294
+ defer t .mutex .RUnlock ()
289
295
// read installed.json
290
- installed := map [string ]string {}
291
- data , err := os .ReadFile (installedFile )
292
- if err == nil {
293
- err = json .Unmarshal (data , & installed )
294
- if err != nil {
295
- return nil , err
296
- }
297
- }
298
- return installed , nil
299
- }
300
-
301
- func checkInstalled (folder , key string ) (string , bool , error ) {
302
- installedFile , err := utilities .SafeJoin (folder , "installed.json" )
303
- if err != nil {
304
- return "" , false , err
305
- }
306
- installed , err := readInstalled (installedFile )
307
- if err != nil {
308
- return "" , false , err
309
- }
310
- location , ok := installed [key ]
311
- return location , ok , err
312
- }
313
-
314
- func writeInstalled (folder , path string ) error {
315
- // read installed.json
316
- installedFile , err := utilities .SafeJoin (folder , "installed.json" )
296
+ installedFile , err := utilities .SafeJoin (t .folder , "installed.json" )
317
297
if err != nil {
318
298
return err
319
299
}
320
- installed , err := readInstalled (installedFile )
300
+ data , err := os . ReadFile (installedFile )
321
301
if err != nil {
322
302
return err
323
303
}
304
+ return json .Unmarshal (data , & t .installed )
305
+ }
306
+
307
+ func (t * Tools ) writeInstalled (path string ) error {
308
+ t .mutex .Lock ()
309
+ defer t .mutex .Unlock ()
324
310
325
311
parts := strings .Split (path , string (filepath .Separator ))
326
312
tool := parts [len (parts )- 2 ]
327
313
toolWithVersion := fmt .Sprint (tool , "-" , parts [len (parts )- 1 ])
328
- toolFile , err := utilities .SafeJoin (folder , path )
314
+ toolFile , err := utilities .SafeJoin (t . folder , path )
329
315
if err != nil {
330
316
return err
331
317
}
332
- installed [tool ] = toolFile
333
- installed [toolWithVersion ] = toolFile
318
+ t . installed [tool ] = toolFile
319
+ t . installed [toolWithVersion ] = toolFile
334
320
335
- data , err := json .Marshal (installed )
321
+ data , err := json .Marshal (t .installed )
322
+ if err != nil {
323
+ return err
324
+ }
325
+
326
+ installedFile , err := utilities .SafeJoin (t .folder , "installed.json" )
336
327
if err != nil {
337
328
return err
338
329
}
339
330
340
331
return os .WriteFile (installedFile , data , 0644 )
341
332
}
342
333
334
+ // SetBehaviour sets the download behaviour to either keep or replace
335
+ func (t * Tools ) SetBehaviour (behaviour string ) {
336
+ t .behaviour = behaviour
337
+ }
338
+
343
339
func pathExists (path string ) bool {
344
340
_ , err := os .Stat (path )
345
341
if err == nil {
0 commit comments