config: convert --cache-dir value to an absolute path

This commit is contained in:
albertony 2021-09-10 15:35:53 +02:00
parent 0ffdca42d5
commit f3e71f129c
11 changed files with 47 additions and 33 deletions

View file

@ -143,12 +143,12 @@ oldest chunks until it goes under this value.`,
}}, }},
}, { }, {
Name: "db_path", Name: "db_path",
Default: filepath.Join(config.CacheDir, "cache-backend"), Default: filepath.Join(config.GetCacheDir(), "cache-backend"),
Help: "Directory to store file structure metadata DB.\nThe remote name is used as the DB file name.", Help: "Directory to store file structure metadata DB.\nThe remote name is used as the DB file name.",
Advanced: true, Advanced: true,
}, { }, {
Name: "chunk_path", Name: "chunk_path",
Default: filepath.Join(config.CacheDir, "cache-backend"), Default: filepath.Join(config.GetCacheDir(), "cache-backend"),
Help: `Directory to cache chunk files. Help: `Directory to cache chunk files.
Path to where partial file data (chunks) are stored locally. The remote Path to where partial file data (chunks) are stored locally. The remote
@ -421,8 +421,8 @@ func NewFs(ctx context.Context, name, rootPath string, m configmap.Mapper) (fs.F
dbPath := f.opt.DbPath dbPath := f.opt.DbPath
chunkPath := f.opt.ChunkPath chunkPath := f.opt.ChunkPath
// if the dbPath is non default but the chunk path is default, we overwrite the last to follow the same one as dbPath // if the dbPath is non default but the chunk path is default, we overwrite the last to follow the same one as dbPath
if dbPath != filepath.Join(config.CacheDir, "cache-backend") && if dbPath != filepath.Join(config.GetCacheDir(), "cache-backend") &&
chunkPath == filepath.Join(config.CacheDir, "cache-backend") { chunkPath == filepath.Join(config.GetCacheDir(), "cache-backend") {
chunkPath = dbPath chunkPath = dbPath
} }
if filepath.Ext(dbPath) != "" { if filepath.Ext(dbPath) != "" {

View file

@ -926,9 +926,9 @@ func (r *run) newCacheFs(t *testing.T, remote, id string, needRemote, purge bool
} }
} }
runInstance.rootIsCrypt = rootIsCrypt runInstance.rootIsCrypt = rootIsCrypt
runInstance.dbPath = filepath.Join(config.CacheDir, "cache-backend", cacheRemote+".db") runInstance.dbPath = filepath.Join(config.GetCacheDir(), "cache-backend", cacheRemote+".db")
runInstance.chunkPath = filepath.Join(config.CacheDir, "cache-backend", cacheRemote) runInstance.chunkPath = filepath.Join(config.GetCacheDir(), "cache-backend", cacheRemote)
runInstance.vfsCachePath = filepath.Join(config.CacheDir, "vfs", remote) runInstance.vfsCachePath = filepath.Join(config.GetCacheDir(), "vfs", remote)
boltDb, err := cache.GetPersistent(runInstance.dbPath, runInstance.chunkPath, &cache.Features{PurgeDb: true}) boltDb, err := cache.GetPersistent(runInstance.dbPath, runInstance.chunkPath, &cache.Features{PurgeDb: true})
require.NoError(t, err) require.NoError(t, err)

View file

@ -80,7 +80,7 @@ var configPathsCommand = &cobra.Command{
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(0, 0, command, args) cmd.CheckArgs(0, 0, command, args)
fmt.Printf("Config file: %s\n", config.GetConfigPath()) fmt.Printf("Config file: %s\n", config.GetConfigPath())
fmt.Printf("Cache dir: %s\n", config.CacheDir) fmt.Printf("Cache dir: %s\n", config.GetCacheDir())
fmt.Printf("Temp dir: %s\n", os.TempDir()) fmt.Printf("Temp dir: %s\n", os.TempDir())
}, },
} }

View file

@ -63,11 +63,12 @@ func assertVolumeInfo(t *testing.T, v *docker.VolInfo, name, path string) {
func TestDockerPluginLogic(t *testing.T) { func TestDockerPluginLogic(t *testing.T) {
ctx := context.Background() ctx := context.Background()
oldCacheDir := config.CacheDir oldCacheDir := config.GetCacheDir()
testDir, testFs := initialise(ctx, t) testDir, testFs := initialise(ctx, t)
config.CacheDir = testDir err := config.SetCacheDir(testDir)
require.NoError(t, err)
defer func() { defer func() {
config.CacheDir = oldCacheDir _ = config.SetCacheDir(oldCacheDir)
if !t.Failed() { if !t.Failed() {
fstest.Purge(testFs) fstest.Purge(testFs)
_ = os.RemoveAll(testDir) _ = os.RemoveAll(testDir)
@ -304,11 +305,12 @@ func testMountAPI(t *testing.T, sockAddr string) {
} }
ctx := context.Background() ctx := context.Background()
oldCacheDir := config.CacheDir oldCacheDir := config.GetCacheDir()
testDir, testFs := initialise(ctx, t) testDir, testFs := initialise(ctx, t)
config.CacheDir = testDir err := config.SetCacheDir(testDir)
require.NoError(t, err)
defer func() { defer func() {
config.CacheDir = oldCacheDir _ = config.SetCacheDir(oldCacheDir)
if !t.Failed() { if !t.Failed() {
fstest.Purge(testFs) fstest.Purge(testFs)
_ = os.RemoveAll(testDir) _ = os.RemoveAll(testDir)

View file

@ -41,11 +41,8 @@ type Driver struct {
// NewDriver makes a new docker driver // NewDriver makes a new docker driver
func NewDriver(ctx context.Context, root string, mntOpt *mountlib.Options, vfsOpt *vfscommon.Options, dummy, forgetState bool) (*Driver, error) { func NewDriver(ctx context.Context, root string, mntOpt *mountlib.Options, vfsOpt *vfscommon.Options, dummy, forgetState bool) (*Driver, error) {
// setup directories // setup directories
cacheDir, err := filepath.Abs(config.CacheDir) cacheDir := config.GetCacheDir()
if err != nil { err := file.MkdirAll(cacheDir, 0700)
return nil, errors.Wrap(err, "failed to make --cache-dir absolute")
}
err = file.MkdirAll(cacheDir, 0700)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to create cache directory: %s", cacheDir) return nil, errors.Wrapf(err, "failed to create cache directory: %s", cacheDir)
} }

View file

@ -222,7 +222,7 @@ func (s *server) serve() (err error) {
// Load the private key, from the cache if not explicitly configured // Load the private key, from the cache if not explicitly configured
keyPaths := s.opt.HostKeys keyPaths := s.opt.HostKeys
cachePath := filepath.Join(config.CacheDir, "serve-sftp") cachePath := filepath.Join(config.GetCacheDir(), "serve-sftp")
if len(keyPaths) == 0 { if len(keyPaths) == 0 {
keyPaths = []string{ keyPaths = []string{
filepath.Join(cachePath, "id_rsa"), filepath.Join(cachePath, "id_rsa"),

View file

@ -102,17 +102,13 @@ type Storage interface {
// Global // Global
var ( var (
// CacheDir points to the cache directory. Users of this
// should make a subdirectory and use MkdirAll() to create it
// and any parents.
CacheDir = makeCacheDir()
// Password can be used to configure the random password generator // Password can be used to configure the random password generator
Password = random.Password Password = random.Password
) )
var ( var (
configPath string configPath string
cacheDir string
data Storage data Storage
dataLoaded bool dataLoaded bool
) )
@ -122,6 +118,7 @@ func init() {
fs.ConfigFileGet = FileGetFlag fs.ConfigFileGet = FileGetFlag
fs.ConfigFileSet = SetValueAndSave fs.ConfigFileSet = SetValueAndSave
configPath = makeConfigPath() configPath = makeConfigPath()
cacheDir = makeCacheDir() // Has fallback to tempDir, so set that first
data = newDefaultStorage() data = newDefaultStorage()
} }
@ -713,6 +710,21 @@ func makeCacheDir() (dir string) {
return filepath.Join(dir, "rclone") return filepath.Join(dir, "rclone")
} }
// GetCacheDir returns the default directory for cache
//
// The directory is neither guaranteed to exist nor have accessible permissions.
// Users of this should make a subdirectory and use MkdirAll() to create it
// and any parents.
func GetCacheDir() string {
return cacheDir
}
// SetCacheDir sets new default directory for cache
func SetCacheDir(path string) (err error) {
cacheDir, err = filepath.Abs(path)
return
}
// SetTempDir sets new default directory to use for temporary files. // SetTempDir sets new default directory to use for temporary files.
// //
// Assuming golang's os.TempDir is used to get the directory: // Assuming golang's os.TempDir is used to get the directory:

View file

@ -24,6 +24,7 @@ var (
verbose int verbose int
quiet bool quiet bool
configPath string configPath string
cacheDir string
tempDir string tempDir string
dumpHeaders bool dumpHeaders bool
dumpBodies bool dumpBodies bool
@ -48,7 +49,7 @@ func AddFlags(ci *fs.ConfigInfo, flagSet *pflag.FlagSet) {
flags.IntVarP(flagSet, &ci.Checkers, "checkers", "", ci.Checkers, "Number of checkers to run in parallel.") flags.IntVarP(flagSet, &ci.Checkers, "checkers", "", ci.Checkers, "Number of checkers to run in parallel.")
flags.IntVarP(flagSet, &ci.Transfers, "transfers", "", ci.Transfers, "Number of file transfers to run in parallel.") flags.IntVarP(flagSet, &ci.Transfers, "transfers", "", ci.Transfers, "Number of file transfers to run in parallel.")
flags.StringVarP(flagSet, &configPath, "config", "", config.GetConfigPath(), "Config file.") flags.StringVarP(flagSet, &configPath, "config", "", config.GetConfigPath(), "Config file.")
flags.StringVarP(flagSet, &config.CacheDir, "cache-dir", "", config.CacheDir, "Directory rclone will use for caching.") flags.StringVarP(flagSet, &cacheDir, "cache-dir", "", config.GetCacheDir(), "Directory rclone will use for caching.")
flags.StringVarP(flagSet, &tempDir, "temp-dir", "", os.TempDir(), "Directory rclone will use for temporary files.") flags.StringVarP(flagSet, &tempDir, "temp-dir", "", os.TempDir(), "Directory rclone will use for temporary files.")
flags.BoolVarP(flagSet, &ci.CheckSum, "checksum", "c", ci.CheckSum, "Skip based on checksum (if available) & size, not mod-time & size") flags.BoolVarP(flagSet, &ci.CheckSum, "checksum", "c", ci.CheckSum, "Skip based on checksum (if available) & size, not mod-time & size")
flags.BoolVarP(flagSet, &ci.SizeOnly, "size-only", "", ci.SizeOnly, "Skip based on size only, not mod-time or checksum") flags.BoolVarP(flagSet, &ci.SizeOnly, "size-only", "", ci.SizeOnly, "Skip based on size only, not mod-time or checksum")
@ -281,6 +282,11 @@ func SetFlags(ci *fs.ConfigInfo) {
log.Fatalf("--config: Failed to set %q as config path: %v", configPath, err) log.Fatalf("--config: Failed to set %q as config path: %v", configPath, err)
} }
// Set path to cache dir
if err := config.SetCacheDir(cacheDir); err != nil {
log.Fatalf("--cache-dir: Failed to set %q as cache dir: %v", cacheDir, err)
}
// Set path to temp dir // Set path to temp dir
if err := config.SetTempDir(tempDir); err != nil { if err := config.SetTempDir(tempDir); err != nil {
log.Fatalf("--temp-dir: Failed to set %q as temp dir: %v", tempDir, err) log.Fatalf("--temp-dir: Failed to set %q as temp dir: %v", tempDir, err)

View file

@ -76,7 +76,7 @@ func newServer(ctx context.Context, opt *rc.Options, mux *http.ServeMux) *Server
_ = mime.AddExtensionType(".wasm", "application/wasm") _ = mime.AddExtensionType(".wasm", "application/wasm")
_ = mime.AddExtensionType(".js", "application/javascript") _ = mime.AddExtensionType(".js", "application/javascript")
cachePath := filepath.Join(config.CacheDir, "webgui") cachePath := filepath.Join(config.GetCacheDir(), "webgui")
extractPath := filepath.Join(cachePath, "current/build") extractPath := filepath.Join(cachePath, "current/build")
// File handling // File handling
if opt.Files != "" { if opt.Files != "" {
@ -86,7 +86,7 @@ func newServer(ctx context.Context, opt *rc.Options, mux *http.ServeMux) *Server
fs.Logf(nil, "Serving files from %q", opt.Files) fs.Logf(nil, "Serving files from %q", opt.Files)
fileHandler = http.FileServer(http.Dir(opt.Files)) fileHandler = http.FileServer(http.Dir(opt.Files))
} else if opt.WebUI { } else if opt.WebUI {
if err := webgui.CheckAndDownloadWebGUIRelease(opt.WebGUIUpdate, opt.WebGUIForceUpdate, opt.WebGUIFetchURL, config.CacheDir); err != nil { if err := webgui.CheckAndDownloadWebGUIRelease(opt.WebGUIUpdate, opt.WebGUIForceUpdate, opt.WebGUIFetchURL, config.GetCacheDir()); err != nil {
log.Fatalf("Error while fetching the latest release of Web GUI: %v", err) log.Fatalf("Error while fetching the latest release of Web GUI: %v", err)
} }
if opt.NoAuth { if opt.NoAuth {

View file

@ -89,7 +89,7 @@ func initPluginsOrError() error {
initMutex.Lock() initMutex.Lock()
defer initMutex.Unlock() defer initMutex.Unlock()
if !initSuccess { if !initSuccess {
cachePath = filepath.Join(config.CacheDir, "webgui") cachePath = filepath.Join(config.GetCacheDir(), "webgui")
PluginsPath = filepath.Join(cachePath, "plugins") PluginsPath = filepath.Join(cachePath, "plugins")
pluginsConfigPath = filepath.Join(PluginsPath, "config") pluginsConfigPath = filepath.Join(PluginsPath, "config")
loadedPlugins = newPlugins(availablePluginsJSONPath) loadedPlugins = newPlugins(availablePluginsJSONPath)

View file

@ -82,10 +82,7 @@ func New(ctx context.Context, fremote fs.Fs, opt *vfscommon.Options, avFn AddVir
// Care must be taken when creating OS paths so that the ':' separator following a // Care must be taken when creating OS paths so that the ':' separator following a
// drive letter is not encoded (e.g. into unicode fullwidth colon). // drive letter is not encoded (e.g. into unicode fullwidth colon).
var err error var err error
parentOSPath := config.CacheDir // Assuming string contains a local path in OS encoding parentOSPath := config.GetCacheDir() // Assuming string contains a local absolute path in OS encoding
if parentOSPath, err = filepath.Abs(parentOSPath); err != nil {
return nil, errors.Wrap(err, "failed to make --cache-dir absolute")
}
fs.Debugf(nil, "vfs cache: root is %q", parentOSPath) fs.Debugf(nil, "vfs cache: root is %q", parentOSPath)
parentPath := fromOSPath(parentOSPath) parentPath := fromOSPath(parentOSPath)