storage: Don't read bad completed piece files
This commit is contained in:
parent
7638d678a8
commit
f19e8b32aa
|
@ -59,22 +59,37 @@ func (me pieceFileTorrentStoragePiece) MarkComplete() error {
|
||||||
return me.fs.Rename(me.incompletePath(), me.completedPath())
|
return me.fs.Rename(me.incompletePath(), me.completedPath())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (me pieceFileTorrentStoragePiece) openFile() (f missinggo.File, err error) {
|
||||||
|
f, err = me.fs.OpenFile(me.completedPath(), os.O_RDONLY)
|
||||||
|
if err == nil {
|
||||||
|
var fi os.FileInfo
|
||||||
|
fi, err = f.Stat()
|
||||||
|
if err == nil && fi.Size() == me.p.Length() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f, err = me.fs.OpenFile(me.incompletePath(), os.O_RDONLY)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (me pieceFileTorrentStoragePiece) ReadAt(b []byte, off int64) (n int, err error) {
|
func (me pieceFileTorrentStoragePiece) ReadAt(b []byte, off int64) (n int, err error) {
|
||||||
f, err := me.fs.OpenFile(me.completedPath(), os.O_RDONLY)
|
f, err := me.openFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f, err = me.fs.OpenFile(me.incompletePath(), os.O_RDONLY)
|
return
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = io.ErrUnexpectedEOF
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
missinggo.LimitLen(&b, me.p.Length()-off)
|
||||||
n, err = f.ReadAt(b, off)
|
n, err = f.ReadAt(b, off)
|
||||||
off += int64(n)
|
off += int64(n)
|
||||||
if err == io.EOF && off < me.p.Length() {
|
if off >= me.p.Length() {
|
||||||
|
err = io.EOF
|
||||||
|
} else if err == io.EOF {
|
||||||
err = io.ErrUnexpectedEOF
|
err = io.ErrUnexpectedEOF
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue