apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bo...@apache.org
Subject svn commit: r537067 - in /apr/apr/branches/1.2.x: file_io/unix/readwrite.c test/testfile.c
Date Fri, 11 May 2007 02:54:28 GMT
Author: bojan
Date: Thu May 10 19:54:27 2007
New Revision: 537067

URL: http://svn.apache.org/viewvc?view=rev&rev=537067
Log:
Backport r537066 from the trunk
The file pointer position must be recalculated and set when writev()ing to a
buffered file.
Original fix by Davi Arnaut.

Modified:
    apr/apr/branches/1.2.x/file_io/unix/readwrite.c
    apr/apr/branches/1.2.x/test/testfile.c

Modified: apr/apr/branches/1.2.x/file_io/unix/readwrite.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.2.x/file_io/unix/readwrite.c?view=diff&rev=537067&r1=537066&r2=537067
==============================================================================
--- apr/apr/branches/1.2.x/file_io/unix/readwrite.c (original)
+++ apr/apr/branches/1.2.x/file_io/unix/readwrite.c Thu May 10 19:54:27 2007
@@ -244,7 +244,26 @@
 {
 #ifdef HAVE_WRITEV
     int bytes;
+#endif
 
+    if (thefile->buffered) {
+        apr_status_t rv = apr_file_flush(thefile);
+        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
     if ((bytes = writev(thefile->filedes, vec, nvec)) < 0) {
         *nbytes = 0;
         return errno;

Modified: apr/apr/branches/1.2.x/test/testfile.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.2.x/test/testfile.c?view=diff&rev=537067&r1=537066&r2=537067
==============================================================================
--- apr/apr/branches/1.2.x/test/testfile.c (original)
+++ apr/apr/branches/1.2.x/test/testfile.c Thu May 10 19:54:27 2007
@@ -655,6 +655,75 @@
 
 }
 
+static void test_writev_buffered(abts_case *tc, void *data)
+{
+    apr_file_t *f;
+    apr_size_t nbytes;
+    struct iovec vec[2];
+    const char *fname = "data/testwritev_buffered.dat";
+
+    APR_ASSERT_SUCCESS(tc, "open file for writing",
+                       apr_file_open(&f, fname,
+                                     APR_WRITE | APR_CREATE | APR_TRUNCATE |
+                                     APR_BUFFERED, APR_OS_DEFAULT, p));
+
+    nbytes = strlen(TESTSTR);
+    APR_ASSERT_SUCCESS(tc, "buffered write",
+                       apr_file_write(f, TESTSTR, &nbytes));
+
+    vec[0].iov_base = LINE1;
+    vec[0].iov_len = strlen(LINE1);
+    vec[1].iov_base = LINE2;
+    vec[1].iov_len = strlen(LINE2);
+
+    APR_ASSERT_SUCCESS(tc, "writev of size 2 to file",
+                       apr_file_writev(f, vec, 2, &nbytes));
+
+    APR_ASSERT_SUCCESS(tc, "close for writing",
+                       apr_file_close(f));
+
+    file_contents_equal(tc, fname, TESTSTR LINE1 LINE2,
+                        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;
@@ -880,6 +949,8 @@
     abts_run_test(suite, test_puts, NULL);
     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);



Mime
View raw message