From 42f1ab9fb38a3b5b550b6dc3071a4db99c5a53d6 Mon Sep 17 00:00:00 2001 From: dsingh-mw <136297140+dsingh-mw@users.noreply.github.com> Date: Sun, 14 Jan 2024 16:56:14 +0530 Subject: [PATCH 1/2] 179 skip fchmod flag (#1) * 179:Consider adding a flag for ignoring fchmod errors in copy_file --- test/Jamfile.v2 | 1 + test/issues/179.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/issues/179.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 0ac5e6cc0..736cdc58f 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -108,6 +108,7 @@ run issues/70-71-copy.cpp : : : BOOST_FILESYSTEM_VERSION=4 ; run issues/99_canonical_with_junction_point.cpp : : : BOOST_FILESYSTEM_VERSION=4 ; run issues/reparse_tag_file_placeholder.cpp : : : BOOST_FILESYSTEM_VERSION=4 ; +run issues/179.cpp : : : shared BOOST_FILESYSTEM_VERSION=4 ; if [ os.environ BOOST_FILESYSTEM_TEST_WITH_EXAMPLES ] { diff --git a/test/issues/179.cpp b/test/issues/179.cpp new file mode 100644 index 000000000..a845d02b6 --- /dev/null +++ b/test/issues/179.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include // for mode_t type +#include +#include // for std::ofstream +#include // for std::cout + +namespace fs = boost::filesystem; +using boost::system::error_code; +using boost::system::system_category; +typedef unsigned short mode_t; + +extern "C" int fchmod(int fd, mode_t mode) { + // Return -1 to indicate failure + return -1; +} + +void create_file(const fs::path& ph, const std::string& contents = std::string()) +{ + std::ofstream f(BOOST_FILESYSTEM_C_STR(ph)); + if (!f.is_open()) + throw fs::filesystem_error("operations_test create_file", ph, error_code(errno, system_category())); + if (!contents.empty()) + f << contents; +} + +void verify_file(const fs::path& ph, const std::string& expected) +{ + std::ifstream f(BOOST_FILESYSTEM_C_STR(ph)); + if (!f) + throw fs::filesystem_error("operations_test verify_file", ph, error_code(errno, system_category())); + std::string contents; + f >> contents; + if (contents != expected) + throw fs::filesystem_error("operations_test verify_file contents \"" + contents + "\" != \"" + expected + "\"", ph, error_code()); +} + +int cpp_main(int argc, char* argv[]) +{ + bool file_copied = false; + bool copy_ex_ok = true; + create_file("f1", "content"); // Ensure the source file exists with some content + + try + { + // Attempt to use mocked fcmhod implementation which always errors out. + file_copied = fs::copy_file("f1", "f2", fs::copy_options::ignore_attribute_errors); + } + catch (const fs::filesystem_error&) + { + // If a filesystem_error is thrown, the option did not work as expected + copy_ex_ok = false; + } + + BOOST_TEST(copy_ex_ok); + BOOST_TEST(file_copied); + BOOST_TEST(fs::exists("f2")); // The file should still exist despite the fchmod failure + verify_file("f2", "content"); + + fs::remove("f1"); + fs::remove("f2"); + return 0; +} From 3e9010393ebd1d087a8d89927ae327f744623736 Mon Sep 17 00:00:00 2001 From: dsingh-mw <136297140+dsingh-mw@users.noreply.github.com> Date: Sun, 14 Jan 2024 17:03:38 +0530 Subject: [PATCH 2/2] Update 179.cpp --- test/issues/179.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/issues/179.cpp b/test/issues/179.cpp index a845d02b6..cf000f19e 100644 --- a/test/issues/179.cpp +++ b/test/issues/179.cpp @@ -40,7 +40,7 @@ void verify_file(const fs::path& ph, const std::string& expected) throw fs::filesystem_error("operations_test verify_file contents \"" + contents + "\" != \"" + expected + "\"", ph, error_code()); } -int cpp_main(int argc, char* argv[]) +int main() { bool file_copied = false; bool copy_ex_ok = true;