From def50a7e849135375f61c19c9b5e5db7abc8f541 Mon Sep 17 00:00:00 2001 From: hunili Date: Fri, 12 Sep 2025 16:22:05 +0800 Subject: [PATCH] =?UTF-8?q?[Bug]:=20=E4=BA=91=E7=9B=98=5F003=20https://git?= =?UTF-8?q?ee.com/openharmony/filemanagement=5Fuser=5Ffile=5Fservice/issue?= =?UTF-8?q?s/ICXQZ1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hunili --- bundle.json | 12 +- .../inner_api/cloud_disk_kit_inner/BUILD.gn | 1 - .../kits/native/clouddiskmanager/BUILD.gn | 55 ++++++ .../clouddiskmanager/include/comm_root_info.h | 29 +++ .../include/sync_folder_access_n_exporter.h | 37 ++++ .../kits/native/clouddiskmanager/module.cpp | 59 ++++++ .../clouddiskmanager/src/comm_root_info.cpp | 80 ++++++++ .../src/sync_folder_access_n_exporter.cpp | 182 ++++++++++++++++++ services/file_access_service.cfg | 1 + 9 files changed, 445 insertions(+), 11 deletions(-) create mode 100644 interfaces/kits/native/clouddiskmanager/BUILD.gn create mode 100644 interfaces/kits/native/clouddiskmanager/include/comm_root_info.h create mode 100644 interfaces/kits/native/clouddiskmanager/include/sync_folder_access_n_exporter.h create mode 100644 interfaces/kits/native/clouddiskmanager/module.cpp create mode 100644 interfaces/kits/native/clouddiskmanager/src/comm_root_info.cpp create mode 100644 interfaces/kits/native/clouddiskmanager/src/sync_folder_access_n_exporter.cpp diff --git a/bundle.json b/bundle.json index c601ad44..0ba40d18 100644 --- a/bundle.json +++ b/bundle.json @@ -57,7 +57,8 @@ "//foundation/filemanagement/user_file_service/interfaces/kits/picker:picker", "//foundation/filemanagement/user_file_service/interfaces/kits/picker:cj_picker_ffi", "//foundation/filemanagement/user_file_service/interfaces/kits/native/recent:recent", - "//foundation/filemanagement/user_file_service/interfaces/kits/native/trash:trash" + "//foundation/filemanagement/user_file_service/interfaces/kits/native/trash:trash", + "//foundation/filemanagement/user_file_service/interfaces/kits/native/clouddiskmanager:clouddiskmanager" ], "service_group": [ "//foundation/filemanagement/user_file_service/services:user_file_managers", @@ -95,15 +96,6 @@ ], "header_base": "//foundation/filemanagement/user_file_service/interfaces/inner_api/cloud_disk_kit_inner/include" } - }, - { - "name": "//foundation/filemanagement/user_file_service/interfaces/inner_api/cloud_disk_kit_inner:cloud_disk_manager_js_kit", - "header": { - "header_files": [ - "cloud_disk_js_manager.h" - ], - "header_base": "//foundation/filemanagement/user_file_service/interfaces/inner_api/cloud_disk_kit_inner/include" - } } ], "test": [ diff --git a/interfaces/inner_api/cloud_disk_kit_inner/BUILD.gn b/interfaces/inner_api/cloud_disk_kit_inner/BUILD.gn index 5a2cbcdc..adf93efe 100644 --- a/interfaces/inner_api/cloud_disk_kit_inner/BUILD.gn +++ b/interfaces/inner_api/cloud_disk_kit_inner/BUILD.gn @@ -11,7 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//build/config/components/idl_tool/idl.gni") import("//build/ohos.gni") import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") diff --git a/interfaces/kits/native/clouddiskmanager/BUILD.gn b/interfaces/kits/native/clouddiskmanager/BUILD.gn new file mode 100644 index 00000000..689cd827 --- /dev/null +++ b/interfaces/kits/native/clouddiskmanager/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") + +ohos_shared_library("clouddiskmanager") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + include_dirs = [ + "include", + "${user_file_service_path}/utils" + ] + + sources = [ + "src/comm_root_info.cpp", + "src/sync_folder_access_n_exporter.cpp", + "module.cpp", + ] + + deps = [ + "${user_file_service_path}/interfaces/inner_api/cloud_disk_kit_inner:cloud_disk_manager_js_kit" + ] + + external_deps = [ + "c_utils:utils", + "file_api:filemgmt_libhilog", + "file_api:filemgmt_libn", + "hilog:libhilog", + "napi:ace_napi" + ] + + relative_install_dir = "module/file" + + part_name = "user_file_service" + subsystem_name = "filemanagement" +} diff --git a/interfaces/kits/native/clouddiskmanager/include/comm_root_info.h b/interfaces/kits/native/clouddiskmanager/include/comm_root_info.h new file mode 100644 index 00000000..d3bc9d85 --- /dev/null +++ b/interfaces/kits/native/clouddiskmanager/include/comm_root_info.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef INTERFACES_KITS_NATIVE_CLOUDDISK_COMM_ROOT_INFO_H +#define INTERFACES_KITS_NATIVE_CLOUDDISK_COMM_ROOT_INFO_H + +#include "filemgmt_libn.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { +void InitSyncFolder(napi_env env, napi_value exports); +void InitState(napi_env env, napi_value exports); +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_NATIVE_CLOUDDISK_COMM_ROOT_INFO_H \ No newline at end of file diff --git a/interfaces/kits/native/clouddiskmanager/include/sync_folder_access_n_exporter.h b/interfaces/kits/native/clouddiskmanager/include/sync_folder_access_n_exporter.h new file mode 100644 index 00000000..8a6436fa --- /dev/null +++ b/interfaces/kits/native/clouddiskmanager/include/sync_folder_access_n_exporter.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef INTERFACES_KITS_NATIVE_CLOUDDISK_NAPI_SYNC_FOLDER_ACCESS_H +#define INTERFACES_KITS_NATIVE_CLOUDDISK_NAPI_SYNC_FOLDER_ACCESS_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { +class SyncFolderAccessNExporter final : public LibN::NExporter { +public: + inline static const std::string className_ = "SyncFolderAccessor"; + bool Export() override; + std::string GetClassName() override; + + static napi_value Constructor(napi_env env, napi_callback_info cbinfo); + static napi_value GetAllSyncFolders(napi_env env, napi_callback_info cbinfo); + SyncFolderAccessNExporter(napi_env env, napi_value exports); + ~SyncFolderAccessNExporter() override; +}; +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_NATIVE_CLOUDDISK_NAPI_SYNC_FOLDER_ACCESS_H diff --git a/interfaces/kits/native/clouddiskmanager/module.cpp b/interfaces/kits/native/clouddiskmanager/module.cpp new file mode 100644 index 00000000..6b360291 --- /dev/null +++ b/interfaces/kits/native/clouddiskmanager/module.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "comm_root_info.h" +#include "filemgmt_libn.h" +#include "hilog_wrapper.h" +#include "sync_folder_access_n_exporter.h" +namespace OHOS::FileManagement::CloudDiskService { +using namespace LibN; + +static napi_value InitCloudDisk(napi_env env, napi_value exports) +{ + InitSyncFolder(env, exports); + InitState(env, exports); + std::vector> products; + products.emplace_back(std::make_unique(env, exports)); + for (auto &&product : products) { + if (!product->Export()) { + HILOG_ERROR("INNER BUG. Failed to export class %{public}s for module recent", + product->GetClassName().c_str()); + return nullptr; + } else { + HILOG_WARN("Class %{public}s for module fileio has been exported", product->GetClassName().c_str()); + } + } + + return exports; +} + +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = InitCloudDisk, + .nm_modname = "file.cloudDiskManager", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/interfaces/kits/native/clouddiskmanager/src/comm_root_info.cpp b/interfaces/kits/native/clouddiskmanager/src/comm_root_info.cpp new file mode 100644 index 00000000..6937c879 --- /dev/null +++ b/interfaces/kits/native/clouddiskmanager/src/comm_root_info.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "comm_root_info.h" + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace LibN; + +constexpr const char* PATH = "path"; +constexpr const char* STATE = "state"; +constexpr const char* DISPLAY_NAME_RES_ID = "displayNameResId"; +constexpr const char* CUSTOM_ALIAS = "customAlias"; +constexpr const char* BUNDLE_NAME = "bundleName"; +constexpr const char* ACTIVE = "ACTIVE"; +constexpr const char* INACTIVE = "INACTIVE"; + +enum class State { + INACTIVE = 0, + ACTIVE, +}; + +static napi_value SyncFolderConstructor(napi_env env, napi_callback_info info) +{ + size_t argc = 0; + napi_value args[1] = {0}; // 1: argc + napi_value res = nullptr; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data); + if (status != napi_ok) { + HILOG_ERROR("SyncFolderConstructor, status is not napi_ok"); + return nullptr; + } + + return res; +} + +void InitSyncFolder(napi_env env, napi_value exports) +{ + char className[] = "SyncFolder"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_PROPERTY(PATH, NVal::CreateUTF8String(env, "").val_), + DECLARE_NAPI_PROPERTY(STATE, NVal::CreateInt32(env, static_cast(State::INACTIVE)).val_), + DECLARE_NAPI_PROPERTY(DISPLAY_NAME_RES_ID, NVal::CreateInt32(env, 0).val_), + DECLARE_NAPI_PROPERTY(CUSTOM_ALIAS, NVal::CreateUTF8String(env, "").val_), + DECLARE_NAPI_PROPERTY(BUNDLE_NAME, NVal::CreateUTF8String(env, "").val_) + }; + napi_value obj = nullptr; + napi_define_class(env, className, NAPI_AUTO_LENGTH, SyncFolderConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &obj); + napi_set_named_property(env, exports, className, obj); +} + +void InitState(napi_env env, napi_value exports) +{ + char propertyName[] = "SyncFolderState"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY(ACTIVE, NVal::CreateInt32(env, static_cast(State::ACTIVE)).val_), + DECLARE_NAPI_STATIC_PROPERTY(INACTIVE, NVal::CreateInt32(env, static_cast(State::INACTIVE)).val_) + }; + napi_value obj = nullptr; + napi_create_object(env, &obj); + napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc); + napi_set_named_property(env, exports, propertyName, obj); +} +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/interfaces/kits/native/clouddiskmanager/src/sync_folder_access_n_exporter.cpp b/interfaces/kits/native/clouddiskmanager/src/sync_folder_access_n_exporter.cpp new file mode 100644 index 00000000..2c40caa8 --- /dev/null +++ b/interfaces/kits/native/clouddiskmanager/src/sync_folder_access_n_exporter.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sync_folder_access_n_exporter.h" + +#include + +#include "cloud_disk_js_manager.h" +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; +using namespace LibN; +using namespace FileAccessFwk; +constexpr const char* PATH = "path"; +constexpr const char* STATE = "state"; +constexpr const char* DISPLAY_NAME_RES_ID = "displayNameResId"; +constexpr const char* CUSTOM_ALIAS = "customAlias"; +constexpr const char* BUNDLE_NAME = "bundleName"; + +static napi_status CreateOneSyncFolderExt(napi_env env, + const SyncFolderExt &syncFolderExt, napi_value resVec, size_t index) +{ + napi_value ele = nullptr; + napi_status status = napi_ok; + if ((status = napi_create_object(env, &ele)) != napi_ok) { + HILOG_ERROR("Create object failed"); + return status; + } + if ((status = napi_set_named_property( + env, ele, PATH, NVal::CreateUTF8String(env, syncFolderExt.path_).val_)) != napi_ok) { + HILOG_ERROR("Set named property path failed"); + return status; + } + if ((status = napi_set_named_property(env, ele, STATE, + NVal::CreateInt32(env, static_cast(syncFolderExt.state_)).val_)) != napi_ok) { + HILOG_ERROR("Set named property state failed"); + return status; + } + if (syncFolderExt.displayNameResId_ != 0) { + if ((status = napi_set_named_property(env, ele, DISPLAY_NAME_RES_ID, + NVal::CreateInt32(env, syncFolderExt.displayNameResId_).val_)) != napi_ok) { + HILOG_ERROR("Set named property resId failed"); + return status; + } + } + if (!syncFolderExt.displayName_.empty()) { + if ((status = napi_set_named_property(env, + ele, CUSTOM_ALIAS, NVal::CreateUTF8String(env, syncFolderExt.displayName_).val_)) != napi_ok) { + HILOG_ERROR("Set named property displayName failed"); + return status; + } + } + if ((status = napi_set_named_property(env, + ele, BUNDLE_NAME, NVal::CreateUTF8String(env, syncFolderExt.bundleName_).val_)) != napi_ok) { + HILOG_ERROR("Set named property bundleName failed"); + return status; + } + if ((status = napi_set_element(env, resVec, index, ele)) != napi_ok) { + HILOG_ERROR("Set element failed"); + return status; + } + return napi_ok; +} + +static std::tuple CreateSyncFolderExtList(napi_env env, + const std::vector &syncFolderExts) +{ + napi_value res = nullptr; + napi_status status = napi_ok; + if ((status = napi_create_array(env, &res)) != napi_ok) { + HILOG_ERROR("Create array failed"); + return { status, res }; + } + for (size_t i = 0; i < syncFolderExts.size(); ++i) { + if ((status = CreateOneSyncFolderExt(env, syncFolderExts[i], res, i)) != napi_ok) { + return { status, res }; + } + } + return { napi_ok, res }; +} + +SyncFolderAccessNExporter::SyncFolderAccessNExporter(napi_env env, napi_value exports): NExporter(env, exports) {} + +SyncFolderAccessNExporter::~SyncFolderAccessNExporter() {}; + +napi_value SyncFolderAccessNExporter::GetAllSyncFolders(napi_env env, napi_callback_info cbinfo) +{ + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOG_ERROR("Number of arguments unmatched"); + NError(E_INVALID_PARAM).ThrowErr(env); + return nullptr; + } + auto result = std::make_shared>(); + auto syncFolderJSAccess = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!syncFolderJSAccess) { + HILOG_ERROR("GetEntityOf syncFolderJSAccess failed"); + NError(E_INTERNAL_ERROR).ThrowErr(env); + return nullptr; + } + auto cbExec = [result, syncFolderJSAccess]() -> NError { + if (!syncFolderJSAccess) { + HILOG_ERROR("syncFolderJSAccess is nullptr"); + return NError(E_INTERNAL_ERROR); + } + int ret = syncFolderJSAccess->GetAllSyncFolders(*result); + return NError(ret); + }; + auto cbCompl = [result](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + auto [succSta, syncFolderVec] = CreateSyncFolderExtList(env, *result); + if (succSta == napi_ok) { + return {env, syncFolderVec}; + } + HILOG_ERROR("CreateSyncFolderExtList failed"); + return {env, NError(E_INTERNAL_ERROR).GetNapiErr(env)}; + }; + const string procedureName = "clouddisk_getAllSyncFolders"; + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; +} + +std::string SyncFolderAccessNExporter::GetClassName() +{ + return SyncFolderAccessNExporter::className_; +} + +napi_value SyncFolderAccessNExporter::Constructor(napi_env env, napi_callback_info cbinfo) +{ + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOG_ERROR("Number of arguments unmatched"); + NError(E_INVALID_PARAM).ThrowErr(env); + return nullptr; + } + unique_ptr cloudDiskJSManager = make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), std::move(cloudDiskJSManager))) { + NError(E_INTERNAL_ERROR).ThrowErr(env); + return nullptr; + } + return funcArg.GetThisVar(); +} + +bool SyncFolderAccessNExporter::Export() +{ + vector props = { + NVal::DeclareNapiFunction("getAllSyncFolders", GetAllSyncFolders) + }; + string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + std::tie(succ, classValue) = NClass::DefineClass( + exports_.env_, className, SyncFolderAccessNExporter::Constructor, std::move(props)); + if (!succ) { + HILOG_ERROR("Failed to define class %{public}s", className.c_str()); + NError(E_INTERNAL_ERROR).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOG_ERROR("Failed to save class %{public}s", className.c_str()); + NError(E_INTERNAL_ERROR).ThrowErr(exports_.env_); + return false; + } + return exports_.AddProp(className, classValue); +} +} // namespace OHOS::FileManagement::CloudDiskService diff --git a/services/file_access_service.cfg b/services/file_access_service.cfg index 490b6855..694f59de 100644 --- a/services/file_access_service.cfg +++ b/services/file_access_service.cfg @@ -3,6 +3,7 @@ "name" : "file_access_service", "path" : ["/system/bin/sa_main", "/system/profile/file_access_service.json"], "uid" : "ufs", + "gid" : "ufs", "ondemand" : true, "secon" : "u:r:file_access_service:s0", "permission" : [ -- Gitee