From 7b6ec821595a359715f2b8fdc944fb542c63eeab Mon Sep 17 00:00:00 2001 From: h30051954 Date: Fri, 2 Feb 2024 19:57:51 +0800 Subject: [PATCH] =?UTF-8?q?fixed=2076a6c3d=20from=20https://gitee.com/heme?= =?UTF-8?q?nghao1996/filemanagement=5Ffile=5Fapi/pulls/530=20copy=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: h30051954 --- .../kits/js/src/mod_fs/properties/copy.cpp | 22 +++++++++++++++++-- .../kits/js/src/mod_fs/properties/copy.h | 3 ++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.cpp b/interfaces/kits/js/src/mod_fs/properties/copy.cpp index 678dda4b9..494cc884b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy.cpp @@ -42,6 +42,7 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace AppFileService::ModuleFileUri; +namespace fs = std::filesystem; const std::string FILE_PREFIX_NAME = "file://"; const std::string NETWORK_PARA = "?networkid="; const string PROCEDURE_COPY_NAME = "FileFSCopy"; @@ -249,6 +250,22 @@ static void Deleter(struct NameList *arg) free(arg->namelist); } +std::string Copy::GetRealPath(const std::string& path) +{ + fs::path tempPath(path); + fs::path realPath{}; + for (const auto& component : tempPath) { + if (component == ".") { + continue; + } else if (component == "..") { + realPath = realPath.parent_path(); + } else { + realPath /= component; + } + } + return realPath.string(); +} + uint64_t Copy::GetDirSize(std::shared_ptr infos, std::string path) { unique_ptr pNameList = { new (nothrow) struct NameList, Deleter }; @@ -322,8 +339,8 @@ int Copy::CopyDirFunc(const string &src, const string &dest, std::shared_ptr infos, std::shared_ptr callback) { - if (infos->srcPath == infos->destPath) { - HILOGE("The src and dest are the same."); + if (infos->destPath.find(infos->srcPath) != std::string::npos) { + HILOGE("The src directory is the subdirectory of dest"); return EINVAL; } if (IsFile(infos->srcPath)) { @@ -671,6 +688,7 @@ tuple> Copy::CreateFileInfos( infos->listener = listener; infos->srcPath = ConvertUriToPath(infos->srcUri); infos->destPath = ConvertUriToPath(infos->destUri); + infos->destPath = GetRealPath(infos->destPath); infos->notifyTime = std::chrono::steady_clock::now() + NOTIFY_PROGRESS_DELAY; if (listener) { infos->hasListener = true; diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.h b/interfaces/kits/js/src/mod_fs/properties/copy.h index 1694ddd7d..90200a899 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.h +++ b/interfaces/kits/js/src/mod_fs/properties/copy.h @@ -93,7 +93,7 @@ struct FileInfos { if (srcUri == infos.srcUri) { return destUri < infos.destUri; } - return (srcUri < infos.srcUri || destUri < infos.destUri); + return srcUri < infos.srcUri; } }; @@ -165,6 +165,7 @@ private: static bool IsDirectory(const std::string &path); static bool IsFile(const std::string &path); static std::string ConvertUriToPath(const std::string &uri); + static std::string GetRealPath(const std::string& path); }; } // namespace ModuleFileIO } // namespace FileManagement -- Gitee