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. Does QFileInfo have special handling for .lnk files on Windows? Can it be disabled?
Forum Updated to NodeBB v4.3 + New Features

Does QFileInfo have special handling for .lnk files on Windows? Can it be disabled?

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 6 Posters 3.1k Views 4 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.
  • V Violet Giraffe

    Tested on Qt 5.9.6. QFileInfo("textfile.lnk").exists() also returns false for an existing file that's not actually a Windows link.

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #9

    @Violet-Giraffe
    Docs says
    Returns true if the file exists; otherwise returns false.
    Note: If the file is a symlink that points to a non-existing file, false is returned.

    so it dont seem to be working as advertised.
    Note
    you did use

    QFileInfo("c:\\fullpath\\textfile.lnk").exists() ?
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      Violet Giraffe
      wrote on last edited by Violet Giraffe
      #10

      Right, I missed that bit about exists(), this is the intended behavior.
      So, is there some way to turn off this special handling of links? Some secret attribute, perhaps? It really messes up my program.

      mrjjM 1 Reply Last reply
      0
      • V Violet Giraffe

        Right, I missed that bit about exists(), this is the intended behavior.
        So, is there some way to turn off this special handling of links? Some secret attribute, perhaps? It really messes up my program.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #11

        @Violet-Giraffe
        Hi
        Browsing the source
        https://code.woboq.org/qt5/qtbase/src/corelib/io/qfileinfo.cpp.html

        I dont see any options to allow that.
        Also for real .lnk files, its handled at lower level than Qt by windows.

        V 1 Reply Last reply
        2
        • V Offline
          V Offline
          Violet Giraffe
          wrote on last edited by Violet Giraffe
          #12

          Thanks.
          Are you saying that there's no way to query real info for .lnk files on Windows? I don't quite believe that, Explorer and Total Commander display them properly, with proper size and all. Both valid and invalid links.

          mrjjM 1 Reply Last reply
          0
          • V Violet Giraffe

            Thanks.
            Are you saying that there's no way to query real info for .lnk files on Windows? I don't quite believe that, Explorer and Total Commander display them properly, with proper size and all. Both valid and invalid links.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #13

            @Violet-Giraffe
            Nope, im saying its odd that even running the sample from QFileInfo docs
            does not give the results the sample comments suggests. At least not for me.
            Explorer is using native api calls which surely works.
            I dont what Total Commander uses but most likely not Qt.

            1 Reply Last reply
            0
            • mrjjM mrjj

              @Violet-Giraffe
              Hi
              Browsing the source
              https://code.woboq.org/qt5/qtbase/src/corelib/io/qfileinfo.cpp.html

              I dont see any options to allow that.
              Also for real .lnk files, its handled at lower level than Qt by windows.

              V Offline
              V Offline
              Violet Giraffe
              wrote on last edited by
              #14

              @mrjj said in Does QFileInfo have special handling for .lnk files on Windows? Can it be disabled?:

              Also for real .lnk files, its handled at lower level than Qt by windows.

              I was referring to this part of your reply, which I probably misunderstood. Never mind that.

              mrjjM 1 Reply Last reply
              0
              • V Violet Giraffe

                @mrjj said in Does QFileInfo have special handling for .lnk files on Windows? Can it be disabled?:

                Also for real .lnk files, its handled at lower level than Qt by windows.

                I was referring to this part of your reply, which I probably misunderstood. Never mind that.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #15

                @Violet-Giraffe
                Oh, ok.
                well Windows resolves those links on file access to make it transparent to all application.
                So its very tied to the OS.

                D 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Violet-Giraffe
                  Oh, ok.
                  well Windows resolves those links on file access to make it transparent to all application.
                  So its very tied to the OS.

                  D Offline
                  D Offline
                  deleted426
                  wrote on last edited by deleted426
                  #16

                  @mrjj Hi,
                  I'm stuck here and the implementation of QFileInfo over lnk files is really broken : Violet-Giraffe was talking about a real problem. What's inside the file should not be a Qt concern, lnk files are not symlinks, they are binary equivalent of the .desktop files on Linux, they are not filesystem levels : they are just files that can be placed on any filesystem and edited with any file editing software.

                  Details and context:
                  I'm doing file listing databases with names, checksums and dateTime of every file into some folder.

                  When running the Linux build of the program on some folder (into an USB drive), the lnk files are handled correctly (as normal files), no matter what is the meaning of what's inside : name, checksum, size, and dateTime are correctly handled. I just had to say QDirIterator "QDir::Hidden" to enable file/folder names beginning with a dot.

                  On the Windows build of the same program, running on the same folder on the same USB Disk, I had to enable "QDir::System" to have QDirIterator able to see *.lnk files.
                  Then QFile is able to correctly do the open, read, and checksum of what's inside the file, but QFileInfo give me "0" size and QDateTime from "lastModified()" function is invalid (and has no reason to be : according to here https://doc.qt.io/qt-5/qfileinfo.html QFileInfo on the lnk file is supposed to give information about the lnk file, not about its target).

                  Well, just to say : QFileInfo lnk special handling and interpretation for .lnk file is malfunctioning, as if the content of the file is not talking about a still existing target, QFileInfo functions that are supposed to only focus on the .lnk file itself (date and size at least) are broken and give wrong results.

                  Hope this testimony will help to solve it for future Qt versions !
                  Best regards

                  EDIT : just for information, if I rename the file, Qt stops messing up with the file. But as I'm writing a client application for file versioning and backup, I'm not going to change any file name : for now, I will have to ignore *.lnk files to be sure that no permanent warning / failure message about those files are displayed on the client's screen because of Qt inaptitude to handle them correctly.
                  QFile::size() also fails (while QFile::open() / read() is working... strange !).

                  thamT 1 Reply Last reply
                  3
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #17

                    Hi
                    Just for completeness.
                    What Qt version did you test with?

                    D 1 Reply Last reply
                    0
                    • D deleted426

                      @mrjj Hi,
                      I'm stuck here and the implementation of QFileInfo over lnk files is really broken : Violet-Giraffe was talking about a real problem. What's inside the file should not be a Qt concern, lnk files are not symlinks, they are binary equivalent of the .desktop files on Linux, they are not filesystem levels : they are just files that can be placed on any filesystem and edited with any file editing software.

                      Details and context:
                      I'm doing file listing databases with names, checksums and dateTime of every file into some folder.

                      When running the Linux build of the program on some folder (into an USB drive), the lnk files are handled correctly (as normal files), no matter what is the meaning of what's inside : name, checksum, size, and dateTime are correctly handled. I just had to say QDirIterator "QDir::Hidden" to enable file/folder names beginning with a dot.

                      On the Windows build of the same program, running on the same folder on the same USB Disk, I had to enable "QDir::System" to have QDirIterator able to see *.lnk files.
                      Then QFile is able to correctly do the open, read, and checksum of what's inside the file, but QFileInfo give me "0" size and QDateTime from "lastModified()" function is invalid (and has no reason to be : according to here https://doc.qt.io/qt-5/qfileinfo.html QFileInfo on the lnk file is supposed to give information about the lnk file, not about its target).

                      Well, just to say : QFileInfo lnk special handling and interpretation for .lnk file is malfunctioning, as if the content of the file is not talking about a still existing target, QFileInfo functions that are supposed to only focus on the .lnk file itself (date and size at least) are broken and give wrong results.

                      Hope this testimony will help to solve it for future Qt versions !
                      Best regards

                      EDIT : just for information, if I rename the file, Qt stops messing up with the file. But as I'm writing a client application for file versioning and backup, I'm not going to change any file name : for now, I will have to ignore *.lnk files to be sure that no permanent warning / failure message about those files are displayed on the client's screen because of Qt inaptitude to handle them correctly.
                      QFile::size() also fails (while QFile::open() / read() is working... strange !).

                      thamT Offline
                      thamT Offline
                      tham
                      wrote on last edited by
                      #18

                      @jrflop If rename the file work, maybe you can create a temporary file by QTemporaryFile, read that temp file by QFileInfo and then let the QTemporaryFile delete the temp file for you?

                      1 Reply Last reply
                      0
                      • mrjjM mrjj

                        Hi
                        Just for completeness.
                        What Qt version did you test with?

                        D Offline
                        D Offline
                        deleted426
                        wrote on last edited by deleted426
                        #19

                        @mrjj Hi, it's Qt 5.13.0 (for Windows and Linux)
                        I found the problem is discussed here : https://stackoverflow.com/questions/53411886/qfileinfo-size-is-returning-shortcut-target-size
                        I'm in the case when the file size is returning zero.

                        As a working workaround, Instead of using Qt functions for collecting files information, I'm using :

                        CONFIG += c++17
                        LIBS += -l"stdc++fs"
                        
                        #include <filesystem>
                        record.size = std::filesystem::file_size(record.path.toStdString());
                        std::filesystem::file_time_type time = std::filesystem::last_write_time(record.path.toStdString()); //eeeww... !
                        

                        I will now try to convert std::filesystem::file_time_type time to QDateTime (as I'm using Qt for SQLite databases (disk and ram ones) to have faster operations of file database search / comparison). I will have to care about timezone so every parameter of the QDateTime will be correctly set.

                        Those std functions are pretty new to c++ so it works with msvc2017_32 and msvc2017_64 but on Windows, when using mingw73_64, #include <filesystem> isn't even recognized :/
                        On Linux I had to add LIBS += -l"stdc++fs" because of link errors like : undefined reference to `std::filesystem::file_size(std::filesystem::__cxx11::path const&)'

                        Well I have to say I love using Qt for all the code so that it easier to read (and to setup) : I'm happy to find working std::filesystem functions but let's face it : std functions are ugly, I mean for readability and typing by memory without looking on the Internet at each line, it really is a carnage :

                        • QDateTime dateTime = fileInfoFinder.lastModified();
                          vs
                        • std::filesystem::file_time_type time = std::filesystem::last_write_time(record.path.toStdString());

                        Those who already used std::high resolution clock to measure milliseconds or microseconds probably know how much Qt is easier to write, remember and read.

                        At side of this little inaptitude with *.lnk files information, thanks anyway for the great work.

                        Best regards,
                        A convinced Qt/C++ developer :)

                        1 Reply Last reply
                        2
                        • J Offline
                          J Offline
                          Jiwan
                          wrote on last edited by
                          #20

                          QDateTime dateTime = fileInfoFinder.lastModified();
                          vs
                          std::filesystem::file_time_type time = std::filesystem::last_write_time(record.path.toStdString());

                          It's only longer because you're making it longer :)
                          First, you can always omit the namespace by doing "using namespace std::filesystem;", or just alias it to something short, such as "namespace fs = std::filesystem;"
                          Second, you're writing out the variable types which is unnecessary; just use auto.
                          Third, you're only having the .toStdString() because you're mixing Qt and std; last_write_time() can take a filesystem::path directly.
                          So now we have:

                          auto dateTime = fileInfoFinder.lastModified();
                          vs
                          auto dateTime = last_write_time(myFilePath);

                          (Or more likely with the std approach, one would be using the cached fs::status or fs::directory_entry types rather than paths since checking multiple properties don't incur additional internal system calls: "auto dateTime = myFileItem.last_write_time();" );

                          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