Skip to content

Commit

Permalink
config: convert --cache-dir value to an absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
albertony committed Oct 11, 2021
1 parent 0ffdca4 commit f3e71f1
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 33 deletions.
8 changes: 4 additions & 4 deletions backend/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) != "" {
Expand Down
6 changes: 3 additions & 3 deletions backend/cache/cache_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}
Expand Down
14 changes: 8 additions & 6 deletions cmd/serve/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions cmd/serve/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve/sftp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
22 changes: 17 additions & 5 deletions fs/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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()
}

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion fs/config/configflags/configflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
verbose int
quiet bool
configPath string
cacheDir string
tempDir string
dumpHeaders bool
dumpBodies bool
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions fs/rc/rcserver/rcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion fs/rc/webgui/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions vfs/vfscache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit f3e71f1

Please sign in to comment.