Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-52244

Race condition between (!QFile::exists) and QFile::open(WriteOnly)

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • Not Evaluated
    • 5.11.0 Alpha
    • None
    • Core: I/O
    • None
    • 1a4cc8d57b928509a64f9679e5c0e7afaa05cb54

    Description

      I wasn't sure whether to call this a suggestion or a bug. Since it involves data loss, I think it's a bug. However, Thiago disagrees and says it's a suggestion.

      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.

      Attachments

        For Gerrit Dashboard: QTBUG-52244
        # Subject Branch Project Status CR V

        Activity

          People

            d3fault d3fault
            d3fault d3fault
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes