diff options
author | Eric Fiselier <eric@efcs.ca> | 2018-07-22 21:15:15 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2018-07-22 21:15:15 +0000 |
commit | fb03277e63674d182a41c31f5f71a88b8d89e0a2 (patch) | |
tree | e6ffaa018abc78187898a59a06fc83660031f9f5 /src | |
parent | 1061b65e6e59c1894e22fad012607eeaee8b6d69 (diff) | |
download | libcxx-fb03277e63674d182a41c31f5f71a88b8d89e0a2.tar.gz |
Harden copy_file even more.
This patch removes the O_CREAT open flag when we first
attempt to open the destination file but we expect it to
already exist.
This theoretically avoids the possibility that it was removed
between when we first stat'ed it, and when we attempt to open it.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@337659 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'src')
-rw-r--r-- | src/experimental/filesystem/operations.cpp | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/experimental/filesystem/operations.cpp b/src/experimental/filesystem/operations.cpp index be876ad77..cc744fb9b 100644 --- a/src/experimental/filesystem/operations.cpp +++ b/src/experimental/filesystem/operations.cpp @@ -716,7 +716,7 @@ bool __copy_file(const path& from, const path& to, copy_options options, if (to_exists && skip_existing) return false; - auto ShouldCopy = [&]() { + bool ShouldCopy = [&]() { if (to_exists && update_existing) { auto from_time = detail::extract_mtime(from_stat); auto to_time = detail::extract_mtime(to_stat_path); @@ -730,13 +730,15 @@ bool __copy_file(const path& from, const path& to, copy_options options, if (!to_exists || overwrite_existing) return true; return Error(make_error_code(errc::file_exists)); - }; - if (!ShouldCopy()) + }(); + if (!ShouldCopy) return false; // Don't truncate right away. We may not be opening the file we originally // looked at; we'll check this later. - int to_open_flags = O_WRONLY | O_CREAT; + int to_open_flags = O_WRONLY; + if (!to_exists) + to_open_flags |= O_CREAT; FileDescriptor to_fd = FileDescriptor::create_with_status( &to, m_ec, to_open_flags, from_stat.st_mode); if (m_ec) @@ -745,6 +747,7 @@ bool __copy_file(const path& from, const path& to, copy_options options, if (to_exists) { // Check that the file we initially stat'ed is equivalent to the one // we opened. + // FIXME: report this better. if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat())) return Error(make_error_code(errc::bad_file_descriptor)); @@ -761,7 +764,6 @@ bool __copy_file(const path& from, const path& to, copy_options options, } return true; - } void __copy_symlink(const path& existing_symlink, const path& new_symlink, |