Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
feat(inode): add inode struct
Browse files Browse the repository at this point in the history
  • Loading branch information
overbool committed Sep 25, 2018
1 parent 166e30e commit 758d3ae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
25 changes: 10 additions & 15 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,8 +36,6 @@ type Directory struct {
unixfsDir uio.Directory

modTime time.Time

name string
}

// NewDirectory constructs a new MFS directory.
Expand All @@ -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(),
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -159,15 +154,15 @@ 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
}

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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 4 additions & 9 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions inode.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
2 changes: 1 addition & 1 deletion mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 758d3ae

Please sign in to comment.