Commit 9dc4b1ca authored by Russ Cox's avatar Russ Cox

make Copyn take and return int64.

add Copy.

R=gri
DELTA=52  (37 added, 1 deleted, 14 changed)
OCL=19557
CL=19559
parent 127c65b4
......@@ -40,7 +40,7 @@ export func WriteString(w Write, s string) (n int, err *os.Error) {
return r, e
}
// Read until buffer is full, EOF, or error
// Read until buffer is full, EOF, or error
export func Readn(fd Read, buf *[]byte) (n int, err *os.Error) {
n = 0;
for n < len(buf) {
......@@ -79,34 +79,70 @@ export func MakeFullReader(fd Read) Read {
// Copies n bytes (or until EOF is reached) from src to dst.
// Returns the number of bytes copied and the error, if any.
export func Copyn(src Read, dst Write, n int) (c int, err *os.Error) {
buf := new([]byte, 32*1024); // BUG 6g crashes on non-pointer array slices
c = 0;
for c < n {
l := n - c;
if l > len(buf) {
l = len(buf)
export func Copyn(src Read, dst Write, n int64) (written int64, err *os.Error) {
buf := new([]byte, 32*1024);
for written < n {
var l int;
if n - written > int64(len(buf)) {
l = len(buf);
} else {
l = int(n - written);
}
nr, er := src.Read(buf[0 : l]);
if nr > 0 {
nw, ew := dst.Write(buf[0 : nr]);
if nw != nr || ew != nil {
c += nw;
if ew == nil {
ew = os.EIO
}
if nw > 0 {
written += int64(nw);
}
if ew != nil {
err = ew;
break;
}
c += nr;
if nr != nw {
err = os.EIO;
break;
}
}
if er != nil {
err = er;
break;
}
if nr == 0 {
err = ErrEOF;
break;
}
}
return c, err
return written, err
}
// Copies from src to dst until EOF is reached.
// Returns the number of bytes copied and the error, if any.
export func Copy(src Read, dst Write) (written int64, err *os.Error) {
buf := new([]byte, 32*1024);
for {
nr, er := src.Read(buf);
if nr > 0 {
nw, ew := dst.Write(buf[0:nr]);
if nw > 0 {
written += int64(nw);
}
if ew != nil {
err = ew;
break;
}
if nr != nw {
err = os.EIO;
break;
}
}
if er != nil {
err = er;
break;
}
if nr == 0 {
break;
}
}
return written, err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment