mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
fs: move ToNamespacedPath to c++
Co-Authored-By: Daniel Lemire <daniel@lemire.me> PR-URL: https://github.com/nodejs/node/pull/52135 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
2eff28fb7a
commit
399eb338f1
10 files changed
+303
-231
No files matched your search
@@ -8,6 +8,7 @@
|
||||
#include "node_errors.h"
|
||||
#include "node_external_reference.h"
|
||||
#include "node_file.h"
|
||||
#include "path.h"
|
||||
#include "permission/permission.h"
|
||||
#include "util.h"
|
||||
#include "v8.h"
|
||||
@@ -96,6 +97,7 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
|
||||
auto entry = DataQueue::CreateFdEntry(env, args[0]);
|
||||
|
||||
+37
-5
@@ -30,6 +30,7 @@
|
||||
#include "node_process-inl.h"
|
||||
#include "node_stat_watcher.h"
|
||||
#include "node_url.h"
|
||||
#include "path.h"
|
||||
#include "permission/permission.h"
|
||||
#include "util-inl.h"
|
||||
|
||||
@@ -825,7 +826,6 @@ void AfterMkdirp(uv_fs_t* req) {
|
||||
std::string first_path(req_wrap->continuation_data()->first_path());
|
||||
if (first_path.empty())
|
||||
return req_wrap->Resolve(Undefined(req_wrap->env()->isolate()));
|
||||
node::url::FromNamespacedPath(&first_path);
|
||||
Local<Value> path;
|
||||
Local<Value> error;
|
||||
if (!StringBytes::Encode(req_wrap->env()->isolate(), first_path.c_str(),
|
||||
@@ -942,6 +942,7 @@ void Access(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
if (argc > 2) { // access(path, mode, req)
|
||||
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
|
||||
@@ -998,6 +999,7 @@ static void ExistsSync(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
|
||||
|
||||
@@ -1028,7 +1030,9 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
CHECK(args[0]->IsString());
|
||||
node::Utf8Value path(env->isolate(), args[0]);
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
|
||||
|
||||
@@ -1084,6 +1088,7 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(realm->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
bool use_bigint = args[1]->IsTrue();
|
||||
if (!args[2]->IsUndefined()) { // stat(path, use_bigint, req)
|
||||
@@ -1131,6 +1136,7 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(realm->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
bool use_bigint = args[1]->IsTrue();
|
||||
if (!args[2]->IsUndefined()) { // lstat(path, use_bigint, req)
|
||||
@@ -1210,6 +1216,7 @@ static void StatFs(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(realm->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
bool use_bigint = args[1]->IsTrue();
|
||||
if (argc > 2) { // statfs(path, use_bigint, req)
|
||||
@@ -1268,6 +1275,7 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(isolate, args[1]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
|
||||
@@ -1302,11 +1310,14 @@ static void Link(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue src(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*src);
|
||||
ToNamespacedPath(env, &src);
|
||||
|
||||
const auto src_view = src.ToStringView();
|
||||
|
||||
BufferValue dest(isolate, args[1]);
|
||||
CHECK_NOT_NULL(*dest);
|
||||
ToNamespacedPath(env, &dest);
|
||||
|
||||
const auto dest_view = dest.ToStringView();
|
||||
|
||||
if (argc > 2) { // link(src, dest, req)
|
||||
@@ -1361,6 +1372,7 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
|
||||
|
||||
@@ -1406,10 +1418,12 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue old_path(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*old_path);
|
||||
ToNamespacedPath(env, &old_path);
|
||||
auto view_old_path = old_path.ToStringView();
|
||||
|
||||
BufferValue new_path(isolate, args[1]);
|
||||
CHECK_NOT_NULL(*new_path);
|
||||
ToNamespacedPath(env, &new_path);
|
||||
|
||||
if (argc > 2) { // rename(old_path, new_path, req)
|
||||
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
|
||||
@@ -1539,6 +1553,7 @@ static void Unlink(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
if (argc > 1) { // unlink(path, req)
|
||||
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
|
||||
@@ -1572,6 +1587,7 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
|
||||
@@ -1760,6 +1776,8 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
|
||||
@@ -1790,7 +1808,6 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
|
||||
if (!req_wrap_sync.continuation_data()->first_path().empty()) {
|
||||
Local<Value> error;
|
||||
std::string first_path(req_wrap_sync.continuation_data()->first_path());
|
||||
node::url::FromNamespacedPath(&first_path);
|
||||
MaybeLocal<Value> path = StringBytes::Encode(env->isolate(),
|
||||
first_path.c_str(),
|
||||
UTF8, &error);
|
||||
@@ -1816,6 +1833,7 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
|
||||
|
||||
@@ -1860,6 +1878,7 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
|
||||
|
||||
@@ -2014,6 +2033,7 @@ static void Open(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
CHECK(args[1]->IsInt32());
|
||||
const int flags = args[1].As<Int32>()->Value();
|
||||
@@ -2054,6 +2074,7 @@ static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(realm->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
CHECK(args[1]->IsInt32());
|
||||
const int flags = args[1].As<Int32>()->Value();
|
||||
@@ -2099,9 +2120,11 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue src(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*src);
|
||||
ToNamespacedPath(env, &src);
|
||||
|
||||
BufferValue dest(isolate, args[1]);
|
||||
CHECK_NOT_NULL(*dest);
|
||||
ToNamespacedPath(env, &dest);
|
||||
|
||||
if (argc > 3) { // copyFile(src, dest, flags, req)
|
||||
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
|
||||
@@ -2381,6 +2404,7 @@ static void WriteFileUtf8(const FunctionCallbackInfo<Value>& args) {
|
||||
} else {
|
||||
BufferValue path(isolate, args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
if (CheckOpenPermissions(env, path, flags).IsNothing()) return;
|
||||
|
||||
FSReqWrapSync req_open("open", *path);
|
||||
@@ -2516,6 +2540,7 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
|
||||
} else {
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
if (CheckOpenPermissions(env, path, flags).IsNothing()) return;
|
||||
|
||||
FS_SYNC_TRACE_BEGIN(open);
|
||||
@@ -2524,7 +2549,8 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
|
||||
if (req.result < 0) {
|
||||
uv_fs_req_cleanup(&req);
|
||||
// req will be cleaned up by scope leave.
|
||||
return env->ThrowUVException(req.result, "open", nullptr, path.out());
|
||||
return env->ThrowUVException(
|
||||
static_cast<int>(req.result), "open", nullptr, path.out());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2547,7 +2573,8 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
|
||||
if (req.result < 0) {
|
||||
FS_SYNC_TRACE_END(read);
|
||||
// req will be cleaned up by scope leave.
|
||||
return env->ThrowUVException(req.result, "read", nullptr);
|
||||
return env->ThrowUVException(
|
||||
static_cast<int>(req.result), "read", nullptr);
|
||||
}
|
||||
if (r <= 0) {
|
||||
break;
|
||||
@@ -2620,6 +2647,7 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
|
||||
@@ -2682,6 +2710,7 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
CHECK(IsSafeJsInt(args[1]));
|
||||
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
|
||||
@@ -2756,6 +2785,7 @@ static void LChown(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
|
||||
CHECK(IsSafeJsInt(args[1]));
|
||||
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
|
||||
@@ -2795,6 +2825,7 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
|
||||
@@ -2858,6 +2889,7 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
ToNamespacedPath(env, &path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "node_i18n.h"
|
||||
#include "node_metadata.h"
|
||||
#include "node_process-inl.h"
|
||||
#include "path.h"
|
||||
#include "util-inl.h"
|
||||
#include "v8-fast-api-calls.h"
|
||||
#include "v8.h"
|
||||
|
||||
+49
-2
@@ -3,7 +3,6 @@
|
||||
#include <vector>
|
||||
#include "env-inl.h"
|
||||
#include "node_internals.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace node {
|
||||
|
||||
@@ -89,7 +88,7 @@ std::string NormalizeString(const std::string_view path,
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
bool IsWindowsDeviceRoot(const char c) noexcept {
|
||||
constexpr bool IsWindowsDeviceRoot(const char c) noexcept {
|
||||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
||||
}
|
||||
|
||||
@@ -267,4 +266,52 @@ std::string PathResolve(Environment* env,
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
void ToNamespacedPath(Environment* env, BufferValue* path) {
|
||||
#ifdef _WIN32
|
||||
if (path->length() == 0) return;
|
||||
auto resolved_path = node::PathResolve(env, {path->ToStringView()});
|
||||
if (resolved_path.size() <= 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
// SAFETY: We know that resolved_path.size() > 2, therefore accessing [0],
|
||||
// [1], and [2] is safe.
|
||||
if (resolved_path[0] == '\\') {
|
||||
// Possible UNC root
|
||||
if (resolved_path[1] == '\\') {
|
||||
if (resolved_path[2] != '?' && resolved_path[2] != '.') {
|
||||
// Matched non-long UNC root, convert the path to a long UNC path
|
||||
std::string_view unc_prefix = R"(\\?\UNC\)";
|
||||
std::string_view resolved_path2 = resolved_path.substr(2);
|
||||
size_t new_length = unc_prefix.size() + resolved_path2.size();
|
||||
path->AllocateSufficientStorage(new_length + 1);
|
||||
path->SetLength(new_length);
|
||||
memcpy(path->out(), unc_prefix.data(), unc_prefix.size());
|
||||
memcpy(path->out() + unc_prefix.size(),
|
||||
resolved_path.c_str() + 2,
|
||||
resolved_path2.size() + 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (IsWindowsDeviceRoot(resolved_path[0]) && resolved_path[1] == ':' &&
|
||||
resolved_path[2] == '\\') {
|
||||
// Matched device root, convert the path to a long UNC path
|
||||
std::string_view new_prefix = R"(\\?\)";
|
||||
size_t new_length = new_prefix.size() + resolved_path.size();
|
||||
path->AllocateSufficientStorage(new_length + 1);
|
||||
path->SetLength(new_length);
|
||||
memcpy(path->out(), new_prefix.data(), new_prefix.size());
|
||||
memcpy(path->out() + new_prefix.size(),
|
||||
resolved_path.c_str(),
|
||||
resolved_path.size() + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t new_length = resolved_path.size();
|
||||
path->AllocateSufficientStorage(new_length + 1);
|
||||
path->SetLength(new_length);
|
||||
memcpy(path->out(), resolved_path.c_str(), resolved_path.size() + 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
+10
-3
@@ -5,11 +5,11 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "node_options-inl.h"
|
||||
#include "util-inl.h"
|
||||
|
||||
namespace node {
|
||||
|
||||
class Environment;
|
||||
|
||||
bool IsPathSeparator(const char c) noexcept;
|
||||
|
||||
std::string NormalizeString(const std::string_view path,
|
||||
@@ -17,7 +17,14 @@ std::string NormalizeString(const std::string_view path,
|
||||
const std::string_view separator);
|
||||
|
||||
std::string PathResolve(Environment* env,
|
||||
const std::vector<std::string_view>& args);
|
||||
const std::vector<std::string_view>& paths);
|
||||
|
||||
#ifdef _WIN32
|
||||
constexpr bool IsWindowsDeviceRoot(const char c) noexcept;
|
||||
#endif // _WIN32
|
||||
|
||||
void ToNamespacedPath(Environment* env, BufferValue* path);
|
||||
|
||||
} // namespace node
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
Reference in new issue
Block a user