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. QFile doesn't detect read-only files

QFile doesn't detect read-only files

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 1.7k Views
  • 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.
  • M mchinand

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

    S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #5

    Hi @mchinand, the return value of copying is true, and I can remove the file, even when it's unwritable (with QFile::remove()).

    JonBJ 1 Reply Last reply
    0
    • S Sucharek

      Hi @mchinand, the return value of copying is true, and I can remove the file, even when it's unwritable (with QFile::remove()).

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

      @Sucharek
      You claim QFile::exists() returns false. What does QFileInfo have to say about the file?

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #7

        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;
        }
        

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #8

          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

          M 1 Reply Last reply
          1
          • M mpergand

            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

            M Offline
            M Offline
            mchinand
            wrote on last edited by
            #9

            The documentation says QIODevice::isWritable() checks the OpenMode, so seems like your finding isn't specific to Mac and QIODevice::isWritable() is only useful for open files. QFileInfo::isWritable() is what is needed here.

            M 1 Reply Last reply
            2
            • M mchinand

              The documentation says QIODevice::isWritable() checks the OpenMode, so seems like your finding isn't specific to Mac and QIODevice::isWritable() is only useful for open files. QFileInfo::isWritable() is what is needed here.

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #10

              @mchinand said in QFile doesn't detect read-only files:
              so seems like your finding isn't specific to Mac

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

              1 Reply Last reply
              2
              • S Offline
                S Offline
                Sucharek
                wrote on last edited by Sucharek
                #11

                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.

                M JonBJ 2 Replies Last reply
                0
                • S Sucharek

                  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.

                  M Offline
                  M Offline
                  mchinand
                  wrote on last edited by
                  #12

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

                  S 1 Reply Last reply
                  1
                  • M mchinand

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

                    S Offline
                    S Offline
                    Sucharek
                    wrote on last edited by
                    #13

                    Hi @mchinand, thank you for your answer.
                    I guess that explains my issue.
                    Thanks

                    1 Reply Last reply
                    0
                    • S Sucharek

                      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.

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

                      @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? The QFile 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 say QFile::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 just QFileInfo::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.

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved