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 return err
} }
log.Println("getting torrent info", t.Name()) // only get info if name is not available
if t.Name() == "" {
log.Println("getting torrent info", t.InfoHash())
<-t.GotInfo() <-t.GotInfo()
}
s.s.Add(mpc.Path, t) s.s.Add(mpc.Path, t)
log.Println("torrent added", t.Name())
log.Println("torrent info obtained", t.Name())
torrents = append(torrents, t) torrents = append(torrents, t)
} }

View file

@ -7,6 +7,7 @@ import (
"math" "math"
"syscall" "syscall"
"github.com/ajnavarro/distribyted/iio"
"github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse" "github.com/hanwen/go-fuse/v2/fuse"
) )
@ -14,7 +15,7 @@ import (
var _ fs.NodeGetattrer = &File{} var _ fs.NodeGetattrer = &File{}
var _ fs.NodeOpener = &File{} var _ fs.NodeOpener = &File{}
var _ fs.NodeReader = &File{} var _ fs.NodeReader = &File{}
var _ fs.NodeReleaser = &File{} var _ fs.NodeFlusher = &File{}
// File is a fuse node for files inside a torrent // File is a fuse node for files inside a torrent
type File struct { 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 { 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.Nlink = 1
out.Size = uint64(tr.len) out.Size = uint64(tr.len)
if tr.pieceLen != 0 { 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) { func (tr *File) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
// TODO use filehandle
if tr.r == nil { if tr.r == nil {
r, err := tr.f() r, err := tr.f()
if err != nil { if err != nil {
@ -89,19 +89,11 @@ func (tr *File) Read(ctx context.Context, f fs.FileHandle, dest []byte, off int6
return fuse.ReadResultData(buf), fs.OK return fuse.ReadResultData(buf), fs.OK
} }
func (tr *File) Release(ctx context.Context, f fs.FileHandle) syscall.Errno { func (tr *File) Flush(ctx context.Context, f fs.FileHandle) syscall.Errno {
log.Println("closing file...") if err := iio.CloseIfCloseable(tr.r); err != nil {
if tr.r != nil {
closer, ok := tr.r.(io.Closer)
if ok {
if err := closer.Close(); err != nil {
log.Println("error closing file", err) log.Println("error closing file", err)
return syscall.EIO return syscall.EIO
} }
} else {
log.Println("file is not implementing close method")
}
}
return fs.OK 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 { 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 return fs.OK
} }

View file

@ -11,15 +11,17 @@ import (
"github.com/hanwen/go-fuse/v2/fuse" "github.com/hanwen/go-fuse/v2/fuse"
) )
var _ fs.NodeOnAdder = &Torrent{}
var _ fs.NodeGetattrer = &Torrent{} var _ fs.NodeGetattrer = &Torrent{}
var _ fs.NodeOpendirer = &Torrent{}
type Torrent struct { type Torrent struct {
fs.Inode fs.Inode
t *torrent.Torrent 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() { for _, file := range folder.t.Files() {
file := file file := file
LoadNodeByPath( LoadNodeByPath(
@ -32,10 +34,11 @@ func (folder *Torrent) OnAdd(ctx context.Context) {
int64(file.Torrent().Info().NumPieces()), int64(file.Torrent().Info().NumPieces()),
) )
} }
return fs.OK
} }
func (folder *Torrent) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { 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 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 { 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) out.Size = uint64(z.size)
return fs.OK return fs.OK