config: make temporary directory user-configurable

See #5591
This commit is contained in:
albertony 2021-09-10 14:44:13 +02:00
parent 352f9bcd47
commit dbb6f94d95
4 changed files with 54 additions and 0 deletions

View File

@ -1554,6 +1554,22 @@ If using `--syslog` this sets the syslog facility (e.g. `KERN`, `USER`).
See `man syslog` for a list of possible facilities. The default See `man syslog` for a list of possible facilities. The default
facility is `DAEMON`. facility is `DAEMON`.
### --temp-dir=DIR ###
Specify the directory rclone will use for temporary files, to override
the default. Make sure the directory exists and have accessible permissions.
By default the operating system's temp directory will be used:
- On Unix systems, `$TMPDIR` if non-empty, else `/tmp`.
- On Windows, the first non-empty value from `%TMP%`, `%TEMP%`, `%USERPROFILE%`, or the Windows directory.
When overriding the default with this option, the specified path will be
set as value of environment variable `TMPDIR` on Unix systems
and `TMP` and `TEMP` on Windows.
You can use the [config paths](/commands/rclone_config_paths/)
command to see the current value.
### --tpslimit float ### ### --tpslimit float ###
Limit transactions per second to this number. Default is 0 which is Limit transactions per second to this number. Default is 0 which is

View File

@ -21,6 +21,7 @@ These flags are available for every command.
--bwlimit BwTimetable Bandwidth limit in KiB/s, or use suffix B|K|M|G|T|P or a full timetable. --bwlimit BwTimetable Bandwidth limit in KiB/s, or use suffix B|K|M|G|T|P or a full timetable.
--bwlimit-file BwTimetable Bandwidth limit per file in KiB/s, or use suffix B|K|M|G|T|P or a full timetable. --bwlimit-file BwTimetable Bandwidth limit per file in KiB/s, or use suffix B|K|M|G|T|P or a full timetable.
--ca-cert string CA certificate used to verify servers --ca-cert string CA certificate used to verify servers
--temp-dir string Directory rclone will use for temporary files. (default "$TMPDIR")
--cache-dir string Directory rclone will use for caching. (default "$HOME/.cache/rclone") --cache-dir string Directory rclone will use for caching. (default "$HOME/.cache/rclone")
--check-first Do all the checks before starting transfers. --check-first Do all the checks before starting transfers.
--checkers int Number of checkers to run in parallel. (default 8) --checkers int Number of checkers to run in parallel. (default 8)

View File

@ -712,3 +712,32 @@ func makeCacheDir() (dir string) {
} }
return filepath.Join(dir, "rclone") return filepath.Join(dir, "rclone")
} }
// SetTempDir sets new default directory to use for temporary files.
//
// Assuming golang's os.TempDir is used to get the directory:
// "On Unix systems, it returns $TMPDIR if non-empty, else /tmp. On Windows,
// it uses GetTempPath, returning the first non-empty value from %TMP%, %TEMP%,
// %USERPROFILE%, or the Windows directory."
//
// To override the default we therefore set environment variable TMPDIR
// on Unix systems, and both TMP and TEMP on Windows (they are almost exclusively
// aliases for the same path, and programs may refer to to either of them).
// This should make all libraries and forked processes use the same.
func SetTempDir(path string) (err error) {
var tempDir string
if tempDir, err = filepath.Abs(path); err != nil {
return err
}
if runtime.GOOS == "windows" {
if err = os.Setenv("TMP", tempDir); err != nil {
return err
}
if err = os.Setenv("TEMP", tempDir); err != nil {
return err
}
} else {
return os.Setenv("TMPDIR", tempDir)
}
return nil
}

View File

@ -6,6 +6,7 @@ package configflags
import ( import (
"log" "log"
"net" "net"
"os"
"strconv" "strconv"
"strings" "strings"
@ -23,6 +24,7 @@ var (
verbose int verbose int
quiet bool quiet bool
configPath string configPath string
tempDir string
dumpHeaders bool dumpHeaders bool
dumpBodies bool dumpBodies bool
deleteBefore bool deleteBefore bool
@ -47,6 +49,7 @@ func AddFlags(ci *fs.ConfigInfo, flagSet *pflag.FlagSet) {
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, &config.CacheDir, "cache-dir", "", config.CacheDir, "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.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")
flags.BoolVarP(flagSet, &ci.IgnoreTimes, "ignore-times", "I", ci.IgnoreTimes, "Don't skip files that match size and time - transfer all files") flags.BoolVarP(flagSet, &ci.IgnoreTimes, "ignore-times", "I", ci.IgnoreTimes, "Don't skip files that match size and time - transfer all files")
@ -278,6 +281,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 temp dir
if err := config.SetTempDir(tempDir); err != nil {
log.Fatalf("--temp-dir: Failed to set %q as temp dir: %v", tempDir, err)
}
// Set whether multi-thread-streams was set // Set whether multi-thread-streams was set
multiThreadStreamsFlag := pflag.Lookup("multi-thread-streams") multiThreadStreamsFlag := pflag.Lookup("multi-thread-streams")
ci.MultiThreadSet = multiThreadStreamsFlag != nil && multiThreadStreamsFlag.Changed ci.MultiThreadSet = multiThreadStreamsFlag != nil && multiThreadStreamsFlag.Changed