Skip to content

Commit 2d048cb

Browse files
committed
shell/fsnode: Add a high-level filesystem-node package
Starting to use the core/... (low-level, all the knobs you need) shell/... (high-level, handle the common case easily) disctinction laid out in [1]. Creating a file node from a reader is hard to do right, because there's a lot of filesystem metadata that we don't have access to (access mode, ownership, etc.). You can guess at those based on the adding process's umask, effective user, etc., but figuring out what you want guessed at or what you want set explicitly, or whether you want wrapping metadata at all is complicated. This function isn't going to do any of that [2], it's just a high-level wrapper to create a minimal file object with the default chunking, pinning, etc. all taken care of in ways that will probably work for you ;). [1]: ipfs#1158 [2]: ipfs#1136 (comment)
1 parent c1a347d commit 2d048cb

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

shell/fsnode/add.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fsnode
2+
3+
import (
4+
"io"
5+
6+
core "github.com/ipfs/go-ipfs/core"
7+
importer "github.com/ipfs/go-ipfs/importer"
8+
chunk "github.com/ipfs/go-ipfs/importer/chunk"
9+
dag "github.com/ipfs/go-ipfs/merkledag"
10+
eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
11+
)
12+
13+
var log = eventlog.Logger("shell/fsnode")
14+
15+
// Add builds a merkledag from a reader, pinning all objects to the
16+
// local datastore. Returns the root node.
17+
func AddFromReader(node *core.IpfsNode, reader io.Reader) (*dag.Node, error) {
18+
fileNode, err := importer.BuildDagFromReader(
19+
reader,
20+
node.DAG,
21+
node.Pinning.GetManual(),
22+
chunk.DefaultSplitter,
23+
)
24+
if err != nil {
25+
return nil, err
26+
}
27+
if err := node.Pinning.Flush(); err != nil {
28+
return nil, err
29+
}
30+
return fileNode, nil
31+
}

shell/fsnode/fsnode.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Package fsnode is a high-level interface for filesystem nodes.
3+
4+
Simple wrappers to:
5+
6+
* create file and directory nodes from paths and readers,
7+
* extract file and directory nodes to your local filesytem, and
8+
* print file contents to writers.
9+
*/
10+
package fsnode

shell/shell.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Package shell implements a high-level interface for common IPFS activity.
3+
4+
These wrappers around the low-level core interface make it easy to
5+
accomplish common tasks with default settings. For steps where the
6+
defaults aren't appropriate, you can use the core package directly.
7+
*/
8+
package shell

0 commit comments

Comments
 (0)