From cb5ea14d13e25be568750182722be127c4d31d9b Mon Sep 17 00:00:00 2001 From: gwx1278443 Date: Fri, 13 Oct 2023 09:12:56 +0800 Subject: [PATCH] Move_Dir Signed-off-by: gwx1278443 --- .../kits/js/src/mod_fs/properties/movedir.cpp | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 6f845c17d..e72578d82 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -97,6 +97,41 @@ static tuple, unique_ptr, int> ParseJsOperand(n return { true, move(src), move(dest), mode }; } +static int MoveDirUtime(const string &srcPath, const string &destPath) +{ + if (filesystem::is_directory(srcPath)) { + std::error_code errCode; + if (!filesystem::create_directory(destPath, errCode)) { + HILOGE("Failed to create directory, error code: %{public}d", errCode.value()); + } + } + std::unique_ptr utime_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!utime_req) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + std::unique_ptr stat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!stat_req) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int ret = uv_fs_stat(nullptr, stat_req.get(), destPath.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat srcPath"); + } + double atime = static_cast(stat_req->statbuf.st_atim.tv_sec) + + static_cast(stat_req->statbuf.st_atim.tv_nsec) / NS; + double mtime = static_cast(stat_req->statbuf.st_mtim.tv_sec) + + static_cast(stat_req->statbuf.st_mtim.tv_nsec) / NS; + ret = uv_fs_utime(nullptr, utime_req.get(), destPath.c_str(), atime, mtime, nullptr); + if (ret < 0) { + HILOGE("Failed to utime %s, error code: %d", destPath.c_str(), ret); + } + return ret; +} + static int CopyAndDeleteFile(const string &src, const string &dest) { std::unique_ptr stat_req = { @@ -124,18 +159,8 @@ static int CopyAndDeleteFile(const string &src, const string &dest) HILOGE("Failed to copy file, error code: %{public}d", errCode.value()); return errCode.value(); } - std::unique_ptr utime_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!utime_req) { - HILOGE("Failed to request heap memory."); - return ENOMEM; - } - double atime = static_cast(stat_req->statbuf.st_atim.tv_sec) + - static_cast(stat_req->statbuf.st_atim.tv_nsec) / NS; - double mtime = static_cast(stat_req->statbuf.st_mtim.tv_sec) + - static_cast(stat_req->statbuf.st_mtim.tv_nsec) / NS; - ret = uv_fs_utime(nullptr, utime_req.get(), dstPath.c_str(), atime, mtime, nullptr); - if (ret < 0) { + ret = MoveDirUtime(srcPath, dstPath); + if (ret < 0 || ret == ENOMEM) { HILOGE("Failed to utime dstPath"); return ret; } @@ -178,11 +203,10 @@ static int RenameDir(const string &src, const string &dest, const int mode, vect filesystem::rename(srcPath, destPath, errCode); if (errCode.value() == EXDEV) { HILOGE("Failed to rename file due to EXDEV"); - if (filesystem::create_directory(destPath, errCode)) { - return RecurMoveDir(src, dest, mode, errfiles); - } else { - HILOGE("Failed to create directory, error code: %{public}d", errCode.value()); - return errCode.value(); + int ret = (MoveDirUtime(srcPath, destPath)); + if (ret < 0 || ret == ENOMEM) { + HILOGE("Failed to utime dstPath"); + return ret; } } if (errCode.value() != 0) { -- Gitee