Skip to content

Commit

Permalink
use stricter mutex policy to prevent installed.json corruption (#635)
Browse files Browse the repository at this point in the history
* use stricter mutex policy to prevent installed.json corruption

* move locking before installed.json read
  • Loading branch information
umbynos committed Apr 29, 2021
1 parent f4d6577 commit b696989
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (t *Tools) GetLocation(command string) (string, error) {

// writeMap() writes installed map to the json file "installed.json"
func (t *Tools) writeMap() error {
t.mutex.RLock()
t.mutex.Lock()
b, err := json.Marshal(t.installed)
t.mutex.RUnlock()
defer t.mutex.Unlock()
if err != nil {
return err
}
Expand All @@ -113,13 +113,13 @@ func (t *Tools) writeMap() error {

// readMap() reads the installed map from json file "installed.json"
func (t *Tools) readMap() error {
t.mutex.Lock()
defer t.mutex.Unlock()
filePath := path.Join(dir(), "installed.json")
b, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
t.mutex.Lock()
defer t.mutex.Unlock()
return json.Unmarshal(b, &t.installed)
}

Expand Down

0 comments on commit b696989

Please sign in to comment.