You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by bo...@apache.org on 2007/05/11 04:11:25 UTC

svn commit: r537066 - in /apr/apr/trunk: file_io/unix/readwrite.c test/testfile.c

Author: bojan
Date: Thu May 10 19:11:25 2007
New Revision: 537066

URL: http://svn.apache.org/viewvc?view=rev&rev=537066
Log:
The file pointer position must be recalculated and set when writev()ing to a
buffered file.
Fix by Davi Arnaut for bug #40963

Modified:
    apr/apr/trunk/file_io/unix/readwrite.c
    apr/apr/trunk/test/testfile.c

Modified: apr/apr/trunk/file_io/unix/readwrite.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/unix/readwrite.c?view=diff&rev=537066&r1=537065&r2=537066
==============================================================================
--- apr/apr/trunk/file_io/unix/readwrite.c (original)
+++ apr/apr/trunk/file_io/unix/readwrite.c Thu May 10 19:11:25 2007
@@ -252,6 +252,15 @@
         if (rv != APR_SUCCESS) {
             return rv;
         }
+        if (thefile->direction == 0) {
+            /* Position file pointer for writing at the offset we are
+             * logically reading from
+             */
+            apr_int64_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
+            if (offset != thefile->filePtr)
+                lseek(thefile->filedes, offset, SEEK_SET);
+            thefile->bufpos = thefile->dataRead = 0;
+        }
     }
 
 #ifdef HAVE_WRITEV

Modified: apr/apr/trunk/test/testfile.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/testfile.c?view=diff&rev=537066&r1=537065&r2=537066
==============================================================================
--- apr/apr/trunk/test/testfile.c (original)
+++ apr/apr/trunk/test/testfile.c Thu May 10 19:11:25 2007
@@ -715,6 +715,44 @@
                         strlen(TESTSTR) + strlen(LINE1) + strlen(LINE2));
 }
 
+static void test_writev_buffered_seek(abts_case *tc, void *data)
+{
+    apr_file_t *f;
+    apr_status_t rv;
+    apr_off_t off = 0;
+    struct iovec vec[3];
+    apr_size_t nbytes = strlen(TESTSTR);
+    char *str = apr_pcalloc(p, nbytes+1);
+    const char *fname = "data/testwritev_buffered.dat";
+
+    APR_ASSERT_SUCCESS(tc, "open file for writing",
+                       apr_file_open(&f, fname,
+                                     APR_WRITE | APR_READ | APR_BUFFERED,
+                                     APR_OS_DEFAULT, p));
+
+    rv = apr_file_read(f, str, &nbytes);
+    ABTS_STR_EQUAL(tc, TESTSTR, str);
+    APR_ASSERT_SUCCESS(tc, "buffered seek", apr_file_seek(f, APR_SET, &off));
+
+    vec[0].iov_base = LINE1;
+    vec[0].iov_len = strlen(LINE1);
+    vec[1].iov_base = LINE2;
+    vec[1].iov_len = strlen(LINE2);
+    vec[2].iov_base = TESTSTR;
+    vec[2].iov_len = strlen(TESTSTR);
+
+    APR_ASSERT_SUCCESS(tc, "writev of size 2 to file",
+                       apr_file_writev(f, vec, 3, &nbytes));
+
+    APR_ASSERT_SUCCESS(tc, "close for writing",
+                       apr_file_close(f));
+
+    file_contents_equal(tc, fname, LINE1 LINE2 TESTSTR,
+                        strlen(LINE1) + strlen(LINE2) + strlen(TESTSTR));
+
+    APR_ASSERT_SUCCESS(tc, "remove file", apr_file_remove(fname, p));
+}
+
 static void test_truncate(abts_case *tc, void *data)
 {
     apr_status_t rv;
@@ -941,6 +979,7 @@
     abts_run_test(suite, test_writev, NULL);
     abts_run_test(suite, test_writev_full, NULL);
     abts_run_test(suite, test_writev_buffered, NULL);
+    abts_run_test(suite, test_writev_buffered_seek, NULL);
     abts_run_test(suite, test_bigread, NULL);
     abts_run_test(suite, test_mod_neg, NULL);
     abts_run_test(suite, test_truncate, NULL);