Skip to content

Commit 4347ce3

Browse files
dario-piotrowicztargos
authored andcommitted
src: add new CopyUtimes function to reduce code duplication
PR-URL: #58625 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 713fbad commit 4347ce3

File tree

1 file changed

+36
-47
lines changed

1 file changed

+36
-47
lines changed

src/node_file.cc

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,6 +3326,38 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
33263326
}
33273327
}
33283328

3329+
static bool CopyUtimes(const std::filesystem::path& src,
3330+
const std::filesystem::path& dest,
3331+
Environment* env) {
3332+
uv_fs_t req;
3333+
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
3334+
3335+
auto src_path_str = PathToString(src);
3336+
int result = uv_fs_stat(nullptr, &req, src_path_str.c_str(), nullptr);
3337+
if (is_uv_error(result)) {
3338+
env->ThrowUVException(result, "stat", nullptr, src_path_str.c_str());
3339+
return false;
3340+
}
3341+
3342+
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
3343+
const double source_atime = s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
3344+
const double source_mtime = s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;
3345+
3346+
auto dest_file_path_str = PathToString(dest);
3347+
int utime_result = uv_fs_utime(nullptr,
3348+
&req,
3349+
dest_file_path_str.c_str(),
3350+
source_atime,
3351+
source_mtime,
3352+
nullptr);
3353+
if (is_uv_error(utime_result)) {
3354+
env->ThrowUVException(
3355+
utime_result, "utime", nullptr, dest_file_path_str.c_str());
3356+
return false;
3357+
}
3358+
return true;
3359+
}
3360+
33293361
static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
33303362
Environment* env = Environment::GetCurrent(args);
33313363
Isolate* isolate = env->isolate();
@@ -3373,22 +3405,7 @@ static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
33733405
}
33743406

33753407
if (preserve_timestamps) {
3376-
uv_fs_t req;
3377-
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
3378-
int result = uv_fs_stat(nullptr, &req, *src, nullptr);
3379-
if (is_uv_error(result)) {
3380-
return env->ThrowUVException(result, "stat", nullptr, *src);
3381-
}
3382-
3383-
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
3384-
const double source_atime = s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
3385-
const double source_mtime = s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;
3386-
3387-
int utime_result =
3388-
uv_fs_utime(nullptr, &req, *dest, source_atime, source_mtime, nullptr);
3389-
if (is_uv_error(utime_result)) {
3390-
return env->ThrowUVException(utime_result, "utime", nullptr, *dest);
3391-
}
3408+
CopyUtimes(*src, *dest, env);
33923409
}
33933410
}
33943411

@@ -3569,37 +3586,9 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
35693586
return false;
35703587
}
35713588

3572-
if (preserve_timestamps) {
3573-
uv_fs_t req;
3574-
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
3575-
3576-
auto dir_entry_path_str = PathToString(dir_entry.path());
3577-
int result =
3578-
uv_fs_stat(nullptr, &req, dir_entry_path_str.c_str(), nullptr);
3579-
if (is_uv_error(result)) {
3580-
env->ThrowUVException(
3581-
result, "stat", nullptr, dir_entry_path_str.c_str());
3582-
return false;
3583-
}
3584-
3585-
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
3586-
const double source_atime =
3587-
s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
3588-
const double source_mtime =
3589-
s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;
3590-
3591-
auto dest_file_path_str = PathToString(dest_file_path);
3592-
int utime_result = uv_fs_utime(nullptr,
3593-
&req,
3594-
dest_file_path_str.c_str(),
3595-
source_atime,
3596-
source_mtime,
3597-
nullptr);
3598-
if (is_uv_error(utime_result)) {
3599-
env->ThrowUVException(
3600-
utime_result, "utime", nullptr, dest_file_path_str.c_str());
3601-
return false;
3602-
}
3589+
if (preserve_timestamps &&
3590+
!CopyUtimes(dir_entry.path(), dest_file_path, env)) {
3591+
return false;
36033592
}
36043593
}
36053594
}

0 commit comments

Comments
 (0)