Created
November 10, 2016 00:27
-
-
Save Porges/916840778d9368fc91906b9b16c7d2b6 to your computer and use it in GitHub Desktop.
hardlink a directory recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <exception> | |
#include <filesystem> | |
#include <iostream> | |
#include <string> | |
#include <stack> | |
void hardlink_dir( | |
const std::experimental::filesystem::path& fromRoot, | |
const std::experimental::filesystem::path& toRoot) | |
{ | |
using namespace std::experimental::filesystem; | |
std::stack<path> dirsToVisit{ { path{} } }; | |
while (!dirsToVisit.empty()) | |
{ | |
const auto subDir = std::move(dirsToVisit.top()); | |
dirsToVisit.pop(); | |
const auto fromDir = fromRoot / subDir; | |
const auto toDir = toRoot / subDir; | |
create_directory(toDir); | |
for (const directory_entry& fromDirEntry : directory_iterator(fromDir)) | |
{ | |
const auto fromFilename = fromDirEntry.path().filename(); | |
switch (fromDirEntry.status().type()) | |
{ | |
case file_type::regular: | |
create_hard_link(fromDirEntry.path(), toDir / fromFilename); | |
break; | |
case file_type::directory: | |
dirsToVisit.push(subDir / fromFilename); | |
break; | |
} | |
} | |
} | |
} | |
int main() | |
{ | |
try | |
{ | |
std::experimental::filesystem::path root{ L"C:\\users\\gpollard\\desktop" }; | |
hardlink_dir(root / L"copy", root / L"copy_to"); | |
} | |
catch (const std::exception& ex) | |
{ | |
std::cerr << ex.what() << std::endl; | |
return 1; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment