Commit 9cd85e08 authored by Adphi's avatar Adphi

add logging

parent 1439c36a
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -5,6 +5,7 @@ import (
"os"
"os/signal"
"bazil.org/fuse"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
......@@ -12,7 +13,7 @@ import (
)
func main() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetLevel(logrus.TraceLevel)
mountPoint := "./test-mount"
if err := os.MkdirAll(mountPoint, os.ModePerm); err != nil {
logrus.Fatal("failed to create mount point")
......@@ -38,11 +39,15 @@ func main() {
signal.Notify(c, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-c
cancel()
}()
if os.Getenv("DEBUG") != "" {
fuse.Debug = func(msg interface{}) {
logrus.Trace(msg)
}
}
defer fs.Unmount()
if err := fs.Mount(ctx, mountPoint); err != nil {
logrus.Error("failed to mount file system")
......
......@@ -6,9 +6,9 @@ import (
"os"
"sync"
"github.com/sirupsen/logrus"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
......@@ -21,6 +21,7 @@ type File interface {
fs.HandleWriter
fs.HandleFlusher
fs.NodeSetattrer
fs.NodeFsyncer
}
var _ File = &file{}
......@@ -33,6 +34,7 @@ type file struct {
}
func (f *file) Attr(ctx context.Context, attr *fuse.Attr) error {
logrus.Debugf("Attr %s", f.file.Name())
s, err := f.file.Stat()
if err != nil {
return err
......@@ -42,29 +44,47 @@ func (f *file) Attr(ctx context.Context, attr *fuse.Attr) error {
}
func (f *file) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
logrus.Debugf("Open %s (%s)", f.file.Name(),req.Flags)
if req.Flags.IsReadOnly() {
// we don't need to track read-only handles
return f, nil
}
f.mu.Lock()
defer f.mu.Unlock()
if f.count != 0 {
f.count ++
logrus.Debugf("Open %s count: %d", f.file.Name(), f.count)
return f, nil
}
var err error
f.file, err = f.fs.OpenFile(f.file.Name(), os.O_RDWR, os.ModePerm)
f.file, err = f.fs.OpenFile(f.file.Name(), int(req.Flags), os.ModePerm)
if err != nil {
logrus.Errorf("Open %s: %v", f.file.Name(), err)
return nil, err
}
f.count++
logrus.Debugf("Open %s count: %d", f.file.Name(), f.count)
return f, nil
}
func (f *file) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
logrus.Debugf("Release %s", f.file.Name())
if req.Flags.IsReadOnly() {
// we don't need to track read-only handles
return nil
}
f.mu.Lock()
defer f.mu.Unlock()
f.count--
logrus.Debugf("Release %s count: %d", f.file.Name(), f.count)
if f.count != 0 {
return nil
}
return f.file.Close()
if err := f.file.Close(); err != nil {
return err
}
return nil
}
func (f *file) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
......@@ -85,7 +105,8 @@ func (f *file) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadR
}
func (f *file) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
logrus.Debugf("Write %s", f.file.Name())
s, _ := f.file.Stat()
logrus.Debugf("Write %s (mod time: %v)", f.file.Name(), s.ModTime())
f.mu.Lock()
defer f.mu.Unlock()
n, err := f.file.WriteAt(req.Data, req.Offset)
......@@ -94,14 +115,51 @@ func (f *file) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.Wri
return err
}
resp.Size = n
s, _ = f.file.Stat()
logrus.Debugf("End Write %s (mod time: %v)", f.file.Name(), s.ModTime())
return nil
}
func (f *file) Flush(ctx context.Context, req *fuse.FlushRequest) error {
logrus.Debugf("Flush %s", f.file.Name())
return nil
}
func (f *file) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
logrus.Debugf("Setattr %s", f.file.Name())
s, err := f.file.Stat()
if err != nil {
return err
}
if req.Valid.Size() {
logrus.Debugf("Setattr %s size: %d", f.file.Name(), req.Size)
newLen := int64(req.Size)
switch {
case newLen > s.Size():
if _, err := f.file.WriteAt(make([]byte, newLen-s.Size()), s.Size()); err != nil {
return err
}
case newLen < s.Size():
if err := f.file.Truncate(s.Size() - newLen); err != nil {
return err
}
}
}
if req.Valid.Mode() {
logrus.Debugf("Setattr %s mod: %d", f.file.Name(), req.Mode)
if err := f.fs.Chmod(f.file.Name(), req.Mode); err != nil {
return err
}
}
s, err = f.file.Stat()
if err != nil {
return err
}
attrs(&resp.Attr, s)
return nil
}
func (f *file) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
return nil
}
......@@ -33,6 +33,7 @@ func New(afs afero.Fs) (FS, error) {
if !s.IsDir() {
return nil, errors.New("file system root is not a directory")
}
fuse.MaxReadahead(128*1024)
return &aferoFs{fs: afs, root: root}, nil
}
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment