mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
src: add new CopyUtimes function to reduce code duplication
PR-URL: https://github.com/nodejs/node/pull/58625 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
parent
fe4aa9c502
commit
bce60abdb1
1 changed files with 36 additions and 47 deletions
|
@ -3326,6 +3326,38 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
|
|||
}
|
||||
}
|
||||
|
||||
static bool CopyUtimes(const std::filesystem::path& src,
|
||||
const std::filesystem::path& dest,
|
||||
Environment* env) {
|
||||
uv_fs_t req;
|
||||
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
|
||||
|
||||
auto src_path_str = PathToString(src);
|
||||
int result = uv_fs_stat(nullptr, &req, src_path_str.c_str(), nullptr);
|
||||
if (is_uv_error(result)) {
|
||||
env->ThrowUVException(result, "stat", nullptr, src_path_str.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
auto dest_file_path_str = PathToString(dest);
|
||||
int utime_result = uv_fs_utime(nullptr,
|
||||
&req,
|
||||
dest_file_path_str.c_str(),
|
||||
source_atime,
|
||||
source_mtime,
|
||||
nullptr);
|
||||
if (is_uv_error(utime_result)) {
|
||||
env->ThrowUVException(
|
||||
utime_result, "utime", nullptr, dest_file_path_str.c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
Isolate* isolate = env->isolate();
|
||||
|
@ -3373,22 +3405,7 @@ static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
CopyUtimes(*src, *dest, env);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3569,37 +3586,9 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
|
|||
return false;
|
||||
}
|
||||
|
||||
if (preserve_timestamps) {
|
||||
uv_fs_t req;
|
||||
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
|
||||
|
||||
auto dir_entry_path_str = PathToString(dir_entry.path());
|
||||
int result =
|
||||
uv_fs_stat(nullptr, &req, dir_entry_path_str.c_str(), nullptr);
|
||||
if (is_uv_error(result)) {
|
||||
env->ThrowUVException(
|
||||
result, "stat", nullptr, dir_entry_path_str.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
auto dest_file_path_str = PathToString(dest_file_path);
|
||||
int utime_result = uv_fs_utime(nullptr,
|
||||
&req,
|
||||
dest_file_path_str.c_str(),
|
||||
source_atime,
|
||||
source_mtime,
|
||||
nullptr);
|
||||
if (is_uv_error(utime_result)) {
|
||||
env->ThrowUVException(
|
||||
utime_result, "utime", nullptr, dest_file_path_str.c_str());
|
||||
return false;
|
||||
}
|
||||
if (preserve_timestamps &&
|
||||
!CopyUtimes(dir_entry.path(), dest_file_path, env)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue