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

feat(inode): add inode struct #12

Merged
merged 4 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,8 +35,6 @@ type Directory struct {
unixfsDir uio.Directory

modTime time.Time

name string
}

// NewDirectory constructs a new MFS directory.
Expand All @@ -51,11 +48,13 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent child
}

return &Directory{
dserv: dserv,
inode: inode{
name: name,
parent: parent,
dagService: 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 @@ -108,7 +107,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 @@ -158,15 +157,15 @@ 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
}

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
}
Expand All @@ -178,7 +177,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 @@ -308,7 +307,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 @@ -318,7 +317,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 @@ -356,7 +355,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 @@ -452,7 +451,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
17 changes: 8 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,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{
dserv: dserv,
parent: parent,
name: name,
node: node,
inode: inode{
name: name,
parent: parent,
dagService: dserv,
},
node: node,
}
if node.Cid().Prefix().Version > 0 {
fi.RawLeaves = true
Expand Down Expand Up @@ -82,7 +81,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
21 changes: 21 additions & 0 deletions inode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
}
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