rbb 99/08/16 12:10:20
Modified: apr/file_io/unix filedup.c fileio.h open.c readwrite.c seek.c Log: Get the buffered file I/O stuff in APR for unix finally. This has been on my TODO list for a while, but it's off now. Revision Changes Path 1.16 +18 -0 apache-apr/apr/file_io/unix/filedup.c Index: filedup.c =================================================================== RCS file: /home/cvs/apache-apr/apr/file_io/unix/filedup.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- filedup.c 1999/06/02 18:44:33 1.15 +++ filedup.c 1999/08/16 19:10:11 1.16 @@ -67,6 +67,7 @@ */ ap_status_t ap_dupfile(struct file_t *old_file, struct file_t **new_file) { + char *buf_oflags; (*new_file) = (struct file_t *)ap_palloc(old_file->cntxt, sizeof(struct file_t)); @@ -74,7 +75,24 @@ return APR_ENOMEM; } (*new_file)->cntxt = old_file->cntxt; + if (old_file->buffered) { + switch (old_file->oflags) { + case O_RDONLY: + buf_oflags = "r"; + break; + case O_WRONLY: + buf_oflags = "w"; + break; + case O_RDWR: + buf_oflags = "r+"; + break; + } + (*new_file)->filehand = freopen(old_file->fname, buf_oflags, + old_file->filehand); + } + else { (*new_file)->filedes = dup(old_file->filedes); + } (*new_file)->fname = ap_pstrdup(old_file->cntxt, old_file->fname); (*new_file)->buffered = old_file->buffered; (*new_file)->protection = old_file->protection; 1.10 +2 -0 apache-apr/apr/file_io/unix/fileio.h Index: fileio.h =================================================================== RCS file: /home/cvs/apache-apr/apr/file_io/unix/fileio.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- fileio.h 1999/08/04 17:51:53 1.9 +++ fileio.h 1999/08/16 19:10:12 1.10 @@ -69,7 +69,9 @@ struct file_t { ap_context_t *cntxt; int filedes; + FILE *filehand; char * fname; + int oflags; int buffered; int stated; int eof_hit; 1.34 +36 -1 apache-apr/apr/file_io/unix/open.c Index: open.c =================================================================== RCS file: /home/cvs/apache-apr/apr/file_io/unix/open.c,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- open.c 1999/08/04 17:51:54 1.33 +++ open.c 1999/08/16 19:10:13 1.34 @@ -65,7 +65,15 @@ ap_status_t file_cleanup(void *thefile) { struct file_t *file = thefile; - if (close(file->filedes) == 0) { + int rv; + if (file->buffered) { + rv = fclose(file->filehand); + } + else { + rv = close(file->filedes); + } + + if (rv == 0) { file->filedes = -1; return APR_SUCCESS; } @@ -100,10 +108,12 @@ int oflags = 0; struct stat info; mode_t mode = get_fileperms(perm); + char *buf_oflags; (*new) = (struct file_t *)ap_palloc(cont, sizeof(struct file_t)); (*new)->cntxt = cont; + (*new)->oflags = oflags; if ((flag & APR_READ) && (flag & APR_WRITE)) { oflags = O_RDWR; @@ -154,6 +164,25 @@ (*new)->eof_hit = 1; return errno; } + + if ((*new)->buffered) { + switch (oflags) { + case O_RDONLY: + buf_oflags = "r"; + break; + case O_WRONLY: + buf_oflags = "w"; + break; + case O_RDWR: + buf_oflags = "r+"; + break; + } + (*new)->filehand = fdopen((*new)->filedes, buf_oflags); + if ((*new)->filehand == NULL) { + file_cleanup(*new); + return errno; + } + } (*new)->stated = 0; /* we haven't called stat for this file yet. */ (*new)->eof_hit = 0; ap_register_cleanup((*new)->cntxt, (void *)(*new), file_cleanup, NULL); @@ -239,6 +268,12 @@ ap_status_t ap_eof(ap_file_t *fptr) { char ch; + if (fptr->buffered) { + if (feof(fptr->filehand) == 0) { + return APR_SUCCESS; + } + return APR_EOF; + } if (fptr->eof_hit == 1) { return APR_EOF; } 1.15 +31 -4 apache-apr/apr/file_io/unix/readwrite.c Index: readwrite.c =================================================================== RCS file: /home/cvs/apache-apr/apr/file_io/unix/readwrite.c,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- readwrite.c 1999/08/04 17:51:54 1.14 +++ readwrite.c 1999/08/16 19:10:14 1.15 @@ -82,9 +82,14 @@ return APR_EBADF; } - rv = read(thefile->filedes, buf, *nbytes); + if (thefile->buffered) { + rv = fread(buf, *nbytes, 1, thefile->filehand); + } + else { + rv = read(thefile->filedes, buf, *nbytes); + } - if ((*nbytes != rv) && (errno != EINTR)) { + if ((*nbytes != rv) && (errno != EINTR) && !thefile->buffered) { thefile->eof_hit = 1; } *nbytes = rv; @@ -112,7 +117,12 @@ return APR_EBADF; } - rv = write(thefile->filedes, buf, *nbytes); + if (thefile->buffered) { + rv = fwrite(buf, *nbytes, 1, thefile->filehand); + } + else { + rv = write(thefile->filedes, buf, *nbytes); + } if (strcmp(thefile->fname, "PIPE")) { if (stat(thefile->fname, &info) == 0) { @@ -160,6 +170,12 @@ */ ap_status_t ap_putc(ap_file_t *thefile, char ch) { + if (thefile->buffered) { + if (fputc(ch, thefile->filehand) == ch) { + return APR_SUCCESS; + } + return errno; + } if (write(thefile->filedes, &ch, 1) != 1) { return errno; } @@ -174,7 +190,18 @@ */ ap_status_t ap_getc(ap_file_t *thefile, char *ch) { - ssize_t rv = read(thefile->filedes, ch, 1); + ssize_t rv; + + if (thefile->buffered) { + if ((*ch) = fgetc(thefile->filehand)) { + return APR_SUCCESS; + } + if (feof(thefile->filehand)) { + return APR_EOF; + } + return errno; + } + rv = read(thefile->filedes, ch, 1); if (rv == 0) { thefile->eof_hit = TRUE; return APR_EOF; 1.8 +6 -1 apache-apr/apr/file_io/unix/seek.c Index: seek.c =================================================================== RCS file: /home/cvs/apache-apr/apr/file_io/unix/seek.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- seek.c 1999/07/28 18:10:58 1.7 +++ seek.c 1999/08/16 19:10:16 1.8 @@ -73,7 +73,12 @@ ap_status_t ap_seek(struct file_t *thefile, ap_seek_where_t where, ap_off_t *offset) { ap_off_t rv; - rv = lseek(thefile->filedes, *offset, where); + if (thefile->buffered) { + rv = fseek(thefile->filehand, *offset, where); + } + else { + rv = lseek(thefile->filedes, *offset, where); + } if (rv == -1) { *offset = -1; return errno;