// InitializeKeyspace sets the ipns record for the given key to // point to an empty directory. func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error { emptyDir := &mdag.Node{Data: ft.FolderPBData()} nodek, err := n.DAG.Add(emptyDir) if err != nil { return err } ctx, cancel := context.WithCancel(n.Context()) defer cancel() err = n.Pinning.Pin(ctx, emptyDir, false) if err != nil { return err } err = n.Pinning.Flush() if err != nil { return err } pub := nsys.NewRoutingPublisher(n.Routing) if err := pub.Publish(ctx, key, path.FromKey(nodek)); err != nil { return err } return nil }
func addDir(n *core.IpfsNode, dir files.File) (*merkledag.Node, error) { tree := &merkledag.Node{Data: unixfs.FolderPBData()} Loop: for { file, err := dir.NextFile() switch { case err != nil && err != io.EOF: return nil, err case err == io.EOF: break Loop } node, err := addFile(n, file) if err != nil { return nil, err } _, name := gopath.Split(file.FileName()) if err := tree.AddNodeLink(name, node); err != nil { return nil, err } } if err := addNode(n, tree); err != nil { return nil, err } return tree, nil }
func (d *Directory) Mkdir(name string) (*Directory, error) { d.lock.Lock() defer d.lock.Unlock() _, err := d.childDir(name) if err == nil { return nil, os.ErrExist } _, err = d.childFile(name) if err == nil { return nil, os.ErrExist } ndir := &dag.Node{Data: ft.FolderPBData()} err = d.node.AddNodeLinkClean(name, ndir) if err != nil { return nil, err } err = d.parent.closeChild(d.name, d.node) if err != nil { return nil, err } return d.childDir(name) }
// InitializeKeyspace sets the ipns record for the given key to // point to an empty directory. // TODO: this doesnt feel like it belongs here func InitializeKeyspace(ctx context.Context, ds dag.DAGService, pub Publisher, pins pin.Pinner, key ci.PrivKey) error { emptyDir := &dag.Node{Data: ft.FolderPBData()} nodek, err := ds.Add(emptyDir) if err != nil { return err } // pin recursively because this might already be pinned // and doing a direct pin would throw an error in that case err = pins.Pin(ctx, emptyDir, true) if err != nil { return err } err = pins.Flush() if err != nil { return err } err = pub.Publish(ctx, key, path.FromKey(nodek)) if err != nil { return err } return nil }
func nodeFromTemplate(template string) (*dag.Node, error) { switch template { case "unixfs-dir": nd := new(dag.Node) nd.Data = ft.FolderPBData() return nd, nil default: return nil, fmt.Errorf("template '%s' not found", template) } }
func (params *adder) addDir(file files.File) (*dag.Node, error) { tree := &dag.Node{Data: ft.FolderPBData()} log.Infof("adding directory: %s", file.FileName()) for { file, err := file.NextFile() if err != nil && err != io.EOF { return nil, err } if file == nil { break } node, err := params.addFile(file) if _, ok := err.(*hiddenFileError); ok { // hidden file error, set the node to nil for below node = nil } else if err != nil { return nil, err } if node != nil { _, name := path.Split(file.FileName()) err = tree.AddNodeLink(name, node) if err != nil { return nil, err } } } if err := params.addNode(tree, file.FileName()); err != nil { return nil, err } k, err := params.node.DAG.Add(tree) if err != nil { return nil, err } params.node.Pinning.GetManual().PinWithMode(k, pin.Indirect) return tree, nil }
func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) { if len(req.Arguments()) < 4 { return "", fmt.Errorf("not enough arguments for add-link") } nd, err := req.InvocContext().GetNode() if err != nil { return "", err } path := req.Arguments()[2] childk := key.B58KeyDecode(req.Arguments()[3]) create, _, err := req.Option("create").Bool() if err != nil { return "", err } var createfunc func() *dag.Node if create { createfunc = func() *dag.Node { return &dag.Node{Data: ft.FolderPBData()} } } e := dagutils.NewDagEditor(nd.DAG, root) childnd, err := nd.DAG.Get(req.Context(), childk) if err != nil { return "", err } err = e.InsertNodeAtPath(req.Context(), path, childnd, createfunc) if err != nil { return "", err } nnode := e.GetNode() return nnode.Key() }
// NewEmptyDirectory returns an empty merkledag Node with a folder Data chunk func NewEmptyDirectory() *mdag.Node { return &mdag.Node{Data: format.FolderPBData()} }
// TODO: generalize this to more than unix-fs nodes. func newDirNode() *dag.Node { return &dag.Node{Data: ft.FolderPBData()} }