diff --git a/config.go b/config.go index 9dae6d1b..125d6584 100644 --- a/config.go +++ b/config.go @@ -85,6 +85,20 @@ func btcdHomeDir() string { return "." } +// cleanAndExpandPath expands environement variables and leading ~ in the +// passed path, cleans the result, and returns it. +func cleanAndExpandPath(path string) string { + // Expand initial ~ to OS specific home directory. + if strings.HasPrefix(path, "~") { + homeDir := filepath.Dir(btcdHomeDir()) + path = strings.Replace(path, "~", homeDir, 1) + } + + // NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%, + // but they variables can still be expanded via POSIX-style $VARIABLE. + return filepath.Clean(os.ExpandEnv(path)) +} + // validLogLevel returns whether or not logLevel is a valid debug log level. func validLogLevel(logLevel string) bool { switch logLevel { @@ -302,6 +316,7 @@ func loadConfig() (*config, []string, error) { // All data is specific to a network, so namespacing the data directory // means each individual piece of serialized data does not have to // worry about changing names per network and such. + cfg.DataDir = cleanAndExpandPath(cfg.DataDir) cfg.DataDir = filepath.Join(cfg.DataDir, activeNetParams.netName) // Don't allow ban durations that are too short. diff --git a/sample-btcd.conf b/sample-btcd.conf index f9e9896c..02e370b5 100644 --- a/sample-btcd.conf +++ b/sample-btcd.conf @@ -1,5 +1,18 @@ [Application Options] +; ------------------------------------------------------------------------------ +; Data settings +; ------------------------------------------------------------------------------ + +; The directory to store data such as the block chain and peer addresses. The +; block chain takes several GB, so this location must have a lot of free space. +; The default is ~/.btcd/data on POSIX OSes and $APPDATA/btcd/data on Windows. +; Environment variables are expanded so they may be used. NOTE: Windows +; environment variables are typically %VARIABLE%, but they must be accessed with +; $VARIABLE here. Also, ~ is expanded to $APPDATA on Windows. +; datadir=~/.btcd/data + + ; ------------------------------------------------------------------------------ ; Network settings ; ------------------------------------------------------------------------------