Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve symlinks with boost filesystem

I expect there is a simple way of doing this but I haven't been able to uncover this even with some intense googling.

How can I use boost::filesystem to find the location a symlink ( on *nix systems ) points to?

like image 922
Optimized Life Avatar asked Dec 07 '11 15:12

Optimized Life


2 Answers

If you are using v3 of boost::filesystem you can use the canonical function to get a path with all symlinks resolved. This may work for resolving the path of your symlink.

For example, if sym.link is a symlink:

boost::filesystem::path resolved = boost::filesystem::canonical( 'sym.link' );

I've not actually tried this so I could be wrong, but seems to make sense.

Alternatively, you might have some luck with read_symlink

like image 101
obmarg Avatar answered Oct 12 '22 16:10

obmarg


You can use the read_symlink() function of Version 3:

path read_symlink(const path& p);
path read_symlink(const path& p, system::error_code& ec);

Returns: If p resolves to a symbolic link, a path object containing the contents of that symbolic link. Otherwise an empty path object.

Throws: As specified in Error reporting. [Note: It is an error if p does not resolve to a symbolic link. -- end note]

like image 43
Daniel Trebbien Avatar answered Oct 12 '22 16:10

Daniel Trebbien