From e480ecb09388042fe13485678ba8b3d8d6ddb383 Mon Sep 17 00:00:00 2001 From: nikk gitanes Date: Sun, 27 Aug 2023 02:38:43 +0300 Subject: [PATCH 1/2] add torrent storage capacity func --- server/torr/storage/torrstor/storage.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server/torr/storage/torrstor/storage.go b/server/torr/storage/torrstor/storage.go index 9fcf069..1c92d0b 100644 --- a/server/torr/storage/torrstor/storage.go +++ b/server/torr/storage/torrstor/storage.go @@ -6,7 +6,7 @@ import ( "server/torr/storage" "github.com/anacrolix/torrent/metainfo" - storage2 "github.com/anacrolix/torrent/storage" + ts "github.com/anacrolix/torrent/storage" ) type Storage struct { @@ -24,16 +24,20 @@ func NewStorage(capacity int64) *Storage { return stor } -func (s *Storage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage2.TorrentImpl, error) { +func (s *Storage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (ts.TorrentImpl, error) { + capFunc := func() (int64, bool) { + return s.capacity, true + } s.mu.Lock() defer s.mu.Unlock() ch := NewCache(s.capacity, s) ch.Init(info, infoHash) s.caches[infoHash] = ch // return ch, nil - return storage2.TorrentImpl{ - Piece: ch.Piece, - Close: ch.Close, + return ts.TorrentImpl{ + Piece: ch.Piece, + Close: ch.Close, + Capacity: &capFunc, }, nil } From dd26ff3d5f762a946fe3bafef9ea3ae6d47526cf Mon Sep 17 00:00:00 2001 From: yourok <8yourok8@mail.ru> Date: Sun, 27 Aug 2023 21:46:20 +0300 Subject: [PATCH 2/2] fix nil piece --- server/torr/storage/torrstor/cache.go | 2 +- server/torr/storage/torrstor/piecefake.go | 33 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 server/torr/storage/torrstor/piecefake.go diff --git a/server/torr/storage/torrstor/cache.go b/server/torr/storage/torrstor/cache.go index 3074b31..efe861d 100644 --- a/server/torr/storage/torrstor/cache.go +++ b/server/torr/storage/torrstor/cache.go @@ -83,7 +83,7 @@ func (c *Cache) Piece(m metainfo.Piece) storage.PieceImpl { if val, ok := c.pieces[m.Index()]; ok { return val } - return nil + return &PieceFake{} } func (c *Cache) Close() error { diff --git a/server/torr/storage/torrstor/piecefake.go b/server/torr/storage/torrstor/piecefake.go new file mode 100644 index 0000000..6434bb2 --- /dev/null +++ b/server/torr/storage/torrstor/piecefake.go @@ -0,0 +1,33 @@ +package torrstor + +import ( + "errors" + "github.com/anacrolix/torrent/storage" +) + +type PieceFake struct{} + +func (PieceFake) ReadAt(p []byte, off int64) (n int, err error) { + err = errors.New("fake") + return +} + +func (PieceFake) WriteAt(p []byte, off int64) (n int, err error) { + err = errors.New("fake") + return +} + +func (PieceFake) MarkComplete() error { + return errors.New("fake") +} + +func (PieceFake) MarkNotComplete() error { + return errors.New("fake") +} + +func (PieceFake) Completion() storage.Completion { + return storage.Completion{ + Complete: false, + Ok: true, + } +}