From 38f307db57d199a348806dbb7acb8dae54db7af3 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 22 Aug 2023 05:18:18 +0800 Subject: [PATCH 1/4] refactor: using error is instead of == --- client/rpc/apifile.go | 5 +++-- client/rpc/name.go | 3 ++- client/rpc/pin.go | 2 +- client/rpc/pubsub.go | 3 ++- client/rpc/unixfs.go | 2 +- cmd/ipfs/util/ulimit.go | 4 ++-- config/bootstrap_peers.go | 2 +- config/config.go | 8 ++++---- config/serialize/serialize.go | 2 +- core/bootstrap/bootstrap.go | 2 +- repo/fsrepo/fsrepo.go | 2 +- repo/fsrepo/migrations/httpfetcher.go | 6 +++--- repo/fsrepo/migrations/migrations.go | 10 +++++----- repo/fsrepo/migrations/versions.go | 2 +- routing/composer.go | 3 ++- tar/format.go | 8 ++++---- 16 files changed, 34 insertions(+), 30 deletions(-) diff --git a/client/rpc/apifile.go b/client/rpc/apifile.go index 873a67b7be0..efdb8c34e8d 100644 --- a/client/rpc/apifile.go +++ b/client/rpc/apifile.go @@ -3,6 +3,7 @@ package rpc import ( "context" "encoding/json" + "errors" "fmt" "io" @@ -94,7 +95,7 @@ func (f *apiFile) ReadAt(p []byte, off int64) (int, error) { defer resp.Output.Close() n, err := io.ReadFull(resp.Output, p) - if err == io.ErrUnexpectedEOF { + if errors.Is(err, io.ErrUnexpectedEOF) { err = io.EOF } return n, err @@ -170,7 +171,7 @@ func (it *apiIter) Next() bool { var out lsOutput if err := it.dec.Decode(&out); err != nil { - if err != io.EOF { + if !errors.Is(err, io.EOF) { it.err = err } return false diff --git a/client/rpc/name.go b/client/rpc/name.go index 5ad9d16cb8a..9a5e82783f3 100644 --- a/client/rpc/name.go +++ b/client/rpc/name.go @@ -3,6 +3,7 @@ package rpc import ( "context" "encoding/json" + "errors" "fmt" "io" @@ -79,7 +80,7 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name for { var out struct{ Path string } err := dec.Decode(&out) - if err == io.EOF { + if errors.Is(err, io.EOF) { return } var ires iface.IpnsResult diff --git a/client/rpc/pin.go b/client/rpc/pin.go index e8aecf11cf6..4929b73cb4b 100644 --- a/client/rpc/pin.go +++ b/client/rpc/pin.go @@ -216,7 +216,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan iface.PinStatus, error) { } } if err := dec.Decode(&out); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { return } select { diff --git a/client/rpc/pubsub.go b/client/rpc/pubsub.go index f255da5addf..e5acfcd6f53 100644 --- a/client/rpc/pubsub.go +++ b/client/rpc/pubsub.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "io" iface "github.com/ipfs/boxo/coreiface" @@ -175,7 +176,7 @@ func (api *PubsubAPI) Subscribe(ctx context.Context, topic string, opts ...caopt for { var msg pubsubMessage if err := dec.Decode(&msg); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { return } msg.err = err diff --git a/client/rpc/unixfs.go b/client/rpc/unixfs.go index 2099f190b30..556ba5e61c4 100644 --- a/client/rpc/unixfs.go +++ b/client/rpc/unixfs.go @@ -162,7 +162,7 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p path.Path, opts ...caopts.Unixfs for { var link lsOutput if err := dec.Decode(&link); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { return } select { diff --git a/cmd/ipfs/util/ulimit.go b/cmd/ipfs/util/ulimit.go index a00e810761d..188444d6774 100644 --- a/cmd/ipfs/util/ulimit.go +++ b/cmd/ipfs/util/ulimit.go @@ -82,7 +82,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) { // set the soft value err = setLimit(targetLimit, hard) if err != nil { - err = fmt.Errorf("error setting ulimit without hard limit: %s", err) + err = fmt.Errorf("error setting ulimit without hard limit: %w", err) break } newLimit = targetLimit @@ -107,7 +107,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) { break } default: - err = fmt.Errorf("error setting: ulimit: %s", err) + err = fmt.Errorf("error setting: ulimit: %w", err) } return newLimit > 0, newLimit, err diff --git a/config/bootstrap_peers.go b/config/bootstrap_peers.go index 6222cd6232a..6b9434ae5cb 100644 --- a/config/bootstrap_peers.go +++ b/config/bootstrap_peers.go @@ -36,7 +36,7 @@ func (c *Config) BootstrapPeers() ([]peer.AddrInfo, error) { func DefaultBootstrapPeers() ([]peer.AddrInfo, error) { ps, err := ParseBootstrapPeers(DefaultBootstrapAddresses) if err != nil { - return nil, fmt.Errorf(`failed to parse hardcoded bootstrap peers: %s + return nil, fmt.Errorf(`failed to parse hardcoded bootstrap peers: %w This is a problem with the ipfs codebase. Please report it to the dev team`, err) } return ps, nil diff --git a/config/config.go b/config/config.go index 035fbe2966c..1951784dd1d 100644 --- a/config/config.go +++ b/config/config.go @@ -117,7 +117,7 @@ func FromMap(v map[string]interface{}) (*Config, error) { } var conf Config if err := json.NewDecoder(buf).Decode(&conf); err != nil { - return nil, fmt.Errorf("failure to decode config: %s", err) + return nil, fmt.Errorf("failure to decode config: %w", err) } return &conf, nil } @@ -129,7 +129,7 @@ func ToMap(conf *Config) (map[string]interface{}, error) { } var m map[string]interface{} if err := json.NewDecoder(buf).Decode(&m); err != nil { - return nil, fmt.Errorf("failure to decode config: %s", err) + return nil, fmt.Errorf("failure to decode config: %w", err) } return m, nil } @@ -140,11 +140,11 @@ func (c *Config) Clone() (*Config, error) { var buf bytes.Buffer if err := json.NewEncoder(&buf).Encode(c); err != nil { - return nil, fmt.Errorf("failure to encode config: %s", err) + return nil, fmt.Errorf("failure to encode config: %w", err) } if err := json.NewDecoder(&buf).Decode(&newConfig); err != nil { - return nil, fmt.Errorf("failure to decode config: %s", err) + return nil, fmt.Errorf("failure to decode config: %w", err) } return &newConfig, nil diff --git a/config/serialize/serialize.go b/config/serialize/serialize.go index 616e529cbd0..7cb479f6bad 100644 --- a/config/serialize/serialize.go +++ b/config/serialize/serialize.go @@ -28,7 +28,7 @@ func ReadConfigFile(filename string, cfg interface{}) error { } defer f.Close() if err := json.NewDecoder(f).Decode(cfg); err != nil { - return fmt.Errorf("failure to decode config: %s", err) + return fmt.Errorf("failure to decode config: %w", err) } return nil } diff --git a/core/bootstrap/bootstrap.go b/core/bootstrap/bootstrap.go index ed95d74e18b..231cf105618 100644 --- a/core/bootstrap/bootstrap.go +++ b/core/bootstrap/bootstrap.go @@ -309,7 +309,7 @@ func peersConnect(ctx context.Context, ph host.Host, availablePeers []peer.AddrI log.Debugf("%s bootstrapping to %s", ph.ID(), p.ID) if err := ph.Connect(ctx, p); err != nil { - if ctx.Err() != context.Canceled { + if !errors.Is(ctx.Err(), context.Canceled) { log.Debugf("failed to bootstrap with %v: %s", p.ID, err) } return diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index c2f7ca72aef..13e7e415063 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -449,7 +449,7 @@ func (r *FSRepo) openConfig() error { func (r *FSRepo) openUserResourceOverrides() error { // This filepath is documented in docs/libp2p-resource-management.md and be kept in sync. err := serialize.ReadConfigFile(filepath.Join(r.path, "libp2p-resource-limit-overrides.json"), &r.userResourceOverrides) - if err == serialize.ErrNotInitialized { + if errors.Is(err, serialize.ErrNotInitialized) { err = nil } return err diff --git a/repo/fsrepo/migrations/httpfetcher.go b/repo/fsrepo/migrations/httpfetcher.go index 588a01eadfd..1f3d575a949 100644 --- a/repo/fsrepo/migrations/httpfetcher.go +++ b/repo/fsrepo/migrations/httpfetcher.go @@ -66,7 +66,7 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error req, err := http.NewRequestWithContext(ctx, http.MethodGet, gwURL, nil) if err != nil { - return nil, fmt.Errorf("http.NewRequest error: %s", err) + return nil, fmt.Errorf("http.NewRequest error: %w", err) } if f.userAgent != "" { @@ -75,14 +75,14 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error resp, err := http.DefaultClient.Do(req) if err != nil { - return nil, fmt.Errorf("http.DefaultClient.Do error: %s", err) + return nil, fmt.Errorf("http.DefaultClient.Do error: %w", err) } if resp.StatusCode >= 400 { defer resp.Body.Close() mes, err := io.ReadAll(resp.Body) if err != nil { - return nil, fmt.Errorf("error reading error body: %s", err) + return nil, fmt.Errorf("error reading error body: %w", err) } return nil, fmt.Errorf("GET %s error: %s: %s", gwURL, resp.Status, string(mes)) } diff --git a/repo/fsrepo/migrations/migrations.go b/repo/fsrepo/migrations/migrations.go index 14cc6c2de89..643f78f3a9b 100644 --- a/repo/fsrepo/migrations/migrations.go +++ b/repo/fsrepo/migrations/migrations.go @@ -32,7 +32,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s } fromVer, err := RepoVersion(ipfsDir) if err != nil { - return fmt.Errorf("could not get repo version: %s", err) + return fmt.Errorf("could not get repo version: %w", err) } if fromVer == targetVer { // repo already at target version number @@ -87,7 +87,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s logger.Println("Running migration", migration, "...") err = runMigration(ctx, binPaths[migration], ipfsDir, revert, logger) if err != nil { - return fmt.Errorf("migration %s failed: %s", migration, err) + return fmt.Errorf("migration %s failed: %w", migration, err) } } logger.Printf("Success: fs-repo migrated to version %d.\n", targetVer) @@ -98,7 +98,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s func NeedMigration(target int) (bool, error) { vnum, err := RepoVersion("") if err != nil { - return false, fmt.Errorf("could not get repo version: %s", err) + return false, fmt.Errorf("could not get repo version: %w", err) } return vnum != target, nil @@ -171,7 +171,7 @@ func GetMigrationFetcher(downloadSources []string, distPath string, newIpfsFetch default: u, err := url.Parse(src) if err != nil { - return nil, fmt.Errorf("bad gateway address: %s", err) + return nil, fmt.Errorf("bad gateway address: %w", err) } switch u.Scheme { case "": @@ -293,7 +293,7 @@ func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, dest if len(fails) != 0 { err = fmt.Errorf("failed to download migrations: %s", strings.Join(fails, " ")) if ctx.Err() != nil { - err = fmt.Errorf("%s, %s", ctx.Err(), err) + err = fmt.Errorf("%w, %w", ctx.Err(), err) } return nil, err } diff --git a/repo/fsrepo/migrations/versions.go b/repo/fsrepo/migrations/versions.go index af5bbbbd969..056671c0704 100644 --- a/repo/fsrepo/migrations/versions.go +++ b/repo/fsrepo/migrations/versions.go @@ -57,7 +57,7 @@ func DistVersions(ctx context.Context, fetcher Fetcher, dist string, sortDesc bo vers = append(vers, ver) } if scan.Err() != nil { - return nil, fmt.Errorf("could not read versions: %s", scan.Err()) + return nil, fmt.Errorf("could not read versions: %w", scan.Err()) } if sortDesc { diff --git a/routing/composer.go b/routing/composer.go index 92c351fc53c..3541fc7dd24 100644 --- a/routing/composer.go +++ b/routing/composer.go @@ -2,6 +2,7 @@ package routing import ( "context" + "errors" "github.com/hashicorp/go-multierror" "github.com/ipfs/go-cid" @@ -103,7 +104,7 @@ func (c *Composer) SearchValue(ctx context.Context, key string, opts ...routing. ch, err := c.GetValueRouter.SearchValue(ctx, key, opts...) // avoid nil channels on implementations not supporting SearchValue method. - if err == routing.ErrNotFound && ch == nil { + if errors.Is(err, routing.ErrNotFound) && ch == nil { out := make(chan []byte) close(out) return out, err diff --git a/tar/format.go b/tar/format.go index 626e0982b51..69e5d583e43 100644 --- a/tar/format.go +++ b/tar/format.go @@ -49,7 +49,7 @@ func ImportTar(ctx context.Context, r io.Reader, ds ipld.DAGService) (*dag.Proto for { h, err := tr.Next() if err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } return nil, err @@ -128,7 +128,7 @@ func (tr *tarReader) Read(b []byte) (int, error) { // no header remaining, check for recursive if tr.childRead != nil { n, err := tr.childRead.Read(b) - if err == io.EOF { + if errors.Is(err, io.EOF) { tr.childRead = nil return n, nil } @@ -138,7 +138,7 @@ func (tr *tarReader) Read(b []byte) (int, error) { // check for filedata to be read if tr.fileRead != nil { n, err := tr.fileRead.Read(b) - if err == io.EOF { + if errors.Is(err, io.EOF) { nr := tr.fileRead.n tr.pad = (blockSize - (nr % blockSize)) % blockSize tr.fileRead.Close() @@ -175,7 +175,7 @@ func (tr *tarReader) Read(b []byte) (int, error) { tr.hdrBuf = bytes.NewReader(hndpb.Data()) dataNd, err := hndpb.GetLinkedProtoNode(tr.ctx, tr.ds, "data") - if err != nil && err != dag.ErrLinkNotFound { + if err != nil && !errors.Is(err, dag.ErrLinkNotFound) { return 0, err } From bbb25b73c3747fcdb564122c0cded318c84069e3 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 22 Aug 2023 05:27:31 +0800 Subject: [PATCH 2/4] fix: ci issue --- repo/fsrepo/migrations/migrations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo/fsrepo/migrations/migrations.go b/repo/fsrepo/migrations/migrations.go index 643f78f3a9b..6894d73a777 100644 --- a/repo/fsrepo/migrations/migrations.go +++ b/repo/fsrepo/migrations/migrations.go @@ -293,7 +293,7 @@ func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, dest if len(fails) != 0 { err = fmt.Errorf("failed to download migrations: %s", strings.Join(fails, " ")) if ctx.Err() != nil { - err = fmt.Errorf("%w, %w", ctx.Err(), err) + err = fmt.Errorf("%s, %w", ctx.Err(), err) } return nil, err } From 4eb12b1aa450ea43c4ef35ddd671c2a7df52a1e1 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 22 Aug 2023 20:01:37 +0800 Subject: [PATCH 3/4] fix: remove errors.Is() for io.EOF --- client/rpc/apifile.go | 5 ++--- client/rpc/name.go | 3 +-- client/rpc/pin.go | 2 +- client/rpc/pubsub.go | 3 +-- client/rpc/unixfs.go | 2 +- tar/format.go | 6 +++--- 6 files changed, 9 insertions(+), 12 deletions(-) diff --git a/client/rpc/apifile.go b/client/rpc/apifile.go index efdb8c34e8d..873a67b7be0 100644 --- a/client/rpc/apifile.go +++ b/client/rpc/apifile.go @@ -3,7 +3,6 @@ package rpc import ( "context" "encoding/json" - "errors" "fmt" "io" @@ -95,7 +94,7 @@ func (f *apiFile) ReadAt(p []byte, off int64) (int, error) { defer resp.Output.Close() n, err := io.ReadFull(resp.Output, p) - if errors.Is(err, io.ErrUnexpectedEOF) { + if err == io.ErrUnexpectedEOF { err = io.EOF } return n, err @@ -171,7 +170,7 @@ func (it *apiIter) Next() bool { var out lsOutput if err := it.dec.Decode(&out); err != nil { - if !errors.Is(err, io.EOF) { + if err != io.EOF { it.err = err } return false diff --git a/client/rpc/name.go b/client/rpc/name.go index 9a5e82783f3..5ad9d16cb8a 100644 --- a/client/rpc/name.go +++ b/client/rpc/name.go @@ -3,7 +3,6 @@ package rpc import ( "context" "encoding/json" - "errors" "fmt" "io" @@ -80,7 +79,7 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name for { var out struct{ Path string } err := dec.Decode(&out) - if errors.Is(err, io.EOF) { + if err == io.EOF { return } var ires iface.IpnsResult diff --git a/client/rpc/pin.go b/client/rpc/pin.go index 4929b73cb4b..e8aecf11cf6 100644 --- a/client/rpc/pin.go +++ b/client/rpc/pin.go @@ -216,7 +216,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan iface.PinStatus, error) { } } if err := dec.Decode(&out); err != nil { - if errors.Is(err, io.EOF) { + if err == io.EOF { return } select { diff --git a/client/rpc/pubsub.go b/client/rpc/pubsub.go index e5acfcd6f53..f255da5addf 100644 --- a/client/rpc/pubsub.go +++ b/client/rpc/pubsub.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "io" iface "github.com/ipfs/boxo/coreiface" @@ -176,7 +175,7 @@ func (api *PubsubAPI) Subscribe(ctx context.Context, topic string, opts ...caopt for { var msg pubsubMessage if err := dec.Decode(&msg); err != nil { - if errors.Is(err, io.EOF) { + if err == io.EOF { return } msg.err = err diff --git a/client/rpc/unixfs.go b/client/rpc/unixfs.go index 556ba5e61c4..2099f190b30 100644 --- a/client/rpc/unixfs.go +++ b/client/rpc/unixfs.go @@ -162,7 +162,7 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p path.Path, opts ...caopts.Unixfs for { var link lsOutput if err := dec.Decode(&link); err != nil { - if errors.Is(err, io.EOF) { + if err == io.EOF { return } select { diff --git a/tar/format.go b/tar/format.go index 69e5d583e43..bde92398057 100644 --- a/tar/format.go +++ b/tar/format.go @@ -49,7 +49,7 @@ func ImportTar(ctx context.Context, r io.Reader, ds ipld.DAGService) (*dag.Proto for { h, err := tr.Next() if err != nil { - if errors.Is(err, io.EOF) { + if err == io.EOF { break } return nil, err @@ -128,7 +128,7 @@ func (tr *tarReader) Read(b []byte) (int, error) { // no header remaining, check for recursive if tr.childRead != nil { n, err := tr.childRead.Read(b) - if errors.Is(err, io.EOF) { + if err == io.EOF { tr.childRead = nil return n, nil } @@ -138,7 +138,7 @@ func (tr *tarReader) Read(b []byte) (int, error) { // check for filedata to be read if tr.fileRead != nil { n, err := tr.fileRead.Read(b) - if errors.Is(err, io.EOF) { + if err == io.EOF { nr := tr.fileRead.n tr.pad = (blockSize - (nr % blockSize)) % blockSize tr.fileRead.Close() From f5e33eea65f4ee8b1cbb77831824921917839f8b Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 22 Aug 2023 22:46:48 +0800 Subject: [PATCH 4/4] chore: add suggestion Co-authored-by: Jorropo --- core/bootstrap/bootstrap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/bootstrap/bootstrap.go b/core/bootstrap/bootstrap.go index 231cf105618..ed95d74e18b 100644 --- a/core/bootstrap/bootstrap.go +++ b/core/bootstrap/bootstrap.go @@ -309,7 +309,7 @@ func peersConnect(ctx context.Context, ph host.Host, availablePeers []peer.AddrI log.Debugf("%s bootstrapping to %s", ph.ID(), p.ID) if err := ph.Connect(ctx, p); err != nil { - if !errors.Is(ctx.Err(), context.Canceled) { + if ctx.Err() != context.Canceled { log.Debugf("failed to bootstrap with %v: %s", p.ID, err) } return