提交 9094cc8f 作者: Jeromy Johnson 提交者: GitHub

Merge pull request #3219 from ipfs/fix/api-file-bad

cmd: validate repo/api file and print nicer error message
...@@ -471,7 +471,7 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) { ...@@ -471,7 +471,7 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) {
return fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err), nil return fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err), nil
} }
if err := node.Repo.SetAPIAddr(apiMaddr.String()); err != nil { if err := node.Repo.SetAPIAddr(apiMaddr); err != nil {
return fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err), nil return fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err), nil
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
package main package main
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
...@@ -16,13 +17,6 @@ import ( ...@@ -16,13 +17,6 @@ import (
"syscall" "syscall"
"time" "time"
manet "gx/ipfs/QmT6Cp31887FpAc25z25YHgpFJohZedrYLWPPspRtj1Brp/go-multiaddr-net"
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
context "context"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
cmds "github.com/ipfs/go-ipfs/commands" cmds "github.com/ipfs/go-ipfs/commands"
cmdsCli "github.com/ipfs/go-ipfs/commands/cli" cmdsCli "github.com/ipfs/go-ipfs/commands/cli"
cmdsHttp "github.com/ipfs/go-ipfs/commands/http" cmdsHttp "github.com/ipfs/go-ipfs/commands/http"
...@@ -31,7 +25,13 @@ import ( ...@@ -31,7 +25,13 @@ import (
repo "github.com/ipfs/go-ipfs/repo" repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config" config "github.com/ipfs/go-ipfs/repo/config"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
manet "gx/ipfs/QmT6Cp31887FpAc25z25YHgpFJohZedrYLWPPspRtj1Brp/go-multiaddr-net"
loggables "gx/ipfs/QmTMy4hVSY28DdwJ9kBz6y7q6MuioFzPcpM3Ma3aPjo1i3/go-libp2p-loggables" loggables "gx/ipfs/QmTMy4hVSY28DdwJ9kBz6y7q6MuioFzPcpM3Ma3aPjo1i3/go-libp2p-loggables"
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
) )
// log is the command logger // log is the command logger
...@@ -590,23 +590,51 @@ func profileIfEnabled() (func(), error) { ...@@ -590,23 +590,51 @@ func profileIfEnabled() (func(), error) {
return func() {}, nil return func() {}, nil
} }
var apiFileErrorFmt string = `Failed to parse '%[1]s/api' file.
error: %[2]s
If you're sure go-ipfs isn't running, you can just delete it.
Otherwise check:
`
var checkIPFSUnixFmt = "\tps aux | grep ipfs"
var checkIPFSWinFmt = "\ttasklist | findstr ipfs"
// getApiClient checks the repo, and the given options, checking for // getApiClient checks the repo, and the given options, checking for
// a running API service. if there is one, it returns a client. // a running API service. if there is one, it returns a client.
// otherwise, it returns errApiNotRunning, or another error. // otherwise, it returns errApiNotRunning, or another error.
func getApiClient(repoPath, apiAddrStr string) (cmdsHttp.Client, error) { func getApiClient(repoPath, apiAddrStr string) (cmdsHttp.Client, error) {
var apiErrorFmt string
switch {
case osh.IsUnix():
apiErrorFmt = apiFileErrorFmt + checkIPFSUnixFmt
case osh.IsWindows():
apiErrorFmt = apiFileErrorFmt + checkIPFSWinFmt
default:
apiErrorFmt = apiFileErrorFmt
}
if apiAddrStr == "" { var addr ma.Multiaddr
var err error var err error
if apiAddrStr, err = fsrepo.APIAddr(repoPath); err != nil { if len(apiAddrStr) != 0 {
addr, err = ma.NewMultiaddr(apiAddrStr)
if err != nil {
return nil, err
}
if len(addr.Protocols()) == 0 {
return nil, fmt.Errorf("mulitaddr doesn't provide any protocols")
}
} else {
addr, err = fsrepo.APIAddr(repoPath)
if err == repo.ErrApiNotRunning {
return nil, err return nil, err
} }
}
addr, err := ma.NewMultiaddr(apiAddrStr) if err != nil {
if err != nil { return nil, fmt.Errorf(apiErrorFmt, repoPath, err.Error())
return nil, err }
}
if len(addr.Protocols()) == 0 {
return nil, fmt.Errorf(apiErrorFmt, repoPath, "multiaddr doesn't provide any protocols")
} }
return apiClientForAddr(addr) return apiClientForAddr(addr)
} }
......
...@@ -287,6 +287,12 @@ ...@@ -287,6 +287,12 @@
"hash": "QmQfeKxQtBN721pekQh6Jq24adFUjnU65YdY3GNczfuG2T", "hash": "QmQfeKxQtBN721pekQh6Jq24adFUjnU65YdY3GNczfuG2T",
"name": "dir-index-html", "name": "dir-index-html",
"version": "1.0.3" "version": "1.0.3"
},
{
"author": "Kubuxu",
"hash": "QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93",
"name": "go-os-helper",
"version": "0.0.0"
} }
], ],
"gxVersion": "0.4.0", "gxVersion": "0.4.0",
...@@ -295,4 +301,3 @@ ...@@ -295,4 +301,3 @@
"name": "go-ipfs", "name": "go-ipfs",
"version": "0.4.5-dev" "version": "0.4.5-dev"
} }
...@@ -18,7 +18,9 @@ import ( ...@@ -18,7 +18,9 @@ import (
mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations" mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize" serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
dir "github.com/ipfs/go-ipfs/thirdparty/dir" dir "github.com/ipfs/go-ipfs/thirdparty/dir"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
util "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" util "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
"gx/ipfs/QmeqtHtxGfcsfXiou7wqHJARWPKUTUcPdtSfSYYHp48dtQ/go-ds-measure" "gx/ipfs/QmeqtHtxGfcsfXiou7wqHJARWPKUTUcPdtSfSYYHp48dtQ/go-ds-measure"
) )
...@@ -274,7 +276,7 @@ func LockedByOtherProcess(repoPath string) (bool, error) { ...@@ -274,7 +276,7 @@ func LockedByOtherProcess(repoPath string) (bool, error) {
// in the fsrepo. This is a concurrent operation, meaning that any // in the fsrepo. This is a concurrent operation, meaning that any
// process may read this file. modifying this file, therefore, should // process may read this file. modifying this file, therefore, should
// use "mv" to replace the whole file and avoid interleaved read/writes. // use "mv" to replace the whole file and avoid interleaved read/writes.
func APIAddr(repoPath string) (string, error) { func APIAddr(repoPath string) (ma.Multiaddr, error) {
repoPath = filepath.Clean(repoPath) repoPath = filepath.Clean(repoPath)
apiFilePath := filepath.Join(repoPath, apiFile) apiFilePath := filepath.Join(repoPath, apiFile)
...@@ -282,9 +284,9 @@ func APIAddr(repoPath string) (string, error) { ...@@ -282,9 +284,9 @@ func APIAddr(repoPath string) (string, error) {
f, err := os.Open(apiFilePath) f, err := os.Open(apiFilePath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return "", repo.ErrApiNotRunning return nil, repo.ErrApiNotRunning
} }
return "", err return nil, err
} }
defer f.Close() defer f.Close()
...@@ -293,23 +295,23 @@ func APIAddr(repoPath string) (string, error) { ...@@ -293,23 +295,23 @@ func APIAddr(repoPath string) (string, error) {
buf := make([]byte, 2048) buf := make([]byte, 2048)
n, err := f.Read(buf) n, err := f.Read(buf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return "", err return nil, err
} }
s := string(buf[:n]) s := string(buf[:n])
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
return s, nil return ma.NewMultiaddr(s)
} }
// SetAPIAddr writes the API Addr to the /api file. // SetAPIAddr writes the API Addr to the /api file.
func (r *FSRepo) SetAPIAddr(addr string) error { func (r *FSRepo) SetAPIAddr(addr ma.Multiaddr) error {
f, err := os.Create(filepath.Join(r.path, apiFile)) f, err := os.Create(filepath.Join(r.path, apiFile))
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
_, err = f.WriteString(addr) _, err = f.WriteString(addr.String())
return err return err
} }
......
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"errors" "errors"
"github.com/ipfs/go-ipfs/repo/config" "github.com/ipfs/go-ipfs/repo/config"
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
) )
var errTODO = errors.New("TODO: mock repo") var errTODO = errors.New("TODO: mock repo")
...@@ -37,4 +39,4 @@ func (m *Mock) GetStorageUsage() (uint64, error) { return 0, nil } ...@@ -37,4 +39,4 @@ func (m *Mock) GetStorageUsage() (uint64, error) { return 0, nil }
func (m *Mock) Close() error { return errTODO } func (m *Mock) Close() error { return errTODO }
func (m *Mock) SetAPIAddr(addr string) error { return errTODO } func (m *Mock) SetAPIAddr(addr ma.Multiaddr) error { return errTODO }
...@@ -5,6 +5,8 @@ import ( ...@@ -5,6 +5,8 @@ import (
"io" "io"
config "github.com/ipfs/go-ipfs/repo/config" config "github.com/ipfs/go-ipfs/repo/config"
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore" ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
) )
...@@ -23,7 +25,7 @@ type Repo interface { ...@@ -23,7 +25,7 @@ type Repo interface {
GetStorageUsage() (uint64, error) GetStorageUsage() (uint64, error)
// SetAPIAddr sets the API address in the repo. // SetAPIAddr sets the API address in the repo.
SetAPIAddr(addr string) error SetAPIAddr(addr ma.Multiaddr) error
io.Closer io.Closer
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论