fs: support removing read-only files in rmSync on Windows#64453
Open
SparshGarg999 wants to merge 1 commit into
Open
fs: support removing read-only files in rmSync on Windows#64453SparshGarg999 wants to merge 1 commit into
SparshGarg999 wants to merge 1 commit into
Conversation
On Windows, libc++ std::filesystem::remove and remove_all do not automatically clear the read-only attribute before deleting a file (unlike MSVC STL). This causes s.rmSync to fail with EPERM`n(operation_not_permitted) when trying to remove read-only files in environments where Node.js is built using clang libc++ (such as Electron). This commit introduces a Windows-specific helper ClearReadOnlyAttributeW`nwhich clears the FILE_ATTRIBUTE_READONLY attribute recursively (or for a single file) when operation_not_permitted is returned, allowing mSync to successfully delete read-only files/folders. Fixes: nodejs#64374 Signed-off-by: SparshGarg999 <sparshgarg999@gmail.com>
93e7ab9 to
8d6ce84
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ensure
fs.rmSynccan delete read-only files and directories containing them on Windows when Node is built with clanglibc++(e.g. inside Electron).Description
Since the migration to
std::filesystem::removeandstd::filesystem::remove_allon Windows, Node'sfs.rmSyncbehaves differently depending on whether it was built with MSVC STL or clanglibc++.MSVC STL's implementation of
std::filesystem::removeautomatically clears the read-only attribute of a file before attempting to delete it. However,libc++'s implementation on Windows does not clear the read-only attribute, which causes the deletion to fail withoperation_not_permitted(EPERM). This issue directly impacts downstream platforms like Electron that compile Node.js withlibc++on Windows (seeelectron/electron#52253).This commit introduces a Windows-specific helper
ClearReadOnlyAttributeWinsrc/node_file.ccthat is called ifoperation_not_permittedis encountered. It manually clears theFILE_ATTRIBUTE_READONLYattribute from the file (or recursively from files inside a directory) and retries the removal operation, bringinglibc++builds to parity with MSVC STL builds.Verification
Added a new test in
test/parallel/test-fs-rm.jsthat makes files read-only usingfs.chmodSync(file, 0o444)(which sets the file attribute on Windows) and validates thatfs.rmSynccan delete them both directly and recursively as part of a directory.Fixes: #64374