On Thu, 2007-03-22 at 16:42 +0000, Joe Orton wrote:
> That definitely looks broken. Either the buffered read code needs to be
> factored out of apr_file_read so it can be called here, or _gets could
> just fall back on the slow !buffered path if flags & APR_XTHREAD (ugly
> but safe).
This passed the compilation and make test.
--
Bojan
Index: file_io/unix/readwrite.c
===================================================================
--- file_io/unix/readwrite.c (revision 523185)
+++ file_io/unix/readwrite.c (working copy)
@@ -25,6 +25,62 @@
#define USE_WAIT_FOR_IO
#endif
+static apr_status_t apr_file_read_buffered(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
+{
+ apr_ssize_t rv;
+ char *pos = (char *)buf;
+ apr_uint64_t blocksize;
+ apr_uint64_t size = *nbytes;
+
+ if (thefile->direction == 1) {
+ rv = apr_file_flush(thefile);
+ if (rv) {
+ return rv;
+ }
+ thefile->bufpos = 0;
+ thefile->direction = 0;
+ thefile->dataRead = 0;
+ }
+
+ rv = 0;
+ if (thefile->ungetchar != -1) {
+ *pos = (char)thefile->ungetchar;
+ ++pos;
+ --size;
+ thefile->ungetchar = -1;
+ }
+ while (rv == 0 && size > 0) {
+ if (thefile->bufpos >= thefile->dataRead) {
+ int bytesread = read(thefile->filedes, thefile->buffer,
+ thefile->bufsize);
+ if (bytesread == 0) {
+ thefile->eof_hit = TRUE;
+ rv = APR_EOF;
+ break;
+ }
+ else if (bytesread == -1) {
+ rv = errno;
+ break;
+ }
+ thefile->dataRead = bytesread;
+ thefile->filePtr += thefile->dataRead;
+ thefile->bufpos = 0;
+ }
+
+ blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
+ memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
+ thefile->bufpos += blocksize;
+ pos += blocksize;
+ size -= blocksize;
+ }
+
+ *nbytes = pos - (char *)buf;
+ if (*nbytes) {
+ rv = 0;
+ }
+ return rv;
+}
+
APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
{
apr_ssize_t rv;
@@ -36,67 +92,14 @@
}
if (thefile->buffered) {
- char *pos = (char *)buf;
- apr_uint64_t blocksize;
- apr_uint64_t size = *nbytes;
-
#if APR_HAS_THREADS
if (thefile->thlock) {
apr_thread_mutex_lock(thefile->thlock);
}
#endif
- if (thefile->direction == 1) {
- rv = apr_file_flush(thefile);
- if (rv) {
-#if APR_HAS_THREADS
- if (thefile->thlock) {
- apr_thread_mutex_unlock(thefile->thlock);
- }
-#endif
- return rv;
- }
- thefile->bufpos = 0;
- thefile->direction = 0;
- thefile->dataRead = 0;
- }
+ rv = apr_file_read_buffered(thefile, buf, nbytes);
- rv = 0;
- if (thefile->ungetchar != -1) {
- *pos = (char)thefile->ungetchar;
- ++pos;
- --size;
- thefile->ungetchar = -1;
- }
- while (rv == 0 && size > 0) {
- if (thefile->bufpos >= thefile->dataRead) {
- int bytesread = read(thefile->filedes, thefile->buffer,
- thefile->bufsize);
- if (bytesread == 0) {
- thefile->eof_hit = TRUE;
- rv = APR_EOF;
- break;
- }
- else if (bytesread == -1) {
- rv = errno;
- break;
- }
- thefile->dataRead = bytesread;
- thefile->filePtr += thefile->dataRead;
- thefile->bufpos = 0;
- }
-
- blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
- memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
- thefile->bufpos += blocksize;
- pos += blocksize;
- size -= blocksize;
- }
-
- *nbytes = pos - (char *)buf;
- if (*nbytes) {
- rv = 0;
- }
#if APR_HAS_THREADS
if (thefile->thlock) {
apr_thread_mutex_unlock(thefile->thlock);
@@ -374,7 +377,7 @@
}
else {
nbytes = 1;
- rv = apr_file_read(thefile, str, &nbytes);
+ rv = apr_file_read_buffered(thefile, str, &nbytes);
if (rv != APR_SUCCESS) {
break;
}