Qt : waiting for a file being closed by another process
I've a daemon (obexpushd) that writes some files in a directory. In my Qt application, I listen to changes in this directory thanks to QFile开发者_如何学JAVASystemWatcher. Yet, the directoryChanged signal is emitted when obexpushd creates the file, not when it has finished to write it.
So, I've to wait for obexpush to finish writing the file before processing it. What is the best strategy ?
Check the size remains the same for a fixed amount of time ? Call lsof at fixed interval ? Any better solution ?
Is obexpushd a program that you can change? If so, the easiest thing to do would probably be have obexpushd write to temporary file, then rename the file when it's done writing. That would be something you could monitor in your Qt program. A more robust and elegant way would be to have obexpushd communicate with your Qt program, maybe via dbus, to explicitly say when it's done.
If obexpushd isn't under your control, then one of the strategies you've listed is probably your best bet.
You could watch for file timestamps to change. QFileInfo
makes that possible.
There seems to be an open bug on this issue http://bugreports.qt-project.org/browse/QTBUG-3576. If you know the filenames of the files you are waiting on, perhaps you can use QFileSystemWatcher::fileChanged()
.
精彩评论