diff --git a/storage/completion.go b/storage/completion.go index 13c99e2d..a5c28fcb 100644 --- a/storage/completion.go +++ b/storage/completion.go @@ -7,8 +7,8 @@ import ( ) type pieceCompletion interface { - Get(metainfo.Piece) bool - Set(metainfo.Piece, bool) + Get(metainfo.Piece) (bool, error) + Set(metainfo.Piece, bool) error Close() } diff --git a/storage/completion_piece_map.go b/storage/completion_piece_map.go index 8b904fb9..b8a2d5b4 100644 --- a/storage/completion_piece_map.go +++ b/storage/completion_piece_map.go @@ -10,12 +10,12 @@ type mapPieceCompletion struct { func (mapPieceCompletion) Close() {} -func (me *mapPieceCompletion) Get(p metainfo.Piece) bool { +func (me *mapPieceCompletion) Get(p metainfo.Piece) (bool, error) { _, ok := me.m[p.Key()] - return ok + return ok, nil } -func (me *mapPieceCompletion) Set(p metainfo.Piece, b bool) { +func (me *mapPieceCompletion) Set(p metainfo.Piece, b bool) error { if b { if me.m == nil { me.m = make(map[metainfo.PieceKey]struct{}) @@ -24,4 +24,5 @@ func (me *mapPieceCompletion) Set(p metainfo.Piece, b bool) { } else { delete(me.m, p.Key()) } + return nil } diff --git a/storage/db.go b/storage/db.go index f7e126d0..db95a11d 100644 --- a/storage/db.go +++ b/storage/db.go @@ -26,25 +26,19 @@ func newDBPieceCompletion(dir string) (ret *dbPieceCompletion, err error) { return } -func (me *dbPieceCompletion) Get(p metainfo.Piece) (ret bool) { +func (me *dbPieceCompletion) Get(p metainfo.Piece) (ret bool, err error) { row := me.db.QueryRow(`select exists(select * from completed where infohash=? and "index"=?)`, p.Info.Hash().HexString(), p.Index()) - err := row.Scan(&ret) - if err != nil { - panic(err) - } + err = row.Scan(&ret) return } -func (me *dbPieceCompletion) Set(p metainfo.Piece, b bool) { - var err error +func (me *dbPieceCompletion) Set(p metainfo.Piece, b bool) (err error) { if b { _, err = me.db.Exec(`insert into completed (infohash, "index") values (?, ?)`, p.Info.Hash().HexString(), p.Index()) } else { _, err = me.db.Exec(`delete from completed where infohash=? and "index"=?`, p.Info.Hash().HexString(), p.Index()) } - if err != nil { - panic(err) - } + return } func (me *dbPieceCompletion) Close() { diff --git a/storage/file_storage_piece.go b/storage/file_storage_piece.go index fec993a3..44648c13 100644 --- a/storage/file_storage_piece.go +++ b/storage/file_storage_piece.go @@ -14,10 +14,10 @@ type fileStoragePiece struct { r io.ReaderAt } -func (fs *fileStoragePiece) GetIsComplete() (ret bool) { - ret = fs.completion.Get(fs.p) - if !ret { - return +func (fs *fileStoragePiece) GetIsComplete() bool { + ret, err := fs.completion.Get(fs.p) + if err != nil || !ret { + return false } // If it's allegedly complete, check that its constituent files have the // necessary length. @@ -29,11 +29,11 @@ func (fs *fileStoragePiece) GetIsComplete() (ret bool) { } } if ret { - return + return true } // The completion was wrong, fix it. fs.completion.Set(fs.p, false) - return + return false } func (fs *fileStoragePiece) MarkComplete() error { diff --git a/storage/mmap.go b/storage/mmap.go index 836b6f0f..6a81f26e 100644 --- a/storage/mmap.go +++ b/storage/mmap.go @@ -58,8 +58,9 @@ type mmapStoragePiece struct { io.WriterAt } -func (sp mmapStoragePiece) GetIsComplete() bool { - return sp.pc.Get(sp.p) +func (sp mmapStoragePiece) GetIsComplete() (ret bool) { + ret, _ = sp.pc.Get(sp.p) + return } func (sp mmapStoragePiece) MarkComplete() error {