Skip to content

Commit ec9c421

Browse files
committed
refactor to put an IpnsFilesystem into the core.IpfsNode for ease of closability
1 parent 58dbb4f commit ec9c421

File tree

9 files changed

+153
-73
lines changed

9 files changed

+153
-73
lines changed

cmd/ipfs/init.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
cmds "github.com/jbenet/go-ipfs/commands"
1111
core "github.com/jbenet/go-ipfs/core"
1212
coreunix "github.com/jbenet/go-ipfs/core/coreunix"
13-
ipnsfs "github.com/jbenet/go-ipfs/ipnsfs"
1413
config "github.com/jbenet/go-ipfs/repo/config"
1514
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
1615
uio "github.com/jbenet/go-ipfs/unixfs/io"
@@ -172,5 +171,5 @@ func initializeIpnsKeyspace(repoRoot string) error {
172171
return err
173172
}
174173

175-
return ipnsfs.InitializeKeyspace(nd, nd.PrivateKey)
174+
return nd.IpnsFs.InitializeKeyspace(ctx, nd.PrivateKey)
176175
}

core/core.go

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
rp "github.com/jbenet/go-ipfs/exchange/reprovide"
3838

3939
mount "github.com/jbenet/go-ipfs/fuse/mount"
40+
ipnsfs "github.com/jbenet/go-ipfs/ipnsfs"
4041
merkledag "github.com/jbenet/go-ipfs/merkledag"
4142
namesys "github.com/jbenet/go-ipfs/namesys"
4243
path "github.com/jbenet/go-ipfs/path"
@@ -89,6 +90,8 @@ type IpfsNode struct {
8990
Diagnostics *diag.Diagnostics // the diagnostics service
9091
Reprovider *rp.Reprovider // the value reprovider system
9192

93+
IpnsFs *ipnsfs.Filesystem
94+
9295
ctxgroup.ContextGroup
9396

9497
mode mode
@@ -268,6 +271,13 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
268271

269272
// setup name system
270273
n.Namesys = namesys.NewNameSystem(n.Routing)
274+
275+
// Setup the mutable ipns filesystem structure
276+
fs, err := ipnsfs.NewFilesystem(n.Context(), n.DAG, n.Namesys, n.Pinning, n.PrivateKey)
277+
if err != nil {
278+
return debugerror.Wrap(err)
279+
}
280+
n.IpnsFs = fs
271281
return nil
272282
}
273283

@@ -288,6 +298,10 @@ func (n *IpfsNode) teardown() error {
288298
}
289299
}
290300

301+
if n.IpnsFs != nil {
302+
addCloser(n.IpnsFs)
303+
}
304+
291305
addCloser(n.Bootstrapper)
292306
if dht, ok := n.Routing.(*dht.IpfsDHT); ok {
293307
addCloser(dht)

fuse/ipns/ipns_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package ipns
33
import (
44
"bytes"
55
"crypto/rand"
6-
//context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
76
"io/ioutil"
87
"os"
98
"testing"
109

1110
fstest "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs/fstestutil"
1211

12+
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
1313
core "github.com/jbenet/go-ipfs/core"
14-
//u "github.com/jbenet/go-ipfs/util"
14+
nsfs "github.com/jbenet/go-ipfs/ipnsfs"
1515
ci "github.com/jbenet/go-ipfs/util/testutil/ci"
1616
)
1717

@@ -103,6 +103,13 @@ func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *fstest.M
103103
if err != nil {
104104
t.Fatal(err)
105105
}
106+
107+
ipnsfs, err := nsfs.NewFilesystem(context.TODO(), node.DAG, node.Namesys, node.Pinning, node.PrivateKey)
108+
if err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
node.IpnsFs = ipnsfs
106113
}
107114

108115
fs, err := NewFileSystem(node, node.PrivateKey, "")

fuse/ipns/ipns_unix.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@ func NewFileSystem(ipfs *core.IpfsNode, sk ci.PrivKey, ipfspath string) (*FileSy
4747
}
4848

4949
// Root constructs the Root of the filesystem, a Root object.
50-
func (f FileSystem) Root() (fs.Node, error) {
50+
func (f *FileSystem) Root() (fs.Node, error) {
5151
log.Debug("Filesystem, get root")
5252
return f.RootNode, nil
5353
}
5454

55+
func (f *FileSystem) Destroy() {
56+
err := f.RootNode.Close()
57+
if err != nil {
58+
log.Errorf("Error Shutting Down Filesystem: %s\n", err)
59+
}
60+
}
61+
5562
// Root is the root object of the filesystem tree.
5663
type Root struct {
5764
Ipfs *core.IpfsNode
@@ -67,10 +74,6 @@ type Root struct {
6774
}
6875

6976
func CreateRoot(ipfs *core.IpfsNode, keys []ci.PrivKey, ipfspath string) (*Root, error) {
70-
fi, err := nsfs.NewFilesystem(ipfs, keys...)
71-
if err != nil {
72-
return nil, err
73-
}
7477
ldirs := make(map[string]fs.Node)
7578
roots := make(map[string]*nsfs.KeyRoot)
7679
for _, k := range keys {
@@ -79,7 +82,7 @@ func CreateRoot(ipfs *core.IpfsNode, keys []ci.PrivKey, ipfspath string) (*Root,
7982
return nil, err
8083
}
8184
name := u.Key(pkh).B58String()
82-
root, err := fi.GetRoot(name)
85+
root, err := ipfs.IpnsFs.GetRoot(name)
8386
if err != nil {
8487
return nil, err
8588
}
@@ -90,14 +93,14 @@ func CreateRoot(ipfs *core.IpfsNode, keys []ci.PrivKey, ipfspath string) (*Root,
9093
case *nsfs.Directory:
9194
ldirs[name] = &Directory{val}
9295
case nsfs.File:
93-
ldirs[name] = &File{val}
96+
ldirs[name] = &File{fi: val}
9497
default:
9598
return nil, errors.New("unrecognized type")
9699
}
97100
}
98101

99102
return &Root{
100-
fs: fi,
103+
fs: ipfs.IpnsFs,
101104
Ipfs: ipfs,
102105
IpfsRoot: ipfspath,
103106
Keys: keys,
@@ -149,6 +152,24 @@ func (s *Root) Lookup(ctx context.Context, name string) (fs.Node, error) {
149152
return &Link{s.IpfsRoot + "/" + resolved.B58String()}, nil
150153
}
151154

155+
func (r *Root) Close() error {
156+
for _, kr := range r.Roots {
157+
err := kr.Publish(r.Ipfs.Context())
158+
if err != nil {
159+
return err
160+
}
161+
}
162+
return nil
163+
}
164+
165+
// Forget is called when the filesystem is unmounted. probably.
166+
func (r *Root) Forget() {
167+
err := r.Close()
168+
if err != nil {
169+
log.Error(err)
170+
}
171+
}
172+
152173
// ReadDirAll reads a particular directory. Will show locally available keys
153174
func (r *Root) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
154175
log.Debug("Root ReadDirAll")
@@ -205,6 +226,8 @@ type Directory struct {
205226

206227
type File struct {
207228
fi nsfs.File
229+
230+
fs.NodeRef
208231
}
209232

210233
/*
@@ -245,7 +268,7 @@ func (s *Directory) Lookup(ctx context.Context, name string) (fs.Node, error) {
245268
case *nsfs.Directory:
246269
return &Directory{child}, nil
247270
case nsfs.File:
248-
return &File{child}, nil
271+
return &File{fi: child}, nil
249272
default:
250273
panic("system has proven to be insane")
251274
}
@@ -355,7 +378,7 @@ func (dir *Directory) Create(ctx context.Context, req *fuse.CreateRequest, resp
355378
return nil, nil, errors.New("child creation failed")
356379
}
357380

358-
nodechild := &File{fi}
381+
nodechild := &File{fi: fi}
359382
return nodechild, nodechild, nil
360383
}
361384

ipnsfs/dir.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
type Directory struct {
15-
dserv dag.DAGService
15+
fs *Filesystem
1616
parent childCloser
1717
childDirs map[string]*Directory
1818
files map[string]*file
@@ -25,9 +25,9 @@ type Directory struct {
2525
refLock sync.Mutex
2626
}
2727

28-
func NewDirectory(name string, node *dag.Node, parent childCloser, dserv dag.DAGService) *Directory {
28+
func NewDirectory(name string, node *dag.Node, parent childCloser, fs *Filesystem) *Directory {
2929
return &Directory{
30-
dserv: dserv,
30+
fs: fs,
3131
name: name,
3232
node: node,
3333
parent: parent,
@@ -49,7 +49,7 @@ func (d *Directory) Open(tpath []string, mode int) (File, error) {
4949
if mode|os.O_CREATE != 0 {
5050
fnode := new(dag.Node)
5151
fnode.Data = ft.FilePBData(nil, 0)
52-
nfi, err := NewFile(tpath[0], fnode, d, d.dserv)
52+
nfi, err := NewFile(tpath[0], fnode, d, d.fs)
5353
if err != nil {
5454
return nil, err
5555
}
@@ -83,7 +83,7 @@ func (d *Directory) closeChild(name string) error {
8383
return err
8484
}
8585

86-
_, err = d.dserv.Add(nd)
86+
_, err = d.fs.dserv.Add(nd)
8787
if err != nil {
8888
return err
8989
}
@@ -114,7 +114,7 @@ func (d *Directory) childFile(name string) (*file, error) {
114114
// search dag
115115
for _, lnk := range d.node.Links {
116116
if lnk.Name == name {
117-
nd, err := lnk.GetNode(d.dserv)
117+
nd, err := lnk.GetNode(d.fs.dserv)
118118
if err != nil {
119119
return nil, err
120120
}
@@ -127,7 +127,7 @@ func (d *Directory) childFile(name string) (*file, error) {
127127
case ufspb.Data_Directory:
128128
return nil, ErrIsDirectory
129129
case ufspb.Data_File:
130-
nfi, err := NewFile(name, nd, d, d.dserv)
130+
nfi, err := NewFile(name, nd, d, d.fs)
131131
if err != nil {
132132
return nil, err
133133
}
@@ -151,7 +151,7 @@ func (d *Directory) childDir(name string) (*Directory, error) {
151151

152152
for _, lnk := range d.node.Links {
153153
if lnk.Name == name {
154-
nd, err := lnk.GetNode(d.dserv)
154+
nd, err := lnk.GetNode(d.fs.dserv)
155155
if err != nil {
156156
return nil, err
157157
}
@@ -162,7 +162,7 @@ func (d *Directory) childDir(name string) (*Directory, error) {
162162

163163
switch i.GetType() {
164164
case ufspb.Data_Directory:
165-
ndir := NewDirectory(name, nd, d, d.dserv)
165+
ndir := NewDirectory(name, nd, d, d.fs)
166166
d.childDirs[name] = ndir
167167
return ndir, nil
168168
case ufspb.Data_File:
@@ -310,9 +310,9 @@ func (d *Directory) AddChild(name string, nd *dag.Node) error {
310310

311311
switch pbn.GetType() {
312312
case ft.TDirectory:
313-
d.childDirs[name] = NewDirectory(name, nd, d, d.dserv)
313+
d.childDirs[name] = NewDirectory(name, nd, d, d.fs)
314314
case ft.TFile, ft.TMetadata, ft.TRaw:
315-
nfi, err := NewFile(name, nd, d, d.dserv)
315+
nfi, err := NewFile(name, nd, d, d.fs)
316316
if err != nil {
317317
return err
318318
}

ipnsfs/file.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ type File interface {
2525

2626
type file struct {
2727
parent childCloser
28-
dserv dag.DAGService
2928
node *dag.Node
29+
fs *Filesystem
3030

31-
name string
32-
openMode int
31+
name string
3332

3433
refLk sync.Mutex
3534
ref int
@@ -38,15 +37,15 @@ type file struct {
3837
mod *mod.DagModifier
3938
}
4039

41-
func NewFile(name string, node *dag.Node, parent childCloser, dserv dag.DAGService) (*file, error) {
42-
dmod, err := mod.NewDagModifier(context.TODO(), node, dserv, nil, chunk.DefaultSplitter)
40+
func NewFile(name string, node *dag.Node, parent childCloser, fs *Filesystem) (*file, error) {
41+
dmod, err := mod.NewDagModifier(context.TODO(), node, fs.dserv, fs.pins.GetManual(), chunk.DefaultSplitter)
4342
if err != nil {
4443
return nil, err
4544
}
4645

4746
return &file{
47+
fs: fs,
4848
parent: parent,
49-
dserv: dserv,
5049
node: node,
5150
name: name,
5251
mod: dmod,

0 commit comments

Comments
 (0)