apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1044440 - /apr/apr/trunk/file_io/unix/seek.c
Date Fri, 10 Dec 2010 17:06:50 GMT
Author: mturk
Date: Fri Dec 10 17:06:49 2010
New Revision: 1044440

URL: http://svn.apache.org/viewvc?rev=1044440&view=rev
Log:
Fix file_trunc for buffered files. Make sure the write buffer is flushed before truncate call

Modified:
    apr/apr/trunk/file_io/unix/seek.c

Modified: apr/apr/trunk/file_io/unix/seek.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/unix/seek.c?rev=1044440&r1=1044439&r2=1044440&view=diff
==============================================================================
--- apr/apr/trunk/file_io/unix/seek.c (original)
+++ apr/apr/trunk/file_io/unix/seek.c Fri Dec 10 17:06:49 2010
@@ -33,7 +33,7 @@ static apr_status_t setptr(apr_file_t *t
     if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
         thefile->bufpos = newbufpos;
         rv = APR_SUCCESS;
-    } 
+    }
     else {
         if (lseek(thefile->filedes, pos, SEEK_SET) != -1) {
             thefile->bufpos = thefile->dataRead = 0;
@@ -98,6 +98,30 @@ APR_DECLARE(apr_status_t) apr_file_seek(
 
 apr_status_t apr_file_trunc(apr_file_t *fp, apr_off_t offset)
 {
+    if (fp->buffered) {
+        int rc = 0;
+        file_lock(fp);
+        if (fp->direction == 1 && fp->bufpos != 0) {
+            apr_off_t len = fp->filePtr + fp->bufpos;
+            if (offset < len) {
+                /* New file end fall below our write buffer limit.
+                 * Figure out if and what needs to be flushed.
+                 */
+                apr_off_t off = len - offset;
+                if (off >= 0 && off <= fp->bufpos)
+                    fp->bufpos = fp->bufpos - (size_t)off;
+                else
+                    fp->bufpos = 0;
+            }
+            rc = apr_file_flush_locked(fp);
+            /* Reset buffer positions for write mode */
+            fp->bufpos = fp->direction = fp->dataRead = 0;
+        }
+        if (rc) {
+            return rc;
+        }
+        file_unlock(fp);
+    }
     if (ftruncate(fp->filedes, offset) == -1) {
         return errno;
     }



Mime
View raw message