From 3a2b615f55a24d4b72edec6afbe7ffbdbbbe8bb0 Mon Sep 17 00:00:00 2001 From: royalcat Date: Tue, 18 Jun 2024 22:27:18 +0300 Subject: [PATCH] fix exclude file --- go.mod | 4 ++-- go.sum | 6 ++++++ src/sources/torrent/controller.go | 16 +++++++++++++--- src/sources/torrent/fs.go | 14 +++++++------- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index f153548..da53300 100644 --- a/go.mod +++ b/go.mod @@ -35,8 +35,8 @@ require ( github.com/ravilushqa/otelgqlgen v0.15.0 github.com/royalcat/ctxio v0.0.0-20240602060200-590d464c39be github.com/royalcat/ctxprogress v0.0.0-20240614113930-3cc5bb935bff - github.com/royalcat/kv v0.0.0-20240615090409-961d9afa99b6 - github.com/royalcat/kv/kvbadger v0.0.0-20240615090409-961d9afa99b6 + github.com/royalcat/kv v0.0.0-20240617101007-c9c746b3916f + github.com/royalcat/kv/kvbadger v0.0.0-20240617101007-c9c746b3916f github.com/rs/zerolog v1.32.0 github.com/samber/slog-multi v1.0.2 github.com/samber/slog-zerolog v1.0.0 diff --git a/go.sum b/go.sum index f782da3..ed38701 100644 --- a/go.sum +++ b/go.sum @@ -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/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-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/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/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/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= diff --git a/src/sources/torrent/controller.go b/src/sources/torrent/controller.go index 0c863a3..b90c32f 100644 --- a/src/sources/torrent/controller.go +++ b/src/sources/torrent/controller.go @@ -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 { diff --git a/src/sources/torrent/fs.go b/src/sources/torrent/fs.go index 396ae97..fe77d1a 100644 --- a/src/sources/torrent/fs.go +++ b/src/sources/torrent/fs.go @@ -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 {