Remove previous POCs
Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
This commit is contained in:
parent
401a79379f
commit
b8392c4c50
50 changed files with 0 additions and 1958 deletions
99
node/file.go
Normal file
99
node/file.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"syscall"
|
||||
|
||||
"github.com/ajnavarro/distribyted/iio"
|
||||
"github.com/hanwen/go-fuse/v2/fs"
|
||||
"github.com/hanwen/go-fuse/v2/fuse"
|
||||
)
|
||||
|
||||
var _ fs.NodeGetattrer = &File{}
|
||||
var _ fs.NodeOpener = &File{}
|
||||
var _ fs.NodeReader = &File{}
|
||||
var _ fs.NodeFlusher = &File{}
|
||||
|
||||
// File is a fuse node for files inside a torrent
|
||||
type File struct {
|
||||
fs.Inode
|
||||
|
||||
f ReaderFunc
|
||||
r io.ReaderAt
|
||||
len int64
|
||||
pieceLen int32
|
||||
numPieces int64
|
||||
}
|
||||
|
||||
func NewFile(readerFunc ReaderFunc, len int64) *File {
|
||||
return &File{
|
||||
f: readerFunc,
|
||||
len: len,
|
||||
}
|
||||
}
|
||||
|
||||
func NewFileWithBlocks(readerFunc ReaderFunc, len int64, pieceLen int32, numPieces int64) *File {
|
||||
return &File{
|
||||
f: readerFunc,
|
||||
len: len,
|
||||
pieceLen: pieceLen,
|
||||
numPieces: numPieces,
|
||||
}
|
||||
}
|
||||
|
||||
func (tr *File) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
||||
out.Mode = syscall.S_IFREG & 0555
|
||||
out.Nlink = 1
|
||||
out.Size = uint64(tr.len)
|
||||
if tr.pieceLen != 0 {
|
||||
out.Blksize = uint32(tr.pieceLen)
|
||||
out.Blocks = uint64(tr.numPieces)
|
||||
}
|
||||
|
||||
return fs.OK
|
||||
}
|
||||
|
||||
func (tr *File) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
|
||||
if tr.r == nil {
|
||||
r, err := tr.f()
|
||||
if err != nil {
|
||||
log.Println("error opening reader for file", err)
|
||||
return nil, 0, syscall.EIO
|
||||
}
|
||||
|
||||
tr.r = r
|
||||
}
|
||||
|
||||
return nil, fuse.FOPEN_KEEP_CACHE, fs.OK
|
||||
}
|
||||
|
||||
func (tr *File) Read(ctx context.Context, f fs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
|
||||
end := int(math.Min(float64(len(dest)), float64(int64(tr.len)-off)))
|
||||
if end < 0 {
|
||||
end = 0
|
||||
}
|
||||
|
||||
buf := dest[:end]
|
||||
|
||||
n, err := tr.r.ReadAt(buf, off)
|
||||
|
||||
if err != nil && err != io.EOF {
|
||||
log.Println("error read data", err)
|
||||
return nil, syscall.EIO
|
||||
}
|
||||
|
||||
buf = buf[:n]
|
||||
return fuse.ReadResultData(buf), fs.OK
|
||||
}
|
||||
|
||||
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
|
||||
}
|
39
node/root.go
Normal file
39
node/root.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/hanwen/go-fuse/v2/fs"
|
||||
"github.com/hanwen/go-fuse/v2/fuse"
|
||||
)
|
||||
|
||||
var _ fs.NodeOnAdder = &Root{}
|
||||
var _ fs.NodeGetattrer = &Root{}
|
||||
|
||||
type Root struct {
|
||||
fs.Inode
|
||||
torrents []*torrent.Torrent
|
||||
}
|
||||
|
||||
func NewRoot(torrents []*torrent.Torrent) *Root {
|
||||
return &Root{torrents: torrents}
|
||||
}
|
||||
|
||||
func (root *Root) OnAdd(ctx context.Context) {
|
||||
for _, torrent := range root.torrents {
|
||||
root.AddChild(
|
||||
filepath.Clean(torrent.Name()),
|
||||
root.NewPersistentInode(ctx, &Torrent{t: torrent}, fs.StableAttr{
|
||||
Mode: syscall.S_IFDIR,
|
||||
}), true)
|
||||
}
|
||||
}
|
||||
|
||||
func (root *Root) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
||||
out.Mode = syscall.S_IFDIR & 0555
|
||||
|
||||
return fs.OK
|
||||
}
|
44
node/torrent.go
Normal file
44
node/torrent.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
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"
|
||||
)
|
||||
|
||||
var _ fs.NodeGetattrer = &Torrent{}
|
||||
var _ fs.NodeOpendirer = &Torrent{}
|
||||
|
||||
type Torrent struct {
|
||||
fs.Inode
|
||||
t *torrent.Torrent
|
||||
}
|
||||
|
||||
func (folder *Torrent) Opendir(ctx context.Context) syscall.Errno {
|
||||
<-folder.t.GotInfo()
|
||||
|
||||
for _, file := range folder.t.Files() {
|
||||
file := file
|
||||
LoadNodeByPath(
|
||||
ctx,
|
||||
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()),
|
||||
)
|
||||
}
|
||||
return fs.OK
|
||||
}
|
||||
|
||||
func (folder *Torrent) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
||||
out.Mode = syscall.S_IFDIR & 0555
|
||||
|
||||
return fs.OK
|
||||
}
|
61
node/utils.go
Normal file
61
node/utils.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/hanwen/go-fuse/v2/fs"
|
||||
"github.com/hanwen/go-fuse/v2/fuse"
|
||||
)
|
||||
|
||||
type ReaderFunc func() (io.ReaderAt, error)
|
||||
|
||||
func LoadNodeByPath(ctx context.Context, fp string, reader ReaderFunc, parent *fs.Inode, fileLength int64, pieceLen int32, numPieces int64) {
|
||||
p := parent
|
||||
dir, base := filepath.Split(filepath.Clean(fp))
|
||||
for i, component := range strings.Split(dir, "/") {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(component) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
ch := p.GetChild(component)
|
||||
if ch == nil {
|
||||
ch = p.NewPersistentInode(ctx, &fs.Inode{},
|
||||
fs.StableAttr{Mode: fuse.S_IFDIR})
|
||||
p.AddChild(component, ch, true)
|
||||
}
|
||||
|
||||
p = ch
|
||||
}
|
||||
|
||||
ext := path.Ext(base)
|
||||
switch ext {
|
||||
case ".zip":
|
||||
n := NewZip(reader, fileLength)
|
||||
p.AddChild(
|
||||
strings.TrimRight(base, ext),
|
||||
p.NewPersistentInode(ctx, n, fs.StableAttr{
|
||||
Mode: fuse.S_IFDIR,
|
||||
}), true)
|
||||
default:
|
||||
n := NewFileWithBlocks(
|
||||
reader,
|
||||
fileLength,
|
||||
pieceLen,
|
||||
numPieces,
|
||||
)
|
||||
p.AddChild(
|
||||
base,
|
||||
p.NewPersistentInode(ctx, n, fs.StableAttr{
|
||||
Mode: syscall.S_IFREG,
|
||||
}), true)
|
||||
}
|
||||
}
|
81
node/zip.go
Normal file
81
node/zip.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package node
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"syscall"
|
||||
|
||||
"github.com/ajnavarro/distribyted/iio"
|
||||
"github.com/hanwen/go-fuse/v2/fs"
|
||||
"github.com/hanwen/go-fuse/v2/fuse"
|
||||
)
|
||||
|
||||
var _ fs.NodeGetattrer = &Zip{}
|
||||
var _ fs.NodeOpendirer = &Zip{}
|
||||
|
||||
type Zip struct {
|
||||
fs.Inode
|
||||
|
||||
reader ReaderFunc
|
||||
size int64
|
||||
files []*zip.File
|
||||
}
|
||||
|
||||
func NewZip(reader ReaderFunc, size int64) *Zip {
|
||||
return &Zip{
|
||||
reader: reader,
|
||||
size: size,
|
||||
}
|
||||
}
|
||||
|
||||
func (z *Zip) Opendir(ctx context.Context) syscall.Errno {
|
||||
if z.files == nil {
|
||||
r, err := z.reader()
|
||||
if err != nil {
|
||||
log.Println("error opening reader for zip", err)
|
||||
return syscall.EIO
|
||||
}
|
||||
zr, err := zip.NewReader(r, z.size)
|
||||
if err != nil {
|
||||
log.Println("error getting zip reader from reader", err)
|
||||
return syscall.EIO
|
||||
}
|
||||
|
||||
for _, f := range zr.File {
|
||||
f := f
|
||||
if f.FileInfo().IsDir() {
|
||||
continue
|
||||
}
|
||||
LoadNodeByPath(
|
||||
ctx,
|
||||
f.Name,
|
||||
func() (io.ReaderAt, error) {
|
||||
zfr, err := f.Open()
|
||||
if err != nil {
|
||||
log.Println("ERROR OPENING ZIP", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return iio.NewDiskTeeReader(zfr)
|
||||
},
|
||||
&z.Inode,
|
||||
int64(f.UncompressedSize64),
|
||||
0,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
z.files = zr.File
|
||||
}
|
||||
|
||||
return fs.OK
|
||||
}
|
||||
|
||||
func (z *Zip) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
||||
out.Mode = syscall.S_IFDIR & 0555
|
||||
out.Size = uint64(z.size)
|
||||
|
||||
return fs.OK
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue