os: do not assume syscall i/o funcs return n=0 on error

Fixes #9007.

LGTM=iant, r
R=r, iant
CC=golang-codereviews
https://golang.org/cl/160670043
This commit is contained in:
Russ Cox 2014-10-28 15:00:13 -04:00
parent bd1169dd21
commit a62da2027b
6 changed files with 22 additions and 16 deletions

View file

@ -187,7 +187,7 @@ func (f *File) read(b []byte) (n int, err error) {
if needsMaxRW && len(b) > maxRW {
b = b[:maxRW]
}
return syscall.Read(f.fd, b)
return fixCount(syscall.Read(f.fd, b))
}
// pread reads len(b) bytes from the File starting at byte offset off.
@ -197,7 +197,7 @@ func (f *File) pread(b []byte, off int64) (n int, err error) {
if needsMaxRW && len(b) > maxRW {
b = b[:maxRW]
}
return syscall.Pread(f.fd, b, off)
return fixCount(syscall.Pread(f.fd, b, off))
}
// write writes len(b) bytes to the File.
@ -208,7 +208,7 @@ func (f *File) write(b []byte) (n int, err error) {
if needsMaxRW && len(bcap) > maxRW {
bcap = bcap[:maxRW]
}
m, err := syscall.Write(f.fd, bcap)
m, err := fixCount(syscall.Write(f.fd, bcap))
n += m
// If the syscall wrote some data but not all (short write)
@ -234,7 +234,7 @@ func (f *File) pwrite(b []byte, off int64) (n int, err error) {
if needsMaxRW && len(b) > maxRW {
b = b[:maxRW]
}
return syscall.Pwrite(f.fd, b, off)
return fixCount(syscall.Pwrite(f.fd, b, off))
}
// seek sets the offset for the next Read or Write on file to offset, interpreted
@ -242,7 +242,7 @@ func (f *File) pwrite(b []byte, off int64) (n int, err error) {
// relative to the current offset, and 2 means relative to the end.
// It returns the new offset and an error, if any.
func (f *File) seek(offset int64, whence int) (ret int64, err error) {
return syscall.Seek(f.fd, offset, whence)
return fixCount(syscall.Seek(f.fd, offset, whence))
}
// Truncate changes the size of the named file.