Skip to content

<filesystem>: Fix the path returned when there is an error with temp_directory_path and simplify the validation logic #5561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

YexuanXiao
Copy link
Contributor

@YexuanXiao YexuanXiao commented May 31, 2025

Fix #5557.

In the current implementation, temp_directory_path would first use GetTempPath/GetTempPath2W to obtain the temp path, then use GetFileAttributesW to obtain file attributes to verify if it was a directory. If the file is not a directory but is a symlink, the symlink would be opened to verify that the symlink is correct. Since GetFileAttributesW internally opens the file and uses GetFileInformationByHandleEx to obtain file attributes, the current logic contains redundancy. Instead, we should unconditionally open the path obtained by GetTempPath/GetTempPath2W, follow symlinks, and verify that it is a directory through GetFileInformationByHandleEx.

@YexuanXiao YexuanXiao requested a review from a team as a code owner May 31, 2025 14:47
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews May 31, 2025
@YexuanXiao

This comment was marked as outdated.

@YexuanXiao
Copy link
Contributor Author

YexuanXiao commented May 31, 2025

STL/stl/inc/filesystem

Lines 4049 to 4052 in 7841cf8

if (_Temp_result._Error == __std_win_error::_Max) { // path could be retrieved, but was not a directory
_Ec = _STD make_error_code(errc::not_a_directory);
} else {
_Ec = _Make_ec(_Temp_result._Error);

@StephanTLavavej What's the significance of using different categories here? The standard doesn't seem to require it.

[fs.err.report]/3.1

If a call by the implementation to an operating system or other underlying API results in an error that prevents the function from meeting its specifications, the error_code& argument is set as appropriate for the specific operating system dependent error.

@StephanTLavavej
Copy link
Member

What Windows error code would we synthesize to indicate "not a directory"?

@YexuanXiao
Copy link
Contributor Author

YexuanXiao commented May 31, 2025

What Windows error code would we synthesize to indicate "not a directory"?

If the path points to a symlink but the target does not exist then CreateFile will report _File_not_found; or the target's class does not match the symlink, then CreateFile will report _Directory_name_is_invalid. For consistency, L830 might need to be the same.

The File APIs does not seem to strictly distinguish between files and directories in its error codes, and the error codes appear to be unstable. Unless necessary, identifying or conversion should be avoided.

@StephanTLavavej StephanTLavavej changed the title <filesystem> Fix the path returned when there is an error with temp_directory_path <filesystem>: Fix the path returned when there is an error with temp_directory_path Jun 1, 2025
@StephanTLavavej StephanTLavavej added bug Something isn't working filesystem C++17 filesystem labels Jun 1, 2025
@StephanTLavavej StephanTLavavej self-assigned this Jun 1, 2025
@StephanTLavavej
Copy link
Member

StephanTLavavej commented Jun 2, 2025

There are a lot of logic changes here, much more than are needed to simply clear the path on error. Can you please update your PR description to explain how the code is changing? This is both for my benefit while reviewing, and for future maintainers/contributors to understand what changed.

Edit: Indeed, it looks like these are unrelated behavioral changes. We try to avoid mixing together bugfixes with other behavioral changes (or even cleanups) unless they are inextricably linked.

@StephanTLavavej StephanTLavavej removed their assignment Jun 2, 2025
@StephanTLavavej StephanTLavavej moved this from Initial Review to Work In Progress in STL Code Reviews Jun 2, 2025
@YexuanXiao YexuanXiao changed the title <filesystem>: Fix the path returned when there is an error with temp_directory_path <filesystem>: Fix the path returned when there is an error with temp_directory_path and simplify the validation logic Jun 3, 2025
@YexuanXiao
Copy link
Contributor Author

There are a lot of logic changes here, much more than are needed to simply clear the path on error. Can you please update your PR description to explain how the code is changing? This is both for my benefit while reviewing, and for future maintainers/contributors to understand what changed.

Edit: Indeed, it looks like these are unrelated behavioral changes. We try to avoid mixing together bugfixes with other behavioral changes (or even cleanups) unless they are inextricably linked.

Should I split it into two PRs?

@StephanTLavavej
Copy link
Member

Should I split it into two PRs?

Yes, please.

@YexuanXiao YexuanXiao closed this Jun 3, 2025
@github-project-automation github-project-automation bot moved this from Work In Progress to Done in STL Code Reviews Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working filesystem C++17 filesystem
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

<filesystem>: temp_directory_path(error_code&) doesn't return an empty path for errors
2 participants