Implement a better copy_file.
This patch improves both the performance, and the safety of the
copy_file implementation.
The performance improvements are achieved by using sendfile on
Linux and copyfile on OS X when available.
The TOCTOU hardening is achieved by opening the source and
destination files and then using fstat to check their attributes to
see if we can copy them.
Unfortunately for the destination file, there is no way to open
it without accidentally creating it, so we first have to use
stat to determine if it exists, and if we should copy to it.
Then, once we're sure we should try to copy, we open the dest
file and ensure it names the same entity we previously stat'ed.
llvm-svn: 337649
diff --git a/libcxx/test/support/filesystem_test_helper.hpp b/libcxx/test/support/filesystem_test_helper.hpp
index e3f46a1..de06868c 100644
--- a/libcxx/test/support/filesystem_test_helper.hpp
+++ b/libcxx/test/support/filesystem_test_helper.hpp
@@ -105,6 +105,20 @@
#error LIBCXX_FILESYSTEM_DYNAMIC_TEST_HELPER must be defined
#endif
+namespace random_utils {
+inline char to_hex(int ch) {
+ return ch < 10 ? static_cast<char>('0' + ch)
+ : static_cast<char>('a' + (ch - 10));
+}
+
+inline char random_hex_char() {
+ static std::mt19937 rd{std::random_device{}()};
+ static std::uniform_int_distribution<int> mrand{0, 15};
+ return to_hex(mrand(rd));
+}
+
+} // namespace random_utils
+
struct scoped_test_env
{
scoped_test_env() : test_root(random_env_path())
@@ -179,21 +193,11 @@
fs::path const test_root;
private:
- static char to_hex(int ch) {
- return ch < 10 ? static_cast<char>('0' + ch)
- : static_cast<char>('a' + (ch - 10));
- }
-
- static char random_hex_char() {
- static std::mt19937 rd { std::random_device{}() };
- static std::uniform_int_distribution<int> mrand{0, 15};
- return to_hex( mrand(rd) );
- }
-
static std::string unique_path_suffix() {
std::string model = "test.%%%%%%";
for (auto & ch : model) {
- if (ch == '%') ch = random_hex_char();
+ if (ch == '%')
+ ch = random_utils::random_hex_char();
}
return model;
}