How to copy a symlink file on Mac?
-
wrote on 30 Jan 2012, 21:16 last edited by
We have to copy symbolic link files on Mac OS, but the QFile::copy fails.
We've tried to get the symLinkTarget() as well but it returns absolute target path and we have to support relative symlink targets as well.
Can anyone suggest a solution for copying symlinks as files?
-
wrote on 30 Jan 2012, 23:10 last edited by
If you want to copy only the symbolic link only try the following idea:
Check if selected files is a symlink using "QFileInfo::isSymLink":http://developer.qt.nokia.com/doc/qt-4.8/qfileinfo.html#isSymLink
If it is a symlink get absolute path to the file that the links point to using "QFileInfo::symLinkTarget ()":http://developer.qt.nokia.com/doc/qt-4.8/qfileinfo.html#symLinkTarget
Create a copy of the link using "QFile::link":http://developer.qt.nokia.com/doc/qt-4.8/qfile.html#link
-
wrote on 30 Jan 2012, 23:53 last edited by
Thanks leon, we tried it, but QFileInfo::symLinkTarget() returns the absolute path of the target and we have to support relative pathes as well as absolute ones.
-
wrote on 31 Jan 2012, 08:20 last edited by
[quote author="viktor.benei" date="1327967601"]Thanks leon, we tried it, but QFileInfo::symLinkTarget() returns the absolute path of the target and we have to support relative pathes as well as absolute ones.[/quote]
My idea was to create a new sym link using a relative path as an argument of QFile::link ( const QString & linkName ) at step 3.
-
wrote on 31 Jan 2012, 09:29 last edited by
leon: thanks. We tried that one as well. The problem: you cannot get the stored target of a symlink file in Qt. It returns absolute path every time, even if the symlink file points relative.
We have to copy a whole folder-tree. So if we 'copy' symlinks the way you suggested, the symlinks will point either to the original files (if we use symlink-target as absolute path) or relatively, which can mess up absolute symlink pathes.
We use this copying to make backup of applications, folders, etc. before operating on them, so we have to support both absolute and relative pathes.
-
wrote on 31 Jan 2012, 09:38 last edited by
Currently this is what we do (it works, but it's platform dependent):
- on Windows we simply QFile::copy the shortcuts and symlinks (only junk-link seems to be an issue when we try to delete them, because just-links are readonly)
- on Mac we get the symlink's target with Carbon (it returns the actual target: if it's a relative path then a relative one, if points to an absolute path it returns an absolute one) and use it as target for QFile::link()
** there's only 1 exception: aliases, but we don't have to support Mac aliases currently (aliases are detected as symlinks in Qt, but you cannot get an alias' target with Qt nor with the Carbon solution we use)
Why can't we get symlink-targets as they are in Qt? Why is it an absolute path in every case? It makes this type of copying really hard...
-
wrote on 31 Jan 2012, 11:56 last edited by
On the Mac, you could try to use "/bin/cp" in a QProcess instead of QFile::copy() to achieve this. It preserves the symlinks.
5/7