1
mirror of https://github.com/rclone/rclone synced 2024-12-25 17:03:45 +01:00

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",
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) != "" {

View File

@ -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)

View File

@ -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())
},
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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"),

View File

@ -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:

View File

@ -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)

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(".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 {

View File

@ -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)

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
// 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)