diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index d03b72ebd41c6e417eb2f18c7162f2f97b1eff79..cec3e4ee6072d6d729728bac00235c6ad52792db 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -85,9 +85,15 @@ static int CopyAndDeleteFile(const string &src, const string &dest) { int ret = 0; #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - filesystem::path srcPath(src); filesystem::path dstPath(dest); - error_code errCode; + 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); 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(); @@ -105,9 +111,10 @@ static int CopyAndDeleteFile(const string &src, const string &dest) ret = uv_fs_unlink(nullptr, &unlink_req, src.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to unlink src file"); - ret = uv_fs_unlink(nullptr, &unlink_req, dest.c_str(), nullptr); - if (ret < 0) { + int result = uv_fs_unlink(nullptr, &unlink_req, dest.c_str(), nullptr); + if (result < 0) { HILOGE("Failed to unlink dest file"); + return result; } uv_fs_req_cleanup(&unlink_req); return ret;