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

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

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • None
    • 5.5.0 Alpha
    • Core: I/O
    • 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.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            d3fault d3fault
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes