From e633250c3830b985d170e77c84e043c5914b0c61 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 1 May 2015 16:32:40 +0200 Subject: [PATCH 1/5] http gw: remove unused interface --- core/corehttp/gateway_handler.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 78795b636b6..434b2ed80e1 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -29,13 +29,6 @@ const ( IpnsPathPrefix = "/ipns/" ) -type gateway interface { - ResolvePath(string) (*dag.Node, error) - NewDagFromReader(io.Reader) (*dag.Node, error) - AddNodeToDAG(nd *dag.Node) (u.Key, error) - NewDagReader(nd *dag.Node) (uio.ReadSeekCloser, error) -} - // shortcut for templating type webHandler map[string]interface{} From 1502f6bc71ba8fa48fcc4d1959f405f038d04d9a Mon Sep 17 00:00:00 2001 From: Henry Date: Sun, 3 May 2015 05:04:05 +0200 Subject: [PATCH 2/5] http gw: removed ResolvePath() in favour of core.Resolve() --- core/corehttp/gateway_handler.go | 102 ++++++++++++------------------- 1 file changed, 39 insertions(+), 63 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 434b2ed80e1..ebc77f7f70a 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -69,39 +69,8 @@ func (i *gatewayHandler) loadTemplate() error { return nil } -func (i *gatewayHandler) resolveNamePath(ctx context.Context, p string) (string, error) { - p = gopath.Clean(p) - - if strings.HasPrefix(p, IpnsPathPrefix) { - elements := strings.Split(p[len(IpnsPathPrefix):], "/") - hash := elements[0] - rp, err := i.node.Namesys.Resolve(ctx, hash) - if err != nil { - return "", err - } - - elements = append(rp.Segments(), elements[1:]...) - p = gopath.Join(elements...) - } - if !strings.HasPrefix(p, IpfsPathPrefix) { - p = gopath.Join(IpfsPathPrefix, p) - } - return p, nil -} - -func (i *gatewayHandler) ResolvePath(ctx context.Context, p string) (*dag.Node, string, error) { - p, err := i.resolveNamePath(ctx, p) - if err != nil { - return nil, "", err - } - - node, err := i.node.Resolver.ResolvePath(ctx, path.Path(p)) - if err != nil { - return nil, "", err - } - return node, p, err -} +// TODO(cryptix): these four helper funcs shoudl also be available elsewhere, i think? func (i *gatewayHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) { return importer.BuildDagFromReader( r, i.node.DAG, i.node.Pinning.GetManual(), chunk.DefaultSplitter) @@ -119,30 +88,23 @@ func (i *gatewayHandler) NewDagReader(nd *dag.Node) (uio.ReadSeekCloser, error) return uio.NewDagReader(i.node.Context(), nd, i.node.DAG) } -// TODO(btc): break this apart into separate handlers using a more expressive -// muxer +// TODO(btc): break this apart into separate handlers using a more expressive muxer func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if i.config.Writable && r.Method == "POST" { - i.postHandler(w, r) - return - } - - if i.config.Writable && r.Method == "PUT" { - i.putHandler(w, r) - return - } - - if i.config.Writable && r.Method == "DELETE" { - i.deleteHandler(w, r) - return - } - - if r.Method == "GET" { - i.getOrHeadHandler(w, r) - return + if i.config.Writable { + switch r.Method { + case "POST": + i.postHandler(w, r) + return + case "PUT": + i.putHandler(w, r) + return + case "DELETE": + i.deleteHandler(w, r) + return + } } - if r.Method == "HEAD" { + if r.Method == "GET" || r.Method == "HEAD" { i.getOrHeadHandler(w, r) return } @@ -156,7 +118,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { errmsg = errmsg + "bad request for " + r.URL.Path } w.Write([]byte(errmsg)) - log.Debug(errmsg) + log.Error(errmsg) // TODO(cryptix): Why are we ignoring handler errors? } func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) { @@ -171,19 +133,19 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } - nd, p, err := i.ResolvePath(ctx, urlPath) + nd, err := core.Resolve(ctx, i.node, path.Path(urlPath)) if err != nil { webError(w, "Path Resolve error", err, http.StatusBadRequest) return } - etag := gopath.Base(p) + etag := gopath.Base(urlPath) if r.Header.Get("If-None-Match") == etag { w.WriteHeader(http.StatusNotModified) return } - w.Header().Set("X-IPFS-Path", p) + w.Header().Set("X-IPFS-Path", urlPath) // Suborigin header, sandboxes apps from each other in the browser (even // though they are served from the same gateway domain). NOTE: This is not @@ -232,7 +194,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request log.Debug("found index") foundIndex = true // return index page instead. - nd, _, err := i.ResolvePath(ctx, urlPath+"/index.html") + nd, err := core.Resolve(ctx, i.node, path.Path(urlPath+"/index.html")) if err != nil { internalWebError(w, err) return @@ -328,14 +290,20 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(i.node.Context()) defer cancel() - ipfspath, err := i.resolveNamePath(ctx, urlPath) + ipfsNode, err := core.Resolve(ctx, i.node, path.Path(urlPath)) if err != nil { // FIXME HTTP error code webError(w, "Could not resolve name", err, http.StatusInternalServerError) return } - h, components, err := path.SplitAbsPath(path.Path(ipfspath)) + k, err := ipfsNode.Key() + if err != nil { + webError(w, "Could not get key from resolved node", err, http.StatusInternalServerError) + return + } + + h, components, err := path.SplitAbsPath(path.FromKey(k)) if err != nil { webError(w, "Could not split path", err, http.StatusInternalServerError) return @@ -351,6 +319,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { tctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() + // TODO(cryptix): could this be core.Resolve() too? rootnd, err := i.node.Resolver.DAG.Get(tctx, u.Key(h)) if err != nil { webError(w, "Could not resolve root object", err, http.StatusBadRequest) @@ -400,14 +369,20 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(i.node.Context()) defer cancel() - ipfspath, err := i.resolveNamePath(ctx, urlPath) + ipfsNode, err := core.Resolve(ctx, i.node, path.Path(urlPath)) if err != nil { // FIXME HTTP error code webError(w, "Could not resolve name", err, http.StatusInternalServerError) return } - h, components, err := path.SplitAbsPath(path.Path(ipfspath)) + k, err := ipfsNode.Key() + if err != nil { + webError(w, "Could not get key from resolved node", err, http.StatusInternalServerError) + return + } + + h, components, err := path.SplitAbsPath(path.FromKey(k)) if err != nil { webError(w, "Could not split path", err, http.StatusInternalServerError) return @@ -427,6 +402,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { return } + // TODO(cyrptix): assumes len(path_nodes) > 1 - not found is an error above? err = path_nodes[len(path_nodes)-1].RemoveNodeLink(components[len(components)-1]) if err != nil { webError(w, "Could not delete link", err, http.StatusBadRequest) @@ -481,7 +457,7 @@ func webErrorWithCode(w http.ResponseWriter, message string, err error, code int func internalWebError(w http.ResponseWriter, err error) { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) - log.Debug("%s", err) + log.Error("%s", err) // TODO(cryptix): Why are we ignoring handler errors? } // Directory listing template From 96846358cc0d1ed383cfa829dd2178ebf2bc4c58 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 4 May 2015 13:39:30 +0200 Subject: [PATCH 3/5] http gw: some golinting and unexport unused symbols - NewDagReader() used the wrong context - Ip?sPathPrefix isn't used anywhere - a little bit of error handling cleanup --- core/corehttp/gateway_handler.go | 83 ++++++++++++++------------------ 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index ebc77f7f70a..2999494b178 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -25,8 +25,8 @@ import ( ) const ( - IpfsPathPrefix = "/ipfs/" - IpnsPathPrefix = "/ipns/" + ipfsPathPrefix = "/ipfs/" + ipnsPathPrefix = "/ipns/" ) // shortcut for templating @@ -69,25 +69,16 @@ func (i *gatewayHandler) loadTemplate() error { return nil } - -// TODO(cryptix): these four helper funcs shoudl also be available elsewhere, i think? -func (i *gatewayHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) { +// TODO(cryptix): find these helpers somewhere else +func (i *gatewayHandler) newDagFromReader(r io.Reader) (*dag.Node, error) { return importer.BuildDagFromReader( r, i.node.DAG, i.node.Pinning.GetManual(), chunk.DefaultSplitter) } -func NewDagEmptyDir() *dag.Node { +func newDagEmptyDir() *dag.Node { return &dag.Node{Data: ufs.FolderPBData()} } -func (i *gatewayHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) { - return i.node.DAG.Add(nd) -} - -func (i *gatewayHandler) NewDagReader(nd *dag.Node) (uio.ReadSeekCloser, error) { - return uio.NewDagReader(i.node.Context(), nd, i.node.DAG) -} - // TODO(btc): break this apart into separate handlers using a more expressive muxer func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if i.config.Writable { @@ -117,8 +108,8 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) errmsg = errmsg + "bad request for " + r.URL.Path } - w.Write([]byte(errmsg)) - log.Error(errmsg) // TODO(cryptix): Why are we ignoring handler errors? + fmt.Fprint(w, errmsg) + log.Error(errmsg) // TODO(cryptix): log errors until we have a better way to expose these (counter metrics maybe) } func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) { @@ -153,7 +144,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request pathRoot := strings.SplitN(urlPath, "/", 4)[2] w.Header().Set("Suborigin", pathRoot) - dr, err := i.NewDagReader(nd) + dr, err := uio.NewDagReader(ctx, nd, i.node.DAG) if err != nil && err != uio.ErrIsDir { // not a directory and still an error internalWebError(w, err) @@ -165,7 +156,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // and only if it's /ipfs! // TODO: break this out when we split /ipfs /ipns routes. modtime := time.Now() - if strings.HasPrefix(urlPath, IpfsPathPrefix) { + if strings.HasPrefix(urlPath, ipfsPathPrefix) { w.Header().Set("Etag", etag) w.Header().Set("Cache-Control", "public, max-age=29030400") @@ -199,7 +190,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request internalWebError(w, err) return } - dr, err := i.NewDagReader(nd) + dr, err := uio.NewDagReader(ctx, nd, i.node.DAG) if err != nil { internalWebError(w, err) return @@ -234,13 +225,13 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { - nd, err := i.NewDagFromReader(r.Body) + nd, err := i.newDagFromReader(r.Body) if err != nil { internalWebError(w, err) return } - k, err := i.AddNodeToDAG(nd) + k, err := i.node.DAG.Add(nd) if err != nil { internalWebError(w, err) return @@ -248,11 +239,11 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { h := mh.Multihash(k).B58String() w.Header().Set("IPFS-Hash", h) - http.Redirect(w, r, IpfsPathPrefix+h, http.StatusCreated) + http.Redirect(w, r, ipfsPathPrefix+h, http.StatusCreated) } func (i *gatewayHandler) putEmptyDirHandler(w http.ResponseWriter, r *http.Request) { - newnode := NewDagEmptyDir() + newnode := newDagEmptyDir() key, err := i.node.DAG.Add(newnode) if err != nil { @@ -261,7 +252,7 @@ func (i *gatewayHandler) putEmptyDirHandler(w http.ResponseWriter, r *http.Reque } w.Header().Set("IPFS-Hash", key.String()) - http.Redirect(w, r, IpfsPathPrefix+key.String()+"/", http.StatusCreated) + http.Redirect(w, r, ipfsPathPrefix+key.String()+"/", http.StatusCreated) } func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { @@ -271,16 +262,16 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { urlPath := r.URL.Path pathext := urlPath[5:] var err error - if urlPath == IpfsPathPrefix+"QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" { + if urlPath == ipfsPathPrefix+"QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/" { i.putEmptyDirHandler(w, r) return } var newnode *dag.Node if pathext[len(pathext)-1] == '/' { - newnode = NewDagEmptyDir() + newnode = newDagEmptyDir() } else { - newnode, err = i.NewDagFromReader(r.Body) + newnode, err = i.newDagFromReader(r.Body) if err != nil { webError(w, "Could not create DAG from request", err, http.StatusInternalServerError) return @@ -311,9 +302,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { if len(components) < 1 { err = fmt.Errorf("Cannot override existing object") - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) - log.Debug("%s", err) + webError(w, "http gateway", err, http.StatusBadRequest) return } @@ -328,19 +317,19 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { // resolving path components into merkledag nodes. if a component does not // resolve, create empty directories (which will be linked and populated below.) - path_nodes, err := i.node.Resolver.ResolveLinks(tctx, rootnd, components[:len(components)-1]) + pathNodes, err := i.node.Resolver.ResolveLinks(tctx, rootnd, components[:len(components)-1]) if _, ok := err.(path.ErrNoLink); ok { // Create empty directories, links will be made further down the code - for len(path_nodes) < len(components) { - path_nodes = append(path_nodes, NewDagEmptyDir()) + for len(pathNodes) < len(components) { + pathNodes = append(pathNodes, newDagEmptyDir()) } } else if err != nil { webError(w, "Could not resolve parent object", err, http.StatusBadRequest) return } - for i := len(path_nodes) - 1; i >= 0; i-- { - newnode, err = path_nodes[i].UpdateNodeLink(components[i], newnode) + for i := len(pathNodes) - 1; i >= 0; i-- { + newnode, err = pathNodes[i].UpdateNodeLink(components[i], newnode) if err != nil { webError(w, "Could not update node links", err, http.StatusInternalServerError) return @@ -361,7 +350,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { } w.Header().Set("IPFS-Hash", key.String()) - http.Redirect(w, r, IpfsPathPrefix+key.String()+"/"+strings.Join(components, "/"), http.StatusCreated) + http.Redirect(w, r, ipfsPathPrefix+key.String()+"/"+strings.Join(components, "/"), http.StatusCreated) } func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { @@ -396,22 +385,22 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { return } - path_nodes, err := i.node.Resolver.ResolveLinks(tctx, rootnd, components[:len(components)-1]) + pathNodes, err := i.node.Resolver.ResolveLinks(tctx, rootnd, components[:len(components)-1]) if err != nil { webError(w, "Could not resolve parent object", err, http.StatusBadRequest) return } - // TODO(cyrptix): assumes len(path_nodes) > 1 - not found is an error above? - err = path_nodes[len(path_nodes)-1].RemoveNodeLink(components[len(components)-1]) + // TODO(cyrptix): assumes len(pathNodes) > 1 - not found is an error above? + err = pathNodes[len(pathNodes)-1].RemoveNodeLink(components[len(components)-1]) if err != nil { webError(w, "Could not delete link", err, http.StatusBadRequest) return } - newnode := path_nodes[len(path_nodes)-1] - for i := len(path_nodes) - 2; i >= 0; i-- { - newnode, err = path_nodes[i].UpdateNodeLink(components[i], newnode) + newnode := pathNodes[len(pathNodes)-1] + for i := len(pathNodes) - 2; i >= 0; i-- { + newnode, err = pathNodes[i].UpdateNodeLink(components[i], newnode) if err != nil { webError(w, "Could not update node links", err, http.StatusInternalServerError) return @@ -432,7 +421,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { } w.Header().Set("IPFS-Hash", key.String()) - http.Redirect(w, r, IpfsPathPrefix+key.String()+"/"+strings.Join(components[:len(components)-1], "/"), http.StatusCreated) + http.Redirect(w, r, ipfsPathPrefix+key.String()+"/"+strings.Join(components[:len(components)-1], "/"), http.StatusCreated) } func webError(w http.ResponseWriter, message string, err error, defaultCode int) { @@ -449,15 +438,13 @@ func webError(w http.ResponseWriter, message string, err error, defaultCode int) func webErrorWithCode(w http.ResponseWriter, message string, err error, code int) { w.WriteHeader(code) - log.Debugf("%s: %s", message, err) - w.Write([]byte(message + ": " + err.Error())) + log.Errorf("%s: %s", message, err) // TODO(cryptix): log errors until we have a better way to expose these (counter metrics maybe) + fmt.Fprintf(w, "%s: %s", message, err) } // return a 500 error and log func internalWebError(w http.ResponseWriter, err error) { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - log.Error("%s", err) // TODO(cryptix): Why are we ignoring handler errors? + webErrorWithCode(w, "internalWebError", err, http.StatusInternalServerError) } // Directory listing template From 31b83abfe4d12b89229d2d0610cd0731ff5f5653 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 4 May 2015 14:02:04 +0200 Subject: [PATCH 4/5] http gw: remove newDagEmptyDir helper --- core/corehttp/gateway_handler.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 2999494b178..31ead923979 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -19,7 +19,6 @@ import ( dag "github.com/ipfs/go-ipfs/merkledag" path "github.com/ipfs/go-ipfs/path" "github.com/ipfs/go-ipfs/routing" - ufs "github.com/ipfs/go-ipfs/unixfs" uio "github.com/ipfs/go-ipfs/unixfs/io" u "github.com/ipfs/go-ipfs/util" ) @@ -71,14 +70,12 @@ func (i *gatewayHandler) loadTemplate() error { // TODO(cryptix): find these helpers somewhere else func (i *gatewayHandler) newDagFromReader(r io.Reader) (*dag.Node, error) { + // TODO(cryptix): change and remove this helper once PR1136 is merged + // return ufs.AddFromReader(i.node, r.Body) return importer.BuildDagFromReader( r, i.node.DAG, i.node.Pinning.GetManual(), chunk.DefaultSplitter) } -func newDagEmptyDir() *dag.Node { - return &dag.Node{Data: ufs.FolderPBData()} -} - // TODO(btc): break this apart into separate handlers using a more expressive muxer func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if i.config.Writable { @@ -243,7 +240,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { } func (i *gatewayHandler) putEmptyDirHandler(w http.ResponseWriter, r *http.Request) { - newnode := newDagEmptyDir() + newnode := uio.NewDirectory(i.node.DAG).GetNode() key, err := i.node.DAG.Add(newnode) if err != nil { @@ -269,7 +266,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { var newnode *dag.Node if pathext[len(pathext)-1] == '/' { - newnode = newDagEmptyDir() + newnode = uio.NewDirectory(i.node.DAG).GetNode() } else { newnode, err = i.newDagFromReader(r.Body) if err != nil { @@ -321,7 +318,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { if _, ok := err.(path.ErrNoLink); ok { // Create empty directories, links will be made further down the code for len(pathNodes) < len(components) { - pathNodes = append(pathNodes, newDagEmptyDir()) + pathNodes = append(pathNodes, uio.NewDirectory(i.node.DAG).GetNode()) } } else if err != nil { webError(w, "Could not resolve parent object", err, http.StatusBadRequest) From 87ce7abe47a8fa24a23f01e71fd172cd2f05d1d5 Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 9 May 2015 11:49:02 +0200 Subject: [PATCH 5/5] unixfs/io: added NewEmptyDirectory() some golinting along the way --- core/corehttp/gateway_handler.go | 4 ++-- unixfs/io/dirbuilder.go | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 31ead923979..8a28e30938a 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -240,7 +240,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { } func (i *gatewayHandler) putEmptyDirHandler(w http.ResponseWriter, r *http.Request) { - newnode := uio.NewDirectory(i.node.DAG).GetNode() + newnode := uio.NewEmptyDirectory() key, err := i.node.DAG.Add(newnode) if err != nil { @@ -266,7 +266,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { var newnode *dag.Node if pathext[len(pathext)-1] == '/' { - newnode = uio.NewDirectory(i.node.DAG).GetNode() + newnode = uio.NewEmptyDirectory() } else { newnode, err = i.newDagFromReader(r.Body) if err != nil { diff --git a/unixfs/io/dirbuilder.go b/unixfs/io/dirbuilder.go index b30d9ea3ae4..ef74f3de03d 100644 --- a/unixfs/io/dirbuilder.go +++ b/unixfs/io/dirbuilder.go @@ -15,15 +15,22 @@ type directoryBuilder struct { dirnode *mdag.Node } +// NewEmptyDirectory returns an empty merkledag Node with a folder Data chunk +func NewEmptyDirectory() *mdag.Node { + return &mdag.Node{Data: format.FolderPBData()} +} + +// NewDirectory returns a directoryBuilder. It needs a DAGService to add the Children func NewDirectory(dserv mdag.DAGService) *directoryBuilder { db := new(directoryBuilder) db.dserv = dserv - db.dirnode = new(mdag.Node) - db.dirnode.Data = format.FolderPBData() + db.dirnode = NewEmptyDirectory() return db } +// AddChild adds a (name, key)-pair to the root node. func (d *directoryBuilder) AddChild(name string, k u.Key) error { + // TODO(cryptix): consolidate context managment ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) defer cancel() @@ -40,6 +47,7 @@ func (d *directoryBuilder) AddChild(name string, k u.Key) error { return nil } +// GetNode returns the root of this directoryBuilder func (d *directoryBuilder) GetNode() *mdag.Node { return d.dirnode }