Implement LWG issue 2720. Replace perms::resolve_symlinks with perms::symlink_nofollow.

This changes how filesystem::permissions(p, perms) handles symlinks. Previously
symlinks were not resolved by default instead only getting resolved when
"perms::resolve_symlinks" was used. After this change symlinks are resolved
by default and perms::symlink_nofollow must be given to change this.

This issue has not yet been moved to Ready status, and I will revert if it
doesn't get moved at the current meeting. However I feel confident that it
will and it's nice to have implementations when moving issues.

llvm-svn: 273328
This commit is contained in:
Eric Fiselier 2016-06-21 22:42:42 +00:00
parent 25da86a5db
commit 44e2ebadb2
4 changed files with 21 additions and 13 deletions

View File

@ -292,7 +292,7 @@ enum class _LIBCPP_TYPE_VIS perms : unsigned
add_perms = 0x10000,
remove_perms = 0x20000,
resolve_symlinks = 0x40000
symlink_nofollow = 0x40000
};
_LIBCPP_INLINE_VISIBILITY

View File

@ -594,7 +594,7 @@ void __last_write_time(const path& p, file_time_type new_time,
void __permissions(const path& p, perms prms, std::error_code *ec)
{
const bool resolve_symlinks = bool(perms::resolve_symlinks & prms);
const bool resolve_symlinks = !bool(perms::symlink_nofollow & prms);
const bool add_perms = bool(perms::add_perms & prms);
const bool remove_perms = bool(perms::remove_perms & prms);
@ -605,6 +605,8 @@ void __permissions(const path& p, perms prms, std::error_code *ec)
file_status st = detail::posix_lstat(p, &m_ec);
if (m_ec) return set_or_throw(m_ec, ec, "permissions", p);
// AT_SYMLINK_NOFOLLOW can only be used on symlinks, using it on a regular
// file will cause fchmodat to report an error on some systems.
const bool set_sym_perms = is_symlink(st) && !resolve_symlinks;
if ((resolve_symlinks && is_symlink(st)) && (add_perms || remove_perms)) {
@ -622,14 +624,16 @@ void __permissions(const path& p, perms prms, std::error_code *ec)
# if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
return set_or_throw(ec, "permissions", p);
}
# else
if (set_sym_perms)
return set_or_throw(make_error_code(errc::operation_not_supported),
ec, "permissions", p);
if (::chmod(p.c_str(), real_perms) == -1) {
# endif
return set_or_throw(ec, "permissions", p);
}
# endif
if (ec) ec->clear();
}

View File

@ -63,6 +63,6 @@ int main() {
E::unknown == ME(0xFFFF) &&
E::add_perms == ME(0x10000) &&
E::remove_perms == ME(0x20000) &&
E::resolve_symlinks == ME(0x40000),
E::symlink_nofollow == ME(0x40000),
"Expected enumeration values do not match");
}

View File

@ -67,9 +67,9 @@ TEST_CASE(test_error_reporting)
}
{
std::error_code ec;
fs::permissions(dne_sym, perms::resolve_symlinks, ec);
fs::permissions(dne_sym, fs::perms{}, ec);
TEST_REQUIRE(ec);
TEST_CHECK(checkThrow(dne_sym, perms::resolve_symlinks, ec));
TEST_CHECK(checkThrow(dne_sym, fs::perms{}, ec));
}
}
@ -82,7 +82,7 @@ TEST_CASE(basic_permissions_test)
const path sym = env.create_symlink(file_for_sym, "sym");
const perms AP = perms::add_perms;
const perms RP = perms::remove_perms;
const perms RS = perms::resolve_symlinks;
const perms NF = perms::symlink_nofollow;
struct TestCase {
path p;
perms set_perms;
@ -98,11 +98,15 @@ TEST_CASE(basic_permissions_test)
{dir, perms::owner_all, perms::owner_all},
{dir, perms::group_all | AP, perms::owner_all | perms::group_all},
{dir, perms::group_all | RP, perms::owner_all},
// test symlink with resolve symlinks on symlink
{sym, perms::none | RS, perms::none},
{sym, perms::owner_all | RS, perms::owner_all},
{sym, perms::group_all | AP | RS, perms::owner_all | perms::group_all},
{sym, perms::group_all | RP | RS, perms::owner_all}
// test symlink without symlink_nofollow
{sym, perms::none, perms::none},
{sym, perms::owner_all, perms::owner_all},
{sym, perms::group_all | AP, perms::owner_all | perms::group_all},
{sym, perms::group_all | RP , perms::owner_all},
// test non-symlink with symlink_nofollow. The last test on file/dir
// will have set their permissions to perms::owner_all
{file, perms::group_all | AP | NF, perms::owner_all | perms::group_all},
{dir, perms::group_all | AP | NF, perms::owner_all | perms::group_all}
};
for (auto const& TC : cases) {
TEST_CHECK(status(TC.p).permissions() != TC.expected);
@ -144,7 +148,7 @@ TEST_CASE(test_no_resolve_symlink_on_symlink)
std::error_code expected_ec = std::make_error_code(std::errc::operation_not_supported);
#endif
std::error_code ec = std::make_error_code(std::errc::bad_address);
permissions(sym, TC.set_perms, ec);
permissions(sym, TC.set_perms | perms::symlink_nofollow, ec);
TEST_CHECK(ec == expected_ec);
TEST_CHECK(status(file).permissions() == file_perms);
TEST_CHECK(symlink_status(sym).permissions() == expected_link_perms);