[Qt-interest] Symbolic link real value?

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Tue Jul 28 14:05:57 CEST 2009


Lukas Lipavsky wrote on Tuesday, July 28, 2009 1:40 PM:

> 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.

Absolute paths are usually more useful, because it is clear where they point to - so yes, the Qt API returns absolute paths.

Relative paths on the other hand are only meaningful for a specific application (e.g. "relative to the current working directory", "relative to the install location", "relative to any other application-specific directory", ...) - so there is not much use for a general purpose library such as Qt to return "relative paths".

What *would* be useful though would be a method like "Given these two paths A and B, return me their relative path to each other, such that this relative path R would lead from A to B".

Example 1

A: c:\foo\bar\
B: c:\foo\bar\barfoo
R: .\barfoo

A: c:\foo\bar\
B: c:\foo\hoo\barfoo\
R: ..\hoo\barfoo\

A: c:\foo\bar\
B: d:\bar\foo\ (different drive!)
R: d:\bar\foo\ (absolute path!)

and so on (the fun starts when you are also dealing with UNC style paths, such as \\server\foo\bar, and paths pointing to files instead of directories etc.)

I am not aware of such a functionality within Qt (if there is, please tell me :) - back then I ended up writing my own method (can't remember whether it also handled UNC paths), inspired by some codeproject.org exampe (which I can't find anymore right now), but it was similar to

  http://mrpmorris.blogspot.com/2007/05/convert-absolute-path-to-relative-path.html

(C#, but should be easy to convert to C++ - in fact, it should be almost as easy as "copy & paste, with slight modifications" ) ;)

Just now I also discovered that there is a Windows API function "PathRelativePathTo" ;) (But my method works also on Unix ;)


Summary:

- Call the Qt API QFileInfo::symLinkTarget()
- Convert the resulting absolute path to a relative path as shown above, with the
  "reference" path of your choice (e.g. the absolute path of your application)

Cheers, Oliver




More information about the Qt-interest-old mailing list