Author: bojan Date: Mon Feb 6 20:55:55 2012 New Revision: 1241175 URL: http://svn.apache.org/viewvc?rev=1241175&view=rev Log: Backport r1044440 from trunk to 1.5.x. Fix file_trunc for buffered files. Make sure the write buffer is flushed before truncate call Modified: apr/apr/branches/1.5.x/ (props changed) apr/apr/branches/1.5.x/file_io/unix/seek.c Propchange: apr/apr/branches/1.5.x/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon Feb 6 20:55:55 2012 @@ -1,2 +1,2 @@ /apr/apr/branches/1.4.x:1101301 -/apr/apr/trunk:733052,747990,748361,748371,748565,748888,748902,748988,749810,760443,782838,783398,783958,784633,784773,788588,793192-793193,794118,794485,795267,799497,800627,809745,809854,810472,811455,813063,821306,829490,831641,835607,908427,910419,917819,917837-917838,979891,983618,990435,1055657,1072165,1078845,1183683,1183685-1183686,1183688,1183693,1183698,1236970,1237078,1237507,1240472 +/apr/apr/trunk:733052,747990,748361,748371,748565,748888,748902,748988,749810,760443,782838,783398,783958,784633,784773,788588,793192-793193,794118,794485,795267,799497,800627,809745,809854,810472,811455,813063,821306,829490,831641,835607,908427,910419,917819,917837-917838,979891,983618,990435,1044440,1055657,1072165,1078845,1183683,1183685-1183686,1183688,1183693,1183698,1236970,1237078,1237507,1240472 Modified: apr/apr/branches/1.5.x/file_io/unix/seek.c URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/file_io/unix/seek.c?rev=1241175&r1=1241174&r2=1241175&view=diff ============================================================================== --- apr/apr/branches/1.5.x/file_io/unix/seek.c (original) +++ apr/apr/branches/1.5.x/file_io/unix/seek.c Mon Feb 6 20:55:55 2012 @@ -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; }