From b4c32bb9bb5d5f038ab178128b6856c5ad286bcc Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Fri, 30 Jun 2023 09:45:16 +0800 Subject: [PATCH] fix_dir_use_fs_copyfile Signed-off-by: 18721213663 --- .../kits/js/src/mod_fs/properties/copydir.cpp | 19 +++++++------------ .../kits/js/src/mod_fs/properties/movedir.cpp | 13 +++++++------ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 16329aff9..6682b02de 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -108,18 +108,13 @@ static int CopyFile(const string &src, const string &dest) HILOGE("Failed to copy file due to existing destPath with throw err"); return EEXIST; } - std::unique_ptr copyfile_req = { - new (nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; - if (copyfile_req == nullptr) { - HILOGE("Failed to request heap memory."); - return ENOMEM; - } - - int ret = uv_fs_copyfile(nullptr, copyfile_req.get(), src.c_str(), dest.c_str(), - UV_FS_COPYFILE_EXCL, nullptr); - if (ret < 0) { - HILOGE("Failed to move file using copyfile interface"); - return -ret; + filesystem::path srcPath(src); + filesystem::path dstPath(dest); + std::error_code errCode; + auto copyFileRes = filesystem::copy_file(srcPath, dstPath, filesystem::copy_options::overwrite_existing, errCode); + if (!copyFileRes) { + HILOGE("Failed to copy file, error code: %{public}d", errCode.value()); + return errCode.value(); } return ERRNO_NOERR; } diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 195aa0e29..d20384cfc 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -86,13 +86,14 @@ static tuple, unique_ptr, int> ParseJsOperand(n static int CopyAndDeleteFile(const string &src, const string &dest) { - uv_fs_t copyfile_req; - int ret = uv_fs_copyfile(nullptr, ©file_req, src.c_str(), dest.c_str(), UV_FS_COPYFILE_FICLONE_FORCE, nullptr); - if (ret < 0) { - HILOGE("Failed to move file using copyfile interface"); - return -ret; + filesystem::path srcPath(src); + filesystem::path dstPath(dest); + std::error_code errCode; + auto copyFileRes = filesystem::copy_file(srcPath, dstPath, filesystem::copy_options::overwrite_existing, errCode); + if (!copyFileRes) { + HILOGE("Failed to copy file, error code: %{public}d", errCode.value()); + return errCode.value(); } - uv_fs_req_cleanup(©file_req); uv_fs_t unlink_req; int unlinkSrcRes = uv_fs_unlink(nullptr, &unlink_req, src.c_str(), nullptr); if (unlinkSrcRes < 0) { -- Gitee