1 | #include <iostream>
|
---|
2 | #include <boost/filesystem.hpp>
|
---|
3 |
|
---|
4 | using boost::filesystem::path;
|
---|
5 | using std::cout;
|
---|
6 | using std::endl;
|
---|
7 | using std::ofstream;
|
---|
8 |
|
---|
9 |
|
---|
10 | int main(int argc, char** argv) {
|
---|
11 | // Creates a directory symlink-example/dir containing a file hello.txt
|
---|
12 | path tmp = boost::filesystem::temp_directory_path() / "symlink-example";
|
---|
13 | boost::filesystem::remove_all(tmp);
|
---|
14 | path myDir = tmp / "dir";
|
---|
15 | boost::filesystem::create_directories(myDir);
|
---|
16 | path myFile = myDir / "hello.txt";
|
---|
17 | {
|
---|
18 | ofstream filestream(myFile.string().c_str(), ofstream::out);
|
---|
19 | filestream << "hello, world!" << endl;
|
---|
20 | }
|
---|
21 |
|
---|
22 | // Creates a directory symlink symlink-example/sym -> symlink-example/dir
|
---|
23 | path mySym = tmp / "sym";
|
---|
24 | boost::filesystem::create_directory_symlink(myDir, mySym);
|
---|
25 |
|
---|
26 | // when cwd is a directory, c: is not considered a symlink and canonical works
|
---|
27 | boost::filesystem::current_path(myDir);
|
---|
28 | cout << "c: is symlink? " << boost::filesystem::is_symlink(path("c:")) << endl; // outputs 0
|
---|
29 | boost::filesystem::canonical(myFile);
|
---|
30 |
|
---|
31 | // when cwd is the symlink to the directory, "c:" is considered a symlink to dir!
|
---|
32 | boost::filesystem::current_path(mySym);
|
---|
33 | cout << "c: is symlink? " << boost::filesystem::is_symlink(path("c:")) << endl; // outputs 1
|
---|
34 |
|
---|
35 | /*
|
---|
36 | Canonical will enter an infinite loop. By way of example, assume
|
---|
37 | myDir = c:\temp\symlink-example\dir
|
---|
38 | mySym = c:\temp\symlink-example\sym -> myDir
|
---|
39 | myFile = c:\temp\symlink-example\dir\hello.txt
|
---|
40 | It iterates over the path components of myFile (operations.cpp:827), starting with c:
|
---|
41 | c: is considered a symlink (operations.cpp:844) with target myDir, so it expands it to
|
---|
42 | c:\temp\symlink-example\dir\temp\symlink-example\dir\hello.txt
|
---|
43 | scan is set true and the loop repeats, resulting in
|
---|
44 | c:\temp\symlink-example\dir\temp\symlink-example\dir\temp\symlink-example\dir\hello.txt
|
---|
45 | and so on in an infinite loop resulting in an infinitely expanding path.
|
---|
46 | */
|
---|
47 | boost::filesystem::canonical(myFile);
|
---|
48 | }
|
---|