@cryptotaxi247 / kubo / commits / a23609fc4

improve conn refused error check

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com> rewrite path to filepath in fsrepo License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com> remove api file on repo close License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com> update function to check normal net.OpErrors License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Nov 8, 2015 at 11:14 UTC a23609fc4d40513a66ccb22d8146cd4cb6d9a9b5
2 files changed +34 -15
cmd/ipfs/main.go
+13 -2
@@ -6,6 +6,8 @@ import (
6 "fmt"
7 "io"
8 "math/rand"
9 + "net"
10 + "net/url"
11 "os"
12 "os/signal"
13 "runtime"
@@ -671,6 +673,15 @@ func apiClientForAddr(addr ma.Multiaddr) (cmdsHttp.Client, error) {
673 }
674
675 func isConnRefused(err error) bool {
674 - return strings.Contains(err.Error(), "connection refused") ||
675 - strings.Contains(err.Error(), "target machine actively refused it")
676 + // unwrap url errors from http calls
677 + if urlerr, ok := err.(*url.Error); ok {
678 + err = urlerr.Err
679 + }
680 +
681 + netoperr, ok := err.(*net.OpError)
682 + if !ok {
683 + return false
684 + }
685 +
686 + return netoperr.Op == "dial"
687 }
repo/fsrepo/fsrepo.go
+21 -13
@@ -5,7 +5,7 @@ import (
5 "fmt"
6 "io"
7 "os"
8 - "path"
8 + "path/filepath"
9 "strconv"
10 "strings"
11 "sync"
@@ -26,8 +26,11 @@ import (
26 u "github.com/ipfs/go-ipfs/util"
27 util "github.com/ipfs/go-ipfs/util"
28 ds2 "github.com/ipfs/go-ipfs/util/datastore2"
29 + logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
30 )
31
32 +var log = logging.Logger("fsrepo")
33 +
34 // version number that we are currently expecting to see
35 var RepoVersion = "2"
36
@@ -163,7 +166,7 @@ func open(repoPath string) (repo.Repo, error) {
166 }
167
168 func newFSRepo(rpath string) (*FSRepo, error) {
166 - expPath, err := u.TildeExpansion(path.Clean(rpath))
169 + expPath, err := u.TildeExpansion(filepath.Clean(rpath))
170 if err != nil {
171 return nil, err
172 }
@@ -247,17 +250,17 @@ func Init(repoPath string, conf *config.Config) error {
250
251 // The actual datastore contents are initialized lazily when Opened.
252 // During Init, we merely check that the directory is writeable.
250 - leveldbPath := path.Join(repoPath, leveldbDirectory)
253 + leveldbPath := filepath.Join(repoPath, leveldbDirectory)
254 if err := dir.Writable(leveldbPath); err != nil {
255 return fmt.Errorf("datastore: %s", err)
256 }
257
255 - flatfsPath := path.Join(repoPath, flatfsDirectory)
258 + flatfsPath := filepath.Join(repoPath, flatfsDirectory)
259 if err := dir.Writable(flatfsPath); err != nil {
260 return fmt.Errorf("datastore: %s", err)
261 }
262
260 - if err := dir.Writable(path.Join(repoPath, "logs")); err != nil {
263 + if err := dir.Writable(filepath.Join(repoPath, "logs")); err != nil {
264 return err
265 }
266
@@ -270,14 +273,14 @@ func Init(repoPath string, conf *config.Config) error {
273
274 // Remove recursively removes the FSRepo at |path|.
275 func Remove(repoPath string) error {
273 - repoPath = path.Clean(repoPath)
276 + repoPath = filepath.Clean(repoPath)
277 return os.RemoveAll(repoPath)
278 }
279
280 // LockedByOtherProcess returns true if the FSRepo is locked by another
281 // process. If true, then the repo cannot be opened by this process.
282 func LockedByOtherProcess(repoPath string) (bool, error) {
280 - repoPath = path.Clean(repoPath)
283 + repoPath = filepath.Clean(repoPath)
284 // NB: the lock is only held when repos are Open
285 return lockfile.Locked(repoPath)
286 }
@@ -287,8 +290,8 @@ func LockedByOtherProcess(repoPath string) (bool, error) {
290 // process may read this file. modifying this file, therefore, should
291 // use "mv" to replace the whole file and avoid interleaved read/writes.
292 func APIAddr(repoPath string) (string, error) {
290 - repoPath = path.Clean(repoPath)
291 - apiFilePath := path.Join(repoPath, apiFile)
293 + repoPath = filepath.Clean(repoPath)
294 + apiFilePath := filepath.Join(repoPath, apiFile)
295
296 // if there is no file, assume there is no api addr.
297 f, err := os.Open(apiFilePath)
@@ -315,7 +318,7 @@ func APIAddr(repoPath string) (string, error) {
318
319 // SetAPIAddr writes the API Addr to the /api file.
320 func (r *FSRepo) SetAPIAddr(addr string) error {
318 - f, err := os.Create(path.Join(r.path, apiFile))
321 + f, err := os.Create(filepath.Join(r.path, apiFile))
322 if err != nil {
323 return err
324 }
@@ -341,7 +344,7 @@ func (r *FSRepo) openConfig() error {
344
345 // openDatastore returns an error if the config file is not present.
346 func (r *FSRepo) openDatastore() error {
344 - leveldbPath := path.Join(r.path, leveldbDirectory)
347 + leveldbPath := filepath.Join(r.path, leveldbDirectory)
348 var err error
349 // save leveldb reference so it can be neatly closed afterward
350 leveldbDS, err := levelds.NewDatastore(leveldbPath, &levelds.Options{
@@ -359,7 +362,7 @@ func (r *FSRepo) openDatastore() error {
362 // including "/" from datastore.Key and 2 bytes from multihash. To
363 // reach a uniform 256-way split, we need approximately 4 bytes of
364 // prefix.
362 - blocksDS, err := flatfs.New(path.Join(r.path, flatfsDirectory), 4)
365 + blocksDS, err := flatfs.New(filepath.Join(r.path, flatfsDirectory), 4)
366 if err != nil {
367 return errors.New("unable to open flatfs datastore")
368 }
@@ -410,6 +413,11 @@ func (r *FSRepo) Close() error {
413 return err
414 }
415
416 + err := os.Remove(filepath.Join(r.path, apiFile))
417 + if err != nil {
418 + log.Warning("error removing api file: ", err)
419 + }
420 +
421 // This code existed in the previous versions, but
422 // EventlogComponent.Close was never called. Preserving here
423 // pending further discussion.
@@ -600,7 +608,7 @@ func isInitializedUnsynced(repoPath string) bool {
608 if !configIsInitialized(repoPath) {
609 return false
610 }
603 - if !util.FileExists(path.Join(repoPath, leveldbDirectory)) {
611 + if !util.FileExists(filepath.Join(repoPath, leveldbDirectory)) {
612 return false
613 }
614 return true