lib/kv: accept dir argument instead of internally using config.GetCacheDir()

This change makes it possible to use lib/kv for other purposes while allowing
to store the db in a place more accessible for the user
This commit is contained in:
Marcelo Waisman 2025-03-11 19:46:19 +02:00
parent 0b9671313b
commit 329de0a2f6
3 changed files with 6 additions and 7 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/cache"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/configstruct"
"github.com/rclone/rclone/fs/fspath"
@ -157,7 +158,7 @@ func NewFs(ctx context.Context, fsname, rpath string, cmap configmap.Mapper) (fs
if f.opt.MaxAge > 0 {
gob.Register(hashRecord{})
db, err := kv.Start(ctx, "hasher", f.Fs)
db, err := kv.Start(ctx, "hasher", config.GetCacheDir(), f.Fs)
if err != nil {
return nil, err
}

View File

@ -13,7 +13,6 @@ import (
"time"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/lib/encoder"
"go.etcd.io/bbolt"
)
@ -66,14 +65,12 @@ func makeName(facility string, f fs.Fs) string {
}
// Start a new key-value database
func Start(ctx context.Context, facility string, f fs.Fs) (*DB, error) {
func Start(ctx context.Context, facility string, dir string, f fs.Fs) (*DB, error) {
dbMut.Lock()
defer dbMut.Unlock()
if db := lockedGet(facility, f); db != nil {
return db, nil
}
dir := filepath.Join(config.GetCacheDir(), "kv")
if err := os.MkdirAll(dir, dbDirMode); err != nil {
return nil, err
}

View File

@ -8,6 +8,7 @@ import (
"sync"
"testing"
"github.com/rclone/rclone/fs/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -22,7 +23,7 @@ func TestKvConcurrency(t *testing.T) {
wg.Add(threadNum)
for i := range threadNum {
go func(i int) {
db, err := Start(ctx, "test", nil)
db, err := Start(ctx, "test", config.GetCacheDir(), nil)
require.NoError(t, err)
require.NotNil(t, db)
results[i] = db
@ -57,7 +58,7 @@ func TestKvExit(t *testing.T) {
for i := range dbNum {
facility := fmt.Sprintf("test-%d", i)
for j := 0; j <= i; j++ {
db, err := Start(ctx, facility, nil)
db, err := Start(ctx, facility, config.GetCacheDir(), nil)
require.NoError(t, err)
require.NotNil(t, db)
}