In the native file-based storage, mark pieces incomplete if the necessary file data is missing, or there's a read error on a piece.
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package storage
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
)
|
|
|
|
type fileStoragePiece struct {
|
|
*fileTorrentStorage
|
|
p metainfo.Piece
|
|
io.WriterAt
|
|
r io.ReaderAt
|
|
}
|
|
|
|
func (fs *fileStoragePiece) GetIsComplete() (ret bool) {
|
|
ret = fs.completion.Get(fs.p)
|
|
if !ret {
|
|
return
|
|
}
|
|
// If it's allegedly complete, check that its constituent files have the
|
|
// necessary length.
|
|
for _, fi := range extentCompleteRequiredLengths(&fs.p.Info.Info, fs.p.Offset(), fs.p.Length()) {
|
|
s, err := os.Stat(fs.fileInfoName(fi))
|
|
if err != nil || s.Size() < fi.Length {
|
|
ret = false
|
|
break
|
|
}
|
|
}
|
|
if ret {
|
|
return
|
|
}
|
|
// The completion was wrong, fix it.
|
|
fs.completion.Set(fs.p, false)
|
|
return
|
|
}
|
|
|
|
func (fs *fileStoragePiece) MarkComplete() error {
|
|
fs.completion.Set(fs.p, true)
|
|
return nil
|
|
}
|
|
|
|
func (fsp *fileStoragePiece) ReadAt(b []byte, off int64) (n int, err error) {
|
|
n, err = fsp.r.ReadAt(b, off)
|
|
if n != 0 {
|
|
err = nil
|
|
return
|
|
}
|
|
if off < 0 || off >= fsp.p.Length() {
|
|
return
|
|
}
|
|
fsp.completion.Set(fsp.p, false)
|
|
return
|
|
}
|