@@ -10,6 +10,7 @@ import (
10
10
"io/ioutil"
11
11
"os"
12
12
"path/filepath"
13
+ "sync"
13
14
"time"
14
15
15
16
"golang.org/x/vuln/client"
@@ -18,12 +19,6 @@ import (
18
19
19
20
// copy from x/vuln/cmd/govulncheck/cache.go
20
21
21
- // NOTE: this cache implementation should be kept internal to the go tooling
22
- // (i.e. cmd/go/internal/something) so that the vulndb cache is owned by the
23
- // go command. Also it is currently NOT CONCURRENCY SAFE since it does not
24
- // implement file locking. If ported to the stdlib it should use
25
- // cmd/go/internal/lockedfile.
26
-
27
22
// The cache uses a single JSON index file for each vulnerability database
28
23
// which contains the map from packages to the time the last
29
24
// vulnerability for that package was added/modified and the time that
@@ -42,9 +37,11 @@ import (
42
37
// $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/{import path}/vulns.json
43
38
// []*osv.Entry
44
39
45
- // fsCache is file-system cache implementing osv.Cache
46
- // TODO: make cache thread-safe
40
+ // fsCache is a thread-safe file-system cache implementing osv.Cache
41
+ //
42
+ // TODO: use something like cmd/go/internal/lockedfile for thread safety?
47
43
type fsCache struct {
44
+ mu sync.Mutex
48
45
rootDir string
49
46
}
50
47
@@ -61,6 +58,9 @@ type cachedIndex struct {
61
58
}
62
59
63
60
func (c * fsCache ) ReadIndex (dbName string ) (client.DBIndex , time.Time , error ) {
61
+ c .mu .Lock ()
62
+ defer c .mu .Unlock ()
63
+
64
64
b , err := ioutil .ReadFile (filepath .Join (c .rootDir , dbName , "index.json" ))
65
65
if err != nil {
66
66
if os .IsNotExist (err ) {
@@ -76,6 +76,9 @@ func (c *fsCache) ReadIndex(dbName string) (client.DBIndex, time.Time, error) {
76
76
}
77
77
78
78
func (c * fsCache ) WriteIndex (dbName string , index client.DBIndex , retrieved time.Time ) error {
79
+ c .mu .Lock ()
80
+ defer c .mu .Unlock ()
81
+
79
82
path := filepath .Join (c .rootDir , dbName )
80
83
if err := os .MkdirAll (path , 0755 ); err != nil {
81
84
return err
@@ -94,6 +97,9 @@ func (c *fsCache) WriteIndex(dbName string, index client.DBIndex, retrieved time
94
97
}
95
98
96
99
func (c * fsCache ) ReadEntries (dbName string , p string ) ([]* osv.Entry , error ) {
100
+ c .mu .Lock ()
101
+ defer c .mu .Unlock ()
102
+
97
103
b , err := ioutil .ReadFile (filepath .Join (c .rootDir , dbName , p , "vulns.json" ))
98
104
if err != nil {
99
105
if os .IsNotExist (err ) {
@@ -109,6 +115,9 @@ func (c *fsCache) ReadEntries(dbName string, p string) ([]*osv.Entry, error) {
109
115
}
110
116
111
117
func (c * fsCache ) WriteEntries (dbName string , p string , entries []* osv.Entry ) error {
118
+ c .mu .Lock ()
119
+ defer c .mu .Unlock ()
120
+
112
121
path := filepath .Join (c .rootDir , dbName , p )
113
122
if err := os .MkdirAll (path , 0777 ); err != nil {
114
123
return err
0 commit comments