fix exclude file
Some checks failed
docker / build-docker (linux/amd64) (push) Successful in 1m54s
docker / build-docker (linux/arm64) (push) Has been cancelled

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

4
go.mod
View file

@ -35,8 +35,8 @@ require (
github.com/ravilushqa/otelgqlgen v0.15.0 github.com/ravilushqa/otelgqlgen v0.15.0
github.com/royalcat/ctxio v0.0.0-20240602060200-590d464c39be github.com/royalcat/ctxio v0.0.0-20240602060200-590d464c39be
github.com/royalcat/ctxprogress v0.0.0-20240614113930-3cc5bb935bff github.com/royalcat/ctxprogress v0.0.0-20240614113930-3cc5bb935bff
github.com/royalcat/kv v0.0.0-20240615090409-961d9afa99b6 github.com/royalcat/kv v0.0.0-20240617101007-c9c746b3916f
github.com/royalcat/kv/kvbadger v0.0.0-20240615090409-961d9afa99b6 github.com/royalcat/kv/kvbadger v0.0.0-20240617101007-c9c746b3916f
github.com/rs/zerolog v1.32.0 github.com/rs/zerolog v1.32.0
github.com/samber/slog-multi v1.0.2 github.com/samber/slog-multi v1.0.2
github.com/samber/slog-zerolog v1.0.0 github.com/samber/slog-zerolog v1.0.0

6
go.sum
View file

@ -545,10 +545,16 @@ github.com/royalcat/ctxprogress v0.0.0-20240614113930-3cc5bb935bff h1:KlZaOEZYhC
github.com/royalcat/ctxprogress v0.0.0-20240614113930-3cc5bb935bff/go.mod h1:RcUpbosy/m3bJ3JsVO18MXEbrKRHOHkmYBXigDGekaA= github.com/royalcat/ctxprogress v0.0.0-20240614113930-3cc5bb935bff/go.mod h1:RcUpbosy/m3bJ3JsVO18MXEbrKRHOHkmYBXigDGekaA=
github.com/royalcat/kv v0.0.0-20240615090409-961d9afa99b6 h1:rGXhPFpOVLeOO/Da2qxBNZY5yaQdTCGxQV2dUDXXf7U= github.com/royalcat/kv v0.0.0-20240615090409-961d9afa99b6 h1:rGXhPFpOVLeOO/Da2qxBNZY5yaQdTCGxQV2dUDXXf7U=
github.com/royalcat/kv v0.0.0-20240615090409-961d9afa99b6/go.mod h1:UB/VwpTut8c3IXLJFvYWFxAAZymk9eBuJRMJmpSpwYU= github.com/royalcat/kv v0.0.0-20240615090409-961d9afa99b6/go.mod h1:UB/VwpTut8c3IXLJFvYWFxAAZymk9eBuJRMJmpSpwYU=
github.com/royalcat/kv v0.0.0-20240617074755-245a773511b7 h1:ZtxuWjMwbFwaj5zcT0VZqvsyViYYzYJkprFe/9p5PUs=
github.com/royalcat/kv v0.0.0-20240617074755-245a773511b7/go.mod h1:UB/VwpTut8c3IXLJFvYWFxAAZymk9eBuJRMJmpSpwYU=
github.com/royalcat/kv v0.0.0-20240617101007-c9c746b3916f h1:bG8Pp/YXkpC2eFI7psTiTAL7QTBqHQcP/lEpiqmBXn4=
github.com/royalcat/kv v0.0.0-20240617101007-c9c746b3916f/go.mod h1:UB/VwpTut8c3IXLJFvYWFxAAZymk9eBuJRMJmpSpwYU=
github.com/royalcat/kv/kvbadger v0.0.0-20240612224509-6aa0da315950 h1:rKG2P4TNLgA4/Jl7LPayifjcw4txVGVSPkpHVhn3wnw= github.com/royalcat/kv/kvbadger v0.0.0-20240612224509-6aa0da315950 h1:rKG2P4TNLgA4/Jl7LPayifjcw4txVGVSPkpHVhn3wnw=
github.com/royalcat/kv/kvbadger v0.0.0-20240612224509-6aa0da315950/go.mod h1:JxgA1VGwbqu+WqdmjmjT0v6KeWoWlN6Y5lesjmphExM= github.com/royalcat/kv/kvbadger v0.0.0-20240612224509-6aa0da315950/go.mod h1:JxgA1VGwbqu+WqdmjmjT0v6KeWoWlN6Y5lesjmphExM=
github.com/royalcat/kv/kvbadger v0.0.0-20240615090409-961d9afa99b6 h1:/TWa41uAL8Vk0MkvZc03EjA1/bS2otK5q0/+6bSWKJI= github.com/royalcat/kv/kvbadger v0.0.0-20240615090409-961d9afa99b6 h1:/TWa41uAL8Vk0MkvZc03EjA1/bS2otK5q0/+6bSWKJI=
github.com/royalcat/kv/kvbadger v0.0.0-20240615090409-961d9afa99b6/go.mod h1:JxgA1VGwbqu+WqdmjmjT0v6KeWoWlN6Y5lesjmphExM= github.com/royalcat/kv/kvbadger v0.0.0-20240615090409-961d9afa99b6/go.mod h1:JxgA1VGwbqu+WqdmjmjT0v6KeWoWlN6Y5lesjmphExM=
github.com/royalcat/kv/kvbadger v0.0.0-20240617101007-c9c746b3916f h1:wz3pvg7YJdibZXQRV6B5pVPeDK8bgnuJVnBf7OFtCWI=
github.com/royalcat/kv/kvbadger v0.0.0-20240617101007-c9c746b3916f/go.mod h1:JxgA1VGwbqu+WqdmjmjT0v6KeWoWlN6Y5lesjmphExM=
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 h1:18kd+8ZUlt/ARXhljq+14TwAoKa61q6dX8jtwOf6DH8= github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 h1:18kd+8ZUlt/ARXhljq+14TwAoKa61q6dX8jtwOf6DH8=
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA= github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=

View file

@ -29,11 +29,11 @@ type Controller struct {
log *rlog.Logger 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{ return &Controller{
t: t, t: t,
storage: storage, storage: storage,
fileProperties: fileProperties, fileProperties: torrentFileProperties,
log: rlog.Component("torrent-client", "controller").With(slog.String("infohash", t.InfoHash().HexString())), 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 := s.log.With(slog.String("file", f.Path()))
log.Info(ctx, "excluding file") 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 v.Excluded = true
return v, nil 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 { func (s *Controller) isFileComplete(startIndex int, endIndex int) bool {

View file

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