Improve permissions, close files and lazy metadata loading.

Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
This commit is contained in:
Antonio Navarro Perez 2020-06-07 12:52:00 +02:00
parent ae4be076f9
commit 92a219e9c8
6 changed files with 42 additions and 23 deletions

21
torrent/iio/utils.go Normal file
View file

@ -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()
}

View file

@ -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)
}

View file

@ -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

View file

@ -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
}

View file

@ -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
}

View file

@ -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