manoj 99/10/25 14:12:42
Modified: src/main buff.c
Log:
The first layer of buff changes to abandon errno. Now .*_with_errors and
doerror are errno independant, but export the old errno-using interface.
Revision Changes Path
1.12 +54 -49 apache-2.0/src/main/buff.c
Index: buff.c
===================================================================
RCS file: /home/cvs/apache-2.0/src/main/buff.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -u -r1.11 -r1.12
--- buff.c 1999/10/24 19:23:57 1.11
+++ buff.c 1999/10/25 21:12:34 1.12
@@ -136,13 +136,11 @@
static void doerror(BUFF *fb, int direction)
{
- int errsave = errno; /* Save errno to prevent overwriting it below */
-
+ /* TODO: doerror should look at fb->saved_errno instead of errno */
+ errno = fb->saved_errno;
fb->flags |= (direction == B_RD ? B_RDERR : B_WRERR);
if (fb->error != NULL)
(*fb->error) (fb, direction, fb->error_data);
-
- errno = errsave;
}
/* Buffering routines */
@@ -316,24 +314,22 @@
/* a wrapper around iol_read which checks for errors and EOFs */
-static int read_with_errors(BUFF *fb, void *buf, int nbyte)
+static ap_status_t read_with_errors(BUFF *fb, void *buf, ap_size_t nbyte,
+ ap_ssize_t *bytes_read)
{
- int rv;
- ap_ssize_t bytes_read;
+ ap_status_t rv;
- rv = iol_read(fb->iol, buf, nbyte, &bytes_read);
- if (rv == APR_SUCCESS && bytes_read == 0) {
+ rv = iol_read(fb->iol, buf, nbyte, bytes_read);
+ if (rv == APR_SUCCESS && *bytes_read == 0) {
fb->flags |= B_EOF;
}
else if (rv != APR_SUCCESS) {
- errno = rv;
fb->saved_errno = rv;
if (rv != APR_EAGAIN) {
doerror(fb, B_RD);
}
- return -1;
}
- return bytes_read;
+ return rv;
}
@@ -345,6 +341,7 @@
API_EXPORT(int) ap_bread(BUFF *fb, void *buf, int nbyte)
{
int i, nrd;
+ ap_status_t rv;
if (fb->flags & B_RDERR) {
errno = fb->saved_errno;
@@ -363,7 +360,12 @@
fb->inptr += i;
return i;
}
- return read_with_errors(fb, buf, nbyte);
+ rv = read_with_errors(fb, buf, nbyte, &i);
+ if (rv == APR_SUCCESS) {
+ return i;
+ }
+ errno = rv;
+ return -1;
}
nrd = fb->incnt;
@@ -387,16 +389,18 @@
/* do a single read */
if (nbyte >= fb->bufsiz) {
/* read directly into caller's buffer */
- i = read_with_errors(fb, buf, nbyte);
- if (i == -1) {
+ rv = read_with_errors(fb, buf, nbyte, &i);
+ if (rv != APR_SUCCESS) {
+ errno = rv;
return nrd ? nrd : -1;
}
}
else {
/* read into hold buffer, then memcpy */
fb->inptr = fb->inbase;
- i = read_with_errors(fb, fb->inptr, fb->bufsiz);
- if (i == -1) {
+ rv = read_with_errors(fb, fb->inptr, fb->bufsiz, &i);
+ if (rv != APR_SUCCESS) {
+ errno = rv;
return nrd ? nrd : -1;
}
fb->incnt = i;
@@ -431,6 +435,7 @@
API_EXPORT(int) ap_bgets(char *buff, int n, BUFF *fb)
{
int i, ch, ct;
+ ap_status_t rv;
/* Can't do bgets on an unbuffered stream */
if (!(fb->flags & B_RD)) {
@@ -451,8 +456,9 @@
fb->incnt = 0;
if (fb->flags & B_EOF)
break;
- i = read_with_errors(fb, fb->inptr, fb->bufsiz);
- if (i == -1) {
+ rv = read_with_errors(fb, fb->inptr, fb->bufsiz, &i);
+ if (rv != APR_SUCCESS) {
+ errno = rv;
buff[ct] = '\0';
return ct ? ct : -1;
}
@@ -540,51 +546,51 @@
/* A wrapper for writev which deals with error conditions and
* bytes_sent.
*/
-static int writev_with_errors(BUFF *fb, const struct iovec *vec, int nvec)
+static ap_status_t writev_with_errors(BUFF *fb, const struct iovec *vec,
+ int nvec, ap_ssize_t *bytes_written)
{
- int rv;
- ap_ssize_t bytes_written;
+ ap_status_t rv;
- rv = iol_writev(fb->iol, vec, nvec, &bytes_written);
+ rv = iol_writev(fb->iol, vec, nvec, bytes_written);
if (rv != APR_SUCCESS) {
- errno = rv;
fb->saved_errno = rv;
if (rv != APR_EAGAIN) {
doerror(fb, B_WR);
}
- return -1;
}
- fb->bytes_sent += bytes_written;
- return bytes_written;
+ fb->bytes_sent += *bytes_written;
+ return rv;
}
static int writev_it_all(BUFF *fb, struct iovec *vec, int nvec)
{
int i;
- int rv;
+ ap_status_t rv;
+ ap_ssize_t bytes_written;
int total;
i = 0;
total = 0;
while (i < nvec) {
- rv = writev_with_errors(fb, vec + i, nvec - i);
- if (rv < 0) {
+ rv = writev_with_errors(fb, vec + i, nvec - i, &bytes_written);
+ if (rv != APR_SUCCESS) {
+ errno = rv;
return total ? total : -1;
}
- total += rv;
+ total += bytes_written;
if (fb->flags & B_NONBLOCK) {
return total;
}
/* recalculate to deal with partial writes */
- while (rv > 0) {
- if (rv < vec[i].iov_len) {
- vec[i].iov_base = (char *) vec[i].iov_base + rv;
- vec[i].iov_len -= rv;
+ while (bytes_written > 0) {
+ if (bytes_written < vec[i].iov_len) {
+ vec[i].iov_base = (char *) vec[i].iov_base + bytes_written;
+ vec[i].iov_len -= bytes_written;
break;
}
else {
- rv -= vec[i].iov_len;
+ bytes_written -= vec[i].iov_len;
++i;
}
}
@@ -687,45 +693,44 @@
/* A wrapper for write which deals with error conditions and
* bytes_sent.
*/
-static int write_with_errors(BUFF *fb, const void *buf, int nbyte)
+static ap_status_t write_with_errors(BUFF *fb, const void *buf, ap_size_t
nbyte,
+ ap_ssize_t *bytes_written)
{
- int rv;
- ap_ssize_t bytes_written;
+ ap_status_t rv;
- rv = iol_write(fb->iol, buf, nbyte, &bytes_written);
+ rv = iol_write(fb->iol, buf, nbyte, bytes_written);
if (rv != APR_SUCCESS) {
- errno = rv;
fb->saved_errno = rv;
if (rv != APR_EAGAIN) {
doerror(fb, B_WR);
}
- return -1;
}
- fb->bytes_sent += bytes_written;
- return bytes_written;
+ fb->bytes_sent += *bytes_written;
+ return rv;
}
static int bflush_core(BUFF *fb)
{
int total;
- int rv;
+ ap_ssize_t bytes_written;
if (fb->flags & B_CHUNK) {
end_chunk(fb, 0);
}
total = 0;
while (fb->outcnt > 0) {
- rv = write_with_errors(fb, fb->outbase + total, fb->outcnt);
- if (rv <= 0) {
+ (void) write_with_errors(fb, fb->outbase + total, fb->outcnt,
+ &bytes_written);
+ if (bytes_written <= 0) { /* error or eof */
if (total) {
memmove(fb->outbase, fb->outbase + total, fb->outcnt);
return total;
}
return -1;
}
- fb->outcnt -= rv;
- total += rv;
+ fb->outcnt -= bytes_written;
+ total += bytes_written;
}
if (fb->flags & B_CHUNK) {
start_chunk(fb);