From 90acd8823e6b4f093c1fbed00eb61dd28cac61df Mon Sep 17 00:00:00 2001 From: Overbool Date: Tue, 25 Sep 2018 16:35:08 +0800 Subject: [PATCH 1/4] feat(inode): add inode struct --- dir.go | 25 ++++++++++--------------- fd.go | 2 +- file.go | 13 ++++--------- inode.go | 30 ++++++++++++++++++++++++++++++ mfs_test.go | 2 +- system.go | 2 +- 6 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 inode.go diff --git a/dir.go b/dir.go index f26b136..2532b86 100644 --- a/dir.go +++ b/dir.go @@ -22,8 +22,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 @@ -36,8 +35,6 @@ type Directory struct { unixfsDir uio.Directory modTime time.Time - - name string } // NewDirectory constructs a new MFS directory. @@ -51,11 +48,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(), @@ -108,7 +103,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 } @@ -158,7 +153,7 @@ func (d *Directory) cacheNode(name string, nd ipld.Node) (FSNode, error) { switch fsn.Type() { case ft.TDirectory, ft.THAMTShard: - 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 } @@ -166,7 +161,7 @@ func (d *Directory) cacheNode(name string, nd ipld.Node) (FSNode, error) { d.childDirs[name] = ndir return ndir, nil case ft.TFile, ft.TRaw, ft.TSymlink: - nfi, err := NewFile(name, nd, d, d.dserv) + nfi, err := NewFile(name, nd, d, d.dagService) if err != nil { return nil, err } @@ -178,7 +173,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 } @@ -308,7 +303,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 } @@ -318,7 +313,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 } @@ -356,7 +351,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 } @@ -452,7 +447,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..54f064c --- /dev/null +++ b/inode.go @@ -0,0 +1,30 @@ +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 of this `inode` in the MFS path (the same value + // is also stored as the name of the DAG link). + name string + + // parent directory of this `inode` (which may be the `Root`). + parent childCloser + + // dagService used to store modifications made to the contents + // of the file or directory the `inode` belongs to. + 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 } From 06fd27e475c85a704f5c80fffaca4d5d7baea6c5 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 11 Dec 2018 12:43:14 -0800 Subject: [PATCH 2/4] go fmt --- inode.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inode.go b/inode.go index 54f064c..e2b591c 100644 --- a/inode.go +++ b/inode.go @@ -10,10 +10,10 @@ import ( type inode struct { // name of this `inode` in the MFS path (the same value // is also stored as the name of the DAG link). - name string + name string // parent directory of this `inode` (which may be the `Root`). - parent childCloser + parent childCloser // dagService used to store modifications made to the contents // of the file or directory the `inode` belongs to. From 0ae12b2070af108bc56c14cb8b9b6555d2c0fd79 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 11 Dec 2018 12:43:22 -0800 Subject: [PATCH 3/4] avoid unecessary constructors (also avoid exposing a public constructor for a private datastructure) --- dir.go | 6 +++++- file.go | 8 ++++++-- inode.go | 9 --------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/dir.go b/dir.go index 2532b86..51a00bd 100644 --- a/dir.go +++ b/dir.go @@ -48,7 +48,11 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent child } return &Directory{ - inode: NewInode(name, parent, dserv), + inode: &inode{ + name: name, + parent: parent, + dagService: dserv, + }, ctx: ctx, unixfsDir: db, childDirs: make(map[string]*Directory), diff --git a/file.go b/file.go index 86e0071..00f4834 100644 --- a/file.go +++ b/file.go @@ -28,8 +28,12 @@ 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{ - inode: NewInode(name, parent, dserv), - node: node, + inode: &inode{ + name: name, + parent: parent, + dagService: dserv, + }, + node: node, } if node.Cid().Prefix().Version > 0 { fi.RawLeaves = true diff --git a/inode.go b/inode.go index e2b591c..f0330a2 100644 --- a/inode.go +++ b/inode.go @@ -19,12 +19,3 @@ type inode struct { // of the file or directory the `inode` belongs to. 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, - } -} From ec0acc9eae643b3694dac17bf88c4ff298a825ab Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 11 Dec 2018 12:45:52 -0800 Subject: [PATCH 4/4] avoid unecessary indirection --- dir.go | 4 ++-- file.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dir.go b/dir.go index 51a00bd..e42400a 100644 --- a/dir.go +++ b/dir.go @@ -22,7 +22,7 @@ var ErrInvalidChild = errors.New("invalid child node") var ErrDirExists = errors.New("directory already has entry by that name") type Directory struct { - *inode + inode childDirs map[string]*Directory files map[string]*File @@ -48,7 +48,7 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent child } return &Directory{ - inode: &inode{ + inode: inode{ name: name, parent: parent, dagService: dserv, diff --git a/file.go b/file.go index 00f4834..e0be550 100644 --- a/file.go +++ b/file.go @@ -14,7 +14,7 @@ import ( ) type File struct { - *inode + inode desclock sync.RWMutex @@ -28,7 +28,7 @@ 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{ - inode: &inode{ + inode: inode{ name: name, parent: parent, dagService: dserv,