2016-07-10 23:03:59 +10:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fileStoragePiece struct {
|
|
|
|
*fileTorrentStorage
|
|
|
|
p metainfo.Piece
|
|
|
|
io.WriterAt
|
2016-09-02 15:10:57 +10:00
|
|
|
io.ReaderAt
|
2016-07-10 23:03:59 +10:00
|
|
|
}
|
|
|
|
|
2016-08-26 20:29:05 +10:00
|
|
|
func (me *fileStoragePiece) pieceKey() metainfo.PieceKey {
|
|
|
|
return metainfo.PieceKey{me.infoHash, me.p.Index()}
|
|
|
|
}
|
|
|
|
|
2016-07-12 16:45:22 +10:00
|
|
|
func (fs *fileStoragePiece) GetIsComplete() bool {
|
2016-08-26 20:29:05 +10:00
|
|
|
ret, err := fs.completion.Get(fs.pieceKey())
|
2016-07-12 16:45:22 +10:00
|
|
|
if err != nil || !ret {
|
|
|
|
return false
|
2016-07-10 23:03:59 +10:00
|
|
|
}
|
|
|
|
// If it's allegedly complete, check that its constituent files have the
|
|
|
|
// necessary length.
|
2016-08-26 20:29:05 +10:00
|
|
|
for _, fi := range extentCompleteRequiredLengths(fs.p.Info, fs.p.Offset(), fs.p.Length()) {
|
2016-07-10 23:03:59 +10:00
|
|
|
s, err := os.Stat(fs.fileInfoName(fi))
|
|
|
|
if err != nil || s.Size() < fi.Length {
|
|
|
|
ret = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ret {
|
2016-07-12 16:45:22 +10:00
|
|
|
return true
|
2016-07-10 23:03:59 +10:00
|
|
|
}
|
|
|
|
// The completion was wrong, fix it.
|
2016-08-26 20:29:05 +10:00
|
|
|
fs.completion.Set(fs.pieceKey(), false)
|
2016-07-12 16:45:22 +10:00
|
|
|
return false
|
2016-07-10 23:03:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fileStoragePiece) MarkComplete() error {
|
2016-08-26 20:29:05 +10:00
|
|
|
fs.completion.Set(fs.pieceKey(), true)
|
2016-07-10 23:03:59 +10:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 15:10:57 +10:00
|
|
|
func (fs *fileStoragePiece) MarkNotComplete() error {
|
|
|
|
fs.completion.Set(fs.pieceKey(), false)
|
|
|
|
return nil
|
2016-07-10 23:03:59 +10:00
|
|
|
}
|