Skip to content
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

Refactor Filesystem Code for Clarity #2766

Merged
merged 5 commits into from
Jul 1, 2022
Merged

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jun 7, 2022

This makes the code more readable as well as lowers operations to code paths that actually need them

@AZero13 AZero13 requested a review from a team as a code owner June 7, 2022 16:11
@AZero13 AZero13 force-pushed the Test2 branch 3 times, most recently from 67f9ccc to e33a48d Compare June 7, 2022 17:15
@AZero13 AZero13 changed the title Refactor Filesystem files Refactor Filesystem Code for Clarity Jun 7, 2022
stl/inc/filesystem Outdated Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesys.cpp Show resolved Hide resolved
stl/src/filesys.cpp Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
@StephanTLavavej
Copy link
Member

StephanTLavavej commented Jun 8, 2022

Thanks for looking into improving the codebase!

As a maintainer, I find this PR to be concerning. Every code change is risky to some degree (even innocent-seeming changes are occasionally risky, up to and including ABI breaks), and also costs time to review. With a brief glance at the changes, I can see that there's some good stuff here (e.g. I particularly like the use of the SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE constant which is now available), but I have these specific concerns:

  1. This is a "grab bag" of different refactorings, which is harder to review. A PR that performs only one kind of transformation (e.g. De Morgan changes) is significantly less risky and easier to review - even when many sites are being changed. The dangerous thing about mixed changes is that we can nod our heads to a bunch of safe changes and miss the one scary/wrong change in the middle.
    • It can sometimes be acceptable to have one PR with several kinds of refactorings cleanly grouped into separate commits. I suppose I would accept separate commits here.
  2. Substantial behavior changes (whether bug fixes or performance improvements) should not be mixed with stylistic refactorings - that is, <filesystem>: Resize files in a better way #2767 should be addressed in an isolated PR. Occasionally, because we are human (and slightly lazy), it is okay to perform one tiny cleanup in a PR that's otherwise dedicated to fixing a bug or improving performance. However, any substantial mixing really must be avoided. (Same rationale as avoiding mixed refactorings, but even stronger.)
    • Putting behavior changes and refactorings into separate commits doesn't solve this concern, they really need to be separate PRs.
  3. It would be helpful for your PR description to explain why each change is being made. For some changes, I can immediately see the clarity improvement (e.g. De Morganing "test existing files for equivalence" so that it reads "if the serial numbers are the same, and the high parts are the same, and the low parts are the same, then we're equivalent" avoids the confusing negation in the original form - great!). For others, I am unsure how they are improving consistency - e.g. some conditions are being reversed to return-on-success (e.g. __std_fs_get_current_path()), but others are being reversed to return-on-failure (e.g. _Open_dir()). Having a changelog would help me and other reviewers understand the changes. It doesn't need to be super detailed, it just needs to mention why each function is being changed.
    • This is also useful for "source code archaeology" - as the STL is immortal, we occasionally need to look up why things were changed 5 or 10 years ago. Having a PR description that explains each change will be helpful if we need to read it in 2027 or 2032 in our flying cars.

@StephanTLavavej StephanTLavavej self-assigned this Jun 8, 2022
@AZero13 AZero13 force-pushed the Test2 branch 2 times, most recently from d65967f to 56d6012 Compare June 9, 2022 14:32
stl/src/filesys.cpp Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesystem.cpp Show resolved Hide resolved
@AZero13 AZero13 force-pushed the Test2 branch 5 times, most recently from 69241b0 to 6734d90 Compare June 9, 2022 19:57
Copy link
Member

@StephanTLavavej StephanTLavavej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good! I found no correctness issues and only minor stylistic issues, so I'll go ahead and push changes for them.

stl/inc/filesystem Outdated Show resolved Hide resolved
stl/src/filesystem.cpp Outdated Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesystem.cpp Outdated Show resolved Hide resolved
stl/src/filesystem.cpp Outdated Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesystem.cpp Outdated Show resolved Hide resolved
stl/src/filesys.cpp Outdated Show resolved Hide resolved
@StephanTLavavej StephanTLavavej removed their assignment Jun 10, 2022
Copy link
Contributor

@strega-nil-ms strega-nil-ms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I like this; however, comparing BOOL against 0 is not good imo, and so I'm requesting that be changed.

stl/src/filesys.cpp Outdated Show resolved Hide resolved
stl/src/filesystem.cpp Outdated Show resolved Hide resolved
@strega-nil-ms strega-nil-ms removed their assignment Jun 20, 2022
@AZero13
Copy link
Contributor Author

AZero13 commented Jun 21, 2022

Isn't that the convention?

Co-authored-by: nicole mazzuca <[email protected]>
@StephanTLavavej
Copy link
Member

The STL maintainers talked about BOOL on Discord - we hadn't previously settled on a convention for it, since its intended meaning is different from ordinary ints. We agreed with Nicole's preferred direction - directly testing it with if (_Ok), if (!_Ok), etc. (i.e. contextually converting it to bool). (When a real bool is needed, we want to explicitly compare against 0 instead of implicitly converting - that case is fairly rare.)

For ordinary integers, the convention is unchanged - always explicitly compare.

I'll validate and push changes, and also resolve the merge conflict here.

This also reverses the `DeviceIoControl()` test to become if-success.
@StephanTLavavej
Copy link
Member

@strega-nil-ms I pushed a merge with main, resolving a trivial merge conflict with #2802's detached comment. Then I audited filesys.cpp and filesystem.cpp for all comparisons of BOOL and BOOLEAN against 0 - both pre-existing occurrences, and ones that were introduced by this PR. (I checked every single function, instead of doing a search-and-replace, and was careful to not flip any comparisons accidentally.) I avoided changing the code that #2771 is replacing. I also reversed DeviceIoControl()'s flow to be if-success, since that's consistent with other changes here.

@AtariDreams Thanks for your patience - we should be able to merge this soon. The last thing is that we have to avoid significant changes to msvcp140.dll until VS 2022 17.4 Preview 2 is open for changes, so after Nicole double-checks the final changes here, we'll need to hold this PR in a blocked state for a little while along with a few other PRs we've accumulated.

Copy link
Contributor

@strega-nil-ms strega-nil-ms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, approved!

@@ -601,7 +596,7 @@ _Success_(return == __std_win_error::_Success) __std_win_error
return {false, __std_win_error{GetLastError()}};
}
// check if FILE_ATTRIBUTE_READONLY is set
if (_Basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) {
if ((_Basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) != 0u) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still curious why this is using 0u instead of just 0; no changes requested.

@strega-nil-ms strega-nil-ms removed their assignment Jun 23, 2022
@strega-nil-ms
Copy link
Contributor

This is now Ready To Merge, but I'm marking it as Blocked until changes start flowing into VS 2022 17.4 Preview 2 in a couple of weeks.

@StephanTLavavej StephanTLavavej added blocked Something is preventing work on this and removed blocked Something is preventing work on this labels Jun 23, 2022
@StephanTLavavej StephanTLavavej self-assigned this Jun 30, 2022
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit ff97e90 into microsoft:main Jul 1, 2022
@StephanTLavavej
Copy link
Member

Thanks for finding and improving this filesystem code! 📂 ✅ 😸

fsb4000 pushed a commit to fsb4000/STL that referenced this pull request Aug 13, 2022
Co-authored-by: Stephan T. Lavavej <[email protected]>
Co-authored-by: nicole mazzuca <[email protected]>
@AZero13 AZero13 deleted the Test2 branch September 26, 2022 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects redist Results in changes to separately compiled bits filesystem C++17 filesystem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants