From 50c6049f3d7060ddd2e0ec27894fde7440fba220 Mon Sep 17 00:00:00 2001 From: wangtong Date: Mon, 4 Mar 2024 20:58:11 +0800 Subject: [PATCH] fix:set sandbox acl Signed-off-by: wangtong --- file_api.gni | 4 +- interfaces/kits/js/BUILD.gn | 8 +- .../kits/js/src/mod_fs/properties/copy.cpp | 2 +- .../copy_listener/trans_listener.cpp | 149 ++++++------------ .../properties/copy_listener/trans_listener.h | 13 +- 5 files changed, 65 insertions(+), 111 deletions(-) diff --git a/file_api.gni b/file_api.gni index 7fc94ace6..031de538c 100644 --- a/file_api.gni +++ b/file_api.gni @@ -15,11 +15,11 @@ aafwk_kits_path = "//foundation/ability/ability_runtime/frameworks/native" aafwk_path = "${aafwk_kits_path}/frameworks/kits" arkui_napi_path = "//foundation/arkui/napi" file_api_path = "//foundation/filemanagement/file_api" -filemanagement_service_path = "//foundation/filemanagement/dfs_service/services" +dfs_path = "//foundation/filemanagement/dfs_service" hiviewdfx_hilog_path = "//base/hiviewdfx/hilog" src_path = "${file_api_path}/interfaces/kits/js/src" utils_path = "${file_api_path}/utils" use_mac = "${current_os}_${current_cpu}" == "mac_x64" || "${current_os}_${current_cpu}" == "mac_arm64" -use_mingw_win = "${current_os}_${current_cpu}" == "mingw_x86_64" +use_mingw_win = "${current_os}_${current_cpu}" == "mingw_x86_64" \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 20648a0b7..c70cd1883 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -116,7 +116,9 @@ ohos_shared_library("fs") { relative_install_dir = "module/file" include_dirs = [ - "${filemanagement_service_path}/distributedfiledaemon/include/ipc", + "${dfs_path}/services/distributedfiledaemon/include/ipc", + "${dfs_path}/utils/system/include", + "${dfs_path}/utils/log/include", "${src_path}/common", "${src_path}/common/file_helper", "${src_path}/mod_fs", @@ -144,6 +146,8 @@ ohos_shared_library("fs") { "src/mod_fs/properties/stat.cpp", "src/mod_fs/properties/truncate.cpp", "src/mod_fs/properties/utimes.cpp", + "${dfs_path}/utils/system/src/acl.cpp", + "${dfs_path}/utils/log/src/utils_log.cpp", ] cflags_cc = [ "-std=c++17" ] @@ -493,4 +497,4 @@ group("build_kits_js") { ":statfs", ":statvfs", ] -} +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.cpp b/interfaces/kits/js/src/mod_fs/properties/copy.cpp index 1aa3c7465..6bd1d0a03 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy.cpp @@ -789,7 +789,7 @@ napi_value Copy::Async(napi_env env, napi_callback_info info) } auto cbExec = [infos, callback]() -> NError { if (IsRemoteUri(infos->srcUri)) { - return TransListener::CopyFileFromSoftBus(infos->srcUri, infos->destUri, std::move(callback)); + return TransListener::Copy(infos->srcUri, infos->destUri, std::move(callback)); } auto result = Copy::ExecLocal(infos, callback); CloseNotifyFd(infos, callback); 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 2d6f29800..4a804c68c 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 @@ -19,133 +19,80 @@ #include #include "distributed_file_daemon_manager.h" -#include "file_uri.h" #include "ipc_skeleton.h" #include "uri.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { -using namespace OHOS::AppFileService; -using namespace AppFileService::ModuleFileUri; +using namespace Storage::DistributedFile; 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"; -void TransListener::RmDirectory(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; - } - } - } - closedir(dir); -} - -void TransListener::CopyDir(const std::string &path, const std::string &sandboxPath) -{ - 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); - } - } - closedir(dir); -} +std::map TransListener::dstUriCount_; +std::mutex TransListener::countMutex_; -NError TransListener::CopyFileFromSoftBus(const std::string &srcUri, - const std::string &destUri, - std::shared_ptr callback) +int TransListener::CopyFromDFS(const std::string &srcUri, const std::string &destUri, + std::shared_ptr callback) { sptr transListener = new (std::nothrow) TransListener(); if (transListener == nullptr) { HILOGE("new trans listener failed"); - return NError(ENOMEM); + return E_NOMEM; } transListener->callback_ = std::move(callback); - auto networkId = GetNetworkIdFromUri(srcUri); auto ret = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance().PrepareSession(srcUri, destUri, - networkId, transListener); + GetNetworkIdFromUri(srcUri), transListener); if (ret != ERRNO_NOERR) { HILOGE("PrepareSession failed, ret = %{public}d.", ret); - return NError(EIO); + return E_IO; } std::unique_lock lock(transListener->cvMutex_); - transListener->cv_.wait(lock, [&transListener]() { - return transListener->copyEvent_ == SUCCESS || transListener->copyEvent_ == FAILED; - }); + transListener->cv_.wait(lock, + [&transListener]() { return transListener->copyEvent_ == SUCCESS || transListener->copyEvent_ == FAILED; }); if (transListener->copyEvent_ == FAILED) { - return NError(EIO); + return E_IO; } - - 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); + return ERRNO_NOERR; +} +NError TransListener::Copy(const std::string &srcUri, const std::string &destUri, + std::shared_ptr callback) +{ + Uri dstUri(destUri); + if (Acl::AddDfsAcl(dstUri.GetPath()) != ERRNO_NOERR) { + HILOGE("Add acl failed"); + return NError(UNKNOWN_ERROR); } - - FileUri fileUri(destUri); - std::string sandboxPath = fileUri.GetPath(); - if (std::filesystem::exists(sandboxPath) && std::filesystem::is_directory(sandboxPath)) { - HILOGI("Copy dir"); - CopyDir(DISTRIBUTED_PATH, sandboxPath); - } else { - auto pos = srcUri.find_last_of('/'); - if (pos == std::string::npos) { - HILOGE("invalid uri"); - return NError(EIO); + { + std::lock_guard addLock(countMutex_); + if (dstUriCount_.find(destUri) != dstUriCount_.end()) { + dstUriCount_[destUri] += 1; + } else { + dstUriCount_[destUri] = 0; } - auto fileName = srcUri.substr(pos); - auto networkIdPos = fileName.find(NETWORK_PARA); - if (networkIdPos == std::string::npos) { - HILOGE("Not remote uri"); - return NError(EIO); + } + + auto code = CopyFromDFS(srcUri, destUri, std::move(callback)); + + { + std::lock_guard deleteLock(countMutex_); + auto it = dstUriCount_.find(destUri); + if (it != dstUriCount_.end()) { + it->second -= 1; + if (it->second <= 0) { + dstUriCount_.erase(it); + if (Acl::DeleteDfsAcl(dstUri.GetPath()) != ERRNO_NOERR) { + HILOGE("Delete acl failed"); + return NError(UNKNOWN_ERROR); + } + } } - fileName = fileName.substr(0, networkIdPos); - std::filesystem::copy(DISTRIBUTED_PATH + fileName, sandboxPath, std::filesystem::copy_options::update_existing); } - RmDirectory(DISTRIBUTED_PATH); - return NError(ERRNO_NOERR); + + if (code != ERRNO_NOERR) { + HILOGE("Copy failed: %{public}d", code); + return code; + } + return ERRNO_NOERR; } std::string TransListener::GetNetworkIdFromUri(const std::string &uri) 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 b88cddf0b..eebd79845 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 @@ -20,6 +20,7 @@ #include "copy.h" #include "file_trans_listener_stub.h" +#include "acl.h" constexpr int NONE = 0; constexpr int SUCCESS = 1; @@ -32,16 +33,18 @@ public: int32_t OnFileReceive(uint64_t totalBytes, uint64_t processedBytes) override; int32_t OnFinished(const std::string &sessionName) override; int32_t OnFailed(const std::string &sessionName) override; - static NError CopyFileFromSoftBus(const std::string &srcUri, - const std::string &destUri, - std::shared_ptr callback); + static NError Copy(const std::string &srcUri, const std::string &destUri, + std::shared_ptr callback); 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 int CopyFromDFS(const std::string &srcUri, + const std::string &destUri, + std::shared_ptr callback); + static std::map dstUriCount_; + static std::mutex countMutex_; std::mutex cvMutex_; std::condition_variable cv_; int copyEvent_ = NONE; -- Gitee