Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a0eae9b

Browse files
committedAug 3, 2015
make ipns fuse mount use mfs
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent fbc8f7b commit a0eae9b

File tree

4 files changed

+90
-22
lines changed

4 files changed

+90
-22
lines changed
 

‎core/commands/mfs.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
dag "github.com/ipfs/go-ipfs/merkledag"
1616
path "github.com/ipfs/go-ipfs/path"
1717
ft "github.com/ipfs/go-ipfs/unixfs"
18+
19+
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
1820
)
1921

2022
type mfsMountListing struct {
@@ -120,7 +122,7 @@ var MfsCreateCmd = &cmds.Command{
120122
rootnode = &dag.Node{Data: ft.FolderPBData()}
121123
}
122124

123-
_, err = node.Mfs.NewRoot(name, rootnode, func(_ key.Key) error { return nil })
125+
_, err = node.Mfs.NewRoot(name, rootnode, func(_ context.Context, _ key.Key) error { return nil })
124126
if err != nil {
125127
res.SetError(err, cmds.ErrNormal)
126128
return

‎core/pathresolver.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (*merkledag.Node, er
2323
if strings.HasPrefix(p.String(), "/ipns/") {
2424
// resolve ipns paths
2525

26+
log.Error("RESOLVE!!!")
2627
// TODO(cryptix): we sould be able to query the local cache for the path
2728
if n.Namesys == nil {
2829
return nil, ErrNoNamesys

‎fuse/ipns/ipns_unix.go

+74-16
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ import (
1717

1818
key "github.com/ipfs/go-ipfs/blocks/key"
1919
core "github.com/ipfs/go-ipfs/core"
20+
mfs "github.com/ipfs/go-ipfs/ipnsfs"
2021
nsfs "github.com/ipfs/go-ipfs/ipnsfs"
2122
dag "github.com/ipfs/go-ipfs/merkledag"
2223
ci "github.com/ipfs/go-ipfs/p2p/crypto"
24+
path "github.com/ipfs/go-ipfs/path"
2325
ft "github.com/ipfs/go-ipfs/unixfs"
2426
)
2527

28+
func init() {
29+
fuse.Debug = func(msg interface{}) { log.Error(msg) }
30+
}
31+
2632
var log = eventlog.Logger("fuse/ipns")
2733

2834
// FileSystem is the readwrite IPNS Fuse Filesystem.
@@ -62,36 +68,78 @@ type Root struct {
6268
IpfsRoot string
6369
IpnsRoot string
6470
LocalDirs map[string]fs.Node
65-
Roots map[string]*nsfs.KeyRoot
71+
Roots map[string]*keyRoot
6672

6773
fs *nsfs.Filesystem
6874
LocalLink *Link
6975
}
7076

77+
func ipnsPubFunc(ipfs *core.IpfsNode, k ci.PrivKey) mfs.PubFunc {
78+
return func(ctx context.Context, key key.Key) error {
79+
return ipfs.Namesys.Publish(ctx, k, path.FromKey(key))
80+
}
81+
}
82+
83+
func (r *Root) loadKeyRoot(ctx context.Context, name string) (*mfs.KeyRoot, error) {
84+
85+
rt, ok := r.Roots[name]
86+
if !ok {
87+
return nil, fmt.Errorf("no key by name '%s'", name)
88+
}
89+
90+
// already loaded
91+
if rt.root != nil {
92+
return rt.root, nil
93+
}
94+
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 := core.Resolve(ctx, r.Ipfs, p)
102+
if err != nil {
103+
log.Errorf("looking up %s: %s", p, err)
104+
return nil, err
105+
}
106+
107+
// load it lazily
108+
root, err := r.Ipfs.Mfs.NewRoot(name, node, ipnsPubFunc(r.Ipfs, rt.k))
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
rt.root = root
114+
115+
switch val := root.GetValue().(type) {
116+
case *nsfs.Directory:
117+
r.LocalDirs[name] = &Directory{dir: val}
118+
case *nsfs.File:
119+
r.LocalDirs[name] = &File{fi: val}
120+
default:
121+
return nil, errors.New("unrecognized type")
122+
}
123+
124+
return root, nil
125+
}
126+
127+
type keyRoot struct {
128+
k ci.PrivKey
129+
root *mfs.KeyRoot
130+
}
131+
71132
func CreateRoot(ipfs *core.IpfsNode, keys []ci.PrivKey, ipfspath, ipnspath string) (*Root, error) {
72133
ldirs := make(map[string]fs.Node)
73-
roots := make(map[string]*nsfs.KeyRoot)
134+
roots := make(map[string]*keyRoot)
74135
for _, k := range keys {
75136
pkh, err := k.GetPublic().Hash()
76137
if err != nil {
77138
return nil, err
78139
}
79140
name := key.Key(pkh).B58String()
80-
root, err := ipfs.Mfs.GetRoot(name)
81-
if err != nil {
82-
return nil, err
83-
}
84-
85-
roots[name] = root
86141

87-
switch val := root.GetValue().(type) {
88-
case *nsfs.Directory:
89-
ldirs[name] = &Directory{dir: val}
90-
case *nsfs.File:
91-
ldirs[name] = &File{fi: val}
92-
default:
93-
return nil, errors.New("unrecognized type")
94-
}
142+
roots[name] = &keyRoot{k: k}
95143
}
96144

97145
return &Root{
@@ -130,6 +178,16 @@ func (s *Root) Lookup(ctx context.Context, name string) (fs.Node, error) {
130178
}
131179

132180
nd, ok := s.LocalDirs[name]
181+
if !ok {
182+
// see if we have an unloaded root by this name
183+
_, ok = s.Roots[name]
184+
if ok {
185+
_, err := s.loadKeyRoot(ctx, name)
186+
if err != nil {
187+
return nil, err
188+
}
189+
}
190+
}
133191
if ok {
134192
switch nd := nd.(type) {
135193
case *Directory:

‎ipnsfs/system.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func (fs *Filesystem) GetRoot(name string) (*KeyRoot, error) {
9898
if ok {
9999
return r, nil
100100
}
101+
panic("noooo")
101102
return nil, ErrNotExist
102103
}
103104

@@ -167,7 +168,7 @@ type KeyRoot struct {
167168
repub *Republisher
168169
}
169170

170-
type PubFunc func(key.Key) error
171+
type PubFunc func(context.Context, key.Key) error
171172

172173
// newKeyRoot creates a new KeyRoot for the given key, and starts up a republisher routine
173174
// for it
@@ -227,7 +228,7 @@ func (kr *KeyRoot) closeChild(name string, nd *dag.Node) error {
227228
}
228229

229230
func (kr *KeyRoot) Close() error {
230-
return kr.repub.publish()
231+
return kr.repub.Close()
231232
}
232233

233234
// Republisher manages when to publish the ipns entry associated with a given key
@@ -280,6 +281,12 @@ func (p *Republisher) pubNow() {
280281
}
281282
}
282283

284+
func (p *Republisher) Close() error {
285+
err := p.publish(p.ctx)
286+
p.cancel()
287+
return err
288+
}
289+
283290
// Touch signals that an update has occurred since the last publish.
284291
// Multiple consecutive touches may extend the time period before
285292
// the next Publish occurs in order to more efficiently batch updates
@@ -311,7 +318,7 @@ func (np *Republisher) Run() {
311318
case <-np.pubnowch:
312319
}
313320

314-
err := np.publish()
321+
err := np.publish(np.ctx)
315322
if err != nil {
316323
log.Error("republishRoot error: %s", err)
317324
}
@@ -322,13 +329,13 @@ func (np *Republisher) Run() {
322329
}
323330
}
324331

325-
func (np *Republisher) publish() error {
332+
func (np *Republisher) publish(ctx context.Context) error {
326333
np.lk.Lock()
327334
topub := np.val
328335
np.lk.Unlock()
329336

330337
log.Info("Publishing Changes!")
331-
err := np.pubfunc(topub)
338+
err := np.pubfunc(ctx, topub)
332339
if err != nil {
333340
return err
334341
}

0 commit comments

Comments
 (0)
Please sign in to comment.