mirror of
https://github.com/btcsuite/btcd.git
synced 2025-03-10 09:19:28 +01:00
parent
4494f0f852
commit
69fca4d9b1
5 changed files with 61 additions and 30 deletions
|
@ -175,20 +175,13 @@ func loadConfig() (*config, []string, error) {
|
||||||
RPCCert: defaultRPCCertFile,
|
RPCCert: defaultRPCCertFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the home directory if it doesn't already exist.
|
|
||||||
err := os.MkdirAll(btcdHomeDir, 0700)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pre-parse the command line options to see if an alternative config
|
// Pre-parse the command line options to see if an alternative config
|
||||||
// file, the version flag, or the list commands flag was specified. Any
|
// file, the version flag, or the list commands flag was specified. Any
|
||||||
// errors aside from the help message error can be ignored here since
|
// errors aside from the help message error can be ignored here since
|
||||||
// they will be caught by the final parse below.
|
// they will be caught by the final parse below.
|
||||||
preCfg := cfg
|
preCfg := cfg
|
||||||
preParser := flags.NewParser(&preCfg, flags.HelpFlag)
|
preParser := flags.NewParser(&preCfg, flags.HelpFlag)
|
||||||
_, err = preParser.Parse()
|
_, err := preParser.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
@ -282,9 +275,6 @@ func loadConfig() (*config, []string, error) {
|
||||||
// For this it tries to read the btcd config file at its default path, and extract
|
// For this it tries to read the btcd config file at its default path, and extract
|
||||||
// the RPC user and password from it.
|
// the RPC user and password from it.
|
||||||
func createDefaultConfigFile(destinationPath string) error {
|
func createDefaultConfigFile(destinationPath string) error {
|
||||||
// Create the destination directory if it does not exists
|
|
||||||
os.MkdirAll(filepath.Dir(destinationPath), 0700)
|
|
||||||
|
|
||||||
// Read btcd.conf from its default path
|
// Read btcd.conf from its default path
|
||||||
btcdConfigPath := filepath.Join(btcdHomeDir, "btcd.conf")
|
btcdConfigPath := filepath.Join(btcdHomeDir, "btcd.conf")
|
||||||
btcdConfigFile, err := os.Open(btcdConfigPath)
|
btcdConfigFile, err := os.Open(btcdConfigPath)
|
||||||
|
@ -319,14 +309,22 @@ func createDefaultConfigFile(destinationPath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create the destination directory if it does not exists
|
||||||
|
err = os.MkdirAll(filepath.Dir(destinationPath), 0700)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Create the destination file and write the rpcuser and rpcpass to it
|
// Create the destination file and write the rpcuser and rpcpass to it
|
||||||
dest, err := os.OpenFile(destinationPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
|
dest, err := os.OpenFile(destinationPath,
|
||||||
|
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer dest.Close()
|
defer dest.Close()
|
||||||
|
|
||||||
dest.WriteString(fmt.Sprintf("rpcuser=%s\nrpcpass=%s", string(userSubmatches[1]), string(passSubmatches[1])))
|
dest.WriteString(fmt.Sprintf("rpcuser=%s\nrpcpass=%s",
|
||||||
|
string(userSubmatches[1]), string(passSubmatches[1])))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
38
config.go
38
config.go
|
@ -52,18 +52,19 @@ const (
|
||||||
defaultMaxOrphanTransactions = 1000
|
defaultMaxOrphanTransactions = 1000
|
||||||
defaultMaxOrphanTxSize = 5000
|
defaultMaxOrphanTxSize = 5000
|
||||||
defaultSigCacheMaxSize = 100000
|
defaultSigCacheMaxSize = 100000
|
||||||
|
sampleConfigFilename = "sample-btcd.conf"
|
||||||
defaultTxIndex = false
|
defaultTxIndex = false
|
||||||
defaultAddrIndex = false
|
defaultAddrIndex = false
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
btcdHomeDir = btcutil.AppDataDir("btcd", false)
|
defaultHomeDir = btcutil.AppDataDir("btcd", false)
|
||||||
defaultConfigFile = filepath.Join(btcdHomeDir, defaultConfigFilename)
|
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename)
|
||||||
defaultDataDir = filepath.Join(btcdHomeDir, defaultDataDirname)
|
defaultDataDir = filepath.Join(defaultHomeDir, defaultDataDirname)
|
||||||
knownDbTypes = database.SupportedDrivers()
|
knownDbTypes = database.SupportedDrivers()
|
||||||
defaultRPCKeyFile = filepath.Join(btcdHomeDir, "rpc.key")
|
defaultRPCKeyFile = filepath.Join(defaultHomeDir, "rpc.key")
|
||||||
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
|
defaultRPCCertFile = filepath.Join(defaultHomeDir, "rpc.cert")
|
||||||
defaultLogDir = filepath.Join(btcdHomeDir, defaultLogDirname)
|
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname)
|
||||||
)
|
)
|
||||||
|
|
||||||
// runServiceCommand is only set to a real function on Windows. It is used
|
// runServiceCommand is only set to a real function on Windows. It is used
|
||||||
|
@ -153,7 +154,7 @@ type config struct {
|
||||||
minRelayTxFee btcutil.Amount
|
minRelayTxFee btcutil.Amount
|
||||||
}
|
}
|
||||||
|
|
||||||
// serviceOptions defines the configuration options for btcd as a service on
|
// serviceOptions defines the configuration options for the daemon as a service on
|
||||||
// Windows.
|
// Windows.
|
||||||
type serviceOptions struct {
|
type serviceOptions struct {
|
||||||
ServiceCommand string `short:"s" long:"service" description:"Service command {install, remove, start, stop}"`
|
ServiceCommand string `short:"s" long:"service" description:"Service command {install, remove, start, stop}"`
|
||||||
|
@ -164,7 +165,7 @@ type serviceOptions struct {
|
||||||
func cleanAndExpandPath(path string) string {
|
func cleanAndExpandPath(path string) string {
|
||||||
// Expand initial ~ to OS specific home directory.
|
// Expand initial ~ to OS specific home directory.
|
||||||
if strings.HasPrefix(path, "~") {
|
if strings.HasPrefix(path, "~") {
|
||||||
homeDir := filepath.Dir(btcdHomeDir)
|
homeDir := filepath.Dir(defaultHomeDir)
|
||||||
path = strings.Replace(path, "~", homeDir, 1)
|
path = strings.Replace(path, "~", homeDir, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -440,7 +441,7 @@ func loadConfig() (*config, []string, error) {
|
||||||
|
|
||||||
// Create the home directory if it doesn't already exist.
|
// Create the home directory if it doesn't already exist.
|
||||||
funcName := "loadConfig"
|
funcName := "loadConfig"
|
||||||
err = os.MkdirAll(btcdHomeDir, 0700)
|
err = os.MkdirAll(defaultHomeDir, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Show a nicer error message if it's because a symlink is
|
// Show a nicer error message if it's because a symlink is
|
||||||
// linked to a directory that does not exist (probably because
|
// linked to a directory that does not exist (probably because
|
||||||
|
@ -931,15 +932,21 @@ func loadConfig() (*config, []string, error) {
|
||||||
// and populates it with some randomly generated RPC username and password.
|
// and populates it with some randomly generated RPC username and password.
|
||||||
func createDefaultConfigFile(destinationPath string) error {
|
func createDefaultConfigFile(destinationPath string) error {
|
||||||
// Create the destination directory if it does not exists
|
// Create the destination directory if it does not exists
|
||||||
os.MkdirAll(filepath.Dir(destinationPath), 0700)
|
err := os.MkdirAll(filepath.Dir(destinationPath), 0700)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// We get the sample config file path, which is in the same directory as this file.
|
// We assume sample config file path is same as binary
|
||||||
_, path, _, _ := runtime.Caller(0)
|
path, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
sampleConfigPath := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sampleConfigPath := filepath.Join(path, sampleConfigFilename)
|
||||||
|
|
||||||
// We generate a random user and password
|
// We generate a random user and password
|
||||||
randomBytes := make([]byte, 20)
|
randomBytes := make([]byte, 20)
|
||||||
_, err := rand.Read(randomBytes)
|
_, err = rand.Read(randomBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -957,7 +964,8 @@ func createDefaultConfigFile(destinationPath string) error {
|
||||||
}
|
}
|
||||||
defer src.Close()
|
defer src.Close()
|
||||||
|
|
||||||
dest, err := os.OpenFile(destinationPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
|
dest, err := os.OpenFile(destinationPath,
|
||||||
|
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,15 +15,39 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateDefaultConfigFile(t *testing.T) {
|
func TestCreateDefaultConfigFile(t *testing.T) {
|
||||||
|
// find out where the sample config lives
|
||||||
|
_, path, _, ok := runtime.Caller(0)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Failed finding config file path")
|
||||||
|
}
|
||||||
|
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
|
||||||
|
|
||||||
// Setup a temporary directory
|
// Setup a temporary directory
|
||||||
tmpDir, err := ioutil.TempDir("", "btcd")
|
tmpDir, err := ioutil.TempDir("", "btcd")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed creating a temporary directory: %v", err)
|
t.Fatalf("Failed creating a temporary directory: %v", err)
|
||||||
}
|
}
|
||||||
testpath := filepath.Join(tmpDir, "test.conf")
|
testpath := filepath.Join(tmpDir, "test.conf")
|
||||||
|
|
||||||
|
// copy config file to location of btcd binary
|
||||||
|
data, err := ioutil.ReadFile(sampleConfigFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed reading sample config file: %v", err)
|
||||||
|
}
|
||||||
|
appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed obtaining app path: %v", err)
|
||||||
|
}
|
||||||
|
tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf")
|
||||||
|
err = ioutil.WriteFile(tmpConfigFile, data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed copying sample config file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Clean-up
|
// Clean-up
|
||||||
defer func() {
|
defer func() {
|
||||||
os.Remove(testpath)
|
os.Remove(testpath)
|
||||||
|
os.Remove(tmpConfigFile)
|
||||||
os.Remove(tmpDir)
|
os.Remove(tmpDir)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ var elog *eventlog.Log
|
||||||
func logServiceStartOfDay(srvr *server) {
|
func logServiceStartOfDay(srvr *server) {
|
||||||
var message string
|
var message string
|
||||||
message += fmt.Sprintf("Version %s\n", version())
|
message += fmt.Sprintf("Version %s\n", version())
|
||||||
message += fmt.Sprintf("Configuration directory: %s\n", btcdHomeDir)
|
message += fmt.Sprintf("Configuration directory: %s\n", defaultHomeDir)
|
||||||
message += fmt.Sprintf("Configuration file: %s\n", cfg.ConfigFile)
|
message += fmt.Sprintf("Configuration file: %s\n", cfg.ConfigFile)
|
||||||
message += fmt.Sprintf("Data directory: %s\n", cfg.DataDir)
|
message += fmt.Sprintf("Data directory: %s\n", cfg.DataDir)
|
||||||
|
|
||||||
|
|
|
@ -111,7 +111,7 @@ func upgradeDBPaths() error {
|
||||||
func upgradeDataPaths() error {
|
func upgradeDataPaths() error {
|
||||||
// No need to migrate if the old and new home paths are the same.
|
// No need to migrate if the old and new home paths are the same.
|
||||||
oldHomePath := oldBtcdHomeDir()
|
oldHomePath := oldBtcdHomeDir()
|
||||||
newHomePath := btcdHomeDir
|
newHomePath := defaultHomeDir
|
||||||
if oldHomePath == newHomePath {
|
if oldHomePath == newHomePath {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue