[feature] file exclude
Some checks failed
mkdocs / mkdocs (push) Failing after 15s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 3s
docker / build-docker (inux/arm/v6) (push) Failing after 14s
docker / build-docker (linux/386) (push) Failing after 15s
docker / build-docker (linux/amd64) (push) Failing after 7s
docker / build-docker (linux/arm/v7) (push) Failing after 15s
docker / build-docker (linux/arm64) (push) Failing after 15s

This commit is contained in:
royalcat 2023-10-17 21:30:10 +03:00
parent 741bae49a4
commit 5f292a0591
7 changed files with 79 additions and 115 deletions

View file

@ -24,9 +24,6 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

View file

@ -47,6 +47,10 @@ func (s *Service) NewTorrentFs(f vfs.File) (vfs.Filesystem, error) {
}
<-t.GotInfo()
t.AllowDataDownload()
for _, f := range t.Files() {
f.SetPriority(torrent.PiecePriorityReadahead)
}
return vfs.NewTorrentFs(t, s.readTimeout), nil
}

View file

@ -2,6 +2,7 @@ package vfs
import (
"fmt"
"io/fs"
"strings"
"sync"
)
@ -119,28 +120,42 @@ PARTS_LOOP:
}
// func (r *resolver) resolveFile(name string, fs Filesystem) (File, error) {
// fsPath, nestedFs, nestedFsPath, err := r.resolvePath(name, fs)
// if err != nil {
// return nil, err
// }
var ErrNotExist = fs.ErrNotExist
// if nestedFs == nil {
// return fs.Open(fsPath)
// }
func getFile[F File](m map[string]F, name string) (File, error) {
name = clean(name)
if name == Separator {
return &Dir{}, nil
}
// return nestedFs.Open(nestedFsPath)
// }
f, ok := m[name]
if ok {
return f, nil
}
// func (r *resolver) resolveDir(name string, fs Filesystem) (map[string]File, error) {
// fsPath, nestedFs, nestedFsPath, err := r.resolvePath(name, fs)
// if err != nil {
// return nil, err
// }
for p := range m {
if strings.HasPrefix(p, name) {
return &Dir{}, nil
}
}
// if nestedFs == nil {
// return fs.ReadDir(fsPath)
// }
return nil, ErrNotExist
}
// return nestedFs.ReadDir(nestedFsPath)
// }
func listFilesInDir[F File](m map[string]F, name string) (map[string]File, error) {
name = clean(name)
out := map[string]File{}
for p, f := range m {
if strings.HasPrefix(p, name) {
parts := strings.Split(trimRelPath(p, name), Separator)
if len(parts) == 1 {
out[parts[0]] = f
} else {
out[parts[0]] = &Dir{}
}
}
}
return out, nil
}

View file

@ -144,3 +144,29 @@ func TestArchiveFactories(t *testing.T) {
require.NoError(err)
require.NotNil(fs)
}
func TestFiles(t *testing.T) {
t.Parallel()
require := require.New(t)
files := map[string]*Dummy{
"/test/file.txt": &Dummy{},
}
{
file, err := getFile(files, "/test")
require.Nil(err)
require.Equal(&Dir{}, file)
}
{
file, err := getFile(files, "/test/file.txt")
require.Nil(err)
require.Equal(&Dummy{}, file)
}
{
out, err := listFilesInDir(files, "/test")
require.Nil(err)
require.Contains(out, "file.txt")
require.Equal(&Dummy{}, out["file.txt"])
}
}

View file

@ -33,6 +33,10 @@ func (fs *TorrentFs) files() map[string]*torrentFile {
files := make(map[string]*torrentFile)
<-fs.t.GotInfo()
for _, file := range fs.t.Files() {
if file.Priority() == torrent.PiecePriorityNone {
continue
}
p := clean(file.Path())
files[p] = &torrentFile{
readerFunc: file.NewReader,
@ -73,6 +77,12 @@ func (fs *TorrentFs) ReadDir(name string) (map[string]File, error) {
return listFilesInDir(fs.files(), fsPath)
}
func (fs *TorrentFs) Unlink(name string) error {
file := fs.t.Files()[0]
file.SetPriority(torrent.PiecePriorityNone)
return nil
}
type reader interface {
iio.Reader
missinggo.ReadContexter
@ -111,18 +121,11 @@ func readAtLeast(r missinggo.ReadContexter, timeout int, buf []byte, min int) (n
for n < min && err == nil {
var nn int
ctx, cancel := context.WithCancel(context.Background())
timer := time.AfterFunc(
time.Duration(timeout)*time.Second,
func() {
cancel()
},
)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
nn, err = r.ReadContext(ctx, buf[n:])
n += nn
timer.Stop()
}
if n >= min {
err = nil
@ -175,15 +178,8 @@ func (d *torrentFile) Close() error {
func (d *torrentFile) Read(p []byte) (n int, err error) {
d.load()
ctx, cancel := context.WithCancel(context.Background())
timer := time.AfterFunc(
time.Duration(d.timeout)*time.Second,
func() {
cancel()
},
)
defer timer.Stop()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(d.timeout)*time.Second)
defer cancel()
return d.reader.ReadContext(ctx, p)
}

View file

@ -1,51 +1,10 @@
package vfs
import (
"io/fs"
"path"
"strings"
)
var ErrNotExist = fs.ErrNotExist
func getFile[F File](m map[string]F, name string) (File, error) {
name = clean(name)
if name == Separator {
return &Dir{}, nil
}
f, ok := m[name]
if ok {
return f, nil
}
for p := range m {
if strings.HasPrefix(p, name) {
return &Dir{}, nil
}
}
return nil, ErrNotExist
}
func listFilesInDir[F File](m map[string]F, name string) (map[string]File, error) {
name = clean(name)
out := map[string]File{}
for p, f := range m {
if strings.HasPrefix(p, name) {
parts := strings.Split(trimRelPath(p, name), Separator)
if len(parts) == 1 {
out[parts[0]] = f
} else {
out[parts[0]] = &Dir{}
}
}
}
return out, nil
}
func trimRelPath(p, t string) string {
return strings.Trim(strings.TrimPrefix(p, t), "/")
}

View file

@ -1,33 +0,0 @@
package vfs
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestFiles(t *testing.T) {
t.Parallel()
require := require.New(t)
files := map[string]*Dummy{
"/test/file.txt": &Dummy{},
}
{
file, err := getFile(files, "/test")
require.Nil(err)
require.Equal(&Dir{}, file)
}
{
file, err := getFile(files, "/test/file.txt")
require.Nil(err)
require.Equal(&Dummy{}, file)
}
{
out, err := listFilesInDir(files, "/test")
require.Nil(err)
require.Contains(out, "file.txt")
require.Equal(&Dummy{}, out["file.txt"])
}
}