File exists returns true when it doesn't exist!
-
wrote on 21 Feb 2021, 12:37 last edited by
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 ?
-
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.
-
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.
wrote on 21 Feb 2021, 15:03 last edited by@Christian-Ehrlicher , I would have thought that calling exists would check if the file still exists on the instant it is called?
-
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.
-
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.
wrote on 21 Feb 2021, 15:08 last edited by SPlatten@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?
-
Lifetime Qt Championwrote on 21 Feb 2021, 15:19 last edited by 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.
-
@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.
wrote on 21 Feb 2021, 15:22 last edited by@Christian-Ehrlicher , why do you say that? I've deleted the file and emptied the trash.
-
@Christian-Ehrlicher , why do you say that? I've deleted the file and emptied the trash.
wrote on 21 Feb 2021, 15:33 last edited by@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.
-
@Christian-Ehrlicher , why do you say that? I've deleted the file and emptied the trash.
@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.
1/9