[Interest] QFile copy and sync/fsync

Nikos Chantziaras realnc at gmail.com
Wed Jan 2 22:45:01 CET 2013


On 02/01/13 22:49, Duane wrote:
> Qt 4.8 on Linux TinyCore or Fedora.
>
> I have a function to back up some files to a usb stick.  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.
>
> I have tried to use fsync on a per file basis but this doesn't solve my
> problem.  I have a watchdog to maintain so I would need to use
> qApp->processEvents() periodically in busy loops.
>
> Does fsync not force the write to disk?  Is there a way to sync only a
> particular folder?

Once QFile::flush() is called, the data is sent to the OS. From there, 
it's up to the system what to do with the data. Users need to unmount 
the stick before removing it. If they don't want to do that, then they 
need to set up their system such that it mounts the USB stick 
synchronously by default. It's either that or unmount first.

This is not a QFile problem. When someone removes a live filesystem 
without unmounting it, loss of data is to be expected and should come as 
no surprise. If you try to work around it, then you'll be hurting 
performance for users who do know how to properly use their system 
(calling QFile::sync() all the time can mess up buffering.)




More information about the Interest mailing list