From f3e71f129c713c96de6b1b4d56a74657ea4445a3 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:35:53 +0200 Subject: [PATCH] config: convert --cache-dir value to an absolute path --- backend/cache/cache.go | 8 ++++---- backend/cache/cache_internal_test.go | 6 +++--- cmd/config/config.go | 2 +- cmd/serve/docker/docker_test.go | 14 ++++++++------ cmd/serve/docker/driver.go | 7 ++----- cmd/serve/sftp/server.go | 2 +- fs/config/config.go | 22 +++++++++++++++++----- fs/config/configflags/configflags.go | 8 +++++++- fs/rc/rcserver/rcserver.go | 4 ++-- fs/rc/webgui/plugins.go | 2 +- vfs/vfscache/cache.go | 5 +---- 11 files changed, 47 insertions(+), 33 deletions(-) diff --git a/backend/cache/cache.go b/backend/cache/cache.go index a20193db5..161df7118 100644 --- a/backend/cache/cache.go +++ b/backend/cache/cache.go @@ -143,12 +143,12 @@ oldest chunks until it goes under this value.`, }}, }, { 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.", Advanced: true, }, { Name: "chunk_path", - Default: filepath.Join(config.CacheDir, "cache-backend"), + Default: filepath.Join(config.GetCacheDir(), "cache-backend"), Help: `Directory to cache chunk files. 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 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 dbPath != filepath.Join(config.CacheDir, "cache-backend") && - chunkPath == filepath.Join(config.CacheDir, "cache-backend") { + if dbPath != filepath.Join(config.GetCacheDir(), "cache-backend") && + chunkPath == filepath.Join(config.GetCacheDir(), "cache-backend") { chunkPath = dbPath } if filepath.Ext(dbPath) != "" { diff --git a/backend/cache/cache_internal_test.go b/backend/cache/cache_internal_test.go index 87300ec05..281d92af1 100644 --- a/backend/cache/cache_internal_test.go +++ b/backend/cache/cache_internal_test.go @@ -926,9 +926,9 @@ func (r *run) newCacheFs(t *testing.T, remote, id string, needRemote, purge bool } } runInstance.rootIsCrypt = rootIsCrypt - runInstance.dbPath = filepath.Join(config.CacheDir, "cache-backend", cacheRemote+".db") - runInstance.chunkPath = filepath.Join(config.CacheDir, "cache-backend", cacheRemote) - runInstance.vfsCachePath = filepath.Join(config.CacheDir, "vfs", remote) + runInstance.dbPath = filepath.Join(config.GetCacheDir(), "cache-backend", cacheRemote+".db") + runInstance.chunkPath = filepath.Join(config.GetCacheDir(), "cache-backend", cacheRemote) + runInstance.vfsCachePath = filepath.Join(config.GetCacheDir(), "vfs", remote) boltDb, err := cache.GetPersistent(runInstance.dbPath, runInstance.chunkPath, &cache.Features{PurgeDb: true}) require.NoError(t, err) diff --git a/cmd/config/config.go b/cmd/config/config.go index 555ac7063..5a28f3aae 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -80,7 +80,7 @@ var configPathsCommand = &cobra.Command{ Run: func(command *cobra.Command, args []string) { cmd.CheckArgs(0, 0, command, args) 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()) }, } diff --git a/cmd/serve/docker/docker_test.go b/cmd/serve/docker/docker_test.go index 169fd6e70..2a2a1ba59 100644 --- a/cmd/serve/docker/docker_test.go +++ b/cmd/serve/docker/docker_test.go @@ -63,11 +63,12 @@ func assertVolumeInfo(t *testing.T, v *docker.VolInfo, name, path string) { func TestDockerPluginLogic(t *testing.T) { ctx := context.Background() - oldCacheDir := config.CacheDir + oldCacheDir := config.GetCacheDir() testDir, testFs := initialise(ctx, t) - config.CacheDir = testDir + err := config.SetCacheDir(testDir) + require.NoError(t, err) defer func() { - config.CacheDir = oldCacheDir + _ = config.SetCacheDir(oldCacheDir) if !t.Failed() { fstest.Purge(testFs) _ = os.RemoveAll(testDir) @@ -304,11 +305,12 @@ func testMountAPI(t *testing.T, sockAddr string) { } ctx := context.Background() - oldCacheDir := config.CacheDir + oldCacheDir := config.GetCacheDir() testDir, testFs := initialise(ctx, t) - config.CacheDir = testDir + err := config.SetCacheDir(testDir) + require.NoError(t, err) defer func() { - config.CacheDir = oldCacheDir + _ = config.SetCacheDir(oldCacheDir) if !t.Failed() { fstest.Purge(testFs) _ = os.RemoveAll(testDir) diff --git a/cmd/serve/docker/driver.go b/cmd/serve/docker/driver.go index 14970958f..eb8207f52 100644 --- a/cmd/serve/docker/driver.go +++ b/cmd/serve/docker/driver.go @@ -41,11 +41,8 @@ type Driver struct { // NewDriver makes a new docker driver func NewDriver(ctx context.Context, root string, mntOpt *mountlib.Options, vfsOpt *vfscommon.Options, dummy, forgetState bool) (*Driver, error) { // setup directories - cacheDir, err := filepath.Abs(config.CacheDir) - if err != nil { - return nil, errors.Wrap(err, "failed to make --cache-dir absolute") - } - err = file.MkdirAll(cacheDir, 0700) + cacheDir := config.GetCacheDir() + err := file.MkdirAll(cacheDir, 0700) if err != nil { return nil, errors.Wrapf(err, "failed to create cache directory: %s", cacheDir) } diff --git a/cmd/serve/sftp/server.go b/cmd/serve/sftp/server.go index 379f48eb5..79446607d 100644 --- a/cmd/serve/sftp/server.go +++ b/cmd/serve/sftp/server.go @@ -222,7 +222,7 @@ func (s *server) serve() (err error) { // Load the private key, from the cache if not explicitly configured keyPaths := s.opt.HostKeys - cachePath := filepath.Join(config.CacheDir, "serve-sftp") + cachePath := filepath.Join(config.GetCacheDir(), "serve-sftp") if len(keyPaths) == 0 { keyPaths = []string{ filepath.Join(cachePath, "id_rsa"), diff --git a/fs/config/config.go b/fs/config/config.go index 0e72922d4..5ce802224 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -102,17 +102,13 @@ type Storage interface { // Global 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 = random.Password ) var ( configPath string + cacheDir string data Storage dataLoaded bool ) @@ -122,6 +118,7 @@ func init() { fs.ConfigFileGet = FileGetFlag fs.ConfigFileSet = SetValueAndSave configPath = makeConfigPath() + cacheDir = makeCacheDir() // Has fallback to tempDir, so set that first data = newDefaultStorage() } @@ -713,6 +710,21 @@ func makeCacheDir() (dir string) { 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. // // Assuming golang's os.TempDir is used to get the directory: diff --git a/fs/config/configflags/configflags.go b/fs/config/configflags/configflags.go index fbe5a93b1..bb4afbaa6 100644 --- a/fs/config/configflags/configflags.go +++ b/fs/config/configflags/configflags.go @@ -24,6 +24,7 @@ var ( verbose int quiet bool configPath string + cacheDir string tempDir string dumpHeaders 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.Transfers, "transfers", "", ci.Transfers, "Number of file transfers to run in parallel.") 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.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") @@ -281,6 +282,11 @@ func SetFlags(ci *fs.ConfigInfo) { 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 if err := config.SetTempDir(tempDir); err != nil { log.Fatalf("--temp-dir: Failed to set %q as temp dir: %v", tempDir, err) diff --git a/fs/rc/rcserver/rcserver.go b/fs/rc/rcserver/rcserver.go index cf2a6c126..39498e388 100644 --- a/fs/rc/rcserver/rcserver.go +++ b/fs/rc/rcserver/rcserver.go @@ -76,7 +76,7 @@ func newServer(ctx context.Context, opt *rc.Options, mux *http.ServeMux) *Server _ = mime.AddExtensionType(".wasm", "application/wasm") _ = mime.AddExtensionType(".js", "application/javascript") - cachePath := filepath.Join(config.CacheDir, "webgui") + cachePath := filepath.Join(config.GetCacheDir(), "webgui") extractPath := filepath.Join(cachePath, "current/build") // File handling 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) fileHandler = http.FileServer(http.Dir(opt.Files)) } 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) } if opt.NoAuth { diff --git a/fs/rc/webgui/plugins.go b/fs/rc/webgui/plugins.go index 67205f033..7bc22be54 100644 --- a/fs/rc/webgui/plugins.go +++ b/fs/rc/webgui/plugins.go @@ -89,7 +89,7 @@ func initPluginsOrError() error { initMutex.Lock() defer initMutex.Unlock() if !initSuccess { - cachePath = filepath.Join(config.CacheDir, "webgui") + cachePath = filepath.Join(config.GetCacheDir(), "webgui") PluginsPath = filepath.Join(cachePath, "plugins") pluginsConfigPath = filepath.Join(PluginsPath, "config") loadedPlugins = newPlugins(availablePluginsJSONPath) diff --git a/vfs/vfscache/cache.go b/vfs/vfscache/cache.go index c12d5c409..483bdf39c 100644 --- a/vfs/vfscache/cache.go +++ b/vfs/vfscache/cache.go @@ -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 // drive letter is not encoded (e.g. into unicode fullwidth colon). var err error - parentOSPath := config.CacheDir // Assuming string contains a local path in OS encoding - if parentOSPath, err = filepath.Abs(parentOSPath); err != nil { - return nil, errors.Wrap(err, "failed to make --cache-dir absolute") - } + parentOSPath := config.GetCacheDir() // Assuming string contains a local absolute path in OS encoding fs.Debugf(nil, "vfs cache: root is %q", parentOSPath) parentPath := fromOSPath(parentOSPath)