Search code examples
qtsymlinkqfileqfileinfo

Change target of symlink without deletion


Is it possible to change contents of symlink using Qt?

Static QFile::link and non-static one both return false if linkname filepath is already exist.

I just want to retarget symlink. An analogue of ln -sf, but crossplatform.


Solution

  • Short answer: No.

    To quote the docs:

    This function will not overwrite an already existing entity in the file system; in this case, link() will return false and set error() to return RenameError.

    The solution is rather straightforward: First remove the link explicitely, e. g. using QFile::remove() on the link, then use QFile::link() for the targeted file (as you already did).

    BTW, ln -f with or without -s is doing nothing else than calling unlink(). This can be seen conveniently in the BusyBox sources (line 128), while in the GNU coreutils it's a bit more obfuscated.