Skip to content

Commit 69fca4d

Browse files
Reconcile differences between btcd/dcrd.
Fixes btcsuite#793
1 parent 4494f0f commit 69fca4d

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

cmd/btcctl/config.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,13 @@ func loadConfig() (*config, []string, error) {
175175
RPCCert: defaultRPCCertFile,
176176
}
177177

178-
// Create the home directory if it doesn't already exist.
179-
err := os.MkdirAll(btcdHomeDir, 0700)
180-
if err != nil {
181-
fmt.Fprintf(os.Stderr, "%v\n", err)
182-
os.Exit(-1)
183-
}
184-
185178
// Pre-parse the command line options to see if an alternative config
186179
// file, the version flag, or the list commands flag was specified. Any
187180
// errors aside from the help message error can be ignored here since
188181
// they will be caught by the final parse below.
189182
preCfg := cfg
190183
preParser := flags.NewParser(&preCfg, flags.HelpFlag)
191-
_, err = preParser.Parse()
184+
_, err := preParser.Parse()
192185
if err != nil {
193186
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
194187
fmt.Fprintln(os.Stderr, err)
@@ -282,9 +275,6 @@ func loadConfig() (*config, []string, error) {
282275
// For this it tries to read the btcd config file at its default path, and extract
283276
// the RPC user and password from it.
284277
func createDefaultConfigFile(destinationPath string) error {
285-
// Create the destination directory if it does not exists
286-
os.MkdirAll(filepath.Dir(destinationPath), 0700)
287-
288278
// Read btcd.conf from its default path
289279
btcdConfigPath := filepath.Join(btcdHomeDir, "btcd.conf")
290280
btcdConfigFile, err := os.Open(btcdConfigPath)
@@ -319,14 +309,22 @@ func createDefaultConfigFile(destinationPath string) error {
319309
return nil
320310
}
321311

312+
// Create the destination directory if it does not exists
313+
err = os.MkdirAll(filepath.Dir(destinationPath), 0700)
314+
if err != nil {
315+
return err
316+
}
317+
322318
// Create the destination file and write the rpcuser and rpcpass to it
323-
dest, err := os.OpenFile(destinationPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
319+
dest, err := os.OpenFile(destinationPath,
320+
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
324321
if err != nil {
325322
return err
326323
}
327324
defer dest.Close()
328325

329-
dest.WriteString(fmt.Sprintf("rpcuser=%s\nrpcpass=%s", string(userSubmatches[1]), string(passSubmatches[1])))
326+
dest.WriteString(fmt.Sprintf("rpcuser=%s\nrpcpass=%s",
327+
string(userSubmatches[1]), string(passSubmatches[1])))
330328

331329
return nil
332330
}

config.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ const (
5252
defaultMaxOrphanTransactions = 1000
5353
defaultMaxOrphanTxSize = 5000
5454
defaultSigCacheMaxSize = 100000
55+
sampleConfigFilename = "sample-btcd.conf"
5556
defaultTxIndex = false
5657
defaultAddrIndex = false
5758
)
5859

5960
var (
60-
btcdHomeDir = btcutil.AppDataDir("btcd", false)
61-
defaultConfigFile = filepath.Join(btcdHomeDir, defaultConfigFilename)
62-
defaultDataDir = filepath.Join(btcdHomeDir, defaultDataDirname)
61+
defaultHomeDir = btcutil.AppDataDir("btcd", false)
62+
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename)
63+
defaultDataDir = filepath.Join(defaultHomeDir, defaultDataDirname)
6364
knownDbTypes = database.SupportedDrivers()
64-
defaultRPCKeyFile = filepath.Join(btcdHomeDir, "rpc.key")
65-
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
66-
defaultLogDir = filepath.Join(btcdHomeDir, defaultLogDirname)
65+
defaultRPCKeyFile = filepath.Join(defaultHomeDir, "rpc.key")
66+
defaultRPCCertFile = filepath.Join(defaultHomeDir, "rpc.cert")
67+
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname)
6768
)
6869

6970
// runServiceCommand is only set to a real function on Windows. It is used
@@ -153,7 +154,7 @@ type config struct {
153154
minRelayTxFee btcutil.Amount
154155
}
155156

156-
// serviceOptions defines the configuration options for btcd as a service on
157+
// serviceOptions defines the configuration options for the daemon as a service on
157158
// Windows.
158159
type serviceOptions struct {
159160
ServiceCommand string `short:"s" long:"service" description:"Service command {install, remove, start, stop}"`
@@ -164,7 +165,7 @@ type serviceOptions struct {
164165
func cleanAndExpandPath(path string) string {
165166
// Expand initial ~ to OS specific home directory.
166167
if strings.HasPrefix(path, "~") {
167-
homeDir := filepath.Dir(btcdHomeDir)
168+
homeDir := filepath.Dir(defaultHomeDir)
168169
path = strings.Replace(path, "~", homeDir, 1)
169170
}
170171

@@ -440,7 +441,7 @@ func loadConfig() (*config, []string, error) {
440441

441442
// Create the home directory if it doesn't already exist.
442443
funcName := "loadConfig"
443-
err = os.MkdirAll(btcdHomeDir, 0700)
444+
err = os.MkdirAll(defaultHomeDir, 0700)
444445
if err != nil {
445446
// Show a nicer error message if it's because a symlink is
446447
// linked to a directory that does not exist (probably because
@@ -931,15 +932,21 @@ func loadConfig() (*config, []string, error) {
931932
// and populates it with some randomly generated RPC username and password.
932933
func createDefaultConfigFile(destinationPath string) error {
933934
// Create the destination directory if it does not exists
934-
os.MkdirAll(filepath.Dir(destinationPath), 0700)
935+
err := os.MkdirAll(filepath.Dir(destinationPath), 0700)
936+
if err != nil {
937+
return err
938+
}
935939

936-
// We get the sample config file path, which is in the same directory as this file.
937-
_, path, _, _ := runtime.Caller(0)
938-
sampleConfigPath := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
940+
// We assume sample config file path is same as binary
941+
path, err := filepath.Abs(filepath.Dir(os.Args[0]))
942+
if err != nil {
943+
return err
944+
}
945+
sampleConfigPath := filepath.Join(path, sampleConfigFilename)
939946

940947
// We generate a random user and password
941948
randomBytes := make([]byte, 20)
942-
_, err := rand.Read(randomBytes)
949+
_, err = rand.Read(randomBytes)
943950
if err != nil {
944951
return err
945952
}
@@ -957,7 +964,8 @@ func createDefaultConfigFile(destinationPath string) error {
957964
}
958965
defer src.Close()
959966

960-
dest, err := os.OpenFile(destinationPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
967+
dest, err := os.OpenFile(destinationPath,
968+
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
961969
if err != nil {
962970
return err
963971
}

config_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77
"regexp"
8+
"runtime"
89
"testing"
910
)
1011

@@ -14,15 +15,39 @@ var (
1415
)
1516

1617
func TestCreateDefaultConfigFile(t *testing.T) {
18+
// find out where the sample config lives
19+
_, path, _, ok := runtime.Caller(0)
20+
if !ok {
21+
t.Fatalf("Failed finding config file path")
22+
}
23+
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
24+
1725
// Setup a temporary directory
1826
tmpDir, err := ioutil.TempDir("", "btcd")
1927
if err != nil {
2028
t.Fatalf("Failed creating a temporary directory: %v", err)
2129
}
2230
testpath := filepath.Join(tmpDir, "test.conf")
31+
32+
// copy config file to location of btcd binary
33+
data, err := ioutil.ReadFile(sampleConfigFile)
34+
if err != nil {
35+
t.Fatalf("Failed reading sample config file: %v", err)
36+
}
37+
appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
38+
if err != nil {
39+
t.Fatalf("Failed obtaining app path: %v", err)
40+
}
41+
tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf")
42+
err = ioutil.WriteFile(tmpConfigFile, data, 0644)
43+
if err != nil {
44+
t.Fatalf("Failed copying sample config file: %v", err)
45+
}
46+
2347
// Clean-up
2448
defer func() {
2549
os.Remove(testpath)
50+
os.Remove(tmpConfigFile)
2651
os.Remove(tmpDir)
2752
}()
2853

service_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var elog *eventlog.Log
3737
func logServiceStartOfDay(srvr *server) {
3838
var message string
3939
message += fmt.Sprintf("Version %s\n", version())
40-
message += fmt.Sprintf("Configuration directory: %s\n", btcdHomeDir)
40+
message += fmt.Sprintf("Configuration directory: %s\n", defaultHomeDir)
4141
message += fmt.Sprintf("Configuration file: %s\n", cfg.ConfigFile)
4242
message += fmt.Sprintf("Data directory: %s\n", cfg.DataDir)
4343

upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func upgradeDBPaths() error {
111111
func upgradeDataPaths() error {
112112
// No need to migrate if the old and new home paths are the same.
113113
oldHomePath := oldBtcdHomeDir()
114-
newHomePath := btcdHomeDir
114+
newHomePath := defaultHomeDir
115115
if oldHomePath == newHomePath {
116116
return nil
117117
}

0 commit comments

Comments
 (0)