2024-08-22 22:16:16 +00:00
|
|
|
package qbittorrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-24 13:26:15 +00:00
|
|
|
"errors"
|
2024-08-31 23:00:13 +00:00
|
|
|
"fmt"
|
2024-08-22 22:16:16 +00:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2024-09-24 13:26:15 +00:00
|
|
|
"log/slog"
|
2024-08-22 22:16:16 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-08-31 23:00:13 +00:00
|
|
|
"strings"
|
2024-09-24 13:26:15 +00:00
|
|
|
"sync"
|
2024-08-22 22:16:16 +00:00
|
|
|
"time"
|
|
|
|
|
2024-08-31 23:00:13 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/pkg/qbittorrent"
|
2024-09-24 13:26:15 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/pkg/rlog"
|
2024-10-14 00:58:42 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/pkg/uring"
|
2024-08-22 22:16:16 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/vfs"
|
2024-10-14 00:58:42 +00:00
|
|
|
"github.com/iceber/iouring-go"
|
2024-08-22 22:16:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FS struct {
|
2024-09-24 13:26:15 +00:00
|
|
|
mu sync.Mutex
|
2024-08-31 23:00:13 +00:00
|
|
|
client *cacheClient
|
2024-08-22 22:16:16 +00:00
|
|
|
name string
|
|
|
|
hash string
|
2024-09-24 13:26:15 +00:00
|
|
|
dataDir string // directory where torrent files are stored
|
2024-08-31 23:00:13 +00:00
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
ur *iouring.IOURing
|
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
entries map[string]fileEntry
|
|
|
|
|
|
|
|
log *rlog.Logger
|
2024-08-31 23:00:13 +00:00
|
|
|
|
|
|
|
vfs.FilesystemPrototype
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
type fileEntry struct {
|
|
|
|
fs.FileInfo
|
|
|
|
Content *qbittorrent.TorrentContent
|
|
|
|
}
|
|
|
|
|
2024-08-22 22:16:16 +00:00
|
|
|
var _ vfs.Filesystem = (*FS)(nil)
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
func newTorrentFS(ctx context.Context, ur *iouring.IOURing, client *cacheClient, name string, hash string, dataDir string) (*FS, error) {
|
2024-09-24 13:26:15 +00:00
|
|
|
ctx, span := trace.Start(ctx, "newTorrentFS")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-31 23:00:13 +00:00
|
|
|
cnts, err := client.listContent(ctx, hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to list content for hash %s: %w", hash, err)
|
|
|
|
}
|
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
entries := make(map[string]fileEntry, len(cnts))
|
2024-08-31 23:00:13 +00:00
|
|
|
for _, cnt := range cnts {
|
2024-09-24 13:26:15 +00:00
|
|
|
if cnt.Priority == qbittorrent.PriorityDoNotDownload {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
entries[vfs.AbsPath(cnt.Name)] = fileEntry{
|
|
|
|
Content: cnt,
|
|
|
|
FileInfo: vfs.NewFileInfo(cnt.Name, cnt.Size),
|
|
|
|
}
|
2024-08-31 23:00:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 22:16:16 +00:00
|
|
|
return &FS{
|
2024-08-31 23:00:13 +00:00
|
|
|
client: client,
|
|
|
|
name: name,
|
|
|
|
hash: hash,
|
|
|
|
|
2024-08-22 22:16:16 +00:00
|
|
|
dataDir: dataDir,
|
2024-08-31 23:00:13 +00:00
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
entries: entries,
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
ur: ur,
|
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
log: rlog.Component("qbittorrent", "fs"),
|
2024-08-31 23:00:13 +00:00
|
|
|
|
|
|
|
FilesystemPrototype: vfs.FilesystemPrototype(name),
|
2024-08-22 22:16:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-08-31 23:00:13 +00:00
|
|
|
// Open implements vfs.Filesystem.
|
|
|
|
func (f *FS) Open(ctx context.Context, name string) (vfs.File, error) {
|
|
|
|
if name == vfs.Separator {
|
|
|
|
return vfs.NewDirFile(name), nil
|
|
|
|
}
|
2024-08-22 22:16:16 +00:00
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
if entry, ok := f.entries[name]; ok {
|
2024-10-14 00:58:42 +00:00
|
|
|
return openFile(ctx, f.ur, f.client, f.dataDir, f.hash, entry.Content)
|
2024-08-31 23:00:13 +00:00
|
|
|
}
|
2024-08-22 22:16:16 +00:00
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
for p := range f.entries {
|
2024-08-31 23:00:13 +00:00
|
|
|
if strings.HasPrefix(p, name) {
|
|
|
|
return vfs.NewDirFile(name), nil
|
|
|
|
}
|
|
|
|
}
|
2024-08-22 22:16:16 +00:00
|
|
|
|
2024-08-31 23:00:13 +00:00
|
|
|
return nil, vfs.ErrNotExist
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDir implements vfs.Filesystem.
|
2024-09-24 13:26:15 +00:00
|
|
|
func (f *FS) ReadDir(ctx context.Context, name string) ([]fs.DirEntry, error) {
|
|
|
|
infos := make(map[string]fs.FileInfo, len(f.entries))
|
|
|
|
for k, v := range f.entries {
|
|
|
|
infos[k] = v.FileInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
return vfs.ListDirFromInfo(infos, name)
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat implements vfs.Filesystem.
|
2024-08-31 23:00:13 +00:00
|
|
|
func (f *FS) Stat(ctx context.Context, name string) (fs.FileInfo, error) {
|
2024-09-24 13:26:15 +00:00
|
|
|
name = vfs.AbsPath(path.Clean(name))
|
|
|
|
|
|
|
|
if vfs.IsRoot(name) {
|
|
|
|
return vfs.NewDirInfo(f.name), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry, ok := f.entries[name]; ok {
|
|
|
|
return entry.FileInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for p := range f.entries {
|
|
|
|
if strings.HasPrefix(p, name) {
|
|
|
|
return vfs.NewDirInfo(name), nil
|
|
|
|
}
|
2024-08-31 23:00:13 +00:00
|
|
|
}
|
2024-09-24 13:26:15 +00:00
|
|
|
|
|
|
|
return nil, vfs.ErrNotExist
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlink implements vfs.Filesystem.
|
|
|
|
func (f *FS) Unlink(ctx context.Context, filename string) error {
|
2024-09-24 13:26:15 +00:00
|
|
|
filename = vfs.AbsPath(path.Clean(filename))
|
|
|
|
|
|
|
|
// we cannot delete a torrent itself, cause it will be added on next source scan and all delited files will be restored
|
|
|
|
|
|
|
|
if entry, ok := f.entries[filename]; ok {
|
|
|
|
return f.removeFile(ctx, f.hash, entry.Content)
|
|
|
|
}
|
|
|
|
|
|
|
|
for p, entry := range f.entries {
|
|
|
|
if strings.HasPrefix(p, filename) {
|
|
|
|
return f.removeFile(ctx, f.hash, entry.Content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vfs.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
func (f *FS) Rename(ctx context.Context, oldpath string, newpath string) error {
|
|
|
|
oldpath = vfs.AbsPath(path.Clean(oldpath))
|
|
|
|
newpath = vfs.AbsPath(path.Clean(newpath))
|
|
|
|
|
|
|
|
if _, ok := f.entries[oldpath]; ok {
|
|
|
|
err := f.client.qb.Torrent().RenameFile(ctx, f.hash, vfs.RelPath(oldpath), vfs.RelPath(newpath))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to rename file %s to %s: %w", oldpath, newpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
f.entries[newpath] = f.entries[oldpath]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return vfs.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
func (f *FS) removeFile(ctx context.Context, hash string, content *qbittorrent.TorrentContent) error {
|
|
|
|
log := f.log.With(slog.String("hash", hash), slog.String("file", content.Name))
|
|
|
|
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
fpath := vfs.AbsPath(content.Name)
|
|
|
|
|
|
|
|
if _, ok := f.entries[fpath]; !ok {
|
|
|
|
return fmt.Errorf("file %s is does not found", fpath)
|
|
|
|
}
|
|
|
|
delete(f.entries, fpath)
|
|
|
|
|
|
|
|
err := f.client.qb.Torrent().SetFilePriority(ctx, f.hash, content.Index, qbittorrent.PriorityDoNotDownload)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set priority for torrent %s for file %s: %w", hash, content.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(path.Join(f.dataDir, vfs.RelPath(content.Name)))
|
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
log.Warn(ctx, "failed to remove file", rlog.Error(err))
|
|
|
|
return fmt.Errorf("failed to remove file %s: %w", content.Name, err)
|
|
|
|
}
|
|
|
|
return nil
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
func openFile(ctx context.Context, ur *iouring.IOURing, client *cacheClient, torrentDir string, hash string, content *qbittorrent.TorrentContent) (*File, error) {
|
2024-08-31 23:00:13 +00:00
|
|
|
props, err := client.getProperties(ctx, hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-22 22:16:16 +00:00
|
|
|
|
2024-10-19 01:24:14 +00:00
|
|
|
// FIXME error when file not started downloading
|
2024-10-14 00:58:42 +00:00
|
|
|
file, err := os.OpenFile(path.Join(torrentDir, content.Name), os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-22 22:16:16 +00:00
|
|
|
return &File{
|
2024-08-31 23:00:13 +00:00
|
|
|
client: client,
|
|
|
|
hash: hash,
|
|
|
|
torrentDir: torrentDir,
|
|
|
|
|
|
|
|
filePath: content.Name,
|
|
|
|
contentIndex: content.Index,
|
|
|
|
pieceSize: props.PieceSize,
|
|
|
|
fileSize: content.Size,
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
file: uring.NewFile(ur, file),
|
|
|
|
|
2024-08-31 23:00:13 +00:00
|
|
|
offset: 0,
|
|
|
|
}, nil
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
2024-08-31 23:00:13 +00:00
|
|
|
client *cacheClient
|
2024-08-22 22:16:16 +00:00
|
|
|
hash string
|
2024-08-31 23:00:13 +00:00
|
|
|
torrentDir string
|
2024-08-22 22:16:16 +00:00
|
|
|
filePath string // path inside a torrent directory
|
|
|
|
contentIndex int
|
|
|
|
pieceSize int
|
|
|
|
fileSize int64
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
file *uring.File
|
2024-08-22 22:16:16 +00:00
|
|
|
offset int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ vfs.File = (*File)(nil)
|
|
|
|
|
|
|
|
// Info implements vfs.File.
|
|
|
|
func (f *File) Info() (fs.FileInfo, error) {
|
|
|
|
return &fileInfo{name: path.Base(f.filePath), size: f.fileSize}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDir implements vfs.File.
|
|
|
|
func (f *File) IsDir() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek implements vfs.File.
|
|
|
|
func (f *File) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
f.offset = offset
|
|
|
|
case io.SeekCurrent:
|
|
|
|
f.offset += offset
|
|
|
|
case io.SeekEnd:
|
|
|
|
f.offset = f.fileSize + offset
|
|
|
|
}
|
|
|
|
return f.offset, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements vfs.File.
|
|
|
|
func (f *File) Name() string {
|
|
|
|
return path.Base(f.filePath)
|
|
|
|
}
|
|
|
|
|
2024-09-24 13:26:15 +00:00
|
|
|
func (f *File) canExpectSoon(ctx context.Context) (bool, error) {
|
|
|
|
info, err := f.client.getInfo(ctx, f.hash)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.Completed == info.Size || info.State == qbittorrent.TorrentStateCheckingUP || info.State == qbittorrent.TorrentStateDownloading || info.State == qbittorrent.TorrentStateForcedDL, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) isRangeComplete(ctx context.Context, offset int64, size int) (bool, error) {
|
|
|
|
startPieceIndex := int(offset / int64(f.pieceSize))
|
|
|
|
pieceCount := (size + f.pieceSize - 1) / f.pieceSize // rouding up
|
|
|
|
|
|
|
|
for i := range pieceCount {
|
|
|
|
ok, err := f.client.isPieceComplete(ctx, f.hash, startPieceIndex+i)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) waitPieceAvailable(ctx context.Context, offset int64, size int) error {
|
|
|
|
complete, err := f.isRangeComplete(ctx, offset, size)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if complete {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
canExpectSoon, err := f.canExpectSoon(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !canExpectSoon {
|
|
|
|
return fmt.Errorf("torrent is not downloading")
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkingInterval = 1 * time.Second
|
|
|
|
|
|
|
|
ticker := time.NewTicker(checkingInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-ticker.C:
|
|
|
|
complete, err := f.isRangeComplete(ctx, offset, size)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if complete {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-22 22:16:16 +00:00
|
|
|
// Read implements vfs.File.
|
2024-10-14 00:58:42 +00:00
|
|
|
func (f *File) Read(ctx context.Context, p []byte) (int, error) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
2024-08-22 22:16:16 +00:00
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
if err := f.waitPieceAvailable(ctx, f.offset, len(p)); err != nil {
|
2024-08-22 22:16:16 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
n, err := f.file.ReadAt(ctx, p, f.offset)
|
2024-08-22 22:16:16 +00:00
|
|
|
f.offset += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAt implements vfs.File.
|
2024-10-14 00:58:42 +00:00
|
|
|
func (f *File) ReadAt(ctx context.Context, p []byte, off int64) (int, error) {
|
2024-09-24 13:26:15 +00:00
|
|
|
if err := f.waitPieceAvailable(ctx, f.offset, len(p)); err != nil {
|
2024-08-22 22:16:16 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-10-14 00:58:42 +00:00
|
|
|
return f.file.ReadAt(ctx, p, off)
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements vfs.File.
|
|
|
|
func (f *File) Size() int64 {
|
|
|
|
return f.fileSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements vfs.File.
|
|
|
|
func (f *File) Type() fs.FileMode {
|
2024-08-31 23:00:13 +00:00
|
|
|
return fs.ModeDir
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-31 23:00:13 +00:00
|
|
|
// Close implements vfs.File.
|
|
|
|
func (f *File) Close(ctx context.Context) error {
|
2024-10-14 00:58:42 +00:00
|
|
|
return f.file.Close(ctx)
|
2024-08-31 23:00:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 22:16:16 +00:00
|
|
|
type fileInfo struct {
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fs.FileInfo = (*fileInfo)(nil)
|
|
|
|
|
|
|
|
// IsDir implements fs.FileInfo.
|
|
|
|
func (f *fileInfo) IsDir() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime implements fs.FileInfo.
|
|
|
|
func (f *fileInfo) ModTime() time.Time {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode implements fs.FileInfo.
|
|
|
|
func (f *fileInfo) Mode() fs.FileMode {
|
2024-11-15 13:39:56 +00:00
|
|
|
return vfs.ModeFileRO
|
2024-08-22 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements fs.FileInfo.
|
|
|
|
func (f *fileInfo) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements fs.FileInfo.
|
|
|
|
func (f *fileInfo) Size() int64 {
|
|
|
|
return f.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sys implements fs.FileInfo.
|
|
|
|
func (f *fileInfo) Sys() any {
|
|
|
|
return nil
|
|
|
|
}
|