From d6126f1947dd5d73dd606f3140f2bae30585e748 Mon Sep 17 00:00:00 2001 From: fengjq Date: Tue, 26 Sep 2023 18:41:48 +0800 Subject: [PATCH] Fix the usage scenario of srcPath + '/' for moveDir and copyDir interfaces Signed-off-by: fengjq --- .../kits/js/src/mod_fs/properties/copydir.cpp | 16 ++++++++++--- .../kits/js/src/mod_fs/properties/movedir.cpp | 24 +++++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 3dcaea202..41decdf65 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -15,6 +15,7 @@ #include "copydir.h" +#include #include #include #include @@ -188,11 +189,20 @@ static int RecurCopyDir(const string &srcPath, const string &destPath, const int static int CopyDirFunc(const string &src, const string &dest, const int mode, vector &errfiles) { - size_t found = string(src).rfind('/'); + auto realPath = CreateUniquePtr(PATH_MAX); + if (realPath == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + if (realpath(src.c_str(), realPath.get()) == nullptr) { + HILOGE("Failed to get absolute path from src"); + return errno; + } + size_t found = string(realPath.get()).rfind('/'); if (found == std::string::npos) { return EINVAL; } - string dirName = string(src).substr(found); + string dirName = string(realPath.get()).substr(found); string destStr = dest + dirName; if (!filesystem::exists(destStr)) { int res = MakeDir(destStr); @@ -201,7 +211,7 @@ static int CopyDirFunc(const string &src, const string &dest, const int mode, ve return res; } } - int res = RecurCopyDir(src, destStr, mode, errfiles); + int res = RecurCopyDir(realPath.get(), destStr, mode, errfiles); if (!errfiles.empty() && res == ERRNO_NOERR) { return EEXIST; } diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 6f845c17d..fa465ba98 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -15,6 +15,7 @@ #include "movedir.h" +#include #include #include #include @@ -255,15 +256,24 @@ static int RecurMoveDir(const string &srcPath, const string &destPath, const int static int MoveDirFunc(const string &src, const string &dest, const int mode, vector &errfiles) { - size_t found = string(src).rfind('/'); - if (found == std::string::npos) { - return EINVAL; + auto realPath = CreateUniquePtr(PATH_MAX); + if (realPath == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; } - if (access(src.c_str(), W_OK) != 0) { + if (realpath(src.c_str(), realPath.get()) == nullptr) { + HILOGE("Failed to get absolute path from src"); + return errno; + } + if (access(realPath.get(), 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); + size_t found = string(realPath.get()).rfind('/'); + if (found == std::string::npos) { + return EINVAL; + } + string dirName = string(realPath.get()).substr(found); string destStr = dest + dirName; auto [destStrExist, destStrEmpty] = JudgeExistAndEmpty(destStr); if (destStrExist && !destStrEmpty) { @@ -279,13 +289,13 @@ static int MoveDirFunc(const string &src, const string &dest, const int mode, ve return ENOTEMPTY; } } - int res = RenameDir(src, destStr, mode, errfiles); + int res = RenameDir(realPath.get(), destStr, mode, errfiles); if (res == ERRNO_NOERR) { if (mode == DIRMODE_FILE_THROW_ERR && errfiles.size() != 0) { HILOGE("Failed to movedir with some conflicted files"); return EEXIST; } - int removeRes = RmDirectory(src); + int removeRes = RmDirectory(realPath.get()); if (removeRes) { HILOGE("Failed to remove src directory"); return removeRes; -- Gitee