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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

      SPlattenS 1 Reply Last reply
      5
      • Christian EhrlicherC Christian Ehrlicher

        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.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on 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
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

          SPlattenS 1 Reply Last reply
          4
          • Christian EhrlicherC Christian Ehrlicher

            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.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on 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
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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

              SPlattenS 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                @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.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #7

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

                Kind Regards,
                Sy

                KroMignonK Christian EhrlicherC 2 Replies Last reply
                -1
                • SPlattenS SPlatten

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

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on 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
                  • SPlattenS SPlatten

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

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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

                    • Login

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