Skip to content

Commit 83429c9

Browse files
committed
Check file status for status_error in create_directories.
create_directories used to ignore errors returned by status() calls issued internally. The operation would likely fail anyway, but the error codes returned by create_directories would be incorrect. Also, it is better to terminate the operation as early as possible when an error is encountered. Reported in boostorg#182.
1 parent 62515b9 commit 83429c9

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

doc/release_history.html

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ <h2>1.76.0</h2>
4242
<ul>
4343
<li>Updated compatibility with <a href="https://wasi.dev/">WASI</a> platform. (<a href="https://github.com/boostorg/filesystem/pull/169">PR#169</a>)</li>
4444
<li>Fixed an exception being thrown by <code>path::remove_filename</code> if the path is "////". (<a href="https://github.com/boostorg/filesystem/issues/176">#176</a>)</li>
45+
<li>Fixed <code>create_directories</code> disregarding errors from file status query operations issued internally. This could result in incorrect error codes returned by <code>create_directories</code>. (<a href="https://github.com/boostorg/filesystem/issues/182">#182</a>)</li>
4546
</ul>
4647

4748
<h2>1.75.0</h2>

src/operations.cpp

+24-12
Original file line numberDiff line numberDiff line change
@@ -1454,17 +1454,17 @@ void copy_symlink(const path& existing_symlink, const path& new_symlink, system:
14541454
BOOST_FILESYSTEM_DECL
14551455
bool create_directories(const path& p, system::error_code* ec)
14561456
{
1457-
if (p.empty())
1458-
{
1459-
if (!ec)
1460-
{
1461-
BOOST_FILESYSTEM_THROW(filesystem_error(
1462-
"boost::filesystem::create_directories", p,
1463-
system::errc::make_error_code(system::errc::invalid_argument)));
1464-
}
1465-
ec->assign(system::errc::invalid_argument, system::generic_category());
1466-
return false;
1467-
}
1457+
if (p.empty())
1458+
{
1459+
if (!ec)
1460+
{
1461+
BOOST_FILESYSTEM_THROW(filesystem_error(
1462+
"boost::filesystem::create_directories", p,
1463+
system::errc::make_error_code(system::errc::invalid_argument)));
1464+
}
1465+
ec->assign(system::errc::invalid_argument, system::generic_category());
1466+
return false;
1467+
}
14681468

14691469
if (p.filename_is_dot() || p.filename_is_dot_dot())
14701470
return create_directories(p.parent_path(), ec);
@@ -1478,6 +1478,13 @@ bool create_directories(const path& p, system::error_code* ec)
14781478
ec->clear();
14791479
return false;
14801480
}
1481+
else if (BOOST_UNLIKELY(p_status.type() == status_error))
1482+
{
1483+
if (!ec)
1484+
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::create_directories", p, local_ec));
1485+
*ec = local_ec;
1486+
return false;
1487+
}
14811488

14821489
path parent = p.parent_path();
14831490
BOOST_ASSERT_MSG(parent != p, "internal error: p == p.parent_path()");
@@ -1490,14 +1497,19 @@ bool create_directories(const path& p, system::error_code* ec)
14901497
if (parent_status.type() == file_not_found)
14911498
{
14921499
create_directories(parent, local_ec);
1493-
if (local_ec)
1500+
if (BOOST_UNLIKELY(!!local_ec))
14941501
{
1502+
parent_fail_local_ec:
14951503
if (!ec)
14961504
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::create_directories", parent, local_ec));
14971505
*ec = local_ec;
14981506
return false;
14991507
}
15001508
}
1509+
else if (BOOST_UNLIKELY(parent_status.type() == status_error))
1510+
{
1511+
goto parent_fail_local_ec;
1512+
}
15011513
}
15021514

15031515
// create the directory

0 commit comments

Comments
 (0)