Repository: nodejs/node
File: src/node_file.cc (C++)

Code before:

  CHECK_EQ(args.Length(), 4);  // path, maxRetries, recursive, retryDelay

  BufferValue path(isolate, args[0]);
  CHECK_NOT_NULL(*path);
  ToNamespacedPath(env, &path);
  THROW_IF_INSUFFICIENT_PERMISSIONS(
      env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
  auto file_path = path.ToPath();
  std::error_code error;
  auto file_status = std::filesystem::status(file_path, error);

  if (file_status.type() == std::filesystem::file_type::not_found) {
    return;
  }

  int maxRetries = args[1].As<Int32>()->Value();
  int recursive = args[2]->IsTrue();
  int retryDelay = args[3].As<Int32>()->Value();

  // File is a directory and recursive is false
  if (file_status.type() == std::filesystem::file_type::directory &&
      !recursive) {
    auto file_path_str = ConvertPathToUTF8(file_path);
    return THROW_ERR_FS_EISDIR(
        isolate, "Path is a directory: %s", file_path_str);
  }

  // Allowed errors are:
  // - EBUSY: std::errc::device_or_resource_busy
  // - EMFILE: std::errc::too_many_files_open
  // - ENFILE: std::errc::too_many_files_open_in_system
  // - ENOTEMPTY: std::errc::directory_not_empty
  // - EPERM: std::errc::operation_not_permitted
  auto can_omit_error = [](std::error_code error) -> bool {
    return (error == std::errc::device_or_resource_busy ||
            error == std::errc::too_many_files_open ||
            error == std::errc::too_many_files_open_in_system ||
            error == std::errc::directory_not_empty ||
            error == std::errc::operation_not_permitted);
  };

  int i = 1;

  while (maxRetries >= 0) {
    if (recursive) {
      std::filesystem::remove_all(file_path, error);
    } else {
      std::filesystem::remove(file_path, error);
    }


Review comment (suggestion):
```suggestion
        isolate, "Path is a directory: %s", *path);
```

This is now always assumed to contain UTF8 data.

Code after:

  CHECK_EQ(args.Length(), 4);  // path, maxRetries, recursive, retryDelay

  BufferValue path(isolate, args[0]);
  CHECK_NOT_NULL(*path);
  ToNamespacedPath(env, &path);
  THROW_IF_INSUFFICIENT_PERMISSIONS(
      env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
  auto file_path = path.ToPath();
  std::error_code error;
  auto file_status = std::filesystem::status(file_path, error);

  if (file_status.type() == std::filesystem::file_type::not_found) {
    return;
  }

  int maxRetries = args[1].As<Int32>()->Value();
  int recursive = args[2]->IsTrue();
  int retryDelay = args[3].As<Int32>()->Value();

  // 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", path);
  }

  // Allowed errors are:
  // - EBUSY: std::errc::device_or_resource_busy
  // - EMFILE: std::errc::too_many_files_open
  // - ENFILE: std::errc::too_many_files_open_in_system
  // - ENOTEMPTY: std::errc::directory_not_empty
  // - EPERM: std::errc::operation_not_permitted
  auto can_omit_error = [](std::error_code error) -> bool {
    return (error == std::errc::device_or_resource_busy ||
            error == std::errc::too_many_files_open ||
            error == std::errc::too_many_files_open_in_system ||
            error == std::errc::directory_not_empty ||
            error == std::errc::operation_not_permitted);
  };

  int i = 1;

  while (maxRetries >= 0) {
    if (recursive) {
      std::filesystem::remove_all(file_path, error);
    } else {
      std::filesystem::remove(file_path, error);
    }

    if (!error || error == std::errc::no_such_file_or_directory) {
      return;