[Interest] QFile copy and sync/fsync

Duane duane.hebert at group-upc.com
Thu Jan 3 14:47:31 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).
>

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.

>> 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.  But it was much simpler to just use QFile::copy().





More information about the Interest mailing list