[Qt-interest] Symbolic link real value?

Tom Panning lurchvt at gmail.com
Tue Jul 28 15:50:41 CEST 2009


On Tue, Jul 28, 2009 at 9:14 AM, Pau Garcia i
Quiles<pgquiles at elpauer.org> wrote:
> On Tue, Jul 28, 2009 at 2:02 PM, Lukas Lipavsky<llipavsky at suse.cz> wrote:
>> On 28.07. 13:57, Pau Garcia i Quiles wrote:
>>> On Tue, Jul 28, 2009 at 1:52 PM, Lukas Lipavsky<llipavsky at suse.cz> wrote:
>>> > On 28.07. 13:47, Pau Garcia i Quiles wrote:
>>> >> On Tue, Jul 28, 2009 at 1:40 PM, Lukas Lipavsky<llipavsky at suse.cz> wrote:
>>> >> > Hi,
>>> >> >
>>> >> > is there any way to get the path symlink (in linux) is pointing to? But
>>> >> > I need the real value - e.g. '../../dir/file'
>>> >> >
>>> >> > All I found so far was QFileInfo::symLinkTarget() but it returns
>>> >> > *absolute* path, which is useless for me.
>>> >> >
>>> >> > Is there any way to get this information from the Qt library (better
>>> >> > than calling ls)?
>>> >>
>>> >> Use QDir::relativeFilePath with the current directory (QDir::current() )
>>> >>
>>> >> http://doc.trolltech.com/4.5/qdir.html#relativeFilePath
>>> >> http://doc.trolltech.com/4.5/qdir.html#current
>>> >
>>> > thanks,
>>> >
>>> > but unfortunatelly, this won't help me either. To make it more clean, I
>>> > am writing a synchronization app that would synchronize the content of
>>> > two directories (on different hosts). For this I need to be able to
>>> > synchronize the symbolic link to the same state as it is, so if it's
>>> > absolute, keep it absolute, if it's relative, keep it relative...
>>> >
>>> > Is there any way to do this?
>>>
>>> Match the result of QFile::symLinkTarget() against "..":
>>> - It it contains "..", it's a relative symlink and you should then use
>>> QDir::relativeFilePath() and Qdir::current() to get in the destination
>>> machine the same path as in the origin machine.
>>> - It it does not contain "..", it's an absolute symlink and the output
>>> of QFile::symLinkTarget() is what you want.
>>
>> No, it does not:
>> 1. relative path does not need to contain '..'
>
> You are right. Check for "./", ".\", etc too.
>
>> 2. even if it worked, symLinkTarget() never returns path with '..',
>> because it always return absolute path. If it would return relative
>> path, that would be exactly what I need.
>
> You are confusing absolute path with canonical path.From
> http://doc.trolltech.com/4.5/qfileinfo.html#absoluteFilePath

I think both you and Oliver aren't understanding what Lukas is asking.
I'm assuming that it's because you're not very familiar with
Unix-style symbolic links, so I'll give a refresher. In Unix, you can
make a symbolic link either relative or absolute. For instance, if I
want a symbolic link to point to my shell, I can do:
ln -s /bin/bash myshell
This will make a symbolic link ("myshell") that will always point to
/bin/bash. I can move the symbolic link all over the file system, and
it will always point to /bin/bash. Or I can make a relative symbolic
link using:
ln -s ../bin/prog prog
This creates a symbolic link ("prog") in the current directory that
points to prog in the bin directory of the parent directory. If I move
the symlink somewhere else, the symlink will still point to
"../bin/prog".

Both types of links are useful for different things. If you want to
point to something that will never move (e.g., something in /bin or
/lib) you use an absolute link. On the other hand, if you have a
directory subtree (e.g., a C++ project) that has a particular
structure but could be moved around on the system (e.g., put in
different people's home directories) you want to use relative links.

Lukas needs to know what the symlink really points to. An 'ls -l' will
report this. If I had created the two example links from above in my
home directory, 'ls -l' would report
myshell -> /bin/bash
prog -> ../bin/prog
Notice how one is absolute and one is relative. If you use Qt's
QFileInfo::symLinkTarget(), it would report
myshell.symLinkTarget(): /bin/bash
prog.symLinkTarget(): /home/bin/prog

Lukas, as far as I can tell, Qt won't tell you if the symlink is
relative or absolute. But maybe someone will prove me wrong,
Tom



More information about the Qt-interest-old mailing list