[Interest] QFile copy and sync/fsync

Lincoln Ramsay a1291762 at gmail.com
Thu Jan 3 00:08:46 CET 2013


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).

-- 
Link




More information about the Interest mailing list