#include <iostream>
#if defined(BOOST_FILESYSTEM_HAS_MKLINK)
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cstdlib>
#include <vector>
namespace fs = boost::filesystem;
struct TmpDir
{
fs::path path;
TmpDir(const fs::path& base) :
path(fs::absolute(base) / fs::unique_path())
{
fs::create_directories(path);
}
~TmpDir()
{
boost::system::error_code ec;
fs::remove_all(path, ec);
}
};
inline fs::path canonicalize_root_path(fs::path const& p)
{
fs::path root_path = p.root_path();
if (root_path.empty())
return p;
root_path = fs::canonical(root_path);
fs::path rel_path = p.relative_path();
if (!rel_path.empty())
root_path.append(rel_path);
return root_path;
}
int main()
{
const fs::path cwd = canonicalize_root_path(fs::current_path());
const TmpDir tmp(cwd);
const fs::path junction = tmp.path / "junction";
const fs::path real = tmp.path / "real";
const fs::path subDir = "sub";
fs::create_directories(real / subDir);
fs::current_path(tmp.path);
BOOST_TEST(std::system("mklink /J junction real") == 0);
BOOST_TEST(fs::exists(junction));
std::vector< fs::path > paths;
paths.push_back(cwd);
paths.push_back(junction);
paths.push_back(real);
paths.push_back(junction / subDir);
paths.push_back(real / subDir);
for (std::vector< fs::path >::iterator it = paths.begin(); it != paths.end(); ++it)
{
std::cout << "Testing in " << *it << std::endl;
fs::current_path(*it);
BOOST_TEST(fs::read_symlink(junction) == real);
BOOST_TEST(fs::canonical(junction) == real);
BOOST_TEST(fs::canonical(junction / subDir) == real / subDir);
}
fs::current_path(cwd);
return boost::report_errors();
}
#else
int main()
{
std::cout << "Skipping test as the target system does not support mklink." << std::endl;
return 0;
}
#endif