mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 09:52:20 +08:00
Make sure ctx in commands are derived from req.Context
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
This commit is contained in:
@ -54,7 +54,7 @@ func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
|
||||
|
||||
fname := filepath.Base(p)
|
||||
k := key.B58KeyDecode(s)
|
||||
if err := dirb.AddChild(fname, k); err != nil {
|
||||
if err := dirb.AddChild(nd.Context(), fname, k); err != nil {
|
||||
return nil, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
|
||||
}
|
||||
}
|
||||
|
@ -51,8 +51,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
|
||||
|
||||
if req.Context() == nil {
|
||||
log.Warningf("no context set in request")
|
||||
err := req.SetRootContext(context.TODO())
|
||||
if err != nil {
|
||||
if err := req.SetRootContext(context.TODO()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ import (
|
||||
|
||||
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
||||
mafilter "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/multiaddr-filter"
|
||||
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type stringList struct {
|
||||
@ -211,7 +210,7 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3
|
||||
cmds.StringArg("address", true, true, "address of peer to connect to").EnableStdin(),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
ctx := context.TODO()
|
||||
ctx := req.Context()
|
||||
|
||||
n, err := req.InvocContext().GetNode()
|
||||
if err != nil {
|
||||
|
@ -320,7 +320,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
|
||||
|
||||
func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) {
|
||||
log.Warning("trying peer info: ", p)
|
||||
ctx, _ := context.WithTimeout(context.TODO(), time.Second*10)
|
||||
ctx, _ := context.WithTimeout(n.Context(), time.Second*10)
|
||||
err := n.PeerHost.Connect(ctx, p)
|
||||
if err != nil {
|
||||
log.Warning("Failed to connect to peer found by discovery: ", err)
|
||||
@ -367,6 +367,9 @@ func (n *IpfsNode) Close() error {
|
||||
|
||||
// Context returns the IpfsNode context
|
||||
func (n *IpfsNode) Context() context.Context {
|
||||
if n.ctx == nil {
|
||||
n.ctx = context.TODO()
|
||||
}
|
||||
return n.ctx
|
||||
}
|
||||
|
||||
|
@ -66,8 +66,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
|
||||
}
|
||||
|
||||
n.Pinning.GetManual().RemovePinWithMode(k, pin.Indirect)
|
||||
err = n.Pinning.Flush()
|
||||
if err != nil {
|
||||
if err := n.Pinning.Flush(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@ -95,43 +94,28 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *merkle
|
||||
func add(n *core.IpfsNode, reader io.Reader) (*merkledag.Node, error) {
|
||||
mp := n.Pinning.GetManual()
|
||||
|
||||
node, err := importer.BuildDagFromReader(
|
||||
return importer.BuildDagFromReader(
|
||||
n.DAG,
|
||||
chunk.DefaultSplitter(reader),
|
||||
importer.PinIndirectCB(mp),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func addNode(n *core.IpfsNode, node *merkledag.Node) error {
|
||||
err := n.DAG.AddRecursive(node) // add the file to the graph + local storage
|
||||
if err != nil {
|
||||
if err := n.DAG.AddRecursive(node); err != nil { // add the file to the graph + local storage
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
||||
ctx, cancel := context.WithTimeout(n.Context(), time.Minute)
|
||||
defer cancel()
|
||||
err = n.Pinning.Pin(ctx, node, true) // ensure we keep it
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
err := n.Pinning.Pin(ctx, node, true) // ensure we keep it
|
||||
return err
|
||||
}
|
||||
|
||||
func addFile(n *core.IpfsNode, file files.File) (*merkledag.Node, error) {
|
||||
if file.IsDirectory() {
|
||||
return addDir(n, file)
|
||||
}
|
||||
|
||||
dagnode, err := add(n, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dagnode, nil
|
||||
return add(n, file)
|
||||
}
|
||||
|
||||
func addDir(n *core.IpfsNode, dir files.File) (*merkledag.Node, error) {
|
||||
@ -155,14 +139,12 @@ Loop:
|
||||
|
||||
_, name := gopath.Split(file.FileName())
|
||||
|
||||
err = tree.AddNodeLink(name, node)
|
||||
if err != nil {
|
||||
if err := tree.AddNodeLink(name, node); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
err := addNode(n, tree)
|
||||
if err != nil {
|
||||
if err := addNode(n, tree); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tree, nil
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) {
|
||||
ukey := key.B58KeyDecode(skey)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
||||
ctx, cancel := context.WithTimeout(n.Context(), time.Minute)
|
||||
defer cancel()
|
||||
nd, err := n.DAG.Get(ctx, ukey)
|
||||
if err != nil {
|
||||
@ -44,7 +44,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error
|
||||
func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) {
|
||||
ukey := key.B58KeyDecode(skey)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
||||
ctx, cancel := context.WithTimeout(n.Context(), time.Minute)
|
||||
defer cancel()
|
||||
nd, err := n.DAG.Get(ctx, ukey)
|
||||
if err != nil {
|
||||
|
@ -22,7 +22,7 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
||||
ctx, cancel := context.WithTimeout(n.Context(), time.Minute)
|
||||
defer cancel()
|
||||
|
||||
err = n.Pinning.Pin(ctx, emptyDir, false)
|
||||
|
@ -108,8 +108,7 @@ func TrickleAppend(base *dag.Node, db *h.DagBuilderHelper) (out *dag.Node, err_o
|
||||
}
|
||||
|
||||
// Last child in this node may not be a full tree, lets file it up
|
||||
err = appendFillLastChild(ufsn, n-1, layerProgress, db)
|
||||
if err != nil {
|
||||
if err := appendFillLastChild(ufsn, n-1, layerProgress, db); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -26,12 +26,14 @@ type Directory struct {
|
||||
|
||||
lock sync.Mutex
|
||||
node *dag.Node
|
||||
ctx context.Context
|
||||
|
||||
name string
|
||||
}
|
||||
|
||||
func NewDirectory(name string, node *dag.Node, parent childCloser, fs *Filesystem) *Directory {
|
||||
func NewDirectory(ctx context.Context, name string, node *dag.Node, parent childCloser, fs *Filesystem) *Directory {
|
||||
return &Directory{
|
||||
ctx: ctx,
|
||||
fs: fs,
|
||||
name: name,
|
||||
node: node,
|
||||
@ -121,7 +123,7 @@ func (d *Directory) childDir(name string) (*Directory, error) {
|
||||
|
||||
switch i.GetType() {
|
||||
case ufspb.Data_Directory:
|
||||
ndir := NewDirectory(name, nd, d, d.fs)
|
||||
ndir := NewDirectory(d.ctx, name, nd, d, d.fs)
|
||||
d.childDirs[name] = ndir
|
||||
return ndir, nil
|
||||
case ufspb.Data_File:
|
||||
@ -138,7 +140,7 @@ func (d *Directory) childDir(name string) (*Directory, error) {
|
||||
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
|
||||
for _, lnk := range d.node.Links {
|
||||
if lnk.Name == name {
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
||||
ctx, cancel := context.WithTimeout(d.ctx, time.Minute)
|
||||
defer cancel()
|
||||
|
||||
return lnk.GetNode(ctx, d.fs.dserv)
|
||||
@ -244,7 +246,7 @@ func (d *Directory) AddChild(name string, nd *dag.Node) error {
|
||||
|
||||
switch pbn.GetType() {
|
||||
case ft.TDirectory:
|
||||
d.childDirs[name] = NewDirectory(name, nd, d, d.fs)
|
||||
d.childDirs[name] = NewDirectory(d.ctx, name, nd, d, d.fs)
|
||||
case ft.TFile, ft.TMetadata, ft.TRaw:
|
||||
nfi, err := NewFile(name, nd, d, d.fs)
|
||||
if err != nil {
|
||||
|
@ -186,7 +186,7 @@ func (fs *Filesystem) newKeyRoot(parent context.Context, k ci.PrivKey) (*KeyRoot
|
||||
|
||||
switch pbn.GetType() {
|
||||
case ft.TDirectory:
|
||||
root.val = NewDirectory(pointsTo.String(), mnode, root, fs)
|
||||
root.val = NewDirectory(ctx, pointsTo.String(), mnode, root, fs)
|
||||
case ft.TFile, ft.TMetadata, ft.TRaw:
|
||||
fi, err := NewFile(pointsTo.String(), mnode, root, fs)
|
||||
if err != nil {
|
||||
|
@ -48,8 +48,7 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
|
||||
// ensure no link with that name already exists
|
||||
_ = root.RemoveNodeLink(childname) // ignore error, only option is ErrNotFound
|
||||
|
||||
err = root.AddNodeLinkClean(childname, childnd)
|
||||
if err != nil {
|
||||
if err := root.AddNodeLinkClean(childname, childnd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package io
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
|
||||
key "github.com/ipfs/go-ipfs/blocks/key"
|
||||
@ -29,22 +27,13 @@ func NewDirectory(dserv mdag.DAGService) *directoryBuilder {
|
||||
}
|
||||
|
||||
// AddChild adds a (name, key)-pair to the root node.
|
||||
func (d *directoryBuilder) AddChild(name string, k key.Key) error {
|
||||
// TODO(cryptix): consolidate context managment
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
||||
defer cancel()
|
||||
|
||||
func (d *directoryBuilder) AddChild(ctx context.Context, name string, k key.Key) error {
|
||||
cnode, err := d.dserv.Get(ctx, k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = d.dirnode.AddNodeLinkClean(name, cnode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return d.dirnode.AddNodeLinkClean(name, cnode)
|
||||
}
|
||||
|
||||
// GetNode returns the root of this directoryBuilder
|
||||
|
@ -428,7 +428,7 @@ func (dm *DagModifier) Truncate(size int64) error {
|
||||
return dm.expandSparse(int64(size) - realSize)
|
||||
}
|
||||
|
||||
nnode, err := dagTruncate(dm.curNode, uint64(size), dm.dagserv)
|
||||
nnode, err := dagTruncate(dm.ctx, dm.curNode, uint64(size), dm.dagserv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -443,7 +443,7 @@ func (dm *DagModifier) Truncate(size int64) error {
|
||||
}
|
||||
|
||||
// dagTruncate truncates the given node to 'size' and returns the modified Node
|
||||
func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, error) {
|
||||
func dagTruncate(ctx context.Context, nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, error) {
|
||||
if len(nd.Links) == 0 {
|
||||
// TODO: this can likely be done without marshaling and remarshaling
|
||||
pbn, err := ft.FromBytes(nd.Data)
|
||||
@ -460,7 +460,7 @@ func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, er
|
||||
var modified *mdag.Node
|
||||
ndata := new(ft.FSNode)
|
||||
for i, lnk := range nd.Links {
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
||||
_ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
||||
defer cancel()
|
||||
|
||||
child, err := lnk.GetNode(ctx, ds)
|
||||
@ -475,7 +475,7 @@ func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, er
|
||||
|
||||
// found the child we want to cut
|
||||
if size < cur+childsize {
|
||||
nchild, err := dagTruncate(child, size-cur, ds)
|
||||
nchild, err := dagTruncate(_ctx, child, size-cur, ds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user