Qt World Summit: Register Today!

Get size of symbolic links (symlinks)

  • QFileInfo documentation states that

    On Unix (including Mac OS X), the symlink has the same size() has the file it points to, because Unix handles symlinks transparently; similarly, opening a symlink using QFile effectively opens the link's target.

    Problem is, there is no real option to disable this behavior, which is needed in some (I would say) quite common file operations such as copying a full directory tree (preserving symlinks), or calculating its size. This is achieved by native tools by offering a specific toggle, which usually comes disabled by default (eg. the "cp" command accepts the "--preserve=links" switch, allowing to effectively copy a symlink as-is, even if it points to a non-existing file).

    Given a folder with 1 MB file and 99 symlinks to it, I have not found the way of obtaining the real size of the folder by using Qt file handling possibilities. "100MB" is not the correct answer.

    Is there anything obvious that I'm missing?

  • Moderators

    QFileInfo::isSymLink() is not working for you? Also when you list a directory you have the option of filtering symbolic links out.

    Kind regards.

  • Hi @kshegunov
    I'm not sure what do you mean. QFileInfo::isSymLink() works fine, but it is totally unrelated to this issue. The issue is that if you perform QFile::size() of a symlink, it will always return the size of the file targeted by the link, not the size of the symbolic link itself (which is a handful of bytes, but not neccesarily always the same amount).

    My point is that these functions should maybe have a boolean parameter which would allow to operate on the symlinks themselves, and not on the target files. QFile::copy() suffers from the same limitation (how do you copy a relative symlink, as-is?).

    It's that, or I'm just missing something obvious, but as of today I haven't yet found it...

  • For anyone interested on this issue, there is now an open bug report for it:


Log in to reply