fix exclude file

This commit is contained in:
royalcat 2024-06-18 22:27:18 +03:00
parent 3afd12dce3
commit 3a2b615f55
4 changed files with 28 additions and 12 deletions
src/sources/torrent

View file

@ -29,11 +29,11 @@ type Controller struct {
log *rlog.Logger
}
func newController(t *torrent.Torrent, fileProperties kv.Store[string, FileProperties], storage TorrentFileDeleter) *Controller {
func newController(t *torrent.Torrent, torrentFileProperties kv.Store[string, FileProperties], storage TorrentFileDeleter) *Controller {
return &Controller{
t: t,
storage: storage,
fileProperties: fileProperties,
fileProperties: torrentFileProperties,
log: rlog.Component("torrent-client", "controller").With(slog.String("infohash", t.InfoHash().HexString())),
}
}
@ -123,10 +123,20 @@ func (s *Controller) ExcludeFile(ctx context.Context, f *torrent.File) error {
log := s.log.With(slog.String("file", f.Path()))
log.Info(ctx, "excluding file")
return s.fileProperties.Edit(ctx, f.Path(), func(ctx context.Context, v FileProperties) (FileProperties, error) {
err := s.fileProperties.Edit(ctx, f.Path(), func(ctx context.Context, v FileProperties) (FileProperties, error) {
v.Excluded = true
return v, nil
})
if err == kv.ErrKeyNotFound {
err := s.fileProperties.Set(ctx, f.Path(), FileProperties{Excluded: true})
if err != nil {
return err
}
} else if err != nil {
return err
}
return s.storage.DeleteFile(f)
}
func (s *Controller) isFileComplete(startIndex int, endIndex int) bool {

View file

@ -22,10 +22,10 @@ import (
type TorrentFS struct {
name string
mu sync.Mutex
Torrent *Controller
filesCache map[string]vfs.File
filesCacheMu sync.Mutex
filesCache map[string]vfs.File
lastTorrentReadTimeout atomic.Pointer[time.Time]
@ -98,8 +98,8 @@ func (tfs *TorrentFS) FsName() string {
}
func (fs *TorrentFS) files(ctx context.Context) (map[string]vfs.File, error) {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.filesCacheMu.Lock()
defer fs.filesCacheMu.Unlock()
if fs.filesCache != nil {
return fs.filesCache, nil
@ -348,9 +348,6 @@ func (fs *TorrentFS) Unlink(ctx context.Context, name string) error {
name = vfs.AbsPath(name)
fs.mu.Lock()
defer fs.mu.Unlock()
files, err := fs.files(ctx)
if err != nil {
return err
@ -361,7 +358,10 @@ func (fs *TorrentFS) Unlink(ctx context.Context, name string) error {
}
file := files[name]
fs.filesCacheMu.Lock()
delete(fs.filesCache, name)
fs.filesCacheMu.Unlock()
tfile, ok := file.(*torrentFile)
if !ok {