tstor/src/host/vfs/torrent.go

349 lines
6.9 KiB
Go
Raw Normal View History

2023-10-16 09:18:40 +00:00
package vfs
import (
2021-11-29 10:07:54 +00:00
"context"
"io"
2023-12-21 23:15:39 +00:00
"io/fs"
"path"
2023-12-25 22:11:03 +00:00
"slices"
2024-01-01 18:17:32 +00:00
"strings"
"sync"
2024-03-20 21:47:51 +00:00
"git.kmsign.ru/royalcat/tstor/pkg/ctxio"
2024-01-28 20:22:49 +00:00
"git.kmsign.ru/royalcat/tstor/src/host/controller"
"github.com/anacrolix/torrent"
2023-12-25 22:11:03 +00:00
"golang.org/x/exp/maps"
)
2023-10-16 09:18:40 +00:00
var _ Filesystem = &TorrentFs{}
2023-10-16 09:18:40 +00:00
type TorrentFs struct {
2024-03-19 21:30:37 +00:00
name string
mu sync.Mutex
Torrent *controller.Torrent
2023-12-21 23:15:39 +00:00
2024-01-28 20:22:49 +00:00
filesCache map[string]File
2023-12-21 23:15:39 +00:00
2023-10-16 09:18:40 +00:00
resolver *resolver
}
2024-03-20 21:47:51 +00:00
func NewTorrentFs(name string, c *controller.Torrent) *TorrentFs {
2023-10-16 09:18:40 +00:00
return &TorrentFs{
2024-03-20 21:47:51 +00:00
name: name,
Torrent: c,
resolver: newResolver(ArchiveFactories),
}
}
2024-03-19 21:30:37 +00:00
var _ fs.DirEntry = (*TorrentFs)(nil)
// Name implements fs.DirEntry.
func (tfs *TorrentFs) Name() string {
return tfs.name
}
// Info implements fs.DirEntry.
func (tfs *TorrentFs) Info() (fs.FileInfo, error) {
return newDirInfo(tfs.name), nil
}
// IsDir implements fs.DirEntry.
func (tfs *TorrentFs) IsDir() bool {
return true
}
// Type implements fs.DirEntry.
func (tfs *TorrentFs) Type() fs.FileMode {
return fs.ModeDir
}
2024-03-20 21:47:51 +00:00
func (fs *TorrentFs) files(ctx context.Context) (map[string]File, error) {
2024-01-28 20:22:49 +00:00
fs.mu.Lock()
defer fs.mu.Unlock()
2023-12-25 22:11:03 +00:00
2024-01-28 20:22:49 +00:00
if fs.filesCache != nil {
return fs.filesCache, nil
}
2023-12-25 22:11:03 +00:00
2024-03-19 21:30:37 +00:00
files, err := fs.Torrent.Files(context.Background())
2024-01-28 20:22:49 +00:00
if err != nil {
return nil, err
}
2024-01-01 18:17:32 +00:00
2024-01-28 20:22:49 +00:00
fs.filesCache = make(map[string]File)
for _, file := range files {
2024-03-17 21:00:34 +00:00
file.Download()
2024-01-28 20:22:49 +00:00
p := AbsPath(file.Path())
2024-03-20 21:47:51 +00:00
tf, err := openTorrentFile(ctx, path.Base(p), file)
if err != nil {
return nil, err
2024-01-28 20:22:49 +00:00
}
2024-03-20 21:47:51 +00:00
fs.filesCache[p] = tf
2024-01-28 20:22:49 +00:00
}
// TODO optional
2024-03-19 21:30:37 +00:00
if len(fs.filesCache) == 1 && fs.resolver.isNestedFs(fs.Torrent.Name()) {
filepath := "/" + fs.Torrent.Name()
2024-01-28 20:22:49 +00:00
if file, ok := fs.filesCache[filepath]; ok {
2024-03-20 21:47:51 +00:00
nestedFs, err := fs.resolver.nestedFs(ctx, filepath, file)
2024-01-28 20:22:49 +00:00
if err != nil {
return nil, err
}
if nestedFs == nil {
goto DEFAULT_DIR // FIXME
}
2024-03-20 21:47:51 +00:00
fs.filesCache, err = listFilesRecursive(ctx, nestedFs, "/")
2024-01-28 20:22:49 +00:00
if err != nil {
return nil, err
2023-12-25 22:11:03 +00:00
}
2024-01-28 20:22:49 +00:00
return fs.filesCache, nil
}
2024-01-01 18:17:32 +00:00
2024-01-28 20:22:49 +00:00
}
DEFAULT_DIR:
2024-03-19 21:30:37 +00:00
rootDir := "/" + fs.Torrent.Name() + "/"
2024-01-28 20:22:49 +00:00
singleDir := true
for k, _ := range fs.filesCache {
if !strings.HasPrefix(k, rootDir) {
singleDir = false
2023-10-16 09:18:40 +00:00
}
2024-01-28 20:22:49 +00:00
}
if singleDir {
for k, f := range fs.filesCache {
delete(fs.filesCache, k)
k, _ = strings.CutPrefix(k, rootDir)
k = AbsPath(k)
fs.filesCache[k] = f
}
}
return fs.filesCache, nil
}
2024-01-07 17:09:56 +00:00
2024-03-20 21:47:51 +00:00
// func anyPeerHasFiles(file *torrent.File) bool {
// for _, conn := range file.Torrent().PeerConns() {
// if bitmapHaveFile(conn.PeerPieces(), file) {
// return true
// }
// }
// return false
// }
// func bitmapHaveFile(bitmap *roaring.Bitmap, file *torrent.File) bool {
// for i := file.BeginPieceIndex(); i < file.EndPieceIndex(); i++ {
// if !bitmap.ContainsInt(i) {
// return false
// }
// }
// return true
// }
func listFilesRecursive(ctx context.Context, vfs Filesystem, start string) (map[string]File, error) {
2024-01-28 20:22:49 +00:00
out := make(map[string]File, 0)
2024-03-20 21:47:51 +00:00
entries, err := vfs.ReadDir(ctx, start)
2024-01-28 20:22:49 +00:00
if err != nil {
return nil, err
}
for _, entry := range entries {
filename := path.Join(start, entry.Name())
if entry.IsDir() {
2024-03-20 21:47:51 +00:00
rec, err := listFilesRecursive(ctx, vfs, filename)
2024-01-28 20:22:49 +00:00
if err != nil {
return nil, err
2024-01-07 17:09:56 +00:00
}
2024-01-28 20:22:49 +00:00
maps.Copy(out, rec)
} else {
2024-03-20 21:47:51 +00:00
file, err := vfs.Open(ctx, filename)
2024-01-28 20:22:49 +00:00
if err != nil {
return nil, err
2024-01-07 17:09:56 +00:00
}
2024-01-28 20:22:49 +00:00
out[filename] = file
2024-01-07 17:09:56 +00:00
}
2023-10-16 09:18:40 +00:00
}
2024-01-28 20:22:49 +00:00
return out, nil
2023-10-16 09:18:40 +00:00
}
2024-03-20 21:47:51 +00:00
func (fs *TorrentFs) rawOpen(ctx context.Context, path string) (File, error) {
files, err := fs.files(ctx)
2023-12-25 22:11:03 +00:00
if err != nil {
return nil, err
}
file, err := getFile(files, path)
2023-10-16 09:18:40 +00:00
return file, err
}
2024-03-20 21:47:51 +00:00
func (fs *TorrentFs) rawStat(ctx context.Context, filename string) (fs.FileInfo, error) {
files, err := fs.files(ctx)
2023-12-25 22:11:03 +00:00
if err != nil {
return nil, err
}
file, err := getFile(files, filename)
2023-12-21 23:15:39 +00:00
if err != nil {
return nil, err
}
2024-01-28 20:22:49 +00:00
return file.Stat()
2023-12-21 23:15:39 +00:00
}
// Stat implements Filesystem.
2024-03-20 21:47:51 +00:00
func (fs *TorrentFs) Stat(ctx context.Context, filename string) (fs.FileInfo, error) {
2023-12-21 23:15:39 +00:00
if filename == Separator {
return newDirInfo(filename), nil
}
2024-03-20 21:47:51 +00:00
fsPath, nestedFs, nestedFsPath, err := fs.resolver.resolvePath(ctx, filename, fs.rawOpen)
2023-12-21 23:15:39 +00:00
if err != nil {
return nil, err
}
if nestedFs != nil {
2024-03-20 21:47:51 +00:00
return nestedFs.Stat(ctx, nestedFsPath)
2023-12-21 23:15:39 +00:00
}
2024-03-20 21:47:51 +00:00
return fs.rawStat(ctx, fsPath)
2023-12-21 23:15:39 +00:00
}
2024-03-20 21:47:51 +00:00
func (fs *TorrentFs) Open(ctx context.Context, filename string) (File, error) {
fsPath, nestedFs, nestedFsPath, err := fs.resolver.resolvePath(ctx, filename, fs.rawOpen)
2023-10-16 09:18:40 +00:00
if err != nil {
return nil, err
}
2023-10-16 09:18:40 +00:00
if nestedFs != nil {
2024-03-20 21:47:51 +00:00
return nestedFs.Open(ctx, nestedFsPath)
}
2024-03-20 21:47:51 +00:00
return fs.rawOpen(ctx, fsPath)
}
2024-03-20 21:47:51 +00:00
func (fs *TorrentFs) ReadDir(ctx context.Context, name string) ([]fs.DirEntry, error) {
fsPath, nestedFs, nestedFsPath, err := fs.resolver.resolvePath(ctx, name, fs.rawOpen)
2023-10-16 09:18:40 +00:00
if err != nil {
return nil, err
}
if nestedFs != nil {
2024-03-20 21:47:51 +00:00
return nestedFs.ReadDir(ctx, nestedFsPath)
2023-10-16 09:18:40 +00:00
}
2024-03-20 21:47:51 +00:00
files, err := fs.files(ctx)
2023-12-25 22:11:03 +00:00
if err != nil {
return nil, err
}
2023-12-25 22:11:03 +00:00
return listDirFromFiles(files, fsPath)
}
2024-03-20 21:47:51 +00:00
func (fs *TorrentFs) Unlink(ctx context.Context, name string) error {
2023-12-31 22:54:55 +00:00
name = AbsPath(name)
2023-12-25 22:11:03 +00:00
fs.mu.Lock()
defer fs.mu.Unlock()
2024-03-20 21:47:51 +00:00
files, err := fs.files(ctx)
2023-12-25 22:11:03 +00:00
if err != nil {
return err
}
2023-12-31 22:54:55 +00:00
if !slices.Contains(maps.Keys(files), name) {
2023-12-25 22:11:03 +00:00
return ErrNotExist
}
2023-12-31 22:54:55 +00:00
file := files[name]
delete(fs.filesCache, name)
2024-01-28 20:22:49 +00:00
tfile, ok := file.(*torrentFile)
if !ok {
return ErrNotImplemented
}
2024-03-19 21:30:37 +00:00
return fs.Torrent.ExcludeFile(context.Background(), tfile.file)
2023-10-18 09:52:48 +00:00
}
2024-03-20 21:47:51 +00:00
var _ File = &torrentFile{}
type torrentFile struct {
name string
mu sync.Mutex
tr torrent.Reader
file *torrent.File
}
2024-03-20 21:47:51 +00:00
func openTorrentFile(ctx context.Context, name string, file *torrent.File) (*torrentFile, error) {
select {
case <-file.Torrent().GotInfo():
break
case <-ctx.Done():
return nil, ctx.Err()
}
r := file.NewReader()
r.SetReadahead(4096) // TODO configurable
r.SetResponsive()
2024-03-20 21:47:51 +00:00
return &torrentFile{
name: name,
tr: r,
file: file,
}, nil
}
2024-03-20 21:47:51 +00:00
func (tf *torrentFile) Stat() (fs.FileInfo, error) {
return newFileInfo(tf.name, tf.file.Length()), nil
2021-11-29 10:07:54 +00:00
}
2024-03-20 21:47:51 +00:00
func (tf *torrentFile) Size() int64 {
return tf.file.Length()
}
func (tf *torrentFile) IsDir() bool {
return false
}
func (rw *torrentFile) Close(ctx context.Context) error {
2021-12-01 18:59:21 +00:00
rw.mu.Lock()
defer rw.mu.Unlock()
2024-03-20 21:47:51 +00:00
return rw.tr.Close()
}
// Read implements ctxio.Reader.
func (tf *torrentFile) Read(ctx context.Context, p []byte) (n int, err error) {
tf.mu.Lock()
defer tf.mu.Unlock()
return tf.tr.ReadContext(ctx, p)
}
func (yf *torrentFile) ReadAt(ctx context.Context, p []byte, off int64) (int, error) {
yf.mu.Lock()
defer yf.mu.Unlock()
_, err := yf.tr.Seek(off, io.SeekStart)
2021-11-29 10:07:54 +00:00
if err != nil {
return 0, err
}
2021-12-01 18:59:21 +00:00
2024-03-20 21:47:51 +00:00
return readAtLeast(ctx, yf, p, len(p))
}
2024-03-20 21:47:51 +00:00
func readAtLeast(ctx context.Context, r ctxio.Reader, buf []byte, min int) (n int, err error) {
2021-11-29 10:07:54 +00:00
if len(buf) < min {
return 0, io.ErrShortBuffer
}
for n < min && err == nil {
var nn int
2024-03-20 21:47:51 +00:00
nn, err = r.Read(ctx, buf[n:])
2021-11-29 10:07:54 +00:00
n += nn
}
if n >= min {
err = nil
} else if n > 0 && err == io.EOF {
err = io.ErrUnexpectedEOF
}
return
}