2020-04-27 16:46:23 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/ajnavarro/distribyted/iio"
|
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
)
|
|
|
|
|
2020-06-03 09:03:13 +00:00
|
|
|
var _ fs.NodeOnAdder = &Torrent{}
|
|
|
|
var _ fs.NodeGetattrer = &Torrent{}
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2020-06-03 09:03:13 +00:00
|
|
|
type Torrent struct {
|
2020-04-27 16:46:23 +00:00
|
|
|
fs.Inode
|
|
|
|
t *torrent.Torrent
|
|
|
|
|
|
|
|
pool *ants.Pool
|
|
|
|
}
|
|
|
|
|
2020-06-03 09:03:13 +00:00
|
|
|
func (folder *Torrent) OnAdd(ctx context.Context) {
|
2020-04-27 16:46:23 +00:00
|
|
|
for _, file := range folder.t.Files() {
|
|
|
|
file := file
|
|
|
|
LoadNodeByPath(
|
|
|
|
ctx,
|
|
|
|
folder.pool,
|
|
|
|
file.Path(),
|
|
|
|
func() (io.ReaderAt, error) { return iio.NewReadAtWrapper(file.NewReader()), nil },
|
|
|
|
&folder.Inode,
|
|
|
|
file.Length(),
|
|
|
|
int32(file.Torrent().Info().PieceLength),
|
|
|
|
int64(file.Torrent().Info().NumPieces()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 09:03:13 +00:00
|
|
|
func (folder *Torrent) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
2020-04-27 16:46:23 +00:00
|
|
|
out.Mode = syscall.S_IFDIR & 07777
|
|
|
|
|
|
|
|
return fs.OK
|
|
|
|
}
|