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

View file

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

View file

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

View file

@ -46,6 +46,17 @@ func newReadFileHandle(f *File, o fs.Object) (*ReadFileHandle, error) {
return fh, nil 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 // Node returns the Node assocuated with this - satisfies Noder interface
func (fh *ReadFileHandle) Node() Node { func (fh *ReadFileHandle) Node() Node {
return fh.file return fh.file

View file

@ -52,6 +52,17 @@ func newWriteFileHandle(d *Dir, f *File, src fs.ObjectInfo) (*WriteFileHandle, e
return fh, nil 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 // Node returns the Node assocuated with this - satisfies Noder interface
func (fh *WriteFileHandle) Node() Node { func (fh *WriteFileHandle) Node() Node {
return fh.file return fh.file