[Interest] QFile copy and sync/fsync

Lincoln Ramsay a1291762 at gmail.com
Thu Jan 3 15:58:55 CET 2013


On 3/01/13 11:47 PM, Duane wrote:
> I'm using QFile::copy(source,target). I've tried changing it to use 
> the non static copy from QFile but I don't have a pointer to the 
> target. So I create another instance of QFile for that one and call 
> fsync on it after the copy but this doesn't work.

That's not how fsync works. You have to call fsync on the open file 
handle that you've just written to. If you used QFile::copy then you 
were never given this file handle. sync worked because it flushes all 
pending data.

>>> Does fsync not force the write to disk?  Is there a way to sync only a
>>> particular folder?
>> You could try manually copying the data yourself. Open the source file
>> and the destination file. Use the O_SYNC flag and non-blocking IO
>> operations so that you can keep your UI responsive while the copying
>> completes. More work but may get you the results you want (and you can
>> have an accurate progress bar as well).
> This is what I'm thinking of doing.  I think that this way, fsync may
> also work.

If your destination file is open and you have the file handle you can 
call fsync, but if you're using O_SYNC then this shouldn't be required. 
Without O_SYNC you can probably write more data at a time but don't 
write too much or fsync will block for a long time.

> But it was much simpler to just use QFile::copy().

Alas, the difference between "simple" and "correct" is around 10-100x 
more code (at least in my experience).

-- 
Link




More information about the Interest mailing list