diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index 2c6c29957a3b750db36fa42635d88496a85f74df..414f439856587af8f50694e14199a90c9d7da3db 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -83,27 +83,21 @@ static tuple, unique_ptr, int> ParseJsOperand(n static int CopyAndDeleteFile(const string &src, const string &dest) { - std::unique_ptr stat_req = { + std::unique_ptr access_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!stat_req) { + if (access_req == nullptr) { HILOGE("Failed to request heap memory."); return ENOMEM; } - int ret = uv_fs_stat(nullptr, stat_req.get(), src.c_str(), nullptr); + int ret = uv_fs_access(nullptr, access_req.get(), src.c_str(), W_OK, nullptr); if (ret < 0) { - HILOGE("Failed to stat srcPath"); + HILOGE("Failed to move src file due to hasn't write permission"); return ret; } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) filesystem::path dstPath(dest); - std::error_code errCode; - if (filesystem::exists(dstPath)) { - if (!filesystem::remove(dstPath, errCode)) { - HILOGE("Failed to remove dest file, error code: %{public}d", errCode.value()); - return errCode.value(); - } - } filesystem::path srcPath(src); + std::error_code errCode; if (!filesystem::copy_file(srcPath, dstPath, filesystem::copy_options::overwrite_existing, errCode)) { HILOGE("Failed to copy file, error code: %{public}d", errCode.value()); return errCode.value(); @@ -135,9 +129,13 @@ static int CopyAndDeleteFile(const string &src, const string &dest) static int RenameFile(const string &src, const string &dest) { - int ret = 0; - uv_fs_t rename_req; - ret = uv_fs_rename(nullptr, &rename_req, src.c_str(), dest.c_str(), nullptr); + std::unique_ptr rename_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (rename_req == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int ret = uv_fs_rename(nullptr, rename_req.get(), src.c_str(), dest.c_str(), nullptr); if (ret < 0 && (string_view(uv_err_name(ret)) == "EXDEV")) { return CopyAndDeleteFile(src, dest); } @@ -150,16 +148,14 @@ static int RenameFile(const string &src, const string &dest) static int MoveFile(const string &src, const string &dest, int mode) { - uv_fs_t access_req; - int ret = uv_fs_access(nullptr, &access_req, src.c_str(), W_OK, nullptr); - if (ret < 0) { - HILOGE("Failed to move src file due to doesn't exist or hasn't write permission"); - uv_fs_req_cleanup(&access_req); - return ret; - } if (mode == MODE_THROW_ERR) { - ret = uv_fs_access(nullptr, &access_req, dest.c_str(), 0, nullptr); - uv_fs_req_cleanup(&access_req); + std::unique_ptr access_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (access_req == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int ret = uv_fs_access(nullptr, access_req.get(), dest.c_str(), 0, nullptr); if (ret == 0) { HILOGE("Failed to move file due to existing destPath with MODE_THROW_ERR."); return EEXIST; @@ -168,8 +164,6 @@ static int MoveFile(const string &src, const string &dest, int mode) HILOGE("Failed to access destPath with MODE_THROW_ERR."); return ret; } - } else { - uv_fs_req_cleanup(&access_req); } return RenameFile(src, dest); }