cmd: validate repo/api file and print nicer error messag
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Nov 4, 2016 at 12:24 UTC
1e3b4aef665923bb93eb54fae6b28a34d17b8685
6 files changed
+65
-26
cmd/ipfs/daemon.go
+1
-1
@@ -471,7 +471,7 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) {
471
return fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err), nil
472
}
473
474
- if err := node.Repo.SetAPIAddr(apiMaddr.String()); err != nil {
474
+ if err := node.Repo.SetAPIAddr(apiMaddr); err != nil {
475
return fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err), nil
476
}
477
cmd/ipfs/main.go
+43
-15
@@ -2,6 +2,7 @@
2
package main
3
4
import (
5
+ "context"
6
"errors"
7
"fmt"
8
"io"
@@ -16,13 +17,6 @@ import (
17
"syscall"
18
"time"
19
19
- manet "gx/ipfs/QmT6Cp31887FpAc25z25YHgpFJohZedrYLWPPspRtj1Brp/go-multiaddr-net"
20
- ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
21
-
22
- context "context"
23
- logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
24
- u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
25
-
20
cmds "github.com/ipfs/go-ipfs/commands"
21
cmdsCli "github.com/ipfs/go-ipfs/commands/cli"
22
cmdsHttp "github.com/ipfs/go-ipfs/commands/http"
@@ -31,7 +25,13 @@ import (
25
repo "github.com/ipfs/go-ipfs/repo"
26
config "github.com/ipfs/go-ipfs/repo/config"
27
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
28
+
29
+ logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
30
+ manet "gx/ipfs/QmT6Cp31887FpAc25z25YHgpFJohZedrYLWPPspRtj1Brp/go-multiaddr-net"
31
loggables "gx/ipfs/QmTMy4hVSY28DdwJ9kBz6y7q6MuioFzPcpM3Ma3aPjo1i3/go-libp2p-loggables"
32
+ ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
33
+ osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
34
+ u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
35
)
36
37
// log is the command logger
@@ -590,23 +590,51 @@ func profileIfEnabled() (func(), error) {
590
return func() {}, nil
591
}
592
593
+var apiFileErrorFmt string = `Failed to parse '%[1]s/api' file.
594
+ error: %[2]s
595
+If you're sure go-ipfs isn't running, you can just delete it.
596
+Otherwise check:
597
+`
598
+var checkIPFSUnixFmt = "\tps aux | grep ipfs"
599
+var checkIPFSWinFmt = "\ttasklist | findstr ipfs"
600
+
601
// getApiClient checks the repo, and the given options, checking for
602
// a running API service. if there is one, it returns a client.
603
// otherwise, it returns errApiNotRunning, or another error.
604
func getApiClient(repoPath, apiAddrStr string) (cmdsHttp.Client, error) {
605
+ var apiErrorFmt string
606
+ switch {
607
+ case osh.IsUnix():
608
+ apiErrorFmt = apiFileErrorFmt + checkIPFSUnixFmt
609
+ case osh.IsWindows():
610
+ apiErrorFmt = apiFileErrorFmt + checkIPFSWinFmt
611
+ default:
612
+ apiErrorFmt = apiFileErrorFmt
613
+ }
614
598
- if apiAddrStr == "" {
599
- var err error
600
- if apiAddrStr, err = fsrepo.APIAddr(repoPath); err != nil {
615
+ var addr ma.Multiaddr
616
+ var err error
617
+ if len(apiAddrStr) != 0 {
618
+ addr, err = ma.NewMultiaddr(apiAddrStr)
619
+ if err != nil {
620
+ return nil, err
621
+ }
622
+ if len(addr.Protocols()) == 0 {
623
+ return nil, fmt.Errorf("mulitaddr doesn't provide any protocols")
624
+ }
625
+ } else {
626
+ addr, err = fsrepo.APIAddr(repoPath)
627
+ if err == repo.ErrApiNotRunning {
628
return nil, err
629
}
603
- }
630
605
- addr, err := ma.NewMultiaddr(apiAddrStr)
606
- if err != nil {
607
- return nil, err
631
+ if err != nil {
632
+ return nil, fmt.Errorf(apiErrorFmt, repoPath, err.Error())
633
+ }
634
+ }
635
+ if len(addr.Protocols()) == 0 {
636
+ return nil, fmt.Errorf(apiErrorFmt, repoPath, "multiaddr doesn't provide any protocols")
637
}
609
-
638
return apiClientForAddr(addr)
639
}
640
package.json
+6
-1
@@ -287,6 +287,12 @@
287
"hash": "QmQfeKxQtBN721pekQh6Jq24adFUjnU65YdY3GNczfuG2T",
288
"name": "dir-index-html",
289
"version": "1.0.3"
290
+ },
291
+ {
292
+ "author": "Kubuxu",
293
+ "hash": "QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93",
294
+ "name": "go-os-helper",
295
+ "version": "0.0.0"
296
}
297
],
298
"gxVersion": "0.4.0",
@@ -295,4 +301,3 @@
301
"name": "go-ipfs",
302
"version": "0.4.5-dev"
303
}
298
-
repo/fsrepo/fsrepo.go
+9
-7
@@ -18,7 +18,9 @@ import (
18
mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
19
serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
20
dir "github.com/ipfs/go-ipfs/thirdparty/dir"
21
+
22
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
23
+ ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
24
util "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
25
"gx/ipfs/QmeqtHtxGfcsfXiou7wqHJARWPKUTUcPdtSfSYYHp48dtQ/go-ds-measure"
26
)
@@ -274,7 +276,7 @@ func LockedByOtherProcess(repoPath string) (bool, error) {
276
// in the fsrepo. This is a concurrent operation, meaning that any
277
// process may read this file. modifying this file, therefore, should
278
// use "mv" to replace the whole file and avoid interleaved read/writes.
277
-func APIAddr(repoPath string) (string, error) {
279
+func APIAddr(repoPath string) (ma.Multiaddr, error) {
280
repoPath = filepath.Clean(repoPath)
281
apiFilePath := filepath.Join(repoPath, apiFile)
282
@@ -282,9 +284,9 @@ func APIAddr(repoPath string) (string, error) {
284
f, err := os.Open(apiFilePath)
285
if err != nil {
286
if os.IsNotExist(err) {
285
- return "", repo.ErrApiNotRunning
287
+ return nil, repo.ErrApiNotRunning
288
}
287
- return "", err
289
+ return nil, err
290
}
291
defer f.Close()
292
@@ -293,23 +295,23 @@ func APIAddr(repoPath string) (string, error) {
295
buf := make([]byte, 2048)
296
n, err := f.Read(buf)
297
if err != nil && err != io.EOF {
296
- return "", err
298
+ return nil, err
299
}
300
301
s := string(buf[:n])
302
s = strings.TrimSpace(s)
301
- return s, nil
303
+ return ma.NewMultiaddr(s)
304
}
305
306
// SetAPIAddr writes the API Addr to the /api file.
305
-func (r *FSRepo) SetAPIAddr(addr string) error {
307
+func (r *FSRepo) SetAPIAddr(addr ma.Multiaddr) error {
308
f, err := os.Create(filepath.Join(r.path, apiFile))
309
if err != nil {
310
return err
311
}
312
defer f.Close()
313
312
- _, err = f.WriteString(addr)
314
+ _, err = f.WriteString(addr.String())
315
return err
316
}
317
repo/mock.go
+3
-1
@@ -4,6 +4,8 @@ import (
4
"errors"
5
6
"github.com/ipfs/go-ipfs/repo/config"
7
+
8
+ ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
9
)
10
11
var errTODO = errors.New("TODO: mock repo")
@@ -37,4 +39,4 @@ func (m *Mock) GetStorageUsage() (uint64, error) { return 0, nil }
39
40
func (m *Mock) Close() error { return errTODO }
41
40
-func (m *Mock) SetAPIAddr(addr string) error { return errTODO }
42
+func (m *Mock) SetAPIAddr(addr ma.Multiaddr) error { return errTODO }
repo/repo.go
+3
-1
@@ -5,6 +5,8 @@ import (
5
"io"
6
7
config "github.com/ipfs/go-ipfs/repo/config"
8
+
9
+ ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
10
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
11
)
12
@@ -23,7 +25,7 @@ type Repo interface {
25
GetStorageUsage() (uint64, error)
26
27
// SetAPIAddr sets the API address in the repo.
26
- SetAPIAddr(addr string) error
28
+ SetAPIAddr(addr ma.Multiaddr) error
29
30
io.Closer
31
}