From 33936280d7d6205658f3986bebb46c6882c7811f Mon Sep 17 00:00:00 2001 From: fengjq Date: Mon, 4 Sep 2023 22:08:04 +0800 Subject: [PATCH] Bugfix for moveFile and moveDir to cleaning dest. Signed-off-by: fengjq --- .../kits/js/src/mod_fs/properties/move.cpp | 25 +++++++++++++++---- .../kits/js/src/mod_fs/properties/movedir.cpp | 4 +++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index d03b72ebd..0dc32fcf6 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; @@ -133,9 +140,17 @@ 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; + } + uv_fs_req_cleanup(&access_req); if (mode == MODE_THROW_ERR) { uv_fs_t access_req; - int ret = uv_fs_access(nullptr, &access_req, dest.c_str(), 0, nullptr); + ret = uv_fs_access(nullptr, &access_req, dest.c_str(), 0, nullptr); uv_fs_req_cleanup(&access_req); if (ret == 0) { HILOGE("Failed to move file due to existing destPath with MODE_THROW_ERR."); diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index c3e97c0aa..29416bad6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -233,6 +233,10 @@ static int MoveDirFunc(const string &src, const string &dest, const int mode, ve if (found == std::string::npos) { return EINVAL; } + if (access(src.c_str(), W_OK) != 0) { + HILOGE("Failed to move src directory due to doesn't exist or hasn't write permission"); + return errno; + } string dirName = string(src).substr(found); string destStr = dest + dirName; auto [destStrExist, destStrEmpty] = JudgeExistAndEmpty(destStr); -- Gitee