fs: support removing read-only files in rmSync on Windows

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 fs.rmSync to fail with EPERM 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 which clears the FILE_ATTRIBUTE_READONLY
attribute recursively (or for a single file) when
operation_not_permitted is returned, allowing rmSync to
successfully delete read-only files/folders.

Fixes: https://github.com/nodejs/node/issues/64374

Signed-off-by: SparshGarg999 <sparshgarg999@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64453
Fixes: https://github.com/nodejs/node/issues/64374
Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com>
This commit is contained in:
Sparsh :) authored and Antoine du Hamel committed 2026-09-22 16:55:41 +02:00
1 parent ac37bbe109
commit cd1011458f
2 files changed
+81

No files matched your search

+54
View File
@@ -1745,6 +1745,41 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
}
}
#ifdef _WIN32
static void ClearReadOnlyAttributeWHelper(const wchar_t* path) {
DWORD attrs = GetFileAttributesW(path);
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_READONLY)) {
SetFileAttributesW(path, attrs & ~FILE_ATTRIBUTE_READONLY);
}
}
static void ClearReadOnlyAttributeW(const std::filesystem::path& path,
bool recursive) {
std::error_code ec;
auto file_status = std::filesystem::symlink_status(path, ec);
if (ec) return;
if (recursive &&
file_status.type() == std::filesystem::file_type::directory) {
for (const auto& entry : std::filesystem::recursive_directory_iterator(
path,
std::filesystem::directory_options::skip_permission_denied,
ec)) {
std::error_code entry_ec;
auto entry_status = entry.symlink_status(entry_ec);
if (entry_ec) continue;
if (entry_status.type() != std::filesystem::file_type::symlink) {
ClearReadOnlyAttributeWHelper(entry.path().c_str());
}
}
}
if (file_status.type() != std::filesystem::file_type::symlink) {
ClearReadOnlyAttributeWHelper(path.c_str());
}
}
#endif
static void RmSync(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
@@ -1792,6 +1827,9 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
};
int i = 1;
#ifdef _WIN32
bool cleared_readonly = false;
#endif
while (maxRetries >= 0) {
if (recursive) {
@@ -1800,6 +1838,22 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
std::filesystem::remove(file_path, error);
}
#ifdef _WIN32
// On Windows, libc++ does not clear the read-only attribute before
// removing a file (unlike MSVC STL which does). Attempt to clear it
// manually when we get EPERM (operation_not_permitted) so that read-only
// files can be deleted, matching the behavior of official Node.js builds.
if (error == std::errc::operation_not_permitted && !cleared_readonly) {
cleared_readonly = true;
ClearReadOnlyAttributeW(file_path, recursive);
if (recursive) {
std::filesystem::remove_all(file_path, error);
} else {
std::filesystem::remove(file_path, error);
}
}
#endif // _WIN32
if (!error || error == std::errc::no_such_file_or_directory) {
return;
} else if (!can_omit_error(error)) {
+27
View File
@@ -631,3 +631,30 @@ if (isGitPresent) {
}
}
}
{
// Test that rmSync can delete read-only files (and directories containing read-only files recursively)
const dirname = nextDirPath();
const filePath = path.join(dirname, 'readonly-file.txt');
const recursiveDir = path.join(dirname, 'subdir');
const recursiveFilePath = path.join(recursiveDir, 'readonly-nested.txt');
fs.mkdirSync(recursiveDir, { recursive: true });
fs.writeFileSync(filePath, 'hello');
fs.writeFileSync(recursiveFilePath, 'world');
// Make files read-only
fs.chmodSync(filePath, 0o444);
fs.chmodSync(recursiveFilePath, 0o444);
// rmSync without recursive option on a file
fs.rmSync(filePath);
assert.strictEqual(fs.existsSync(filePath), false);
// rmSync with recursive option on a directory containing a read-only file
fs.rmSync(recursiveDir, { recursive: true });
assert.strictEqual(fs.existsSync(recursiveDir), false);
// Clean up parent directory
fs.rmSync(dirname, { recursive: true, force: true });
}