From: Matt Joiner Date: Thu, 5 Jan 2017 06:00:59 +0000 (+1100) Subject: Remove FileStorePieces storage backend X-Git-Tag: v1.0.0~493 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=1c37903a74602e519232bbbc038384c4a2102eaf;p=btrtrc.git Remove FileStorePieces storage backend ResourcePIeces is now preferred. --- diff --git a/client_test.go b/client_test.go index 716ea507..b70e2897 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 TestClientTransferSmallCache(t *testing.T) { func TestClientTransferVarious(t *testing.T) { // Leecher storage for _, ls := range []storageFactory{ - NewFileCacheClientStorageFactory(FileCacheClientStorageFactoryParams{ - Wrapper: fileCachePieceFileStorage, - }), NewFileCacheClientStorageFactory(FileCacheClientStorageFactoryParams{ Wrapper: fileCachePieceResourceStorage, }), @@ -798,12 +791,10 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf } 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 @@ func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) { 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 2760751c..00000000 --- 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) -}