Skip to content

Commit 022846e

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

File tree

6 files changed

+69
-30
lines changed

6 files changed

+69
-30
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"
@@ -16,13 +17,6 @@ import (
1617
"syscall"
1718
"time"
1819

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-
2620
cmds "github.com/ipfs/go-ipfs/commands"
2721
cmdsCli "github.com/ipfs/go-ipfs/commands/cli"
2822
cmdsHttp "github.com/ipfs/go-ipfs/commands/http"
@@ -31,7 +25,13 @@ import (
3125
repo "github.com/ipfs/go-ipfs/repo"
3226
config "github.com/ipfs/go-ipfs/repo/config"
3327
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
28+
29+
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
30+
manet "gx/ipfs/QmT6Cp31887FpAc25z25YHgpFJohZedrYLWPPspRtj1Brp/go-multiaddr-net"
3431
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"
3535
)
3636

3737
// log is the command logger
@@ -590,23 +590,51 @@ func profileIfEnabled() (func(), error) {
590590
return func() {}, nil
591591
}
592592

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+
593601
// getApiClient checks the repo, and the given options, checking for
594602
// a running API service. if there is one, it returns a client.
595603
// otherwise, it returns errApiNotRunning, or another error.
596604
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+
}
597614

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 {
601628
return nil, err
602629
}
603-
}
604630

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")
608637
}
609-
610638
return apiClientForAddr(addr)
611639
}
612640

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@
287287
"hash": "QmQfeKxQtBN721pekQh6Jq24adFUjnU65YdY3GNczfuG2T",
288288
"name": "dir-index-html",
289289
"version": "1.0.3"
290+
},
291+
{
292+
"author": "Kubuxu",
293+
"hash": "QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93",
294+
"name": "go-os-helper",
295+
"version": "0.0.0"
290296
}
291297
],
292298
"gxVersion": "0.4.0",
@@ -295,4 +301,3 @@
295301
"name": "go-ipfs",
296302
"version": "0.4.5-dev"
297303
}
298-

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)