Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1789,6 +1824,9 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
};

int i = 1;
#ifdef _WIN32
bool cleared_readonly = false;
#endif

while (maxRetries >= 0) {
if (recursive) {
Expand All @@ -1797,6 +1835,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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This block sits inside the retry loop, so ClearReadOnlyAttributeW runs again on every retry. Clearing the read-only attribute is deterministic - if the first call didn't make the file deletable, repeating it won't either. If EPERM persists after clearing, the cause is transient, and the existing remove/remove_all retry already covers that. I'd suggest running the clear only once rather than on every retry.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. Added a cleared_readonly flag to ensure ClearReadOnlyAttributeW is only executed once and not repeated on subsequent retries.

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)) {
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}