#include #include using boost::filesystem::path; using std::cout; using std::endl; using std::ofstream; int main(int argc, char** argv) { // Creates a directory symlink-example/dir containing a file hello.txt path tmp = boost::filesystem::temp_directory_path() / "symlink-example"; boost::filesystem::remove_all(tmp); path myDir = tmp / "dir"; boost::filesystem::create_directories(myDir); path myFile = myDir / "hello.txt"; { ofstream filestream(myFile.string().c_str(), ofstream::out); filestream << "hello, world!" << endl; } // Creates a directory symlink symlink-example/sym -> symlink-example/dir path mySym = tmp / "sym"; boost::filesystem::create_directory_symlink(myDir, mySym); // when cwd is a directory, c: is not considered a symlink and canonical works boost::filesystem::current_path(myDir); cout << "c: is symlink? " << boost::filesystem::is_symlink(path("c:")) << endl; // outputs 0 boost::filesystem::canonical(myFile); // when cwd is the symlink to the directory, "c:" is considered a symlink to dir! boost::filesystem::current_path(mySym); cout << "c: is symlink? " << boost::filesystem::is_symlink(path("c:")) << endl; // outputs 1 /* Canonical will enter an infinite loop. By way of example, assume myDir = c:\temp\symlink-example\dir mySym = c:\temp\symlink-example\sym -> myDir myFile = c:\temp\symlink-example\dir\hello.txt It iterates over the path components of myFile (operations.cpp:827), starting with c: c: is considered a symlink (operations.cpp:844) with target myDir, so it expands it to c:\temp\symlink-example\dir\temp\symlink-example\dir\hello.txt scan is set true and the loop repeats, resulting in c:\temp\symlink-example\dir\temp\symlink-example\dir\temp\symlink-example\dir\hello.txt and so on in an infinite loop resulting in an infinitely expanding path. */ boost::filesystem::canonical(myFile); }