client_test.go | 11 +---------- storage/piece_file.go | 103 ----------------------------------------------------- diff --git a/client_test.go b/client_test.go index 716ea5078b608ae6d81fd569e9b9449a28d86bcb..b70e28975cd3b0d0f301dbeeb562f07e1dfd7523 100644 --- a/client_test.go +++ b/client_test.go @@ -292,10 +292,6 @@ func fileCachePieceResourceStorage(fc *filecache.Cache) storage.ClientImpl { return storage.NewResourcePieces(fc.AsResourceProvider()) } -func fileCachePieceFileStorage(fc *filecache.Cache) storage.ClientImpl { - return storage.NewFileStorePieces(fc.AsFileStore()) -} - func TestClientTransferSmallCache(t *testing.T) { testClientTransfer(t, testClientTransferParams{ LeecherStorage: NewFileCacheClientStorageFactory(FileCacheClientStorageFactoryParams{ @@ -316,9 +312,6 @@ func TestClientTransferVarious(t *testing.T) { // Leecher storage for _, ls := range []storageFactory{ - NewFileCacheClientStorageFactory(FileCacheClientStorageFactoryParams{ - Wrapper: fileCachePieceFileStorage, - }), NewFileCacheClientStorageFactory(FileCacheClientStorageFactoryParams{ Wrapper: fileCachePieceResourceStorage, }), @@ -798,12 +791,10 @@ } } func TestAddTorrentPiecesAlreadyCompleted(t *testing.T) { - testAddTorrentPriorPieceCompletion(t, true, fileCachePieceFileStorage) testAddTorrentPriorPieceCompletion(t, true, fileCachePieceResourceStorage) } func TestAddTorrentPiecesNotAlreadyCompleted(t *testing.T) { - testAddTorrentPriorPieceCompletion(t, false, fileCachePieceFileStorage) testAddTorrentPriorPieceCompletion(t, false, fileCachePieceResourceStorage) } @@ -852,7 +843,7 @@ require.NoError(t, err) if ps.SetLeecherStorageCapacity { fc.SetCapacity(ps.LeecherStorageCapacity) } - cfg.DefaultStorage = storage.NewFileStorePieces(fc.AsFileStore()) + cfg.DefaultStorage = storage.NewResourcePieces(fc.AsResourceProvider()) cfg.DataDir = leecherDataDir leecher, _ := NewClient(&cfg) defer leecher.Close() diff --git a/storage/piece_file.go b/storage/piece_file.go deleted file mode 100644 index 2760751c522c74d0552c1c9fb6b612d55fde8c83..0000000000000000000000000000000000000000 --- a/storage/piece_file.go +++ /dev/null @@ -1,103 +0,0 @@ -package storage - -import ( - "io" - "os" - "path" - - "github.com/anacrolix/missinggo" - - "github.com/anacrolix/torrent/metainfo" -) - -type pieceFileStorage struct { - fs missinggo.FileStore -} - -func NewFileStorePieces(fs missinggo.FileStore) ClientImpl { - return &pieceFileStorage{ - fs: fs, - } -} - -func (pieceFileStorage) Close() error { return nil } - -type pieceFileTorrentStorage struct { - s *pieceFileStorage -} - -func (s *pieceFileStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) { - return &pieceFileTorrentStorage{s}, nil -} - -func (s *pieceFileTorrentStorage) Close() error { - return nil -} - -func (s *pieceFileTorrentStorage) Piece(p metainfo.Piece) PieceImpl { - return pieceFileTorrentStoragePiece{s, p, s.s.fs} -} - -type pieceFileTorrentStoragePiece struct { - ts *pieceFileTorrentStorage - p metainfo.Piece - fs missinggo.FileStore -} - -func (s pieceFileTorrentStoragePiece) completedPath() string { - return path.Join("completed", s.p.Hash().HexString()) -} - -func (s pieceFileTorrentStoragePiece) incompletePath() string { - return path.Join("incomplete", s.p.Hash().HexString()) -} - -func (s pieceFileTorrentStoragePiece) GetIsComplete() bool { - fi, err := s.fs.Stat(s.completedPath()) - return err == nil && fi.Size() == s.p.Length() -} - -func (s pieceFileTorrentStoragePiece) MarkComplete() error { - return s.fs.Rename(s.incompletePath(), s.completedPath()) -} - -func (s pieceFileTorrentStoragePiece) MarkNotComplete() error { - return s.fs.Remove(s.completedPath()) -} - -func (s pieceFileTorrentStoragePiece) openFile() (f missinggo.File, err error) { - f, err = s.fs.OpenFile(s.completedPath(), os.O_RDONLY) - if err == nil { - var fi os.FileInfo - fi, err = f.Stat() - if err == nil && fi.Size() == s.p.Length() { - return - } - f.Close() - } else if !os.IsNotExist(err) { - return - } - f, err = s.fs.OpenFile(s.incompletePath(), os.O_RDONLY) - if os.IsNotExist(err) { - err = io.ErrUnexpectedEOF - } - return -} - -func (s pieceFileTorrentStoragePiece) ReadAt(b []byte, off int64) (n int, err error) { - f, err := s.openFile() - if err != nil { - return - } - defer f.Close() - return f.ReadAt(b, off) -} - -func (s pieceFileTorrentStoragePiece) WriteAt(b []byte, off int64) (n int, err error) { - f, err := s.fs.OpenFile(s.incompletePath(), os.O_WRONLY|os.O_CREATE) - if err != nil { - return - } - defer f.Close() - return f.WriteAt(b, off) -}