@@ -29,6 +29,7 @@ import (
2929 "path/filepath"
3030 "runtime"
3131 "strings"
32+ "sync"
3233
3334 "github.com/arduino/arduino-create-agent/gen/tools"
3435 "github.com/arduino/arduino-create-agent/index"
@@ -60,6 +61,7 @@ type Tools struct {
6061 index * index.Resource
6162 folder string
6263 behaviour string
64+ mutex sync.RWMutex
6365}
6466
6567// 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 {
7072 index : index ,
7173 folder : folder ,
7274 behaviour : behaviour ,
75+ mutex : sync.RWMutex {},
7376 }
7477}
7578
@@ -187,7 +190,7 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
187190 }
188191 if ok && pathExists (location ) {
189192 // overwrite the default tool with this one
190- err := writeInstalled ( t . folder , path )
193+ err := t . writeInstalled ( path )
191194 if err != nil {
192195 return nil , err
193196 }
@@ -245,7 +248,7 @@ func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools
245248 }
246249
247250 // Write installed.json for retrocompatibility with v1
248- err = writeInstalled ( t . folder , path )
251+ err = t . writeInstalled ( path )
249252 if err != nil {
250253 return nil , err
251254 }
@@ -311,9 +314,11 @@ func checkInstalled(folder, key string) (string, bool, error) {
311314 return location , ok , err
312315}
313316
314- func writeInstalled (folder , path string ) error {
317+ func (t * Tools ) writeInstalled (path string ) error {
318+ t .mutex .RLock ()
319+ defer t .mutex .RUnlock ()
315320 // read installed.json
316- installedFile , err := utilities .SafeJoin (folder , "installed.json" )
321+ installedFile , err := utilities .SafeJoin (t . folder , "installed.json" )
317322 if err != nil {
318323 return err
319324 }
@@ -325,7 +330,7 @@ func writeInstalled(folder, path string) error {
325330 parts := strings .Split (path , string (filepath .Separator ))
326331 tool := parts [len (parts )- 2 ]
327332 toolWithVersion := fmt .Sprint (tool , "-" , parts [len (parts )- 1 ])
328- toolFile , err := utilities .SafeJoin (folder , path )
333+ toolFile , err := utilities .SafeJoin (t . folder , path )
329334 if err != nil {
330335 return err
331336 }
0 commit comments