Skip to content

fs: improve cpSync dest overriding performance #58160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,12 @@ function getStats(src, dest, opts) {

function onFile(srcStat, destStat, src, dest, opts) {
if (!destStat) return copyFile(srcStat, src, dest, opts);
return mayCopyFile(srcStat, src, dest, opts);
}

// TODO(@anonrig): Move this function to C++.
function mayCopyFile(srcStat, src, dest, opts) {
if (opts.force) {
unlinkSync(dest);
return copyFile(srcStat, src, dest, opts);
} else if (opts.errorOnExist) {
return fsBinding.cpSyncOverrideFile(src, dest, opts.mode, opts.preserveTimestamps);
}

if (opts.errorOnExist) {
throw new ERR_FS_CP_EEXIST({
message: `${dest} already exists`,
path: dest,
Expand Down
7 changes: 7 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,13 @@ inline void Environment::ThrowError(
isolate()->ThrowException(fun(OneByteString(isolate(), errmsg), {}));
}

inline void Environment::ThrowStdErrException(std::error_code error_code,
const char* syscall,
const char* path) {
ThrowErrnoException(
error_code.value(), syscall, error_code.message().c_str(), path);
}

inline void Environment::ThrowErrnoException(int errorno,
const char* syscall,
const char* message,
Expand Down
3 changes: 3 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,9 @@ class Environment final : public MemoryRetainer {
inline void ThrowError(const char* errmsg);
inline void ThrowTypeError(const char* errmsg);
inline void ThrowRangeError(const char* errmsg);
inline void ThrowStdErrException(std::error_code error_code,
const char* syscall,
const char* path = nullptr);
inline void ThrowErrnoException(int errorno,
const char* syscall = nullptr,
const char* message = nullptr,
Expand Down
69 changes: 69 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "uv.h"
#include "v8-fast-api-calls.h"

#include <cstdio>
#include <filesystem>

#if defined(__MINGW32__) || defined(_MSC_VER)
Expand Down Expand Up @@ -3350,6 +3351,72 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
}
}

static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();

CHECK_EQ(args.Length(), 4); // src, dest, mode, preserveTimestamps

BufferValue src(isolate, args[0]);
CHECK_NOT_NULL(*src);
ToNamespacedPath(env, &src);

BufferValue dest(isolate, args[1]);
CHECK_NOT_NULL(*dest);
ToNamespacedPath(env, &dest);

int mode;
if (!GetValidFileMode(env, args[2], UV_FS_COPYFILE).To(&mode)) {
return;
}

bool preserve_timestamps = args[3]->IsTrue();

THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, src.ToStringView());
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView());

std::error_code error;

if (!std::filesystem::remove(*dest, error)) {
return env->ThrowStdErrException(error, "unlink", *dest);
}

if (mode == 0) {
// if no mode is specified use the faster std::filesystem API
if (!std::filesystem::copy_file(*src, *dest, error)) {
return env->ThrowStdErrException(error, "cp", *dest);
}
} else {
uv_fs_t req;
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
auto result = uv_fs_copyfile(nullptr, &req, *src, *dest, mode, nullptr);
if (is_uv_error(result)) {
return env->ThrowUVException(result, "cp", nullptr, *src, *dest);
}
}

if (preserve_timestamps) {
uv_fs_t req;
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
int result = uv_fs_stat(nullptr, &req, *src, nullptr);
if (is_uv_error(result)) {
return env->ThrowUVException(result, "stat", nullptr, *src);
}

const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
const double source_atime = s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
const double source_mtime = s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;

int utime_result =
uv_fs_utime(nullptr, &req, *dest, source_atime, source_mtime, nullptr);
if (is_uv_error(utime_result)) {
return env->ThrowUVException(utime_result, "utime", nullptr, *dest);
}
}
}

BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(
Environment* env, const std::string& file_path) {
THROW_IF_INSUFFICIENT_PERMISSIONS(
Expand Down Expand Up @@ -3689,6 +3756,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, target, "mkdtemp", Mkdtemp);

SetMethod(isolate, target, "cpSyncCheckPaths", CpSyncCheckPaths);
SetMethod(isolate, target, "cpSyncOverrideFile", CpSyncOverrideFile);

StatWatcher::CreatePerIsolateProperties(isolate_data, target);
BindingData::CreatePerIsolateProperties(isolate_data, target);
Expand Down Expand Up @@ -3801,6 +3869,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(CopyFile);

registry->Register(CpSyncCheckPaths);
registry->Register(CpSyncOverrideFile);

registry->Register(Chmod);
registry->Register(FChmod);
Expand Down
2 changes: 2 additions & 0 deletions typings/internalBinding/fs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ declare namespace InternalFSBinding {
function copyFile(src: StringOrBuffer, dest: StringOrBuffer, mode: number, usePromises: typeof kUsePromises): Promise<void>;

function cpSyncCheckPaths(src: StringOrBuffer, dest: StringOrBuffer, dereference: boolean, recursive: boolean): void;
function cpSyncOverrideFile(src: StringOrBuffer, dest: StringOrBuffer, mode: number, preserveTimestamps: boolean): void;

function fchmod(fd: number, mode: number, req: FSReqCallback): void;
function fchmod(fd: number, mode: number): void;
Expand Down Expand Up @@ -260,6 +261,7 @@ export interface FsBinding {
close: typeof InternalFSBinding.close;
copyFile: typeof InternalFSBinding.copyFile;
cpSyncCheckPaths: typeof InternalFSBinding.cpSyncCheckPaths;
cpSyncOverrideFile: typeof InternalFSBinding.cpSyncOverrideFile;
fchmod: typeof InternalFSBinding.fchmod;
fchown: typeof InternalFSBinding.fchown;
fdatasync: typeof InternalFSBinding.fdatasync;
Expand Down
Loading