diff --git a/dir.go b/dir.go index 5a1161c..471d1d2 100644 --- a/dir.go +++ b/dir.go @@ -23,8 +23,7 @@ var ErrInvalidChild = errors.New("invalid child node") var ErrDirExists = errors.New("directory already has entry by that name") type Directory struct { - dserv ipld.DAGService - parent childCloser + *inode childDirs map[string]*Directory files map[string]*File @@ -37,8 +36,6 @@ type Directory struct { unixfsDir uio.Directory modTime time.Time - - name string } // NewDirectory constructs a new MFS directory. @@ -52,11 +49,9 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent child } return &Directory{ - dserv: dserv, + inode: NewInode(name, parent, dserv), ctx: ctx, - name: name, unixfsDir: db, - parent: parent, childDirs: make(map[string]*Directory), files: make(map[string]*File), modTime: time.Now(), @@ -109,7 +104,7 @@ func (d *Directory) flushCurrentNode() (*dag.ProtoNode, error) { return nil, err } - err = d.dserv.Add(d.ctx, nd) + err = d.dagService.Add(d.ctx, nd) if err != nil { return nil, err } @@ -159,7 +154,7 @@ func (d *Directory) cacheNode(name string, nd ipld.Node) (FSNode, error) { switch fsn.Type() { case ufspb.Data_Directory, ufspb.Data_HAMTShard: - ndir, err := NewDirectory(d.ctx, name, nd, d, d.dserv) + ndir, err := NewDirectory(d.ctx, name, nd, d, d.dagService) if err != nil { return nil, err } @@ -167,7 +162,7 @@ func (d *Directory) cacheNode(name string, nd ipld.Node) (FSNode, error) { d.childDirs[name] = ndir return ndir, nil case ufspb.Data_File, ufspb.Data_Raw, ufspb.Data_Symlink: - nfi, err := NewFile(name, nd, d, d.dserv) + nfi, err := NewFile(name, nd, d, d.dagService) if err != nil { return nil, err } @@ -179,7 +174,7 @@ func (d *Directory) cacheNode(name string, nd ipld.Node) (FSNode, error) { return nil, ErrInvalidChild } case *dag.RawNode: - nfi, err := NewFile(name, nd, d, d.dserv) + nfi, err := NewFile(name, nd, d, d.dagService) if err != nil { return nil, err } @@ -309,7 +304,7 @@ func (d *Directory) Mkdir(name string) (*Directory, error) { ndir := ft.EmptyDirNode() ndir.SetCidBuilder(d.GetCidBuilder()) - err = d.dserv.Add(d.ctx, ndir) + err = d.dagService.Add(d.ctx, ndir) if err != nil { return nil, err } @@ -319,7 +314,7 @@ func (d *Directory) Mkdir(name string) (*Directory, error) { return nil, err } - dirobj, err := NewDirectory(d.ctx, name, ndir, d, d.dserv) + dirobj, err := NewDirectory(d.ctx, name, ndir, d, d.dagService) if err != nil { return nil, err } @@ -357,7 +352,7 @@ func (d *Directory) AddChild(name string, nd ipld.Node) error { return ErrDirExists } - err = d.dserv.Add(d.ctx, nd) + err = d.dagService.Add(d.ctx, nd) if err != nil { return err } @@ -453,7 +448,7 @@ func (d *Directory) GetNode() (ipld.Node, error) { return nil, err } - err = d.dserv.Add(d.ctx, nd) + err = d.dagService.Add(d.ctx, nd) if err != nil { return nil, err } diff --git a/fd.go b/fd.go index 0f0d3d4..fd4351b 100644 --- a/fd.go +++ b/fd.go @@ -122,7 +122,7 @@ func (fi *fileDescriptor) flushUp(fullsync bool) error { return err } - err = fi.inode.dserv.Add(context.TODO(), nd) + err = fi.inode.dagService.Add(context.TODO(), nd) if err != nil { return err } diff --git a/file.go b/file.go index 0a49646..86e0071 100644 --- a/file.go +++ b/file.go @@ -14,13 +14,10 @@ import ( ) type File struct { - parent childCloser - - name string + *inode desclock sync.RWMutex - dserv ipld.DAGService node ipld.Node nodelk sync.Mutex @@ -31,10 +28,8 @@ type File struct { // Cid version is non-zero RawLeaves will be enabled. func NewFile(name string, node ipld.Node, parent childCloser, dserv ipld.DAGService) (*File, error) { fi := &File{ - dserv: dserv, - parent: parent, - name: name, - node: node, + inode: NewInode(name, parent, dserv), + node: node, } if node.Cid().Prefix().Version > 0 { fi.RawLeaves = true @@ -82,7 +77,7 @@ func (fi *File) Open(flags int, sync bool) (FileDescriptor, error) { return nil, fmt.Errorf("mode not supported") } - dmod, err := mod.NewDagModifier(context.TODO(), node, fi.dserv, chunker.DefaultSplitter) + dmod, err := mod.NewDagModifier(context.TODO(), node, fi.dagService, chunker.DefaultSplitter) if err != nil { return nil, err } diff --git a/inode.go b/inode.go new file mode 100644 index 0000000..f96d231 --- /dev/null +++ b/inode.go @@ -0,0 +1,23 @@ +package mfs + +import ( + ipld "github.com/ipfs/go-ipld-format" +) + +// inode abstracts the common characteristics of the MFS `File` +// and `Directory`. All of its attributes are initialized at +// creation. +type inode struct { + name string + parent childCloser + dagService ipld.DAGService +} + +// NewInode creates a new `inode` structure and return it's pointer. +func NewInode(name string, parent childCloser, dagService ipld.DAGService) *inode { + return &inode{ + name: name, + parent: parent, + dagService: dagService, + } +} diff --git a/mfs_test.go b/mfs_test.go index e840f6c..456d005 100644 --- a/mfs_test.go +++ b/mfs_test.go @@ -555,7 +555,7 @@ func actorMakeFile(d *Directory) error { } name := randomName() - f, err := NewFile(name, dag.NodeWithData(ft.FilePBData(nil, 0)), d, d.dserv) + f, err := NewFile(name, dag.NodeWithData(ft.FilePBData(nil, 0)), d, d.dagService) if err != nil { return err } diff --git a/system.go b/system.go index cc7e65d..bc0bafa 100644 --- a/system.go +++ b/system.go @@ -145,7 +145,7 @@ func (kr *Root) FlushMemFree(ctx context.Context) error { // closeChild implements the childCloser interface, and signals to the publisher that // there are changes ready to be published. func (kr *Root) closeChild(name string, nd ipld.Node, sync bool) error { - err := kr.GetDirectory().dserv.Add(context.TODO(), nd) + err := kr.GetDirectory().dagService.Add(context.TODO(), nd) if err != nil { return err }