Skip to content

Commit 6068da7

Browse files
committed
cmd: validate repo/api file and print nicer error messag
License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
1 parent a9df187 commit 6068da7

File tree

6 files changed

+70
-32
lines changed

6 files changed

+70
-32
lines changed

cmd/ipfs/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) {
471471
return fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err), nil
472472
}
473473

474-
if err := node.Repo.SetAPIAddr(apiMaddr.String()); err != nil {
474+
if err := node.Repo.SetAPIAddr(apiMaddr); err != nil {
475475
return fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err), nil
476476
}
477477

cmd/ipfs/main.go

+43-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package main
33

44
import (
5+
"context"
56
"errors"
67
"fmt"
78
"io"
@@ -17,13 +18,6 @@ import (
1718
"syscall"
1819
"time"
1920

20-
manet "gx/ipfs/QmT6Cp31887FpAc25z25YHgpFJohZedrYLWPPspRtj1Brp/go-multiaddr-net"
21-
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
22-
23-
context "context"
24-
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
25-
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
26-
2721
cmds "github.com/ipfs/go-ipfs/commands"
2822
cmdsCli "github.com/ipfs/go-ipfs/commands/cli"
2923
cmdsHttp "github.com/ipfs/go-ipfs/commands/http"
@@ -32,7 +26,13 @@ import (
3226
repo "github.com/ipfs/go-ipfs/repo"
3327
config "github.com/ipfs/go-ipfs/repo/config"
3428
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
29+
30+
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
31+
manet "gx/ipfs/QmT6Cp31887FpAc25z25YHgpFJohZedrYLWPPspRtj1Brp/go-multiaddr-net"
3532
loggables "gx/ipfs/QmTMy4hVSY28DdwJ9kBz6y7q6MuioFzPcpM3Ma3aPjo1i3/go-libp2p-loggables"
33+
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
34+
osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
35+
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
3636
)
3737

3838
// log is the command logger
@@ -592,23 +592,51 @@ func profileIfEnabled() (func(), error) {
592592
return func() {}, nil
593593
}
594594

595+
var apiFileErrorFmt string = `Failed to parse '%[1]s/api' file.
596+
error: %[2]s
597+
If you're sure go-ipfs isn't running, you can just delete it.
598+
Otherwise check:
599+
`
600+
var checkIPFSUnixFmt = "\tps aux | grep ipfs"
601+
var checkIPFSWinFmt = "\ttasklist | findstr ipfs"
602+
595603
// getApiClient checks the repo, and the given options, checking for
596604
// a running API service. if there is one, it returns a client.
597605
// otherwise, it returns errApiNotRunning, or another error.
598606
func getApiClient(repoPath, apiAddrStr string) (cmdsHttp.Client, error) {
607+
var apiErrorFmt string
608+
switch {
609+
case osh.IsUnix():
610+
apiErrorFmt = apiFileErrorFmt + checkIPFSUnixFmt
611+
case osh.IsWindows():
612+
apiErrorFmt = apiFileErrorFmt + checkIPFSWinFmt
613+
default:
614+
apiErrorFmt = apiFileErrorFmt
615+
}
599616

600-
if apiAddrStr == "" {
601-
var err error
602-
if apiAddrStr, err = fsrepo.APIAddr(repoPath); err != nil {
617+
var addr ma.Multiaddr
618+
var err error
619+
if len(apiAddrStr) != 0 {
620+
addr, err = ma.NewMultiaddr(apiAddrStr)
621+
if err != nil {
622+
return nil, err
623+
}
624+
if len(addr.Protocols()) == 0 {
625+
return nil, fmt.Errorf("mulitaddr doesn't provide any protocols")
626+
}
627+
} else {
628+
addr, err = fsrepo.APIAddr(repoPath)
629+
if err == repo.ErrApiNotRunning {
603630
return nil, err
604631
}
605-
}
606632

607-
addr, err := ma.NewMultiaddr(apiAddrStr)
608-
if err != nil {
609-
return nil, err
633+
if err != nil {
634+
return nil, fmt.Errorf(apiErrorFmt, repoPath, err.Error())
635+
}
636+
}
637+
if len(addr.Protocols()) == 0 {
638+
return nil, fmt.Errorf(apiErrorFmt, repoPath, "multiaddr doesn't provide any protocols")
610639
}
611-
612640
return apiClientForAddr(addr)
613641
}
614642

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,13 @@
286286
"author": "lgierth",
287287
"hash": "QmQfeKxQtBN721pekQh6Jq24adFUjnU65YdY3GNczfuG2T",
288288
"name": "dir-index-html",
289-
"version": "1.0.3",
290-
"comment": "This one needs code updates in assets/assets.go too."
289+
"version": "1.0.3"
290+
},
291+
{
292+
"author": "Kubuxu",
293+
"hash": "QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93",
294+
"name": "go-os-helper",
295+
"version": "0.0.0"
291296
}
292297
],
293298
"gxVersion": "0.4.0",
@@ -296,4 +301,3 @@
296301
"name": "go-ipfs",
297302
"version": "0.4.5-dev"
298303
}
299-

repo/fsrepo/fsrepo.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
"strings"
1111
"sync"
1212

13+
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
14+
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
15+
util "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
16+
"gx/ipfs/QmeqtHtxGfcsfXiou7wqHJARWPKUTUcPdtSfSYYHp48dtQ/go-ds-measure"
17+
1318
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
1419
repo "github.com/ipfs/go-ipfs/repo"
1520
"github.com/ipfs/go-ipfs/repo/common"
@@ -18,9 +23,6 @@ import (
1823
mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
1924
serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
2025
dir "github.com/ipfs/go-ipfs/thirdparty/dir"
21-
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
22-
util "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
23-
"gx/ipfs/QmeqtHtxGfcsfXiou7wqHJARWPKUTUcPdtSfSYYHp48dtQ/go-ds-measure"
2426
)
2527

2628
var log = logging.Logger("fsrepo")
@@ -274,17 +276,17 @@ func LockedByOtherProcess(repoPath string) (bool, error) {
274276
// in the fsrepo. This is a concurrent operation, meaning that any
275277
// process may read this file. modifying this file, therefore, should
276278
// 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) {
278280
repoPath = filepath.Clean(repoPath)
279281
apiFilePath := filepath.Join(repoPath, apiFile)
280282

281283
// if there is no file, assume there is no api addr.
282284
f, err := os.Open(apiFilePath)
283285
if err != nil {
284286
if os.IsNotExist(err) {
285-
return "", repo.ErrApiNotRunning
287+
return nil, repo.ErrApiNotRunning
286288
}
287-
return "", err
289+
return nil, err
288290
}
289291
defer f.Close()
290292

@@ -293,23 +295,23 @@ func APIAddr(repoPath string) (string, error) {
293295
buf := make([]byte, 2048)
294296
n, err := f.Read(buf)
295297
if err != nil && err != io.EOF {
296-
return "", err
298+
return nil, err
297299
}
298300

299301
s := string(buf[:n])
300302
s = strings.TrimSpace(s)
301-
return s, nil
303+
return ma.NewMultiaddr(s)
302304
}
303305

304306
// 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 {
306308
f, err := os.Create(filepath.Join(r.path, apiFile))
307309
if err != nil {
308310
return err
309311
}
310312
defer f.Close()
311313

312-
_, err = f.WriteString(addr)
314+
_, err = f.WriteString(addr.String())
313315
return err
314316
}
315317

repo/mock.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55

66
"github.com/ipfs/go-ipfs/repo/config"
7+
8+
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
79
)
810

911
var errTODO = errors.New("TODO: mock repo")
@@ -37,4 +39,4 @@ func (m *Mock) GetStorageUsage() (uint64, error) { return 0, nil }
3739

3840
func (m *Mock) Close() error { return errTODO }
3941

40-
func (m *Mock) SetAPIAddr(addr string) error { return errTODO }
42+
func (m *Mock) SetAPIAddr(addr ma.Multiaddr) error { return errTODO }

repo/repo.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"errors"
55
"io"
66

7-
config "github.com/ipfs/go-ipfs/repo/config"
7+
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
88
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
9+
10+
config "github.com/ipfs/go-ipfs/repo/config"
911
)
1012

1113
var (
@@ -23,7 +25,7 @@ type Repo interface {
2325
GetStorageUsage() (uint64, error)
2426

2527
// SetAPIAddr sets the API address in the repo.
26-
SetAPIAddr(addr string) error
28+
SetAPIAddr(addr ma.Multiaddr) error
2729

2830
io.Closer
2931
}

0 commit comments

Comments
 (0)