QFile doesn't detect read-only files
-
Hello, I've came across a problem. I'm making a sorting system, and I'm checking if the file is read-only, so I can check, if the file got moved, or not, but it doesn't work.
I try to detect if the file exists (which it does), but QFile doesn't detect it.
Here's my code for it:qDebug() << "file:" << filename; QFile move(mainPath + filename); bool system = move.isWritable(); if (system == false) { move.copy(mainPath + rPath + filename); QFile check(mainPath + rPath + filename); //path of the moved file if (check.exists()) { //here it detects, always false even if it does exist move.remove(); } else { qDebug() << mainPath + rPath + filename; qDebug() << check.exists(); QMessageBox error; error.setWindowTitle("Error!"); error.setText("Warning! There was an unexpected error!\n" "File has not been manipulated!\n" "File path: " + mainPath + filename); error.exec(); } } else { move.rename(mainPath + filename, mainPath + rPath + filename); qDebug() << "from: " + mainPath + filename; qDebug() << "to: " + mainPath + rPath + filename; }
-
@Sucharek You may be having issues because your file is a .lnk file (shortcut) which I think is treated as a symbolic link, so keep that in mind when you are reading the documentation on various file manipulation methods. For copy(), the documentation says it copies the file that the .lnk file is referring to, not the .lnk file itself.
-
What OS, Qt version and filesystem type do you use?
-
Hi @Christian-Ehrlicher, I'm on windows, my Qt version is 5.15.2 and my filesystem is NTFS
-
@Sucharek said in QFile doesn't detect read-only files:
bool system = move.isWritable(); if (system == false) { move.copy(mainPath + rPath + filename);
You might want to check the return value on your copy command
QFile check(mainPath + rPath + filename); //path of the moved file if (check.exists()) { //here it detects, always false even if it does exist move.remove();
This might be filesystem dependent, but I don't think you will be able to remove a file that you don't have write-access to (this is within the if-block when isWritable() is false).
-
This is working fine for me on a NTFS file system with Qt5.15.2 (assuming a read-only file named 'tmp1.txt' is in D:\
int main(int argc, char** argv) { QCoreApplication app(argc, argv); QString filename = "tmp1.txt"; QString mainPath = "D:/"; QString rPath = "subdir/"; if (!QDir().mkpath(mainPath + rPath)) { qDebug() << "Could not create path " << mainPath + rPath; } QFile move(mainPath + filename); bool system = move.isWritable(); if (system == false) { move.copy(mainPath + rPath + filename); QFile check(mainPath + rPath + filename); //path of the moved file if (check.exists()) { qDebug() << "Copied file at " << check.fileName() << "exists - removing"; move.remove(); } else { qDebug() << "Copied file at " << check.fileName() << "does not exist"; qDebug() << mainPath + rPath + filename; qDebug() << check.exists(); } } else { qDebug() << "File is writeable"; } return 0; }
-
I don't know on Windows but on Mac since the file is not open for writing, isWritable() return false
QFile file(path); QTLog(<<"is writable"<<file.isWritable()) if(file.open(QIODevice::WriteOnly)) { QTLog(<<"is writable"<<file.isWritable()) ...
is writable false
is writable true -
The documentation says
QIODevice::isWritable()
checks theOpenMode
, so seems like your finding isn't specific to Mac andQIODevice::isWritable()
is only useful for open files. QFileInfo::isWritable() is what is needed here. -
@mchinand said in QFile doesn't detect read-only files:
so seems like your finding isn't specific to MacYes I know ;)
All the topic makes no sense because there is a confusion between the open state of a file (read/write) and the permissions for a file. -
Hi, thanks for all the replies, but none of them seem to work.
@JonB QFileInfo returned false.
@Christian-Ehrlicher your method doesn't work for me. Prints:
Copied file at "C:/Users/username/Downloads/Other/Spotify.lnk" does not exist
But it actually does exist. -
@Sucharek You may be having issues because your file is a .lnk file (shortcut) which I think is treated as a symbolic link, so keep that in mind when you are reading the documentation on various file manipulation methods. For copy(), the documentation says it copies the file that the .lnk file is referring to, not the .lnk file itself.
-
@Sucharek said in QFile doesn't detect read-only files:
Copied file at "C:/Users/username/Downloads/Other/Spotify.lnk" does not exist
This is a
.lnk
file. You never mentioned this, even though it behaves differently from all other file types under Windows. Did you not think that relevant? TheQFile
etc. documentation tells you how it handles.lnk
files --- effectively, it always tells you about the file pointed to by the.lnk
file, not the.lnk
file itself. So, for example, when you sayQFile::exists()
reports it as not existing, it is telling you about the file the.lnk
points to. Did that exist?@JonB QFileInfo returned false
You were supposed to look through the various
QFileInfo::is...()
methods, not justQFileInfo::exists()
, to see what the attributes of the file are. For instance, QFileInfo::isShortcut() under Windows would have told you/us what is relevant here.