From 99117ceaee8baf3bda6052a259d5c40feeb91513 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Fri, 15 Sep 2023 17:06:25 +0800 Subject: [PATCH] =?UTF-8?q?move=E6=93=8D=E4=BD=9C=E4=B8=8D=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=9C=80=E8=BF=91=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4?= =?UTF-8?q?=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I3f8faf6c6cfc43c6cbc0fbea5af4721e8c337cd2 --- .../kits/js/src/mod_fs/properties/move.cpp | 32 +++++++++++++++++-- .../kits/js/src/mod_fs/properties/move.h | 1 + .../kits/js/src/mod_fs/properties/movedir.cpp | 26 +++++++++++++++ .../kits/js/src/mod_fs/properties/movedir.h | 2 ++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index 0dc32fcf6..399d7339a 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -81,9 +81,37 @@ static tuple, unique_ptr, int> ParseJsOperand(n return { true, move(src), move(dest), mode }; } +static int ChangeTime(const string &path, uv_fs_t *stat_req) +{ + 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) / NANOSECOND; + double mtime = static_cast(stat_req->statbuf.st_mtim.tv_sec) + + static_cast(stat_req->statbuf.st_mtim.tv_nsec) / NANOSECOND; + int ret = uv_fs_utime(nullptr, utime_req.get(), path.c_str(), atime, mtime, nullptr); + if (ret < 0) { + HILOGE("Failed to utime dstPath"); + } + return ret; +} static int CopyAndDeleteFile(const string &src, const string &dest) { - int ret = 0; + 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(), src.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat srcPath"); + return ret; + } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) filesystem::path dstPath(dest); std::error_code errCode; @@ -120,7 +148,7 @@ static int CopyAndDeleteFile(const string &src, const string &dest) return ret; } uv_fs_req_cleanup(&unlink_req); - return ERRNO_NOERR; + return ChangeTime(dest, stat_req.get()); } static int RenameFile(const string &src, const string &dest) diff --git a/interfaces/kits/js/src/mod_fs/properties/move.h b/interfaces/kits/js/src/mod_fs/properties/move.h index 612494679..24cc3a734 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.h +++ b/interfaces/kits/js/src/mod_fs/properties/move.h @@ -30,6 +30,7 @@ public: }; constexpr int MODE_FORCE_MOVE = 0; constexpr int MODE_THROW_ERR = 1; +const double NANOSECOND = 1e9; const std::string PROCEDURE_MOVE_NAME = "FileIOMove"; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 29416bad6..6f845c17d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -99,6 +99,17 @@ static tuple, unique_ptr, int> ParseJsOperand(n static int CopyAndDeleteFile(const string &src, const string &dest) { + 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(), src.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat srcPath"); + return ret; + } filesystem::path dstPath(dest); if (filesystem::exists(dstPath)) { int removeRes = RemovePath(dest); @@ -113,6 +124,21 @@ 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) { + HILOGE("Failed to utime dstPath"); + return ret; + } return RemovePath(src); } diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.h b/interfaces/kits/js/src/mod_fs/properties/movedir.h index d1a116d23..979ebfc49 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.h +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.h @@ -31,6 +31,8 @@ constexpr int FILE_DISMATCH = 0; constexpr int FILE_MATCH = 1; constexpr int MOVEDIR_DEFAULT_PERM = 0770; +const double NS = 1e9; + enum ModeOfMoveDir { DIRMODE_DIRECTORY_THROW_ERR = 0, DIRMODE_FILE_THROW_ERR, -- Gitee