Skip to content

Commit

Permalink
Follow-up enhancements for <filesystem> (#3881)
Browse files Browse the repository at this point in the history
  • Loading branch information
achabense authored Jul 20, 2023
1 parent c39d63b commit c1c9691
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
8 changes: 4 additions & 4 deletions benchmarks/src/path_lexically_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace {
void BM_lexically_normal(benchmark::State& state) {
using namespace std::literals;
static constexpr std::wstring_view args[5]{
LR"(X:DriveRelative)"sv,
LR"(\\server\\\share)"sv,
LR"(STL/.github/workflows/../..)"sv,
LR"(C:\Program Files\Azure Data Studio\resources\app\extensions\bat\snippets\batchfile.code-snippets)"sv,
LR"(C:Snippets)"sv,
LR"(.\Snippets)"sv,
LR"(..\..\IDE\VC\Snippets)"sv,
LR"(C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\VC\Snippets)"sv,
LR"(/\server/\share/\a/\b/\c/\./\./\d/\../\../\../\../\../\../\../\other/x/y/z/.././..\meow.txt)"sv,
};

Expand Down
63 changes: 26 additions & 37 deletions stl/inc/filesystem
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,13 @@ namespace filesystem {
_EXPORT_STD class path {
template <class _Base_iter>
friend class _Path_iterator;
friend inline __std_win_error _Absolute(path& _Result, const wstring& _Text);
friend inline path absolute(const path& _Input, error_code& _Ec);
friend inline __std_win_error _Canonical(path& _Result, const wstring& _Text);
friend inline path temp_directory_path(error_code& _Ec);
friend inline path current_path(error_code& _Ec);
friend inline void current_path(const path& _To);
friend inline void current_path(const path& _To, error_code& _Ec) noexcept;
friend inline __std_win_error _Read_symlink(const path& _Symlink_path, path& _Result) noexcept;
friend inline __std_win_error _Read_symlink(const path& _Symlink_path, path& _Result);

public:
using value_type = wchar_t;
Expand Down Expand Up @@ -1208,7 +1208,7 @@ namespace filesystem {
vector<wstring_view> _Vec; // Empty wstring_view means directory-separator
// that will be normalized to a preferred-separator.
// Non-empty wstring_view means filename.
_Vec.reserve(13); // avoid frequent re-allocations
_Vec.reserve(19); // avoid frequent re-allocations
bool _Has_root_directory = false; // true: there is a slash right after root-name.
auto _Ptr = _Root_name_end;
if (_Ptr != _Last && _Is_slash(*_Ptr)) {
Expand Down Expand Up @@ -2969,45 +2969,35 @@ namespace filesystem {
return {};
}

_NODISCARD inline __std_win_error _Absolute(path& _Result, const wstring& _Text) { // pre: _Result.empty()
if (_Text.empty()) {
return __std_win_error::_Success;
}

_Result._Text.resize(__std_fs_max_path);
__std_win_error _Err;
for (;;) {
const auto _Requested_size = static_cast<unsigned long>(_Result._Text.size());
const auto _Full_path_result =
__std_fs_get_full_path_name(_Text.c_str(), _Requested_size, _Result._Text.data());
_Err = _Full_path_result._Error;
_Result._Text.resize(_Full_path_result._Size);
if (_Full_path_result._Size < _Requested_size) {
break;
}
}

return _Err;
}

_EXPORT_STD _NODISCARD inline path absolute(const path& _Input) {
_EXPORT_STD _NODISCARD inline path absolute(const path& _Input, error_code& _Ec) {
// normalize path according to system semantics, without touching the disk
// calls GetFullPathNameW
_Ec.clear(); // for exception safety
path _Result;
const auto _Err = _Absolute(_Result, _Input.native());
if (_Err != __std_win_error::_Success) {
_Throw_fs_error("absolute", _Err, _Input);
if (!_Input._Text.empty()) {
_Result._Text.resize(__std_fs_max_path);
for (;;) {
const auto _Requested_size = static_cast<unsigned long>(_Result._Text.size());
const auto _Full_path_result =
__std_fs_get_full_path_name(_Input._Text.c_str(), _Requested_size, _Result._Text.data());
_Result._Text.resize(_Full_path_result._Size);
if (_Full_path_result._Size < _Requested_size) {
_Ec = _Make_ec(_Full_path_result._Error);
break;
}
}
}

return _Result;
}

_EXPORT_STD _NODISCARD inline path absolute(const path& _Input, error_code& _Ec) {
_EXPORT_STD _NODISCARD inline path absolute(const path& _Input) {
// normalize path according to system semantics, without touching the disk
// calls GetFullPathNameW
_Ec.clear(); // for exception safety
path _Result;
_Ec = _Make_ec(_Absolute(_Result, _Input.native()));
error_code _Ec;
path _Result(_STD filesystem::absolute(_Input, _Ec));
if (_Ec) {
_Throw_fs_error("absolute", _Ec, _Input);
}
return _Result;
}

Expand Down Expand Up @@ -3206,7 +3196,7 @@ namespace filesystem {
return __std_win_error::_Success;
}

_NODISCARD inline __std_win_error _Read_symlink(const path& _Symlink_path, path& _Result) noexcept {
_NODISCARD inline __std_win_error _Read_symlink(const path& _Symlink_path, path& _Result) {
__std_win_error _Err;
unique_ptr<char[]> _Buffer_unique_ptr;
{
Expand All @@ -3230,8 +3220,7 @@ namespace filesystem {
return _Err;
}

_Result._Text.resize(_Length);
_CSTD memcpy(_Result._Text.data(), _Offset, _Length * sizeof(wchar_t));
_Result._Text.assign(_Offset, _Length);

return __std_win_error::_Success;
}
Expand Down Expand Up @@ -4055,7 +4044,7 @@ namespace filesystem {
}

_EXPORT_STD _NODISCARD inline path current_path(error_code& _Ec) {
_Ec.clear();
_Ec.clear(); // for exception safety
path _Result;
_Result._Text.resize(__std_fs_max_path);
for (;;) {
Expand Down

0 comments on commit c1c9691

Please sign in to comment.