Skip to content

Commit a66d469

Browse files
committed
feat: store databases in user cache directory by default
1 parent 5be7f01 commit a66d469

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

pkg/database/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type Config struct {
1010
Type string `yaml:"type"`
1111
URL string `yaml:"url"`
1212
WorkingDirectory string `yaml:"working-directory"`
13+
14+
CacheDirectory string `yaml:"-"`
1315
}
1416

1517
// Identifier returns a unique string that can be used to check if a loaded

pkg/database/smart.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type SmartDB struct {
2626
WorkingDirectory string
2727
Offline bool
2828
UpdatedAt string
29+
30+
cacheDirectory string
2931
}
3032

3133
func (db *SmartDB) Name() string { return db.name }
@@ -35,7 +37,7 @@ func (db *SmartDB) cachePath() string {
3537
hash := sha256.Sum256([]byte(db.ArchiveURL))
3638
fileName := fmt.Sprintf("osv-detector-%x-db", hash)
3739

38-
return filepath.Join(os.TempDir(), fileName)
40+
return filepath.Join(db.cacheDirectory, fileName)
3941
}
4042

4143
func (db *SmartDB) cacheFile(name string, content []byte) error {
@@ -96,6 +98,7 @@ func (db *SmartDB) populateFromZip() (*time.Time, error) {
9698
name: db.name,
9799
ArchiveURL: db.ArchiveURL,
98100
WorkingDirectory: db.WorkingDirectory,
101+
cacheDirectory: db.cacheDirectory,
99102
Offline: db.Offline,
100103
}
101104

@@ -311,11 +314,22 @@ func (db *SmartDB) load() error {
311314
}
312315

313316
func NewSmartDB(config Config, offline bool) (*SmartDB, error) {
317+
if config.CacheDirectory == "" {
318+
d, err := setupCacheDirectory()
319+
320+
if err != nil {
321+
return nil, err
322+
}
323+
324+
config.CacheDirectory = d
325+
}
326+
314327
db := &SmartDB{
315328
name: config.Name,
316329
identifier: config.Identifier(),
317330
ArchiveURL: config.URL,
318331
WorkingDirectory: config.WorkingDirectory,
332+
cacheDirectory: config.CacheDirectory,
319333
Offline: offline,
320334
}
321335
if err := db.load(); err != nil {

pkg/database/smart_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestNewSmartDB_Offline_WithoutCache(t *testing.T) {
7373
t.Errorf("a server request was made when running offline")
7474
})
7575

76-
_, err := database.NewSmartDB(database.Config{URL: ts.URL}, true)
76+
_, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, true)
7777

7878
if !errors.Is(err, database.ErrOfflineDatabaseNotFound) {
7979
t.Errorf("expected \"%v\" error but got \"%v\"", database.ErrOfflineDatabaseNotFound, err)
@@ -109,7 +109,7 @@ func TestNewSmartDB_Offline_WithZipCache(t *testing.T) {
109109
}),
110110
})
111111

112-
db, err := database.NewSmartDB(database.Config{URL: ts.URL}, true)
112+
db, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, true)
113113

114114
if err != nil {
115115
t.Fatalf("unexpected error \"%v\"", err)
@@ -152,7 +152,7 @@ func TestNewSmartDB_Offline_WithDirCache(t *testing.T) {
152152
}),
153153
})
154154

155-
db, err := database.NewSmartDB(database.Config{URL: ts.URL}, true)
155+
db, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, true)
156156

157157
if err != nil {
158158
t.Fatalf("unexpected error \"%v\"", err)
@@ -175,7 +175,7 @@ func TestNewSmartDB_BadZip(t *testing.T) {
175175
_, _ = w.Write([]byte("this is not a zip"))
176176
})
177177

178-
_, err := database.NewSmartDB(database.Config{URL: ts.URL}, false)
178+
_, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
179179

180180
if err == nil {
181181
t.Errorf("expected an error but did not get one")
@@ -185,7 +185,7 @@ func TestNewSmartDB_BadZip(t *testing.T) {
185185
func TestNewSmartDB_UnsupportedProtocol(t *testing.T) {
186186
t.Parallel()
187187

188-
_, err := database.NewSmartDB(database.Config{URL: "file://hello-world"}, false)
188+
_, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: "file://hello-world"}, false)
189189

190190
if err == nil {
191191
t.Errorf("expected an error but did not get one")
@@ -221,7 +221,7 @@ func TestNewSmartDB_Online_WithoutCache(t *testing.T) {
221221
}))
222222
})
223223

224-
db, err := database.NewSmartDB(database.Config{URL: ts.URL}, false)
224+
db, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
225225

226226
if err != nil {
227227
t.Fatalf("unexpected error \"%v\"", err)
@@ -246,7 +246,7 @@ func TestNewSmartDB_Online_WithoutCache_NotFound(t *testing.T) {
246246
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{}))
247247
})
248248

249-
_, err := database.NewSmartDB(database.Config{URL: ts.URL}, false)
249+
_, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
250250

251251
if err == nil {
252252
t.Errorf("expected an error but did not get one")
@@ -304,7 +304,7 @@ func TestNewSmartDB_Online_WithExistingDirDB_UpToDate(t *testing.T) {
304304
"GHSA-5.json": withDefaultAffected("GHSA-5"),
305305
})
306306

307-
db, err := database.NewSmartDB(database.Config{URL: ts.URL}, false)
307+
db, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
308308

309309
if err != nil {
310310
t.Fatalf("unexpected error \"%v\"", err)
@@ -356,7 +356,7 @@ func TestNewSmartDB_Online_WithExistingDirDB_NotModified(t *testing.T) {
356356
"GHSA-5.json": withDefaultAffected("GHSA-5"),
357357
})
358358

359-
db, err := database.NewSmartDB(database.Config{URL: ts.URL}, false)
359+
db, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
360360

361361
if err != nil {
362362
t.Fatalf("unexpected error \"%v\"", err)
@@ -419,7 +419,7 @@ func TestNewSmartDB_Online_WithExistingDirDB_Outdated(t *testing.T) {
419419
"GHSA-5.json": withDefaultAffected("GHSA-5"),
420420
})
421421

422-
db, err := database.NewSmartDB(database.Config{URL: ts.URL}, false)
422+
db, err := database.NewSmartDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
423423

424424
if err != nil {
425425
t.Fatalf("unexpected error \"%v\"", err)

pkg/database/zip.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"net/http"
1313
"os"
14+
"path"
1415
"path/filepath"
1516
"strings"
1617
)
@@ -24,6 +25,7 @@ type ZipDB struct {
2425
WorkingDirectory string
2526
Offline bool
2627
UpdatedAt string
28+
cacheDirectory string
2729
}
2830

2931
func (db *ZipDB) Name() string { return db.name }
@@ -44,7 +46,7 @@ func (db *ZipDB) cachePath() string {
4446
hash := sha256.Sum256([]byte(db.ArchiveURL))
4547
fileName := fmt.Sprintf("osv-detector-%x-db.json", hash)
4648

47-
return filepath.Join(os.TempDir(), fileName)
49+
return filepath.Join(db.cacheDirectory, fileName)
4850
}
4951

5052
func (db *ZipDB) fetchZip() ([]byte, error) {
@@ -192,11 +194,22 @@ func (db *ZipDB) load() error {
192194
}
193195

194196
func NewZippedDB(config Config, offline bool) (*ZipDB, error) {
197+
if config.CacheDirectory == "" {
198+
d, err := setupCacheDirectory()
199+
200+
if err != nil {
201+
return nil, err
202+
}
203+
204+
config.CacheDirectory = d
205+
}
206+
195207
db := &ZipDB{
196208
name: config.Name,
197209
identifier: config.Identifier(),
198210
ArchiveURL: config.URL,
199211
WorkingDirectory: config.WorkingDirectory,
212+
cacheDirectory: config.CacheDirectory,
200213
Offline: offline,
201214
}
202215
if err := db.load(); err != nil {
@@ -205,3 +218,22 @@ func NewZippedDB(config Config, offline bool) (*ZipDB, error) {
205218

206219
return db, nil
207220
}
221+
222+
// setupCacheDirectory attempts to set up the directory the detector should use
223+
// to cache things like local databases, attempting to use the user cache directory
224+
// if possible or otherwise falling back to the temp directory
225+
func setupCacheDirectory() (string, error) {
226+
localDBPath, err := os.UserCacheDir()
227+
228+
if err != nil {
229+
localDBPath = os.TempDir()
230+
}
231+
232+
altPath := path.Join(localDBPath, "osv-detector")
233+
err = os.MkdirAll(altPath, 0750)
234+
if err == nil {
235+
return altPath, nil
236+
}
237+
238+
return "", err
239+
}

pkg/database/zip_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func withDefaultAffected(id string) database.OSV {
3636

3737
func withSummary(id string, summary string) database.OSV {
3838
return database.OSV{
39-
ID: id,
39+
ID: id,
4040
Summary: summary,
4141
Affected: []database.Affected{
4242
{
@@ -157,7 +157,7 @@ func TestNewZippedDB_Offline_WithoutCache(t *testing.T) {
157157
t.Errorf("a server request was made when running offline")
158158
})
159159

160-
_, err := database.NewZippedDB(database.Config{URL: ts.URL}, true)
160+
_, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, true)
161161

162162
if !errors.Is(err, database.ErrOfflineDatabaseNotFound) {
163163
t.Errorf("expected \"%v\" error but got \"%v\"", database.ErrOfflineDatabaseNotFound, err)
@@ -193,7 +193,7 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
193193
}),
194194
})
195195

196-
db, err := database.NewZippedDB(database.Config{URL: ts.URL}, true)
196+
db, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, true)
197197

198198
if err != nil {
199199
t.Fatalf("unexpected error \"%v\"", err)
@@ -213,7 +213,7 @@ func TestNewZippedDB_BadZip(t *testing.T) {
213213
_, _ = w.Write([]byte("this is not a zip"))
214214
})
215215

216-
_, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)
216+
_, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
217217

218218
if err == nil {
219219
t.Errorf("expected an error but did not get one")
@@ -223,7 +223,7 @@ func TestNewZippedDB_BadZip(t *testing.T) {
223223
func TestNewZippedDB_UnsupportedProtocol(t *testing.T) {
224224
t.Parallel()
225225

226-
_, err := database.NewZippedDB(database.Config{URL: "file://hello-world"}, false)
226+
_, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: "file://hello-world"}, false)
227227

228228
if err == nil {
229229
t.Errorf("expected an error but did not get one")
@@ -251,7 +251,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
251251
}))
252252
})
253253

254-
db, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)
254+
db, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
255255

256256
if err != nil {
257257
t.Fatalf("unexpected error \"%v\"", err)
@@ -268,7 +268,7 @@ func TestNewZippedDB_Online_WithoutCache_NotFound(t *testing.T) {
268268
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{}))
269269
})
270270

271-
_, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)
271+
_, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
272272

273273
if err == nil {
274274
t.Errorf("expected an error but did not get one")
@@ -306,7 +306,7 @@ func TestNewZippedDB_Online_WithCache(t *testing.T) {
306306
}),
307307
})
308308

309-
db, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)
309+
db, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
310310

311311
if err != nil {
312312
t.Fatalf("unexpected error \"%v\"", err)
@@ -357,7 +357,7 @@ func TestNewZippedDB_Online_WithOldCache(t *testing.T) {
357357
}),
358358
})
359359

360-
db, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)
360+
db, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
361361

362362
if err != nil {
363363
t.Fatalf("unexpected error \"%v\"", err)
@@ -389,7 +389,7 @@ func TestNewZippedDB_Online_WithBadCache(t *testing.T) {
389389

390390
cacheWriteBad(t, ts.URL, "this is not json!")
391391

392-
db, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)
392+
db, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
393393

394394
if err != nil {
395395
t.Fatalf("unexpected error \"%v\"", err)
@@ -413,7 +413,7 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
413413
}))
414414
})
415415

416-
db, err := database.NewZippedDB(database.Config{URL: ts.URL}, false)
416+
db, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL}, false)
417417

418418
if err != nil {
419419
t.Fatalf("unexpected error \"%v\"", err)
@@ -435,7 +435,7 @@ func TestNewZippedDB_WorkingDirectory(t *testing.T) {
435435
}))
436436
})
437437

438-
db, err := database.NewZippedDB(database.Config{URL: ts.URL, WorkingDirectory: "reviewed"}, false)
438+
db, err := database.NewZippedDB(database.Config{CacheDirectory: os.TempDir(), URL: ts.URL, WorkingDirectory: "reviewed"}, false)
439439

440440
if err != nil {
441441
t.Fatalf("unexpected error \"%v\"", err)

0 commit comments

Comments
 (0)