[Interest] QFile copy and sync/fsync

Duane duane.hebert at group-upc.com
Thu Jan 3 16:15:29 CET 2013


On 01/02/2013 06:08 PM, Lincoln Ramsay wrote:
> On 03/01/13 06:49, Duane wrote:
>> I have a function to back up some files to a usb stick.
>
> Using a variant of the FAT filesystem no doubt?
>
>> I've had complaints from users that the files are sometime not copied.  It looks
>> like they're removing the usb stick before the standard sync cycle.  If
>> I call sync from my copy routine this works but it hangs the system
>> waiting for sync to complete.
>>
>> If I mount the usb stick with -o sync, this also solves my problem but
>> it also hangs the system.
>
> USB sticks are quite slow so it can take quite a while to complete a
> 'sync' operation. You could popup a message "Please wait for the copy to
> complete" before calling sync (though you've got to ensure it's visible
> before the system hangs)...
>
>> I have tried to use fsync on a per file basis but this doesn't solve my
>> problem.
>
> This _should_ work but perhaps not on whatever filesystem the USB stick
> uses (or perhaps you're using it wrong?). I guess sync is able to
> complete (from what you've described).
>
>> 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).
>


I ended up writing my own function to copy the files using two QFiles.
After copying I close the target and then use fsync(target.handle()).
(I'm not sure how to use the O_SYNC flag on a QFile...)

If I remove the USb stick right away, it still doesn't work.  I end up 
with an empty directory on USB stick.  This is more than I had using 
QFile::Copy.  So now I add a call to sync(), after copying all of the 
files, sync() now takes very little time and the files are there. 
Sounds like some directory info is not completed until sync is called 
but the files are done.  Does this make sense?




More information about the Interest mailing list