Skip to content

Commit 9cb671f

Browse files
dario-piotrowiczaduh95
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 3fc630e commit 9cb671f

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
@@ -3239,6 +3239,38 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
32393239
}
32403240
}
32413241

3242+
static bool CopyUtimes(const std::filesystem::path& src,
3243+
const std::filesystem::path& dest,
3244+
Environment* env) {
3245+
uv_fs_t req;
3246+
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
3247+
3248+
auto src_path_str = PathToString(src);
3249+
int result = uv_fs_stat(nullptr, &req, src_path_str.c_str(), nullptr);
3250+
if (is_uv_error(result)) {
3251+
env->ThrowUVException(result, "stat", nullptr, src_path_str.c_str());
3252+
return false;
3253+
}
3254+
3255+
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
3256+
const double source_atime = s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
3257+
const double source_mtime = s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;
3258+
3259+
auto dest_file_path_str = PathToString(dest);
3260+
int utime_result = uv_fs_utime(nullptr,
3261+
&req,
3262+
dest_file_path_str.c_str(),
3263+
source_atime,
3264+
source_mtime,
3265+
nullptr);
3266+
if (is_uv_error(utime_result)) {
3267+
env->ThrowUVException(
3268+
utime_result, "utime", nullptr, dest_file_path_str.c_str());
3269+
return false;
3270+
}
3271+
return true;
3272+
}
3273+
32423274
static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
32433275
Environment* env = Environment::GetCurrent(args);
32443276
Isolate* isolate = env->isolate();
@@ -3286,22 +3318,7 @@ static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
32863318
}
32873319

32883320
if (preserve_timestamps) {
3289-
uv_fs_t req;
3290-
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
3291-
int result = uv_fs_stat(nullptr, &req, *src, nullptr);
3292-
if (is_uv_error(result)) {
3293-
return env->ThrowUVException(result, "stat", nullptr, *src);
3294-
}
3295-
3296-
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
3297-
const double source_atime = s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
3298-
const double source_mtime = s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;
3299-
3300-
int utime_result =
3301-
uv_fs_utime(nullptr, &req, *dest, source_atime, source_mtime, nullptr);
3302-
if (is_uv_error(utime_result)) {
3303-
return env->ThrowUVException(utime_result, "utime", nullptr, *dest);
3304-
}
3321+
CopyUtimes(*src, *dest, env);
33053322
}
33063323
}
33073324

@@ -3482,37 +3499,9 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
34823499
return false;
34833500
}
34843501

3485-
if (preserve_timestamps) {
3486-
uv_fs_t req;
3487-
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
3488-
3489-
auto dir_entry_path_str = PathToString(dir_entry.path());
3490-
int result =
3491-
uv_fs_stat(nullptr, &req, dir_entry_path_str.c_str(), nullptr);
3492-
if (is_uv_error(result)) {
3493-
env->ThrowUVException(
3494-
result, "stat", nullptr, dir_entry_path_str.c_str());
3495-
return false;
3496-
}
3497-
3498-
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
3499-
const double source_atime =
3500-
s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
3501-
const double source_mtime =
3502-
s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;
3503-
3504-
auto dest_file_path_str = PathToString(dest_file_path);
3505-
int utime_result = uv_fs_utime(nullptr,
3506-
&req,
3507-
dest_file_path_str.c_str(),
3508-
source_atime,
3509-
source_mtime,
3510-
nullptr);
3511-
if (is_uv_error(utime_result)) {
3512-
env->ThrowUVException(
3513-
utime_result, "utime", nullptr, dest_file_path_str.c_str());
3514-
return false;
3515-
}
3502+
if (preserve_timestamps &&
3503+
!CopyUtimes(dir_entry.path(), dest_file_path, env)) {
3504+
return false;
35163505
}
35173506
}
35183507
}

0 commit comments

Comments
 (0)