From f5a53e364fb2ea1be841fd8ed8ceec342cee2c15 Mon Sep 17 00:00:00 2001 From: h30051954 Date: Tue, 30 Jan 2024 20:21:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E8=BA=AB=E6=8B=B7=E8=B4=9D=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D?= 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 | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 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..df530fd9a 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,21 @@ static void Deleter(struct NameList *arg) free(arg->namelist); } +std::string removeDots(const std::string& path) { + fs::path p(path); + fs::path realPath; + for (const auto& component : p) { + 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 +338,9 @@ 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."); + infos->destPath = removeDots(infos->destPath); + if (infos->destPath.find(infos->srcPath) != std::string::npos) { + HILOGE(The src directory is the subdirectory of dest); return EINVAL; } if (IsFile(infos->srcPath)) { -- Gitee