libstdc++: Use std::string::__resize_and_overwrite in std::filesystem

There are a few places in the std::filesystem code that use a string as
a buffer for OS APIs to write to. We can use the new extension
__resize_and_overwrite to avoid redundant initialization of those
buffers.

libstdc++-v3/ChangeLog:

	* src/c++17/fs_ops.cc (fs::absolute) [FILESYSTEM_IS_WINDOWS]:
	Use __resize_and_overwrite to fill buffer.
	(fs::read_symlink) [HAVE_READLINK]: Likewise.
	* src/filesystem/ops-common.h (get_temp_directory_from_env)
	[FILESYSTEM_IS_WINDOWS]: Likewise.
This commit is contained in:
Jonathan Wakely 2023-08-17 18:50:36 +01:00
parent dcbec954fc
commit 283994cba6
2 changed files with 27 additions and 25 deletions

View file

@ -112,18 +112,17 @@ fs::absolute(const path& p, error_code& ec)
wstring buf;
do
{
buf.resize(len);
len = GetFullPathNameW(s.data(), len, buf.data(), nullptr);
buf.__resize_and_overwrite(len, [&s, &len](wchar_t* p, unsigned n) {
len = GetFullPathNameW(s.data(), n, p, nullptr);
return len > n ? 0 : len;
});
}
while (len > buf.size());
if (len == 0)
ec = __last_system_error();
else
{
buf.resize(len);
ret = std::move(buf);
}
ret = std::move(buf);
#else
ret = current_path(ec);
ret /= p;
@ -1187,31 +1186,33 @@ fs::path fs::read_symlink(const path& p, error_code& ec)
return result;
}
std::string buf(st.st_size ? st.st_size + 1 : 128, '\0');
std::string buf;
size_t bufsz = st.st_size ? st.st_size + 1 : 128;
do
{
ssize_t len = ::readlink(p.c_str(), buf.data(), buf.size());
if (len == -1)
ssize_t len;
buf.__resize_and_overwrite(bufsz, [&p, &len](char* ptr, size_t n) {
len = ::readlink(p.c_str(), ptr, n);
return size_t(len) < n ? len : 0;
});
if (buf.size())
{
result.assign(std::move(buf));
ec.clear();
break;
}
else if (len == -1)
{
ec.assign(errno, std::generic_category());
return result;
}
else if (len == (ssize_t)buf.size())
else if (bufsz > 4096)
{
if (buf.size() > 4096)
{
ec.assign(ENAMETOOLONG, std::generic_category());
return result;
}
buf.resize(buf.size() * 2);
ec.assign(ENAMETOOLONG, std::generic_category());
return result;
}
else
{
buf.resize(len);
result.assign(buf);
ec.clear();
break;
}
bufsz *= 2;
}
while (true);
#else

View file

@ -700,8 +700,10 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
std::wstring buf;
do
{
buf.resize(len);
len = GetTempPathW(buf.size(), buf.data());
buf.__resize_and_overwrite(len, [&len](wchar_t* p, unsigned n) {
len = GetTempPathW(n, p);
return len > n ? 0 : len;
});
}
while (len > buf.size());
@ -710,7 +712,6 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
else
ec.clear();
buf.resize(len);
return buf;
}
#else