-
-
Notifications
You must be signed in to change notification settings - Fork 11
Implement an IO function to mirror a directory using hardlinks #2276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,25 @@ auto starts_with(const std::filesystem::path &path, | |
| return true; | ||
| } | ||
|
|
||
| auto hardlink_directory(const std::filesystem::path &source, | ||
| const std::filesystem::path &destination) -> void { | ||
| assert(std::filesystem::is_directory(source)); | ||
| assert(!std::filesystem::exists(destination) || | ||
| std::filesystem::is_directory(destination)); | ||
| assert(!starts_with(destination, source)); | ||
| std::filesystem::create_directories(destination); | ||
jviotti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (const auto &entry : | ||
| std::filesystem::recursive_directory_iterator{source}) { | ||
| const auto target{destination / | ||
| std::filesystem::relative(entry.path(), source)}; | ||
| if (entry.is_directory()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Severity: medium 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||
| std::filesystem::create_directories(target); | ||
| } else if (entry.is_regular_file()) { | ||
| std::filesystem::create_hard_link(entry.path(), target); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Severity: low 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| auto flush(const std::filesystem::path &path) -> void { | ||
| #if defined(_WIN32) | ||
| HANDLE hFile = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <sourcemeta/core/io.h> | ||
|
|
||
| #include <filesystem> // std::filesystem | ||
| #include <fstream> // std::ofstream, std::ifstream | ||
| #include <string> // std::string | ||
|
|
||
| TEST(IO_hardlink_directory, mirrors_structure) { | ||
| const sourcemeta::core::TemporaryDirectory workspace{ | ||
| std::filesystem::path{BUILD_DIRECTORY}, ".test-hardlink-"}; | ||
| const auto source{workspace.path() / "source"}; | ||
| const auto destination{workspace.path() / "destination"}; | ||
|
|
||
| std::filesystem::create_directories(source / "subdir"); | ||
| std::ofstream{source / "a.txt"} << "alpha"; | ||
| std::ofstream{source / "subdir" / "b.txt"} << "beta"; | ||
|
|
||
| sourcemeta::core::hardlink_directory(source, destination); | ||
|
|
||
| EXPECT_TRUE(std::filesystem::exists(destination / "a.txt")); | ||
| EXPECT_TRUE(std::filesystem::exists(destination / "subdir" / "b.txt")); | ||
| EXPECT_TRUE(std::filesystem::is_directory(destination / "subdir")); | ||
| } | ||
|
|
||
| TEST(IO_hardlink_directory, files_share_inodes) { | ||
| const sourcemeta::core::TemporaryDirectory workspace{ | ||
| std::filesystem::path{BUILD_DIRECTORY}, ".test-hardlink-"}; | ||
| const auto source{workspace.path() / "source"}; | ||
| const auto destination{workspace.path() / "destination"}; | ||
|
|
||
| std::filesystem::create_directory(source); | ||
| std::ofstream{source / "file.txt"} << "content"; | ||
|
|
||
| sourcemeta::core::hardlink_directory(source, destination); | ||
|
|
||
| EXPECT_EQ(std::filesystem::hard_link_count(source / "file.txt"), 2); | ||
| EXPECT_EQ(std::filesystem::hard_link_count(destination / "file.txt"), 2); | ||
| EXPECT_TRUE(std::filesystem::equivalent(source / "file.txt", | ||
| destination / "file.txt")); | ||
| } | ||
|
|
||
| TEST(IO_hardlink_directory, unlink_then_write_independence) { | ||
| const sourcemeta::core::TemporaryDirectory workspace{ | ||
| std::filesystem::path{BUILD_DIRECTORY}, ".test-hardlink-"}; | ||
| const auto source{workspace.path() / "source"}; | ||
| const auto destination{workspace.path() / "destination"}; | ||
|
|
||
| std::filesystem::create_directory(source); | ||
| std::ofstream{source / "file.txt"} << "original"; | ||
|
|
||
| sourcemeta::core::hardlink_directory(source, destination); | ||
|
|
||
| std::filesystem::remove(destination / "file.txt"); | ||
| std::ofstream{destination / "file.txt"} << "modified"; | ||
|
|
||
| std::ifstream source_stream{source / "file.txt"}; | ||
| std::string source_content; | ||
| std::getline(source_stream, source_content); | ||
| EXPECT_EQ(source_content, "original"); | ||
|
|
||
| std::ifstream destination_stream{destination / "file.txt"}; | ||
| std::string destination_content; | ||
| std::getline(destination_stream, destination_content); | ||
| EXPECT_EQ(destination_content, "modified"); | ||
| } | ||
|
|
||
| TEST(IO_hardlink_directory, empty_source) { | ||
| const sourcemeta::core::TemporaryDirectory workspace{ | ||
| std::filesystem::path{BUILD_DIRECTORY}, ".test-hardlink-"}; | ||
| const auto source{workspace.path() / "source"}; | ||
| const auto destination{workspace.path() / "destination"}; | ||
|
|
||
| std::filesystem::create_directory(source); | ||
|
|
||
| sourcemeta::core::hardlink_directory(source, destination); | ||
|
|
||
| EXPECT_TRUE(std::filesystem::exists(destination)); | ||
| EXPECT_TRUE(std::filesystem::is_directory(destination)); | ||
| EXPECT_TRUE(std::filesystem::is_empty(destination)); | ||
| } | ||
|
|
||
| TEST(IO_hardlink_directory, deeply_nested) { | ||
| const sourcemeta::core::TemporaryDirectory workspace{ | ||
| std::filesystem::path{BUILD_DIRECTORY}, ".test-hardlink-"}; | ||
| const auto source{workspace.path() / "source"}; | ||
| const auto destination{workspace.path() / "destination"}; | ||
|
|
||
| std::filesystem::create_directories(source / "a" / "b" / "c"); | ||
| std::ofstream{source / "a" / "b" / "c" / "deep.txt"} << "deep"; | ||
|
|
||
| sourcemeta::core::hardlink_directory(source, destination); | ||
|
|
||
| EXPECT_TRUE( | ||
| std::filesystem::exists(destination / "a" / "b" / "c" / "deep.txt")); | ||
|
|
||
| std::ifstream stream{destination / "a" / "b" / "c" / "deep.txt"}; | ||
| std::string content; | ||
| std::getline(stream, content); | ||
| EXPECT_EQ(content, "deep"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hardlink_directorycreatesdestinationbefore walkingsource; ifdestinationis insidesource(or equal to it), the recursive iterator can traverse the newly created tree and lead to unbounded recursion/work. Consider explicitly rejecting that case.Severity: high
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.