2
0
mirror of synced 2025-02-24 14:48:27 +00:00
torrent/storage/file_storage_piece.go
Matt Joiner f055abe2fc Fix issue #96
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.
2016-07-10 23:03:59 +10:00

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
}