Details
-
Bug
-
Resolution: Done
-
P3: Somewhat important
-
None
-
5.5.0 Alpha
-
None
Description
I wasn't sure whether to call this a suggestion or a bug. Since it involves data loss, I went with the latter.
Use case: A Text Editor on a multi-user system
Suppose two users are on the same system and they're both using a text editor at the same time. If they both choose to save their file at the same location at the same time, there's a race condition where both of the instances of the text editor application return false for QFile::exists, which in turn would cause either a jumbled/corrupt text file, or one of the users file completely (and silently) overwriting the other users file (depending on the OpenMode flags).
Solution: QTemporaryFile uses "create only if not already existing" in an atomic fashion, but requires random characters to be in the filename. I propose a "bool QFile::openCreateOnly(OpenMode)" that returns false if the file already exists, at which point the user/application can decide what filename to try creating next.
Is there some reason I haven't thought of for why this hasn't already been implemented? 'clever' on IRC mentioned portability issues... but if QTemporaryFile can do it, why not a named QFile?
I looked into implementing this myself (would be cake if I just copy/pasted from QTemporaryFile), but doing it properly (refactoring QTempFile) not so much.