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. troubles with QFile::setPermissions
Forum Updated to NodeBB v4.3 + New Features

troubles with QFile::setPermissions

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 788 Views 2 Watching
  • 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.
  • R Offline
    R Offline
    rock37
    wrote on last edited by
    #1

    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 ?

    R C 2 Replies Last reply
    0
    • R rock37

      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 ?

      R Offline
      R Offline
      rock37
      wrote on last edited by
      #2

      @rock37 i forgot to say that it fails at the last QCOMPARE command.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • R rock37

          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 ?

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #4

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

          R 1 Reply Last reply
          2
          • C ChrisW67

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

            R Offline
            R Offline
            rock37
            wrote on last edited by
            #5

            @ChrisW67 hmm, how can i then test this function ?

            R 1 Reply Last reply
            0
            • R rock37

              @ChrisW67 hmm, how can i then test this function ?

              R Offline
              R Offline
              rock37
              wrote on last edited by
              #6

              @rock37 so that it works on all os's...

              JonBJ 1 Reply Last reply
              0
              • R rock37

                @rock37 so that it works on all os's...

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @rock37
                @ChrisW67 is suggesting you try

                QCOMPARE(flags, QFileDevice::ReadOwner | QFileDevice::ReadUser);
                

                And see if that works on all platforms. If it varies you might have to have an OS-specific check, but hopefully it will work.

                R 1 Reply Last reply
                0
                • JonBJ JonB

                  @rock37
                  @ChrisW67 is suggesting you try

                  QCOMPARE(flags, QFileDevice::ReadOwner | QFileDevice::ReadUser);
                  

                  And see if that works on all platforms. If it varies you might have to have an OS-specific check, but hopefully it will work.

                  R Offline
                  R Offline
                  rock37
                  wrote on last edited by
                  #8

                  @JonB it does also fail, i'm atm on the linux platform but it has to work on all platforms.

                  JonBJ 1 Reply Last reply
                  0
                  • R rock37

                    @JonB it does also fail, i'm atm on the linux platform but it has to work on all platforms.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @rock37 So if that's really the case make your test QCOMPARE() flags vary according to platform behaviour for this.

                    C 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @rock37 So if that's really the case make your test QCOMPARE() flags vary according to platform behaviour for this.

                      C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by
                      #10

                      @rock37 If you are only interested if the ReadUser flag, regardless of platform, is set then only test for that. Something like:

                      QVERIFY(flags & QFileDevice::ReadUser);
                      
                      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