Skip to content

Commit 0b11867

Browse files
committed
fs: fix rmSync to handle non-ASCII characters
Update fs.rmSync to properly handle file paths that include non-ASCII characters. This change prevents crashes and errors when attempting to delete files with international or special characters in their names. Add a test in test/parallel to ensure that files with non-ASCII characters can be deleted without issues. This covers cases that previously caused unexpected behavior or crashes on certain file systems. Fixes: #56049
1 parent b1c01fc commit 0b11867

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/node_file.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
16341634
ToNamespacedPath(env, &path);
16351635
THROW_IF_INSUFFICIENT_PERMISSIONS(
16361636
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
1637-
auto file_path = std::filesystem::path(path.ToStringView());
1637+
auto file_path = path.ToPath();
16381638
std::error_code error;
16391639
auto file_status = std::filesystem::status(file_path, error);
16401640

@@ -1650,7 +1650,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
16501650
if (file_status.type() == std::filesystem::file_type::directory &&
16511651
!recursive) {
16521652
return THROW_ERR_FS_EISDIR(
1653-
isolate, "Path is a directory: %s", file_path.c_str());
1653+
isolate, "Path is a directory: %s", *path);
16541654
}
16551655

16561656
// Allowed errors are:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
const assert = require('node:assert');
5+
const fs = require('node:fs');
6+
const path = require('node:path');
7+
8+
// This test ensures that fs.rmSync handles non-ASCII characters in file paths,
9+
// and that errors contain correctly encoded paths and err.path values.
10+
11+
tmpdir.refresh(); // Prepare a clean temporary directory
12+
13+
// Define paths with non-ASCII characters
14+
const dirPath = path.join(tmpdir.path, '速_dir');
15+
const filePath = path.join(tmpdir.path, '速.txt');
16+
17+
// Create a directory and a file with non-ASCII characters
18+
fs.mkdirSync(dirPath);
19+
fs.writeFileSync(filePath, 'This is a test file with special characters.');
20+
fs.rmSync(filePath);
21+
assert.strictEqual(fs.existsSync(filePath), false);
22+
23+
// Ensure rmSync throws an error when trying to remove a directory without recursive
24+
assert.throws(() => {
25+
fs.rmSync(dirPath, { recursive: false });
26+
}, (err) => {
27+
// Assert the error code and check that the error message includes the correct non-ASCII path
28+
assert.strictEqual(err.code, 'ERR_FS_EISDIR');
29+
assert(err.message.includes(dirPath), 'Error message should include the directory path');
30+
assert.strictEqual(err.path, dirPath);
31+
return true;
32+
});

0 commit comments

Comments
 (0)