troubles with QFile::setPermissions
-
hey folks,
i have the following code to test QFile::setPermissions:
QString fn = QString("%1/%2").arg(_temporary_dir.path(), "test.txt"); QFile n = QFile(fn); QVERIFY(n.open(QFile::ReadWrite)); QVERIFY(QFile::exists(fn)); QVERIFY(QFile::setPermissions(fn, {})); QFile::setPermissions(fn, QFileDevice::ReadUser); QFileDevice::Permissions flags = QFile::permissions(fn); QCOMPARE(flags, QFileDevice::ReadUser);
why does this test fails ?
-
hey folks,
i have the following code to test QFile::setPermissions:
QString fn = QString("%1/%2").arg(_temporary_dir.path(), "test.txt"); QFile n = QFile(fn); QVERIFY(n.open(QFile::ReadWrite)); QVERIFY(QFile::exists(fn)); QVERIFY(QFile::setPermissions(fn, {})); QFile::setPermissions(fn, QFileDevice::ReadUser); QFileDevice::Permissions flags = QFile::permissions(fn); QCOMPARE(flags, QFileDevice::ReadUser);
why does this test fails ?
-
Hi,
Why ? Good question
Do you have proper access to that file ?
Do you have the rights to change the permissions to that file ?
What OS are you on ?
Which version of Qt are you using ?
Did you check from somewhere else that the permissions were changed ?
Did you compare the values of the permissions before and after ?
What filesystem are you on ? -
hey folks,
i have the following code to test QFile::setPermissions:
QString fn = QString("%1/%2").arg(_temporary_dir.path(), "test.txt"); QFile n = QFile(fn); QVERIFY(n.open(QFile::ReadWrite)); QVERIFY(QFile::exists(fn)); QVERIFY(QFile::setPermissions(fn, {})); QFile::setPermissions(fn, QFileDevice::ReadUser); QFileDevice::Permissions flags = QFile::permissions(fn); QCOMPARE(flags, QFileDevice::ReadUser);
why does this test fails ?
@rock37 said in troubles with QFile::setPermissions:
why does this test fails ?
What does the test error message tell you about the flags actually returned?
This is what happens on my machine with a fixed file in /tmp.
********* Start testing of QFileTest ********* Config: Using QtTest library 6.6.2, Qt 6.6.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 10.3.1 20210422 (Red Hat 10.3.1-1)), ubuntu 22.04 PASS : QFileTest::initTestCase() FAIL! : QFileTest::test_case1() Compared values are not the same Actual (flags) : 17408 Expected (QFileDevice::ReadUser): 1024 Loc: [/tmp/test/tst_qfiletest.cpp(33)] PASS : QFileTest::cleanupTestCase() Totals: 2 passed, 1 failed, 0 skipped, 0 blacklisted, 0ms ********* Finished testing of QFileTest ********* Press <RETURN> to close this window...
I tested the permissions on the file after the first setPermission() call are as expected.
I tested that the last setPermissions() call reported success.Regarding the failing test. The actual file:
/tmp$ ls -l /tmp/test.txt -r-------- 1 chrisw chrisw 0 Feb 29 07:05 /tmp/test.txt
and the returned value:
17408 is 0x4400 => QFileDevice::ReadOwner | QFileDevice::ReadUser
Please read the warning in the flags QFileDevice::Permissions docs
If you set ReadUser|WriteUser then you get ReadOwner |WriteOwner|ReadUser|WriteUser back.
-
@rock37 said in troubles with QFile::setPermissions:
why does this test fails ?
What does the test error message tell you about the flags actually returned?
This is what happens on my machine with a fixed file in /tmp.
********* Start testing of QFileTest ********* Config: Using QtTest library 6.6.2, Qt 6.6.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 10.3.1 20210422 (Red Hat 10.3.1-1)), ubuntu 22.04 PASS : QFileTest::initTestCase() FAIL! : QFileTest::test_case1() Compared values are not the same Actual (flags) : 17408 Expected (QFileDevice::ReadUser): 1024 Loc: [/tmp/test/tst_qfiletest.cpp(33)] PASS : QFileTest::cleanupTestCase() Totals: 2 passed, 1 failed, 0 skipped, 0 blacklisted, 0ms ********* Finished testing of QFileTest ********* Press <RETURN> to close this window...
I tested the permissions on the file after the first setPermission() call are as expected.
I tested that the last setPermissions() call reported success.Regarding the failing test. The actual file:
/tmp$ ls -l /tmp/test.txt -r-------- 1 chrisw chrisw 0 Feb 29 07:05 /tmp/test.txt
and the returned value:
17408 is 0x4400 => QFileDevice::ReadOwner | QFileDevice::ReadUser
Please read the warning in the flags QFileDevice::Permissions docs
If you set ReadUser|WriteUser then you get ReadOwner |WriteOwner|ReadUser|WriteUser back.
-
-
@JonB it does also fail, i'm atm on the linux platform but it has to work on all platforms.
-
@rock37 So if that's really the case make your test
QCOMPARE()
flags vary according to platform behaviour for this.