QFile permission not reporting correctly
-
I have created file /tmp/abc for testing with QFile, and set file permissions to 000 from the command line. I noticed that I can correctly set permissions using QFile.setPermissions method (verified from command line).
However, when I report the file permissions with the QFile.permissions method they report incorrectly. I'm running on Linux (FC34). If I step through the code below I can watch the permissions change (verified from shell "ls -la" command), yet my code always reports the same file permissions as shown below.
Why are file permissions always reporting incorrectly?
QFile myFile("/tmp/abc"); QFileDevice::Permissions perm; qDebug() << "QFile permissions: " << myFile.permissions(); if (myFile.setPermissions(perm)) qDebug() << "QFile setPermissions successful"; else qDebug() << "QFile setPermissions failed"; qDebug() << "QFile permissions: " << myFile.permissions(); perm.setFlag(QFileDevice::ExeOther); qDebug() << "perm is set to: " << perm; qDebug() << "QFile setPermissions attempted"; if (myFile.setPermissions(perm)) qDebug() << "QFile setPermissions successful"; else qDebug() << "QFile setPermissions failed"; qDebug() << "QFile permissions: " << myFile.permissions();
and the output:
QFile permissions: QFlags(0x200|0x400|0x2000|0x4000) QFile setPermissions successful QFile permissions: QFlags(0x200|0x400|0x2000|0x4000) perm is set to: QFlags(0x1) QFile setPermissions successful QFile permissions: QFlags(0x200|0x400|0x2000|0x4000)
-
Oh my, this should have been fixed, not closed :o
-
Try an independent check (not through existing
myFile
object), like so:qDebug() << "QFile permissions: " << QFileInfo("/tmp/abc").permissions();
-
Hi,
I am wondering if there's not some cache that is used to store that information and your are accessing it again after the permission changed.
-
@ocgltd said in QFile permission not reporting correctly:
Or are you saying Qt might have a cache?
It might do, to speed repeated calls to the same file. The strange thing is it would not be surprising if you changing it externally via
chmod
did not make it update a cache it might have. But you would have thought that withmyFile.setPermissions(perm)
it would know to update anymyFile.permissions()
cached value correspondingly. -
@JonB Yeah, if I changed via command line and Qt didn't see the change then I would suspect a cache. But if writing permissions from Qt I would expect it to update any Qt cache. I would have guess bug, but both QFile and QFileInfo suffering from the same bug makes me think I'm doing something wrong
-
Please report this on Qt bug tracker.
-
@ocgltd , @sierdzio
I checked this on my Ubuntu 20.04, Qt5.15, and it does indeed behave as you say. So then I looked and found bug QFile::permissions() returns old, cached value after call to QFile::setPermissions() on the same QFile object, Qt 4.6, Unresolved:I can understand why QFile would not detect changes to the file's permissions outside of the QFile object, although if the permissions
change is done via QFile::setPermissions() then the object can clear its cached value automatically.
Alternatively, the fact that permissions() returns a cached value should be documented.A related issue is that there is no explicit refresh() method for QFile as there is for QFileInfo, although QFile::exists() does trigger a refresh when called
This task is old and has been idle for a long time. It is therefore being closed on the assumption that it will not be addressed in the foreseeable future. If you really do care about this task, you may reopen it and vote for it.
It was created in 2010 and last updated in 2016. We agree it's a bit bad. You will have to use some alternative, e.g. maybe the
static QFile
permissions methods work, or theQFileInfo
ones. -
Oh my, this should have been fixed, not closed :o
-
-
Thanks! Voted.
-
There is also already a patch for it... sometimes it's interesting to see how long bugs linger around and then a small change (here: chooinsg the correct maintainer) suddenly fixes it :)
-
I guess you can just use the static function!?