Skip to content

Commit a2fd236

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 5ef4985 commit a2fd236

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/node_file.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
16251625
ToNamespacedPath(env, &path);
16261626
THROW_IF_INSUFFICIENT_PERMISSIONS(
16271627
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
1628-
auto file_path = std::filesystem::path(path.ToStringView());
1628+
auto file_path = std::filesystem::path(path.ToU8StringView());
16291629
std::error_code error;
16301630
auto file_status = std::filesystem::status(file_path, error);
16311631

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const assert = require('assert');
6+
7+
// Specify the path for the test file
8+
const filePath = path.join(__dirname, '速.txt');
9+
10+
// Create a file with the specified name
11+
fs.writeFileSync(filePath, 'This is a test file containing special characters in the name.');
12+
13+
// Function to try removing the file and verify the outcome
14+
function testRemoveFile() {
15+
try {
16+
// Attempt to remove the file
17+
fs.rmSync(filePath);
18+
19+
// Check if the file still exists to confirm it was removed
20+
assert.ok(!fs.existsSync(filePath), 'The file should be removed.');
21+
22+
console.log('File removed successfully.');
23+
} catch (error) {
24+
// Output an error if the removal fails
25+
console.error('Failed to remove file:', error);
26+
27+
// Explicitly fail the test if an error is thrown
28+
assert.fail('File removal should not throw an error.');
29+
}
30+
}
31+
32+
// Run the test function
33+
testRemoveFile();

0 commit comments

Comments
 (0)