mountlib: make Nodes also be fmt.Stringer so they debug nicely

This commit is contained in:
Nick Craig-Wood 2017-05-09 11:29:02 +01:00
parent 9c3048580a
commit abda616f84
5 changed files with 46 additions and 3 deletions

View File

@ -44,6 +44,14 @@ func newDir(fsys *FS, f fs.Fs, fsDir *fs.Dir) *Dir {
}
}
// String converts it to printablee
func (d *Dir) String() string {
if d == nil {
return "<nil *Dir>"
}
return d.path + "/"
}
// IsFile returns false for Dir - satisfies Node interface
func (d *Dir) IsFile() bool {
return false
@ -252,7 +260,7 @@ func (d *Dir) lookupNode(leaf string) (item *DirEntry, err error) {
var node Node
switch x := item.Obj.(type) {
case fs.Object:
node, err = newFile(d, x), nil
node, err = newFile(d, x, leaf), nil
case *fs.Dir:
node, err = newDir(d.fsys, d.f, x), nil
default:
@ -308,7 +316,7 @@ func (d *Dir) Create(name string) (*File, *WriteFileHandle, error) {
fs.Debugf(path, "Dir.Create")
src := newCreateInfo(d.f, path)
// This gets added to the directory when the file is written
file := newFile(d, nil)
file := newFile(d, nil, name)
fh, err := newWriteFileHandle(d, file, src)
if err != nil {
fs.Errorf(path, "Dir.Create error: %v", err)

View File

@ -1,6 +1,7 @@
package mountlib
import (
"path"
"sync"
"sync/atomic"
"time"
@ -16,19 +17,29 @@ type File struct {
d *Dir // parent directory - read only
mu sync.RWMutex // protects the following
o fs.Object // NB o may be nil if file is being written
leaf string // leaf name of the object
writers int // number of writers for this file
pendingModTime time.Time // will be applied once o becomes available, i.e. after file was written
}
// newFile creates a new File
func newFile(d *Dir, o fs.Object) *File {
func newFile(d *Dir, o fs.Object, leaf string) *File {
return &File{
d: d,
o: o,
leaf: leaf,
inode: NewInode(),
}
}
// String converts it to printable
func (f *File) String() string {
if f == nil {
return "<nil *File>"
}
return path.Join(f.d.path, f.leaf)
}
// IsFile returns true for File - satisfies Node interface
func (f *File) IsFile() bool {
return true

View File

@ -1,6 +1,7 @@
package mountlib
import (
"fmt"
"strings"
"sync/atomic"
"time"
@ -21,6 +22,7 @@ var (
// Noder represents something which can return a node
type Noder interface {
fmt.Stringer
Node() Node
}

View File

@ -46,6 +46,17 @@ func newReadFileHandle(f *File, o fs.Object) (*ReadFileHandle, error) {
return fh, nil
}
// String converts it to printable
func (fh *ReadFileHandle) String() string {
if fh == nil {
return "<nil *ReadFileHandle>"
}
if fh.file == nil {
return "<nil *ReadFileHandle.file>"
}
return fh.file.String() + " (r)"
}
// Node returns the Node assocuated with this - satisfies Noder interface
func (fh *ReadFileHandle) Node() Node {
return fh.file

View File

@ -52,6 +52,17 @@ func newWriteFileHandle(d *Dir, f *File, src fs.ObjectInfo) (*WriteFileHandle, e
return fh, nil
}
// String converts it to printable
func (fh *WriteFileHandle) String() string {
if fh == nil {
return "<nil *WriteFileHandle>"
}
if fh.file == nil {
return "<nil *WriteFileHandle.file>"
}
return fh.file.String() + " (w)"
}
// Node returns the Node assocuated with this - satisfies Noder interface
func (fh *WriteFileHandle) Node() Node {
return fh.file