Skip to content

Commit 1ae6986

Browse files
authored
Merge pull request #6602 from ipfs/feat/coreapi-mount
mount: switch over to the CoreAPI
2 parents 12892ff + 97bc89d commit 1ae6986

File tree

6 files changed

+56
-125
lines changed

6 files changed

+56
-125
lines changed

core/commands/tar.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import (
55
"io"
66

77
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
8-
"github.com/ipfs/go-ipfs/namesys/resolve"
98
tar "github.com/ipfs/go-ipfs/tar"
109

1110
"github.com/ipfs/go-ipfs-cmds"
1211
dag "github.com/ipfs/go-merkledag"
13-
"github.com/ipfs/go-path"
12+
path "github.com/ipfs/interface-go-ipfs-core/path"
1413
)
1514

1615
var TarCmd = &cmds.Command{
@@ -37,7 +36,7 @@ represent it.
3736
cmds.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
3837
},
3938
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
40-
nd, err := cmdenv.GetNode(env)
39+
api, err := cmdenv.GetApi(env, req)
4140
if err != nil {
4241
return err
4342
}
@@ -53,7 +52,7 @@ represent it.
5352
return err
5453
}
5554

56-
node, err := tar.ImportTar(req.Context, file, nd.DAG)
55+
node, err := tar.ImportTar(req.Context, file, api.Dag())
5756
if err != nil {
5857
return err
5958
}
@@ -86,17 +85,12 @@ var tarCatCmd = &cmds.Command{
8685
cmds.StringArg("path", true, false, "ipfs path of archive to export.").EnableStdin(),
8786
},
8887
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
89-
nd, err := cmdenv.GetNode(env)
88+
api, err := cmdenv.GetApi(env, req)
9089
if err != nil {
9190
return err
9291
}
9392

94-
p, err := path.ParsePath(req.Arguments[0])
95-
if err != nil {
96-
return err
97-
}
98-
99-
root, err := resolve.Resolve(req.Context, nd.Namesys, nd.Resolver, p)
93+
root, err := api.ResolveNode(req.Context, path.New(req.Arguments[0]))
10094
if err != nil {
10195
return err
10296
}
@@ -106,7 +100,7 @@ var tarCatCmd = &cmds.Command{
106100
return dag.ErrNotProtobuf
107101
}
108102

109-
r, err := tar.ExportTar(req.Context, rootpb, nd.DAG)
103+
r, err := tar.ExportTar(req.Context, rootpb, api.Dag())
110104
if err != nil {
111105
return err
112106
}

fuse/ipns/ipns_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"bazil.org/fuse"
1717

1818
core "github.com/ipfs/go-ipfs/core"
19+
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
1920

2021
fstest "bazil.org/fuse/fs/fstestutil"
2122
racedet "github.com/ipfs/go-detect-race"
@@ -115,7 +116,12 @@ func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *mountWra
115116
}
116117
}
117118

118-
fs, err := NewFileSystem(node, node.PrivateKey, "", "")
119+
coreApi, err := coreapi.NewCoreAPI(node)
120+
if err != nil {
121+
t.Fatal(err)
122+
}
123+
124+
fs, err := NewFileSystem(node.Context(), coreApi, "", "")
119125
if err != nil {
120126
t.Fatal(err)
121127
}

fuse/ipns/ipns_unix.go

+36-64
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,19 @@ import (
1010
"fmt"
1111
"io"
1212
"os"
13-
14-
core "github.com/ipfs/go-ipfs/core"
15-
namesys "github.com/ipfs/go-ipfs/namesys"
16-
resolve "github.com/ipfs/go-ipfs/namesys/resolve"
13+
"strings"
1714

1815
dag "github.com/ipfs/go-merkledag"
19-
path "github.com/ipfs/go-path"
2016
ft "github.com/ipfs/go-unixfs"
17+
path "github.com/ipfs/interface-go-ipfs-core/path"
2118

2219
fuse "bazil.org/fuse"
2320
fs "bazil.org/fuse/fs"
2421
cid "github.com/ipfs/go-cid"
2522
logging "github.com/ipfs/go-log"
2623
mfs "github.com/ipfs/go-mfs"
27-
ci "github.com/libp2p/go-libp2p-core/crypto"
28-
peer "github.com/libp2p/go-libp2p-core/peer"
24+
iface "github.com/ipfs/interface-go-ipfs-core"
25+
options "github.com/ipfs/interface-go-ipfs-core/options"
2926
)
3027

3128
func init() {
@@ -40,17 +37,17 @@ var log = logging.Logger("fuse/ipns")
4037

4138
// FileSystem is the readwrite IPNS Fuse Filesystem.
4239
type FileSystem struct {
43-
Ipfs *core.IpfsNode
40+
Ipfs iface.CoreAPI
4441
RootNode *Root
4542
}
4643

4744
// NewFileSystem constructs new fs using given core.IpfsNode instance.
48-
func NewFileSystem(ipfs *core.IpfsNode, sk ci.PrivKey, ipfspath, ipnspath string) (*FileSystem, error) {
49-
50-
kmap := map[string]ci.PrivKey{
51-
"local": sk,
45+
func NewFileSystem(ctx context.Context, ipfs iface.CoreAPI, ipfspath, ipnspath string) (*FileSystem, error) {
46+
key, err := ipfs.Key().Self(ctx)
47+
if err != nil {
48+
return nil, err
5249
}
53-
root, err := CreateRoot(ipfs, kmap, ipfspath, ipnspath)
50+
root, err := CreateRoot(ctx, ipfs, map[string]iface.Key{"local": key}, ipfspath, ipnspath)
5451
if err != nil {
5552
return nil, err
5653
}
@@ -73,80 +70,62 @@ func (f *FileSystem) Destroy() {
7370

7471
// Root is the root object of the filesystem tree.
7572
type Root struct {
76-
Ipfs *core.IpfsNode
77-
Keys map[string]ci.PrivKey
73+
Ipfs iface.CoreAPI
74+
Keys map[string]iface.Key
7875

7976
// Used for symlinking into ipfs
8077
IpfsRoot string
8178
IpnsRoot string
8279
LocalDirs map[string]fs.Node
83-
Roots map[string]*keyRoot
80+
Roots map[string]*mfs.Root
8481

8582
LocalLinks map[string]*Link
8683
}
8784

88-
func ipnsPubFunc(ipfs *core.IpfsNode, k ci.PrivKey) mfs.PubFunc {
85+
func ipnsPubFunc(ipfs iface.CoreAPI, key iface.Key) mfs.PubFunc {
8986
return func(ctx context.Context, c cid.Cid) error {
90-
return ipfs.Namesys.Publish(ctx, k, path.FromCid(c))
87+
_, err := ipfs.Name().Publish(ctx, path.IpfsPath(c), options.Name.Key(key.Name()))
88+
return err
9189
}
9290
}
9391

94-
func loadRoot(ctx context.Context, rt *keyRoot, ipfs *core.IpfsNode, name string) (fs.Node, error) {
95-
p, err := path.ParsePath("/ipns/" + name)
96-
if err != nil {
97-
log.Errorf("mkpath %s: %s", name, err)
98-
return nil, err
99-
}
100-
101-
node, err := resolve.Resolve(ctx, ipfs.Namesys, ipfs.Resolver, p)
92+
func loadRoot(ctx context.Context, ipfs iface.CoreAPI, key iface.Key) (*mfs.Root, fs.Node, error) {
93+
node, err := ipfs.ResolveNode(ctx, key.Path())
10294
switch err {
10395
case nil:
104-
case namesys.ErrResolveFailed:
96+
case iface.ErrResolveFailed:
10597
node = ft.EmptyDirNode()
10698
default:
107-
log.Errorf("looking up %s: %s", p, err)
108-
return nil, err
99+
log.Errorf("looking up %s: %s", key.Path(), err)
100+
return nil, nil, err
109101
}
110102

111103
pbnode, ok := node.(*dag.ProtoNode)
112104
if !ok {
113-
return nil, dag.ErrNotProtobuf
105+
return nil, nil, dag.ErrNotProtobuf
114106
}
115107

116-
root, err := mfs.NewRoot(ctx, ipfs.DAG, pbnode, ipnsPubFunc(ipfs, rt.k))
108+
root, err := mfs.NewRoot(ctx, ipfs.Dag(), pbnode, ipnsPubFunc(ipfs, key))
117109
if err != nil {
118-
return nil, err
110+
return nil, nil, err
119111
}
120112

121-
rt.root = root
122-
123-
return &Directory{dir: root.GetDirectory()}, nil
124-
}
125-
126-
type keyRoot struct {
127-
k ci.PrivKey
128-
alias string
129-
root *mfs.Root
113+
return root, &Directory{dir: root.GetDirectory()}, nil
130114
}
131115

132-
func CreateRoot(ipfs *core.IpfsNode, keys map[string]ci.PrivKey, ipfspath, ipnspath string) (*Root, error) {
116+
func CreateRoot(ctx context.Context, ipfs iface.CoreAPI, keys map[string]iface.Key, ipfspath, ipnspath string) (*Root, error) {
133117
ldirs := make(map[string]fs.Node)
134-
roots := make(map[string]*keyRoot)
118+
roots := make(map[string]*mfs.Root)
135119
links := make(map[string]*Link)
136120
for alias, k := range keys {
137-
pid, err := peer.IDFromPrivateKey(k)
121+
root, fsn, err := loadRoot(ctx, ipfs, k)
138122
if err != nil {
139123
return nil, err
140124
}
141-
name := pid.Pretty()
142125

143-
kr := &keyRoot{k: k, alias: alias}
144-
fsn, err := loadRoot(ipfs.Context(), kr, ipfs, name)
145-
if err != nil {
146-
return nil, err
147-
}
126+
name := k.ID().String()
148127

149-
roots[name] = kr
128+
roots[name] = root
150129
ldirs[name] = fsn
151130

152131
// set up alias symlink
@@ -199,25 +178,22 @@ func (s *Root) Lookup(ctx context.Context, name string) (fs.Node, error) {
199178

200179
// other links go through ipns resolution and are symlinked into the ipfs mountpoint
201180
ipnsName := "/ipns/" + name
202-
resolved, err := s.Ipfs.Namesys.Resolve(s.Ipfs.Context(), ipnsName)
181+
resolved, err := s.Ipfs.Name().Resolve(ctx, ipnsName)
203182
if err != nil {
204183
log.Warnf("ipns: namesys resolve error: %s", err)
205184
return nil, fuse.ENOENT
206185
}
207186

208-
segments := resolved.Segments()
209-
if segments[0] == "ipfs" {
210-
p := path.Join(resolved.Segments()[1:])
211-
return &Link{s.IpfsRoot + "/" + p}, nil
187+
if resolved.Namespace() != "ipfs" {
188+
return nil, errors.New("invalid path from ipns record")
212189
}
213190

214-
log.Error("Invalid path.Path: ", resolved)
215-
return nil, errors.New("invalid path from ipns record")
191+
return &Link{s.IpfsRoot + "/" + strings.TrimPrefix("/ipfs/", resolved.String())}, nil
216192
}
217193

218194
func (r *Root) Close() error {
219195
for _, mr := range r.Roots {
220-
err := mr.root.Close()
196+
err := mr.Close()
221197
if err != nil {
222198
return err
223199
}
@@ -241,12 +217,8 @@ func (r *Root) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
241217

242218
var listing []fuse.Dirent
243219
for alias, k := range r.Keys {
244-
pid, err := peer.IDFromPrivateKey(k)
245-
if err != nil {
246-
continue
247-
}
248220
ent := fuse.Dirent{
249-
Name: pid.Pretty(),
221+
Name: k.ID().Pretty(),
250222
Type: fuse.DT_Dir,
251223
}
252224
link := fuse.Dirent{

fuse/ipns/mount_unix.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@ package ipns
55

66
import (
77
core "github.com/ipfs/go-ipfs/core"
8+
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
89
mount "github.com/ipfs/go-ipfs/fuse/mount"
910
)
1011

1112
// Mount mounts ipns at a given location, and returns a mount.Mount instance.
1213
func Mount(ipfs *core.IpfsNode, ipnsmp, ipfsmp string) (mount.Mount, error) {
14+
coreApi, err := coreapi.NewCoreAPI(ipfs)
15+
if err != nil {
16+
return nil, err
17+
}
18+
1319
cfg, err := ipfs.Repo.Config()
1420
if err != nil {
1521
return nil, err
1622
}
1723

1824
allow_other := cfg.Mounts.FuseAllowOther
1925

20-
fsys, err := NewFileSystem(ipfs, ipfs.PrivateKey, ipfsmp, ipnsmp)
26+
fsys, err := NewFileSystem(ipfs.Context(), coreApi, ipfsmp, ipnsmp)
2127
if err != nil {
2228
return nil, err
2329
}

namesys/resolve/pathresolver_test.go

-32
This file was deleted.

namesys/resolve/resolve.go

-15
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"fmt"
77
"strings"
88

9-
"github.com/ipfs/go-ipld-format"
109
"github.com/ipfs/go-path"
11-
"github.com/ipfs/go-path/resolver"
1210

1311
"github.com/ipfs/go-ipfs/namesys"
1412
)
@@ -52,16 +50,3 @@ func ResolveIPNS(ctx context.Context, nsys namesys.NameSystem, p path.Path) (pat
5250
}
5351
return p, nil
5452
}
55-
56-
// Resolve resolves the given path by parsing out protocol-specific
57-
// entries (e.g. /ipns/<node-key>) and then going through the /ipfs/
58-
// entries and returning the final node.
59-
func Resolve(ctx context.Context, nsys namesys.NameSystem, r *resolver.Resolver, p path.Path) (format.Node, error) {
60-
p, err := ResolveIPNS(ctx, nsys, p)
61-
if err != nil {
62-
return nil, err
63-
}
64-
65-
// ok, we have an IPFS path now (or what we'll treat as one)
66-
return r.ResolvePath(ctx, p)
67-
}

0 commit comments

Comments
 (0)