Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. File exists returns true when it doesn't exist!
QtWS25 Last Chance

File exists returns true when it doesn't exist!

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SPlatten
    wrote on 21 Feb 2021, 12:37 last edited by
    #1

    I have written a class which manages the logging for my application, I'm fault finding and trying to make this class bullet proof. Internally it uses QFile to create an instance of the log file which takes the format:

    AppNameYYYYMMDD.SSS.log
    
    AppName : Aapplication name.
    YYYY    : 4 digit Year
    MM      : 2 digit Month
    DD      : Day in month
    SSS     : 3 digit copy number base 1
    

    When the file reaches a specific size, the SSS number is incremented and a new file name and file generated.

    Whilst debugging in Qt Creator and the application running, I can see a file is created, if I delete the file and then monitor how the class handles it I can see as a regular check in the processing thread calls one of my functions:

    bool clsDebugService::blnIsFileOpen(bool& rblnStatus, clsDebugService* pService) {
        if ( pService == nullptr ) {
            pService = clsDebugService::pGetService();
        }
        if ( pService != nullptr ) {
            QMutexLocker lock(&pService->mMutex);
    
            if ( (rblnStatus = pService->mFile.exists()) == true ) {
                rblnStatus = pService->mFile.isOpen();
            }
            return true;
        }
        return false;
    }
    

    mFile is the instance of QFile that was used to create the file,, however I can see that calling exists returns true even though the file doesn't exist and it has been deleted. Why ?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 21 Feb 2021, 14:14 last edited by
      #2

      This is all correct. In contrast to Windows you can delete a file while it is still open and the process which has the open file handle can work on it without any problems.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply 21 Feb 2021, 15:03
      5
      • C Christian Ehrlicher
        21 Feb 2021, 14:14

        This is all correct. In contrast to Windows you can delete a file while it is still open and the process which has the open file handle can work on it without any problems.

        S Offline
        S Offline
        SPlatten
        wrote on 21 Feb 2021, 15:03 last edited by
        #3

        @Christian-Ehrlicher , I would have thought that calling exists would check if the file still exists on the instant it is called?

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 21 Feb 2021, 15:05 last edited by
          #4

          Yes, but it does exist. You just don't see it anymore in the directory and it gets deleted as soon as the file descriptor is closed. Linux basics.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          S 1 Reply Last reply 21 Feb 2021, 15:08
          4
          • C Christian Ehrlicher
            21 Feb 2021, 15:05

            Yes, but it does exist. You just don't see it anymore in the directory and it gets deleted as soon as the file descriptor is closed. Linux basics.

            S Offline
            S Offline
            SPlatten
            wrote on 21 Feb 2021, 15:08 last edited by SPlatten
            #5

            @Christian-Ehrlicher , is that really correct? I'm working on an iMac, if I delete the file I want the application to recognise that its been deleted so I can manage what I want the code to do... Is there a way to achieve this?

            Shouldn't the documentation: https://doc.qt.io/qt-5/qfile.html#exists-1, detail the behaviour you describe?

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 21 Feb 2021, 15:19 last edited by Christian Ehrlicher
              #6

              @SPlatten said in File exists returns true when it doesn't exist!:

              Shouldn't the documentation: https://doc.qt.io/qt-5/qfile.html#exists-1, detail the behaviour you describe?

              Why? It's nothing Qt but OS-specific and well known. MacOS therefore behaves like linux with (I would wonder if this wouldn't be the case though).

              /edit: and it's QFile::exists() also returns the correct value - the file does exist.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply 21 Feb 2021, 15:22
              2
              • C Christian Ehrlicher
                21 Feb 2021, 15:19

                @SPlatten said in File exists returns true when it doesn't exist!:

                Shouldn't the documentation: https://doc.qt.io/qt-5/qfile.html#exists-1, detail the behaviour you describe?

                Why? It's nothing Qt but OS-specific and well known. MacOS therefore behaves like linux with (I would wonder if this wouldn't be the case though).

                /edit: and it's QFile::exists() also returns the correct value - the file does exist.

                S Offline
                S Offline
                SPlatten
                wrote on 21 Feb 2021, 15:22 last edited by
                #7

                @Christian-Ehrlicher , why do you say that? I've deleted the file and emptied the trash.

                Kind Regards,
                Sy

                K C 2 Replies Last reply 21 Feb 2021, 15:33
                -1
                • S SPlatten
                  21 Feb 2021, 15:22

                  @Christian-Ehrlicher , why do you say that? I've deleted the file and emptied the trash.

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 21 Feb 2021, 15:33 last edited by
                  #8

                  @SPlatten said in File exists returns true when it doesn't exist!:

                  why do you say that? I've deleted the file and emptied the trash.

                  I think @Christian-Ehrlicher was clear about this: if you open a file and leave it open, even if you delete the file on the file system, the file stays available on file system but invisible for others.
                  To be more explicit:

                  // this returns true
                  pService->mFile.exists();
                  
                  // this returns false
                  QFile::exists(QFileInfo(pService->mFile).absoluteFilePath());
                  

                  Again, this is not a Qt bug, but the way the file system on Linux/MacOS works.

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  5
                  • S SPlatten
                    21 Feb 2021, 15:22

                    @Christian-Ehrlicher , why do you say that? I've deleted the file and emptied the trash.

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 21 Feb 2021, 15:44 last edited by
                    #9

                    @SPlatten said in File exists returns true when it doesn't exist!:

                    why do you say that? I've deleted the file and emptied the trash.

                    Read my posts, blame the OS. Noone will say you anything else.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1

                    1/9

                    21 Feb 2021, 12:37

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved