mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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: https://github.com/nodejs/node/issues/56049 PR-URL: https://github.com/nodejs/node/pull/61108 Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
@@ -1634,7 +1634,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
auto file_path = std::filesystem::path(path.ToStringView());
|
||||
auto file_path = path.ToPath();
|
||||
std::error_code error;
|
||||
auto file_status = std::filesystem::symlink_status(file_path, error);
|
||||
|
||||
@@ -1649,8 +1649,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
|
||||
// File is a directory and recursive is false
|
||||
if (file_status.type() == std::filesystem::file_type::directory &&
|
||||
!recursive) {
|
||||
return THROW_ERR_FS_EISDIR(
|
||||
isolate, "Path is a directory: %s", file_path.c_str());
|
||||
return THROW_ERR_FS_EISDIR(isolate, "Path is a directory: %s", path);
|
||||
}
|
||||
|
||||
// Allowed errors are:
|
||||
|
||||
32
test/parallel/test-fs-rmSync-special-char.js
Executable file
32
test/parallel/test-fs-rmSync-special-char.js
Executable file
@@ -0,0 +1,32 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const assert = require('node:assert');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
// This test ensures that fs.rmSync handles non-ASCII characters in file paths,
|
||||
// and that errors contain correctly encoded paths and err.path values.
|
||||
|
||||
tmpdir.refresh(); // Prepare a clean temporary directory
|
||||
|
||||
// Define paths with non-ASCII characters
|
||||
const dirPath = path.join(tmpdir.path, '速_dir');
|
||||
const filePath = path.join(tmpdir.path, '速.txt');
|
||||
|
||||
// Create a directory and a file with non-ASCII characters
|
||||
fs.mkdirSync(dirPath);
|
||||
fs.writeFileSync(filePath, 'This is a test file with special characters.');
|
||||
fs.rmSync(filePath);
|
||||
assert.strictEqual(fs.existsSync(filePath), false);
|
||||
|
||||
// Ensure rmSync throws an error when trying to remove a directory without recursive
|
||||
assert.throws(() => {
|
||||
fs.rmSync(dirPath, { recursive: false });
|
||||
}, (err) => {
|
||||
// Assert the error code and check that the error message includes the correct non-ASCII path
|
||||
assert.strictEqual(err.code, 'ERR_FS_EISDIR');
|
||||
assert(err.message.includes(dirPath), 'Error message should include the directory path');
|
||||
assert.strictEqual(err.path, dirPath);
|
||||
return true;
|
||||
});
|
||||
Reference in New Issue
Block a user