Author: colm Date: Tue Oct 18 07:48:23 2005 New Revision: 326116 URL: http://svn.apache.org/viewcvs?rev=326116&view=rev Log: Add apr-wide definitions and unix implementation of apr_file_buffer_set() and apr_file_buffer_size_get() functions, to support variable buffer sizes for file handles. Modified: apr/apr/trunk/CHANGES apr/apr/trunk/file_io/unix/filedup.c apr/apr/trunk/file_io/unix/open.c apr/apr/trunk/file_io/unix/readwrite.c apr/apr/trunk/include/apr_file_io.h apr/apr/trunk/include/arch/unix/apr_arch_file_io.h apr/apr/trunk/test/testfile.c Modified: apr/apr/trunk/CHANGES URL: http://svn.apache.org/viewcvs/apr/apr/trunk/CHANGES?rev=326116&r1=326115&r2=326116&view=diff ============================================================================== --- apr/apr/trunk/CHANGES (original) +++ apr/apr/trunk/CHANGES Tue Oct 18 07:48:23 2005 @@ -1,5 +1,9 @@ Changes for APR 1.3.0 + *) Add apr_file_buffer_set() and apr_file_buffer_size_get() functions + to support variable buffer sizes with APR file handles. + [Colm MacCarthaigh] + *) Add apr_file_open_flags_std[err|out|in]() functions. [Colm MacCarthaigh] Modified: apr/apr/trunk/file_io/unix/filedup.c URL: http://svn.apache.org/viewcvs/apr/apr/trunk/file_io/unix/filedup.c?rev=326116&r1=326115&r2=326116&view=diff ============================================================================== --- apr/apr/trunk/file_io/unix/filedup.c (original) +++ apr/apr/trunk/file_io/unix/filedup.c Tue Oct 18 07:48:23 2005 @@ -62,7 +62,8 @@ * got one. */ if ((*new_file)->buffered && !(*new_file)->buffer) { - (*new_file)->buffer = apr_palloc(p, APR_FILE_BUFSIZE); + (*new_file)->buffer = apr_palloc(p, old_file->bufsize); + (*new_file)->bufsize = old_file->bufsize; } /* this is the way dup() works */ @@ -121,7 +122,8 @@ memcpy(*new_file, old_file, sizeof(apr_file_t)); (*new_file)->pool = p; if (old_file->buffered) { - (*new_file)->buffer = apr_palloc(p, APR_FILE_BUFSIZE); + (*new_file)->buffer = apr_palloc(p, old_file->bufsize); + (*new_file)->bufsize = old_file->bufsize; if (old_file->direction == 1) { memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos); } Modified: apr/apr/trunk/file_io/unix/open.c URL: http://svn.apache.org/viewcvs/apr/apr/trunk/file_io/unix/open.c?rev=326116&r1=326115&r2=326116&view=diff ============================================================================== --- apr/apr/trunk/file_io/unix/open.c (original) +++ apr/apr/trunk/file_io/unix/open.c Tue Oct 18 07:48:23 2005 @@ -150,7 +150,8 @@ (*new)->buffered = (flag & APR_BUFFERED) > 0; if ((*new)->buffered) { - (*new)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE); + (*new)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE); + (*new)->bufsize = APR_FILE_DEFAULT_BUFSIZE; #if APR_HAS_THREADS if ((*new)->flags & APR_XTHREAD) { (*new)->thlock = thlock; @@ -239,7 +240,8 @@ #endif if ((*file)->buffered) { - (*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE); + (*file)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE); + (*file)->bufsize = APR_FILE_DEFAULT_BUFSIZE; #if APR_HAS_THREADS if ((*file)->flags & APR_XTHREAD) { apr_status_t rv; Modified: apr/apr/trunk/file_io/unix/readwrite.c URL: http://svn.apache.org/viewcvs/apr/apr/trunk/file_io/unix/readwrite.c?rev=326116&r1=326115&r2=326116&view=diff ============================================================================== --- apr/apr/trunk/file_io/unix/readwrite.c (original) +++ apr/apr/trunk/file_io/unix/readwrite.c Tue Oct 18 07:48:23 2005 @@ -70,7 +70,8 @@ } while (rv == 0 && size > 0) { if (thefile->bufpos >= thefile->dataRead) { - int bytesread = read(thefile->filedes, thefile->buffer, APR_FILE_BUFSIZE); + int bytesread = read(thefile->filedes, thefile->buffer, + thefile->bufsize); if (bytesread == 0) { thefile->eof_hit = TRUE; rv = APR_EOF; @@ -177,11 +178,11 @@ rv = 0; while (rv == 0 && size > 0) { - if (thefile->bufpos == APR_FILE_BUFSIZE) /* write buffer is full*/ + if (thefile->bufpos == thefile->bufsize) /* write buffer is full*/ rv = apr_file_flush(thefile); - blocksize = size > APR_FILE_BUFSIZE - thefile->bufpos ? - APR_FILE_BUFSIZE - thefile->bufpos : size; + blocksize = size > thefile->bufsize - thefile->bufpos ? + thefile->bufsize - thefile->bufpos : size; memcpy(thefile->buffer + thefile->bufpos, pos, blocksize); thefile->bufpos += blocksize; pos += blocksize; Modified: apr/apr/trunk/include/apr_file_io.h URL: http://svn.apache.org/viewcvs/apr/apr/trunk/include/apr_file_io.h?rev=326116&r1=326115&r2=326116&view=diff ============================================================================== --- apr/apr/trunk/include/apr_file_io.h (original) +++ apr/apr/trunk/include/apr_file_io.h Tue Oct 18 07:48:23 2005 @@ -576,6 +576,27 @@ apr_pool_t *p); /** + * Give the specified apr file handle a new buffer + * @param thefile The file handle that is to be modified + * @param buffer The buffer + * @param bufsize The size of the buffer + * @remark It is possible to add a buffer to previously unbuffered + * file handles, the APR_BUFFERED flag will be added to + * the file handle's flags. Likewise, with buffer=NULL and + * bufsize=0 arguments it is possible to make a previously + * buffered file handle unbuffered. + */ +APR_DECLARE(apr_status_t) apr_file_buffer_set(apr_file_t *thefile, + char * buffer, + apr_size_t bufsize); + +/** + * Get the size of any buffer for the specified apr file handle + * @param thefile The file handle + */ +APR_DECLARE(apr_size_t) apr_file_buffer_size_get(apr_file_t *thefile); + +/** * Move the read/write file offset to a specified byte within a file. * @param thefile The file descriptor * @param where How to move the pointer, one of: Modified: apr/apr/trunk/include/arch/unix/apr_arch_file_io.h URL: http://svn.apache.org/viewcvs/apr/apr/trunk/include/arch/unix/apr_arch_file_io.h?rev=326116&r1=326115&r2=326116&view=diff ============================================================================== --- apr/apr/trunk/include/arch/unix/apr_arch_file_io.h (original) +++ apr/apr/trunk/include/arch/unix/apr_arch_file_io.h Tue Oct 18 07:48:23 2005 @@ -82,7 +82,9 @@ #endif /* End System headers */ -#define APR_FILE_BUFSIZE 4096 +#define APR_FILE_DEFAULT_BUFSIZE 4096 +/* For backwards-compat */ +#define APR_FILE_BUFSIZE APR_FILE_DEFAULT_BUFSIZE struct apr_file_t { apr_pool_t *pool; @@ -101,7 +103,8 @@ #endif /* Stuff for buffered mode */ char *buffer; - int bufpos; /* Read/Write position in buffer */ + apr_size_t bufpos; /* Read/Write position in buffer */ + apr_size_t bufsize; /* The size of the 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 */ Modified: apr/apr/trunk/test/testfile.c URL: http://svn.apache.org/viewcvs/apr/apr/trunk/test/testfile.c?rev=326116&r1=326115&r2=326116&view=diff ============================================================================== --- apr/apr/trunk/test/testfile.c (original) +++ apr/apr/trunk/test/testfile.c Tue Oct 18 07:48:23 2005 @@ -326,6 +326,35 @@ apr_file_close(filetest); } +static void test_buffer_set_get(abts_case *tc, void *data) +{ + apr_status_t rv; + apr_file_t *filetest = NULL; + char * buffer; + + rv = apr_file_open(&filetest, FILENAME, + APR_WRITE | APR_BUFFERED, + APR_UREAD | APR_UWRITE | APR_GREAD, p); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + + rv = apr_file_buffer_size_get(filetest); + ABTS_INT_EQUAL(tc, APR_BUFFERSIZE, rv); + + buffer = apr_pcalloc(p, 10240); + rv = apr_file_buffer_set(filetest, buffer, 10240); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + + rv = apr_file_buffer_size_get(filetest); + ABTS_INT_EQUAL(tc, 10240, rv); + + rv = apr_file_buffer_set(filetest, buffer, 12); + ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); + + rv = apr_file_buffer_size_get(filetest); + ABTS_INT_EQUAL(tc, 12, rv); + + apr_file_close(filetest); +} static void test_getc(abts_case *tc, void *data) { apr_file_t *f = NULL; @@ -802,6 +831,7 @@ abts_run_test(suite, test_bigfprintf, NULL); abts_run_test(suite, test_fail_write_flush, NULL); abts_run_test(suite, test_fail_read_flush, NULL); + abts_run_test(suite, test_buffer_set_get, NULL); return suite; }