diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 6f845c17dc2b4828b266e82244f83a62579e11ed..313f38305a5c58d0abf5a2d618a071191e377a52 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -35,7 +35,7 @@ using namespace OHOS::FileManagement::LibN; static int RecurMoveDir(const string &srcPath, const string &destPath, const int mode, vector &errfiles); - +static int SetAccessAndModificationTime(const string &path); static tuple JudgeExistAndEmpty(const string &path) { filesystem::path pathName(path); @@ -124,19 +124,9 @@ 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); + ret = SetAccessAndModificationTime(dstPath); if (ret < 0) { - HILOGE("Failed to utime dstPath"); + HILOGE("Failed to set time for %s, error code: %d", dstPath.c_str(), ret); return ret; } return RemovePath(src); @@ -169,6 +159,46 @@ static int32_t FilterFunc(const struct dirent *filename) return FILE_MATCH; } +// 封装设置访问时间和修改时间的函数 +static int MoveDir(const string &path) +{ + 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; + } + //创建文件夹 + if(!filesystem::create_directory(destPath, errCode)){ + HILOGE("Failed to create directory, error code: %{public}d", errCode.value()); + return errCode.value(); + } + + //获取指定路径的文件或目录的状态信息 + 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(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat srcPath"); + return ret; + } + 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(), path.c_str(), atime, mtime, nullptr); + if (ret < 0) { + HILOGE("Failed to utime %s, error code: %d", path.c_str(), ret); + return ret; + } + + return ret; +} + static int RenameDir(const string &src, const string &dest, const int mode, vector &errfiles) { filesystem::path destPath(dest); @@ -178,11 +208,11 @@ 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 = MoveDir(destPath); + if (ret < 0){ + HILOGE("Failed to MoveDir_utime %s, error code: %d", path.c_str(), ret); + return ret; + } } } if (errCode.value() != 0) {