diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 49baa2f844620a5ead525d580975af6e34045626..34d62196d80da2ac3d6aba3ddb63dd6590315c45 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -496,4 +496,4 @@ group("build_kits_js") { ":statfs", ":statvfs", ] -} +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp index 2d6f298001502aad3d1742ac6567c996c61f1029..4d1cb38139b175248270361ed6a311cdcedd867d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "distributed_file_daemon_manager.h" #include "file_uri.h" @@ -31,65 +32,31 @@ using namespace AppFileService::ModuleFileUri; const std::string NETWORK_PARA = "?networkid="; const std::string FILE_MANAGER_AUTHORITY = "docs"; const std::string MEDIA_AUTHORITY = "media"; -const std::string DISTRIBUTED_PATH = "/data/storage/el2/distributedfiles"; +const std::string DISTRIBUTED_PATH = "/data/storage/el2/distributedfiles/"; -void TransListener::RmDirectory(const std::string &path) +void TransListener::RmDir(const std::string &path) { - DIR *dir = opendir(path.c_str()); - if (dir == nullptr) { - HILOGE("Open dir failed"); - return; - } - dirent *entry = nullptr; - while ((entry = readdir(dir)) != nullptr) { - if (strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, ".") == 0 || - strcmp(entry->d_name, ".remote_share") == 0) { - continue; - } - std::string subPath = path + "/" + entry->d_name; - std::filesystem::path pathName(subPath); - if (std::filesystem::exists(pathName)) { - std::error_code errCode; - std::filesystem::remove_all(pathName, errCode); - if (errCode.value() != 0) { - closedir(dir); - HILOGE("Failed to remove directory, error code: %{public}d", errCode.value()); - return; - } + std::filesystem::path pathName(path); + if (std::filesystem::exists(pathName)) { + std::error_code errCode; + std::filesystem::remove_all(pathName, errCode); + if (errCode.value() != 0) { + HILOGE("Failed to remove directory, error code: %{public}d", errCode.value()); } } - closedir(dir); } -void TransListener::CopyDir(const std::string &path, const std::string &sandboxPath) +std::string TransListener::CreateDfsCopyPath() { - DIR *dir = opendir(path.c_str()); - if (dir == nullptr) { - HILOGE("Open dir failed"); - return; - } - dirent *entry = nullptr; - while ((entry = readdir(dir)) != nullptr) { - if (strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, ".") == 0 || - strcmp(entry->d_name, ".remote_share") == 0) { - continue; - } - std::string subPath = path + "/" + entry->d_name; - if (std::filesystem::is_directory(subPath)) { - auto pos = subPath.find_last_of('/'); - if (pos == std::string::npos) { - closedir(dir); - return; - } - auto dirName = subPath.substr(pos); - std::filesystem::create_directories(sandboxPath + dirName); - std::filesystem::copy(subPath, sandboxPath + dirName, - std::filesystem::copy_options::recursive | std::filesystem::copy_options::update_existing); - } else { - std::filesystem::copy(subPath, sandboxPath, std::filesystem::copy_options::update_existing); + std::string random; + std::random_device rd; + while (true) { + random = std::to_string(rd()); + if (!std::filesystem::exists(DISTRIBUTED_PATH + random)) { + break; } } - closedir(dir); + return random; } NError TransListener::CopyFileFromSoftBus(const std::string &srcUri, @@ -102,11 +69,27 @@ NError TransListener::CopyFileFromSoftBus(const std::string &srcUri, return NError(ENOMEM); } transListener->callback_ = std::move(callback); + + Uri uri(destUri); + auto authority = uri.GetAuthority(); + + std::string tmpDir; + std::string disSandboxPath; + if (authority != FILE_MANAGER_AUTHORITY && authority != MEDIA_AUTHORITY) { + tmpDir = CreateDfsCopyPath(); + disSandboxPath = DISTRIBUTED_PATH + tmpDir; + if (std::filesystem::create_directory(disSandboxPath)) { + HILOGE("Create dir failed"); + return NError(EIO); + } + } + auto networkId = GetNetworkIdFromUri(srcUri); auto ret = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance().PrepareSession(srcUri, destUri, - networkId, transListener); + networkId, transListener, tmpDir); if (ret != ERRNO_NOERR) { HILOGE("PrepareSession failed, ret = %{public}d.", ret); + RmDir(disSandboxPath); return NError(EIO); } std::unique_lock lock(transListener->cvMutex_); @@ -114,40 +97,49 @@ NError TransListener::CopyFileFromSoftBus(const std::string &srcUri, return transListener->copyEvent_ == SUCCESS || transListener->copyEvent_ == FAILED; }); if (transListener->copyEvent_ == FAILED) { + RmDir(disSandboxPath); return NError(EIO); } - Uri uri(destUri); - auto authority = uri.GetAuthority(); if (authority == FILE_MANAGER_AUTHORITY || authority == MEDIA_AUTHORITY) { HILOGW("Public or media path not copy"); return NError(ERRNO_NOERR); } - FileUri fileUri(destUri); - std::string sandboxPath = fileUri.GetPath(); + std::string sandboxPath = uri.GetPath(); if (std::filesystem::exists(sandboxPath) && std::filesystem::is_directory(sandboxPath)) { HILOGI("Copy dir"); - CopyDir(DISTRIBUTED_PATH, sandboxPath); + std::filesystem::copy(disSandboxPath, sandboxPath, + std::filesystem::copy_options::recursive | std::filesystem::copy_options::update_existing); } else { - auto pos = srcUri.find_last_of('/'); - if (pos == std::string::npos) { - HILOGE("invalid uri"); + auto fileName = GetFileName(srcUri); + if (fileName.empty()) { + HILOGE("Get filename failed"); + RmDir(disSandboxPath); return NError(EIO); } - auto fileName = srcUri.substr(pos); - auto networkIdPos = fileName.find(NETWORK_PARA); - if (networkIdPos == std::string::npos) { - HILOGE("Not remote uri"); - return NError(EIO); - } - fileName = fileName.substr(0, networkIdPos); - std::filesystem::copy(DISTRIBUTED_PATH + fileName, sandboxPath, std::filesystem::copy_options::update_existing); + std::filesystem::copy(disSandboxPath + fileName, sandboxPath, std::filesystem::copy_options::update_existing); } - RmDirectory(DISTRIBUTED_PATH); + RmDir(disSandboxPath); return NError(ERRNO_NOERR); } +std::string TransListener::GetFileName(const std::string &uri) +{ + auto pos = uri.find_last_of('/'); + if (pos == std::string::npos) { + HILOGE("invalid uri"); + return ""; + } + auto fileName = uri.substr(pos); + auto networkIdPos = fileName.find(NETWORK_PARA); + if (networkIdPos == std::string::npos) { + HILOGE("Not remote uri"); + return ""; + } + return fileName.substr(0, networkIdPos); +} + std::string TransListener::GetNetworkIdFromUri(const std::string &uri) { return uri.substr(uri.find(NETWORK_PARA) + NETWORK_PARA.size(), uri.size()); diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.h b/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.h index b88cddf0b9fbdabc3846a241c25a808409889876..0bccdc612d0948777e6b6c59fc1deaa05afb076b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.h +++ b/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.h @@ -39,8 +39,9 @@ public: private: static std::string GetNetworkIdFromUri(const std::string &uri); static void CallbackComplete(uv_work_t *work, int stat); - static void RmDirectory(const std::string &path); - static void CopyDir(const std::string &path, const std::string &sandboxPath); + static void RmDir(const std::string &path); + static std::string CreateDfsCopyPath(); + static std::string GetFileName(const std::string &path); std::mutex cvMutex_; std::condition_variable cv_;