httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@locus.apache.org
Subject cvs commit: apache-2.0/src/lib/apr/file_io/unix filedup.c fileio.h open.c pipe.c readwrite.c seek.c
Date Fri, 14 Apr 2000 23:36:13 GMT
rbb         00/04/14 16:36:13

  Modified:    .        STATUS
               src/lib/apr/file_io/unix filedup.c fileio.h open.c pipe.c
                        readwrite.c seek.c
  Log:
  Add buffered I/O back into APR's file I/O on unix.  This code comes from
  the OS/2 code, and has been munged to make it compile.  This has not been
  tested, but unbuffered I/O still works.
  
  Revision  Changes    Path
  1.49      +1 -6      apache-2.0/STATUS
  
  Index: STATUS
  ===================================================================
  RCS file: /home/cvs/apache-2.0/STATUS,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -r1.48 -r1.49
  --- STATUS	2000/04/14 18:08:05	1.48
  +++ STATUS	2000/04/14 23:36:12	1.49
  @@ -1,5 +1,5 @@
   Apache 2.0 STATUS:
  -Last modified at [$Date: 2000/04/14 18:08:05 $]
  +Last modified at [$Date: 2000/04/14 23:36:12 $]
   
   Release:
   
  @@ -16,11 +16,6 @@
               default cgi module on Unix with a threaded MPM is the cgid
               module.  This should be override-able of course.
             Status: Jim Jagielski is looking into this.
  -
  -    * Put buffered file I/O back into APR.  This should be done using the
  -      buffering logic from the OS/2 directory.  stdio FILE *'s should not be
  -      used on Unix because of the limitations these impose on some platforms,
  -      for example the 256 File * limit on Solaris.
   
       * Fix SAFEREAD.
   	see <Pine.LNX.4.21.0004101511380.7822-100000@twinlark.arctic.org>
  
  
  
  1.18      +1 -0      apache-2.0/src/lib/apr/file_io/unix/filedup.c
  
  Index: filedup.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/file_io/unix/filedup.c,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- filedup.c	2000/04/14 00:58:38	1.17
  +++ filedup.c	2000/04/14 23:36:12	1.18
  @@ -79,6 +79,7 @@
           (*new_file)->filedes = dup(old_file->filedes); 
       }
       (*new_file)->fname = ap_pstrdup(old_file->cntxt, old_file->fname);
  +    (*new_file)->buffered = old_file->buffered;
       ap_register_cleanup((*new_file)->cntxt, (void *)(*new_file), ap_unix_file_cleanup,
                           ap_null_cleanup);
       return APR_SUCCESS;
  
  
  
  1.17      +13 -0     apache-2.0/src/lib/apr/file_io/unix/fileio.h
  
  Index: fileio.h
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/file_io/unix/fileio.h,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- fileio.h	2000/04/14 15:58:22	1.16
  +++ fileio.h	2000/04/14 23:36:12	1.17
  @@ -100,6 +100,8 @@
   #endif
   /* End System headers */
   
  +#define APR_FILE_BUFSIZE 4096
  +
   struct ap_file_t {
       ap_pool_t *cntxt;
       int filedes;
  @@ -108,7 +110,18 @@
       int eof_hit;
       int pipe;
       int timeout;
  +    int buffered;
       int ungetchar;    /* Last char provided by an unget op. (-1 = no char)*/
  +
  +    /* Stuff for buffered mode */
  +    char *buffer;
  +    int bufpos;               /* Read/Write position in buffer */
  +    unsigned long dataRead;   /* amount of valid data read into buffer */
  +    int direction;            /* buffer being used for 0 = read, 1 = write */
  +    unsigned long filePtr;    /* position in file of handle */
  +#if APR_HAS_THREADS
  +    ap_lock_t *thlock;
  +#endif
   };
   
   struct ap_dir_t {
  
  
  
  1.47      +17 -1     apache-2.0/src/lib/apr/file_io/unix/open.c
  
  Index: open.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/file_io/unix/open.c,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- open.c	2000/04/14 15:58:22	1.46
  +++ open.c	2000/04/14 23:36:12	1.47
  @@ -64,7 +64,11 @@
   
       if (rv == 0) {
           file->filedes = -1;
  +#if APR_HAS_THREADS
  +        return ap_destroy_lock(file->thlock);
  +#else
           return APR_SUCCESS;
  +#endif
       }
       else {
           return errno;
  @@ -80,12 +84,16 @@
           return APR_EBADARG;
   
       if ((*new) == NULL) {
  -        (*new) = (ap_file_t *)ap_palloc(cont, sizeof(ap_file_t));
  +        (*new) = (ap_file_t *)ap_pcalloc(cont, sizeof(ap_file_t));
       }
   
       (*new)->cntxt = cont;
       (*new)->oflags = oflags;
       (*new)->filedes = -1;
  +    (*new)->buffer = NULL;
  +#if APR_HAS_THREADS
  +    ap_create_lock(&((*new)->thlock), APR_MUTEX, APR_INTRAPROCESS, NULL, cont);
  +#endif
   
       if ((flag & APR_READ) && (flag & APR_WRITE)) {
           oflags = O_RDWR;
  @@ -102,6 +110,8 @@
   
       (*new)->fname = ap_pstrdup(cont, fname);
   
  +    (*new)->buffered = (flag & APR_BUFFERED) > 0;
  +
       if (flag & APR_CREATE) {
           oflags |= O_CREAT; 
   	if (flag & APR_EXCL) {
  @@ -139,6 +149,10 @@
       (*new)->timeout = -1;
       (*new)->ungetchar = -1;
       (*new)->eof_hit = 0;
  +    (*new)->filePtr = 0;
  +    (*new)->bufpos = 0;
  +    (*new)->dataRead = 0;
  +    (*new)->direction = 0;
       ap_register_cleanup((*new)->cntxt, (void *)(*new), ap_unix_file_cleanup,
                           ap_null_cleanup);
       return APR_SUCCESS;
  @@ -194,6 +208,7 @@
           (*file)->cntxt = cont;
       }
       (*file)->eof_hit = 0;
  +    (*file)->buffered = 0;
       (*file)->timeout = -1;
       (*file)->filedes = *dafile;
       return APR_SUCCESS;
  @@ -232,6 +247,7 @@
       }
       (*thefile)->filedes = STDERR_FILENO;
       (*thefile)->cntxt = cont;
  +    (*thefile)->buffered = 0;
       (*thefile)->fname = NULL;
       (*thefile)->eof_hit = 0;
   
  
  
  
  1.26      +2 -0      apache-2.0/src/lib/apr/file_io/unix/pipe.c
  
  Index: pipe.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/file_io/unix/pipe.c,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- pipe.c	2000/04/14 15:58:23	1.25
  +++ pipe.c	2000/04/14 23:36:12	1.26
  @@ -105,6 +105,7 @@
       (*in)->filedes = filedes[0];
       (*in)->pipe = 1;
       (*in)->fname = ap_pstrdup(cont, "PIPE");
  +    (*in)->buffered = 0;
       (*in)->timeout = -1;
   
       (*out) = (ap_file_t *)ap_palloc(cont, sizeof(ap_file_t));
  @@ -112,6 +113,7 @@
       (*out)->filedes = filedes[1];
       (*out)->pipe = 1;
       (*out)->fname = ap_pstrdup(cont, "PIPE");
  +    (*out)->buffered = 0;
       (*out)->timeout = -1;
   
       pipenonblock(*in);
  
  
  
  1.41      +151 -61   apache-2.0/src/lib/apr/file_io/unix/readwrite.c
  
  Index: readwrite.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/file_io/unix/readwrite.c,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- readwrite.c	2000/04/14 10:23:30	1.40
  +++ readwrite.c	2000/04/14 23:36:13	1.41
  @@ -94,47 +94,88 @@
   	return APR_SUCCESS;
       }
   
  -    bytes_read = 0;
  -    if (thefile->ungetchar != -1) {
  -        bytes_read = 1;
  -        *(char *)buf = (char)thefile->ungetchar;
  -        buf = (char *)buf + 1;
  -        (*nbytes)--;
  -        thefile->ungetchar = -1;
  -	if (*nbytes == 0) {
  -	    *nbytes = bytes_read;
  -	    return APR_SUCCESS;
  -	}
  -    }
  +    if (thefile->buffered) {
  +        char *pos = (char *)buf;
  +        ap_uint64_t blocksize;
  +        ap_uint64_t size = *nbytes;
   
  -    do {
  -        rv = read(thefile->filedes, buf, *nbytes);
  -    } while (rv == -1 && errno == EINTR);
  -
  -    if (rv == -1 && 
  -        (errno == EAGAIN || errno == EWOULDBLOCK) && 
  -        thefile->timeout != 0) {
  -        ap_status_t arv = wait_for_io_or_timeout(thefile, 1);
  -        if (arv != APR_SUCCESS) {
  -            *nbytes = bytes_read;
  -            return arv;
  -        }
  -        else {
  -            do {
  -                rv = read(thefile->filedes, buf, *nbytes);
  -            } while (rv == -1 && errno == EINTR);
  +#if APR_HAS_THREADS
  +        ap_lock(thefile->thlock);
  +#endif
  +
  +        if (thefile->direction == 1) {
  +            ap_flush(thefile);
  +            thefile->bufpos = 0;
  +            thefile->direction = 0;
  +            thefile->dataRead = 0;
           }
  -    }  
   
  -    *nbytes = bytes_read;
  -    if (rv == 0) {
  -	return APR_EOF;
  +        while (rv == 0 && size > 0) {
  +            if (thefile->bufpos >= thefile->dataRead) {
  +                thefile->dataRead = read(thefile->filedes, thefile->buffer, APR_FILE_BUFSIZE);
  +                if (thefile->dataRead == 0) {
  +                    thefile->eof_hit = TRUE;
  +                    break;
  +                }
  +                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 = rv == 0 ? pos - (char *)buf : 0;
  +#if APR_HAS_THREADS
  +        ap_unlock(thefile->thlock);
  +#endif
  +        return rv;
       }
  -    if (rv > 0) {
  -	*nbytes += rv;
  -	return APR_SUCCESS;
  +    else {
  +        bytes_read = 0;
  +        if (thefile->ungetchar != -1) {
  +            bytes_read = 1;
  +            *(char *)buf = (char)thefile->ungetchar;
  +            buf = (char *)buf + 1;
  +            (*nbytes)--;
  +            thefile->ungetchar = -1;
  +	if     (*nbytes == 0) {
  +	        *nbytes = bytes_read;
  +	        return APR_SUCCESS;
  +	}
  +        }
  +
  +        do {
  +            rv = read(thefile->filedes, buf, *nbytes);
  +        } while (rv == -1 && errno == EINTR);
  +
  +        if (rv == -1 && 
  +            (errno == EAGAIN || errno == EWOULDBLOCK) && 
  +            thefile->timeout != 0) {
  +            ap_status_t arv = wait_for_io_or_timeout(thefile, 1);
  +            if (arv != APR_SUCCESS) {
  +                *nbytes = bytes_read;
  +                return arv;
  +            }
  +            else {
  +                do {
  +                    rv = read(thefile->filedes, buf, *nbytes);
  +                } while (rv == -1 && errno == EINTR);
  +            }
  +        }  
  +
  +        *nbytes = bytes_read;
  +        if (rv == 0) {
  +	return     APR_EOF;
  +        }
  +        if (rv > 0) {
  +	*nbytes     += rv;
  +	return     APR_SUCCESS;
  +        }
  +        return errno;
       }
  -    return errno;
   }
   
   ap_status_t ap_write(ap_file_t *thefile, void *buf, ap_ssize_t *nbytes)
  @@ -143,32 +184,70 @@
   
       if(thefile == NULL || nbytes == NULL || (buf == NULL && *nbytes != 0))
           return APR_EBADARG;
  +
  +    if (thefile->buffered) {
  +        char *pos = (char *)buf;
  +        int blocksize;
  +        int size = *nbytes;
  +
  +#if APR_HAS_THREADS
  +        ap_lock(thefile->thlock);
  +#endif
   
  -    do {
  -        rv = write(thefile->filedes, buf, *nbytes);
  -    } while (rv == (ap_size_t)-1 && errno == EINTR);
  -
  -    if (rv == (ap_size_t)-1 &&
  -        (errno == EAGAIN || errno == EWOULDBLOCK) && 
  -        thefile->timeout != 0) {
  -        ap_status_t arv = wait_for_io_or_timeout(thefile, 0);
  -        if (arv != APR_SUCCESS) {
  -            *nbytes = 0;
  -            return arv;
  -        }
  -        else {
  -            do {
  -                rv = write(thefile->filedes, buf, *nbytes);
  -	    } while (rv == (ap_size_t)-1 && errno == EINTR);
  +        if ( thefile->direction == 0 ) {
  +            /* Position file pointer for writing at the offset we are 
  +             * logically reading from
  +             */
  +            ap_int64_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
  +            if (offset != thefile->filePtr)
  +                lseek(thefile->filedes, offset, SEEK_SET);
  +            thefile->bufpos = thefile->dataRead = 0;
  +            thefile->direction = 1;
           }
  -    }  
   
  -    if (rv == (ap_size_t)-1) {
  -        (*nbytes) = 0;
  -        return errno;
  +        while (rv == 0 && size > 0) {
  +            if (thefile->bufpos == APR_FILE_BUFSIZE)   /* write buffer is full*/
  +                ap_flush(thefile);
  +
  +            blocksize = size > APR_FILE_BUFSIZE - thefile->bufpos ? 
  +                        APR_FILE_BUFSIZE - thefile->bufpos : size;
  +            memcpy(thefile->buffer + thefile->bufpos, pos, blocksize);          
           thefile->bufpos += blocksize;
  +            pos += blocksize;
  +            size -= blocksize;
  +        }
  +
  +#if APR_HAS_THREADS
  +        ap_unlock(thefile->thlock);
  +#endif
  +        return rv;
       }
  -    *nbytes = rv;
  -    return APR_SUCCESS;
  +    else {
  +        do {
  +            rv = write(thefile->filedes, buf, *nbytes);
  +        } while (rv == (ap_size_t)-1 && errno == EINTR);
  +
  +        if (rv == (ap_size_t)-1 &&
  +            (errno == EAGAIN || errno == EWOULDBLOCK) && 
  +            thefile->timeout != 0) {
  +            ap_status_t arv = wait_for_io_or_timeout(thefile, 0);
  +            if (arv != APR_SUCCESS) {
  +                *nbytes = 0;
  +                return arv;
  +            }
  +            else {
  +                do {
  +                    rv = write(thefile->filedes, buf, *nbytes);
  +	        } while (rv == (ap_size_t)-1 && errno == EINTR);
  +            }
  +        }  
  +
  +        if (rv == (ap_size_t)-1) {
  +            (*nbytes) = 0;
  +            return errno;
  +        }
  +        *nbytes = rv;
  +        return APR_SUCCESS;
  +    }
   }
   
   ap_status_t ap_writev(ap_file_t *thefile, const struct iovec *vec,
  @@ -255,12 +334,23 @@
   
   ap_status_t ap_flush(ap_file_t *thefile)
   {
  -/* Another function to get rid of once we finish removing buffered I/O
  - * and we are sure nobody is using it.
  - */
       if(thefile == NULL)
           return APR_EBADARG;
   
  +    if (thefile->buffered) {
  +        ap_int64_t written = 0;
  +        int rc = 0;
  +
  +        if (thefile->direction == 1 && thefile->bufpos) {
  +            written= write(thefile->filedes, thefile->buffer, thefile->bufpos);
  +            thefile->filePtr += written;
  +
  +            if (rc == 0)
  +                thefile->bufpos = 0;
  +        }
  +
  +        return rc;
  +    }
       /* There isn't anything to do if we aren't buffering the output
        * so just return success.
        */
  
  
  
  1.11      +63 -8     apache-2.0/src/lib/apr/file_io/unix/seek.c
  
  Index: seek.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/src/lib/apr/file_io/unix/seek.c,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- seek.c	2000/04/14 00:55:00	1.10
  +++ seek.c	2000/04/14 23:36:13	1.11
  @@ -54,16 +54,71 @@
   
   #include "fileio.h"
   
  -ap_status_t ap_seek(ap_file_t *thefile, ap_seek_where_t where, ap_off_t *offset)
  +static ap_status_t setptr(ap_file_t *thefile, unsigned long pos )
   {
  -    ap_off_t rv;
  -    rv = lseek(thefile->filedes, *offset, where);
  -    if (rv == -1) {
  -        *offset = -1;
  -        return errno;
  +    long newbufpos;
  +    int rc;
  +
  +    if (thefile->direction == 1) {
  +        ap_flush(thefile);
  +        thefile->bufpos = thefile->direction = thefile->dataRead = 0;
       }
  +
  +    newbufpos = pos - (thefile->filePtr - thefile->dataRead);
  +    if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
  +        thefile->bufpos = newbufpos;
  +        rc = 0;
  +    } 
       else {
  -        *offset = rv;
  -        return APR_SUCCESS;
  +        rc = lseek(thefile->filedes, pos, SEEK_SET);
  +
  +        if (rc != -1 ) {
  +            thefile->bufpos = thefile->dataRead = 0;
  +            rc == errno;
  +        }
  +    }
  +
  +    return rc;
  +}
  +
  +
  +ap_status_t ap_seek(ap_file_t *thefile, ap_seek_where_t where, ap_off_t *offset)
  +{
  +    ap_off_t rv;
  +
  +
  +    if (thefile->buffered) {
  +        int rc = EINVAL;
  +        ap_finfo_t finfo;
  +
  +        switch (where) {
  +        case APR_SET:
  +            rc = setptr(thefile, *offset);
  +            break;
  +
  +        case APR_CUR:
  +            rc = setptr(thefile, thefile->filePtr - thefile->dataRead + thefile->bufpos
+ *offset);
  +            break;
  +
  +        case APR_END:
  +            rc = ap_getfileinfo(&finfo, thefile);
  +            if (rc == APR_SUCCESS)
  +                rc = setptr(thefile, finfo.size - *offset);
  +            break;
  +        }
  +
  +        *offset = thefile->filePtr + thefile->bufpos;
  +        return rc;
  +    } else {
  +
  +        rv = lseek(thefile->filedes, *offset, where);
  +        if (rv == -1) {
  +            *offset = -1;
  +            return errno;
  +        }
  +        else {
  +            *offset = rv;
  +            return APR_SUCCESS;
  +        }
       }
   }
  
  
  

Mime
View raw message