diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index 0dc32fcf6666e78ab365b6b5bba8b68bb8aa8e04..399d7339ab5354726e4e7c4a93523c666cc28ba4 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 6124946792e7b613fd97e1202c12a9d5cab27f8d..24cc3a734f91df6d6d13c2437cf8f2f2ac172013 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 29416bad68fc5afcb54d4c603940a1295c44b43e..6f845c17dc2b4828b266e82244f83a62579e11ed 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 d1a116d23891527a536a4afbabca0f1e1d2f6834..979ebfc49216e0f296c9570b61d235ec0fa3abaf 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,