Skip to content

Commit

Permalink
Fix issue #95: Closing torrent storage also closed client storage for…
Browse files Browse the repository at this point in the history
… some storage types
  • Loading branch information
anacrolix committed Jul 8, 2016
1 parent 408686b commit 11a53fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
24 changes: 13 additions & 11 deletions storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,50 @@ import (
// File-based storage for torrents, that isn't yet bound to a particular
// torrent.
type fileStorage struct {
baseDir string
completion pieceCompletion
baseDir string
}

func NewFile(baseDir string) Client {
return &fileStorage{
baseDir: baseDir,
completion: pieceCompletionForDir(baseDir),
baseDir: baseDir,
}
}

func (fs *fileStorage) OpenTorrent(info *metainfo.InfoEx) (Torrent, error) {
return fileTorrentStorage{fs}, nil
return &fileTorrentStorage{
fs,
pieceCompletionForDir(fs.baseDir),
}, nil
}

// File-based torrent storage, not yet bound to a Torrent.
type fileTorrentStorage struct {
*fileStorage
fs *fileStorage
completion pieceCompletion
}

func (fs *fileStorage) Piece(p metainfo.Piece) Piece {
func (fts *fileTorrentStorage) Piece(p metainfo.Piece) Piece {
// Create a view onto the file-based torrent storage.
_io := &fileStorageTorrent{
p.Info,
fs.baseDir,
fts.fs.baseDir,
}
// Return the appropriate segments of this.
return &fileStoragePiece{
fs,
fts,
p,
missinggo.NewSectionWriter(_io, p.Offset(), p.Length()),
io.NewSectionReader(_io, p.Offset(), p.Length()),
}
}

func (fs *fileStorage) Close() error {
func (fs *fileTorrentStorage) Close() error {
fs.completion.Close()
return nil
}

type fileStoragePiece struct {
*fileStorage
*fileTorrentStorage
p metainfo.Piece
io.WriterAt
io.ReaderAt
Expand Down
8 changes: 3 additions & 5 deletions storage/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ import (
)

type mmapStorage struct {
baseDir string
completion pieceCompletion
baseDir string
}

func NewMMap(baseDir string) Client {
return &mmapStorage{
baseDir: baseDir,
completion: pieceCompletionForDir(baseDir),
baseDir: baseDir,
}
}

func (s *mmapStorage) OpenTorrent(info *metainfo.InfoEx) (t Torrent, err error) {
span, err := mMapTorrent(&info.Info, s.baseDir)
t = &mmapTorrentStorage{
span: span,
pc: s.completion,
pc: pieceCompletionForDir(s.baseDir),
}
return
}
Expand Down

0 comments on commit 11a53fa

Please sign in to comment.