mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 03:03:27 +00:00
src/os: loop in File.ReadAt to handle short reads
This matches what upstream Go does. This also means len(b) == 0 successfully reads 0 bytes without any extra logic. The tests in archive/zip test for this behaviour.
This commit is contained in:
+19
-3
@@ -10,6 +10,7 @@
|
|||||||
package os
|
package os
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
@@ -106,14 +107,29 @@ func (f *File) Read(b []byte) (n int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errNegativeOffset = errors.New("negative offset")
|
||||||
|
|
||||||
// ReadAt reads up to len(b) bytes from the File at the given absolute offset.
|
// ReadAt reads up to len(b) bytes from the File at the given absolute offset.
|
||||||
// It returns the number of bytes read and any error encountered, possible io.EOF.
|
// It returns the number of bytes read and any error encountered, possible io.EOF.
|
||||||
// At end of file, Read returns 0, io.EOF.
|
// At end of file, Read returns 0, io.EOF.
|
||||||
func (f *File) ReadAt(b []byte, offset int64) (n int, err error) {
|
func (f *File) ReadAt(b []byte, offset int64) (n int, err error) {
|
||||||
n, err = f.handle.ReadAt(b, offset)
|
if offset < 0 {
|
||||||
if err != nil && err != io.EOF {
|
return 0, &PathError{Op: "readat", Path: f.name, Err: errNegativeOffset}
|
||||||
err = &PathError{"readat", f.name, err}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for len(b) > 0 {
|
||||||
|
m, e := f.handle.ReadAt(b, offset)
|
||||||
|
if e != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
err = &PathError{"readat", f.name, err}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n += m
|
||||||
|
b = b[m:]
|
||||||
|
offset += int64(m)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user