From 92a219e9c86c6ae775476bbe2b434059c73c47eb Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Sun, 7 Jun 2020 12:52:00 +0200 Subject: [PATCH] Improve permissions, close files and lazy metadata loading. Signed-off-by: Antonio Navarro Perez --- torrent/iio/utils.go | 21 +++++++++++++++++++++ torrent/mount/handler.go | 9 ++++++--- torrent/node/file.go | 22 +++++++--------------- torrent/node/root.go | 2 +- torrent/node/torrent.go | 9 ++++++--- torrent/node/zip.go | 2 +- 6 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 torrent/iio/utils.go diff --git a/torrent/iio/utils.go b/torrent/iio/utils.go new file mode 100644 index 0000000..fa2bb59 --- /dev/null +++ b/torrent/iio/utils.go @@ -0,0 +1,21 @@ +package iio + +import ( + "io" + "log" +) + +func CloseIfCloseable(r interface{}) error { + log.Println("closing file...") + if r == nil { + return nil + } + + closer, ok := r.(io.Closer) + if !ok { + log.Println("file is not implementing close method") + return nil + } + + return closer.Close() +} diff --git a/torrent/mount/handler.go b/torrent/mount/handler.go index 202c70b..863b42d 100644 --- a/torrent/mount/handler.go +++ b/torrent/mount/handler.go @@ -51,12 +51,15 @@ func (s *Handler) Mount(mpc *config.MountPoint) error { return err } - log.Println("getting torrent info", t.Name()) - <-t.GotInfo() + // only get info if name is not available + if t.Name() == "" { + log.Println("getting torrent info", t.InfoHash()) + <-t.GotInfo() + } s.s.Add(mpc.Path, t) + log.Println("torrent added", t.Name()) - log.Println("torrent info obtained", t.Name()) torrents = append(torrents, t) } diff --git a/torrent/node/file.go b/torrent/node/file.go index 9adb6c9..2193eb3 100644 --- a/torrent/node/file.go +++ b/torrent/node/file.go @@ -7,6 +7,7 @@ import ( "math" "syscall" + "github.com/ajnavarro/distribyted/iio" "github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fuse" ) @@ -14,7 +15,7 @@ import ( var _ fs.NodeGetattrer = &File{} var _ fs.NodeOpener = &File{} var _ fs.NodeReader = &File{} -var _ fs.NodeReleaser = &File{} +var _ fs.NodeFlusher = &File{} // File is a fuse node for files inside a torrent type File struct { @@ -44,7 +45,7 @@ func NewFileWithBlocks(readerFunc ReaderFunc, len int64, pieceLen int32, numPiec } func (tr *File) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { - out.Mode = syscall.S_IFREG & 07777 + out.Mode = syscall.S_IFREG & 0555 out.Nlink = 1 out.Size = uint64(tr.len) if tr.pieceLen != 0 { @@ -56,7 +57,6 @@ func (tr *File) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) } func (tr *File) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) { - // TODO use filehandle if tr.r == nil { r, err := tr.f() if err != nil { @@ -89,18 +89,10 @@ func (tr *File) Read(ctx context.Context, f fs.FileHandle, dest []byte, off int6 return fuse.ReadResultData(buf), fs.OK } -func (tr *File) Release(ctx context.Context, f fs.FileHandle) syscall.Errno { - log.Println("closing file...") - if tr.r != nil { - closer, ok := tr.r.(io.Closer) - if ok { - if err := closer.Close(); err != nil { - log.Println("error closing file", err) - return syscall.EIO - } - } else { - log.Println("file is not implementing close method") - } +func (tr *File) Flush(ctx context.Context, f fs.FileHandle) syscall.Errno { + if err := iio.CloseIfCloseable(tr.r); err != nil { + log.Println("error closing file", err) + return syscall.EIO } return fs.OK diff --git a/torrent/node/root.go b/torrent/node/root.go index eb40df9..afff936 100644 --- a/torrent/node/root.go +++ b/torrent/node/root.go @@ -33,7 +33,7 @@ func (root *Root) OnAdd(ctx context.Context) { } func (root *Root) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { - out.Mode = syscall.S_IFDIR & 07777 + out.Mode = syscall.S_IFDIR & 0555 return fs.OK } diff --git a/torrent/node/torrent.go b/torrent/node/torrent.go index 34bb562..5dd2c52 100644 --- a/torrent/node/torrent.go +++ b/torrent/node/torrent.go @@ -11,15 +11,17 @@ import ( "github.com/hanwen/go-fuse/v2/fuse" ) -var _ fs.NodeOnAdder = &Torrent{} var _ fs.NodeGetattrer = &Torrent{} +var _ fs.NodeOpendirer = &Torrent{} type Torrent struct { fs.Inode t *torrent.Torrent } -func (folder *Torrent) OnAdd(ctx context.Context) { +func (folder *Torrent) Opendir(ctx context.Context) syscall.Errno { + <-folder.t.GotInfo() + for _, file := range folder.t.Files() { file := file LoadNodeByPath( @@ -32,10 +34,11 @@ func (folder *Torrent) OnAdd(ctx context.Context) { int64(file.Torrent().Info().NumPieces()), ) } + return fs.OK } func (folder *Torrent) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { - out.Mode = syscall.S_IFDIR & 07777 + out.Mode = syscall.S_IFDIR & 0555 return fs.OK } diff --git a/torrent/node/zip.go b/torrent/node/zip.go index 37dcef2..2ffabbf 100644 --- a/torrent/node/zip.go +++ b/torrent/node/zip.go @@ -74,7 +74,7 @@ func (z *Zip) Opendir(ctx context.Context) syscall.Errno { } func (z *Zip) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { - out.Mode = syscall.S_IFDIR & 07777 + out.Mode = syscall.S_IFDIR & 0555 out.Size = uint64(z.size) return fs.OK