2017-10-12 13:09:32 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2018-01-12 07:41:42 +08:00
|
|
|
"log"
|
2017-10-12 13:09:32 +08:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type filePieceImpl struct {
|
|
|
|
*fileTorrentImpl
|
|
|
|
p metainfo.Piece
|
|
|
|
io.WriterAt
|
|
|
|
io.ReaderAt
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PieceImpl = (*filePieceImpl)(nil)
|
|
|
|
|
|
|
|
func (me *filePieceImpl) pieceKey() metainfo.PieceKey {
|
|
|
|
return metainfo.PieceKey{me.infoHash, me.p.Index()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *filePieceImpl) Completion() Completion {
|
|
|
|
c, err := fs.completion.Get(fs.pieceKey())
|
2018-01-12 07:41:42 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting piece completion: %s", err)
|
|
|
|
c.Ok = false
|
|
|
|
return c
|
2017-10-12 13:09:32 +08:00
|
|
|
}
|
2023-02-13 11:26:03 +08:00
|
|
|
|
|
|
|
verified := true
|
2020-10-11 09:54:03 +08:00
|
|
|
if c.Complete {
|
|
|
|
// If it's allegedly complete, check that its constituent files have the necessary length.
|
|
|
|
for _, fi := range extentCompleteRequiredLengths(fs.p.Info, fs.p.Offset(), fs.p.Length()) {
|
2020-10-15 12:45:08 +08:00
|
|
|
s, err := os.Stat(fs.files[fi.fileIndex].path)
|
|
|
|
if err != nil || s.Size() < fi.length {
|
2023-02-13 11:26:03 +08:00
|
|
|
verified = false
|
2020-10-11 09:54:03 +08:00
|
|
|
break
|
|
|
|
}
|
2017-10-12 13:09:32 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-13 11:26:03 +08:00
|
|
|
|
|
|
|
if !verified {
|
2017-10-12 13:09:32 +08:00
|
|
|
// The completion was wrong, fix it.
|
2023-02-13 11:26:03 +08:00
|
|
|
c.Complete = false
|
2017-10-12 13:09:32 +08:00
|
|
|
fs.completion.Set(fs.pieceKey(), false)
|
|
|
|
}
|
2023-02-13 11:26:03 +08:00
|
|
|
|
2017-10-12 13:09:32 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *filePieceImpl) MarkComplete() error {
|
|
|
|
return fs.completion.Set(fs.pieceKey(), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *filePieceImpl) MarkNotComplete() error {
|
|
|
|
return fs.completion.Set(fs.pieceKey(), false)
|
|
|
|
}
|