From 722e127693966a5ae276188c26994d9c5c75da4c Mon Sep 17 00:00:00 2001 From: tlbl Date: Sun, 7 Sep 2025 09:23:18 +0800 Subject: [PATCH] clouddiskservice for napi Signed-off-by: tlbl --- BUILD.gn | 12 + bundle.json | 21 +- distributedfile.gni | 1 + .../cloud_disk_service_callback_client.h | 68 +++ .../cloud_disk_service_callback_stub.h | 41 ++ .../include/cloud_disk_service_manager_impl.h | 49 ++ .../include/service_proxy.h | 45 ++ .../src/cloud_disk_common.cpp | 352 ++++++++++++++ .../cloud_disk_service_callback_client.cpp | 19 + .../src/cloud_disk_service_callback_stub.cpp | 118 +++++ .../src/cloud_disk_service_manager.cpp | 24 + .../src/cloud_disk_service_manager_impl.cpp | 208 +++++++++ .../src/cloud_disk_service_task_manager.cpp | 152 ++++++ .../src/service_proxy.cpp | 114 +++++ .../clouddiskservice_kit_inner/BUILD.gn | 102 ++++ .../cloud_disk_common.h | 110 +++++ .../cloud_disk_service_callback.h | 32 ++ .../cloud_disk_service_manager.h | 39 ++ .../cloud_disk_service_task_manager.h | 71 +++ .../i_cloud_disk_service_callback.h | 38 ++ .../svc_death_recipient.h | 41 ++ services/5207.json | 12 + services/BUILD.gn | 12 + services/clouddiskservice.cfg | 11 + services/clouddiskservice/BUILD.gn | 125 +++++ .../clouddiskservice/ICloudDiskService.idl | 28 ++ .../ipc/include/account_status_listener.h | 47 ++ .../ipc/include/cloud_disk_service.h | 72 +++ .../cloud_disk_service_callback_manager.h | 45 ++ .../cloud_disk_service_callback_proxy.h | 38 ++ .../ipc/include/cloud_disk_sync_folder.h | 51 ++ .../ipc/src/account_status_listener.cpp | 85 ++++ .../ipc/src/cloud_disk_service.cpp | 436 ++++++++++++++++++ .../cloud_disk_service_callback_manager.cpp | 104 +++++ .../src/cloud_disk_service_callback_proxy.cpp | 137 ++++++ .../ipc/src/cloud_disk_sync_folder.cpp | 92 ++++ .../system/include/dfsu_access_token_helper.h | 1 + 37 files changed, 2952 insertions(+), 1 deletion(-) create mode 100644 frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_client.h create mode 100644 frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_stub.h create mode 100644 frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_manager_impl.h create mode 100644 frameworks/native/clouddiskservice_kit_inner/include/service_proxy.h create mode 100644 frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp create mode 100644 frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_client.cpp create mode 100644 frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_stub.cpp create mode 100644 frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager.cpp create mode 100644 frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager_impl.cpp create mode 100644 frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_task_manager.cpp create mode 100644 frameworks/native/clouddiskservice_kit_inner/src/service_proxy.cpp create mode 100644 interfaces/inner_api/native/clouddiskservice_kit_inner/BUILD.gn create mode 100644 interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_common.h create mode 100644 interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_callback.h create mode 100644 interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_manager.h create mode 100644 interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_task_manager.h create mode 100644 interfaces/inner_api/native/clouddiskservice_kit_inner/i_cloud_disk_service_callback.h create mode 100644 interfaces/inner_api/native/clouddiskservice_kit_inner/svc_death_recipient.h create mode 100644 services/5207.json create mode 100644 services/clouddiskservice.cfg create mode 100644 services/clouddiskservice/BUILD.gn create mode 100644 services/clouddiskservice/ICloudDiskService.idl create mode 100644 services/clouddiskservice/ipc/include/account_status_listener.h create mode 100644 services/clouddiskservice/ipc/include/cloud_disk_service.h create mode 100644 services/clouddiskservice/ipc/include/cloud_disk_service_callback_manager.h create mode 100644 services/clouddiskservice/ipc/include/cloud_disk_service_callback_proxy.h create mode 100644 services/clouddiskservice/ipc/include/cloud_disk_sync_folder.h create mode 100644 services/clouddiskservice/ipc/src/account_status_listener.cpp create mode 100644 services/clouddiskservice/ipc/src/cloud_disk_service.cpp create mode 100644 services/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp create mode 100644 services/clouddiskservice/ipc/src/cloud_disk_service_callback_proxy.cpp create mode 100644 services/clouddiskservice/ipc/src/cloud_disk_sync_folder.cpp diff --git a/BUILD.gn b/BUILD.gn index e77869cad..506ddd5ed 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -33,6 +33,13 @@ group("services_target") { if (dfs_service_feature_enable_cloud_adapter) { deps += [ "${services_path}:cloudfiledaemon_etc" ] } + if (dfs_service_feature_enable_cloud_disk) { + deps += [ + "${services_path}:clouddiskservice_sa_profile", + "${services_path}/clouddiskservice:clouddiskservice_sa", + "${services_path}:clouddiskservice_etc" + ] + } } group("cloudsync_kit_inner_target") { @@ -50,6 +57,11 @@ group("cloud_file_kit_inner_target") { deps = [ "interfaces/inner_api/native/cloud_file_kit_inner:cloudfile_kit" ] } +group("clouddiskservice_kit_inner_target") { + deps = + [ "interfaces/inner_api/native/clouddiskservice_kit_inner:clouddiskservice_kit_inner" ] +} + group("cloudsync_asset_kit_inner_target") { deps = [ "interfaces/inner_api/native/cloudsync_kit_inner:cloudsync_asset_kit_inner", diff --git a/bundle.json b/bundle.json index 5460115ae..d50b83552 100644 --- a/bundle.json +++ b/bundle.json @@ -20,6 +20,7 @@ ], "features": [ "dfs_service_feature_enable_cloud_adapter", + "dfs_service_feature_enable_cloud_disk", "dfs_service_feature_enable_dist_file_daemon" ], "adapted_system_type": [ @@ -71,8 +72,11 @@ "thermal_manager", "os_account", "power_manager", + "resource_schedule_service", + "user_file_service", "libfuse", - "zlib" + "zlib", + "user_file_service" ], "third_party": [] }, @@ -82,6 +86,7 @@ "fwk_group": [], "service_group": [ "//foundation/filemanagement/dfs_service:services_target", + "//foundation/filemanagement/dfs_service:clouddiskservice_kit_inner_target", "//foundation/filemanagement/dfs_service:cloudsync_kit_inner_target", "//foundation/filemanagement/dfs_service:cloud_daemon_kit_inner_target", "//foundation/filemanagement/dfs_service:cloud_file_kit_inner_target", @@ -108,6 +113,20 @@ "header_base": "//foundation/filemanagement/dfs_service/interfaces/inner_api/native/cloudsync_kit_inner" } }, + { + "name": "//foundation/filemanagement/dfs_service/interfaces/inner_api/native/clouddiskservice_kit_inner:clouddiskservice_kit_inner", + "header": { + "header_files": [ + "cloud_disk_common.h", + "cloud_disk_service_callback.h", + "cloud_disk_service_manager.h", + "cloud_disk_service_task_manager.h", + "i_cloud_disk_service_callback.h", + "svc_death_recipient.h" + ], + "header_base": "//foundation/filemanagement/dfs_service/interfaces/inner_api/native/clouddiskservice_kit_inner" + } + }, { "name": "//foundation/filemanagement/dfs_service/interfaces/inner_api/native/cloudsync_kit_inner:cloudsync_asset_kit_inner", "header": { diff --git a/distributedfile.gni b/distributedfile.gni index 2e8d8a0c1..3dafabb3b 100644 --- a/distributedfile.gni +++ b/distributedfile.gni @@ -37,6 +37,7 @@ declare_args() { cloudsync_service_power = false } dfs_service_feature_enable_cloud_adapter = false + dfs_service_feature_enable_cloud_disk = true dfs_service_feature_enable_dist_file_daemon = true if (defined(global_parts_info) && diff --git a/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_client.h b/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_client.h new file mode 100644 index 000000000..2149c09e1 --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_client.h @@ -0,0 +1,68 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_CLIENT_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_CLIENT_H + +#include "cloud_disk_service_callback.h" +#include "cloud_disk_service_callback_stub.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { +class CloudDiskServiceCallbackClient final : public CloudDiskServiceCallbackStub { +public: + explicit CloudDiskServiceCallbackClient(const std::shared_ptr &callback) + :callback_(callback) {} + + void OnChangeData(std::vector& changeData) override { + if (callback_ == nullptr) { + LOGE("callback_ is nullptr"); + return; + } + callback_->OnChangeData(changeData); + } + + void OnReturnSetFailedList(std::vector& failedList) override { + if (callback_ == nullptr) { + LOGE("callback_ is nullptr"); + return; + } + callback_->OnReturnSetFailedList(failedList); + } + + void OnReturnGetResult(std::vector& resultList) override { + if (callback_ == nullptr) { + LOGE("callback_ is nullptr"); + return; + } + callback_->OnReturnGetResult(resultList); + } +private: + std::shared_ptr callback_{ nullptr }; +}; + +// class CloudDiskServiceCallbackImpl final : public CloudDiskServiceCallback { +// public: +// void OnChangeData(std::vector& changeData) override { +// LOGE("start in client"); +// } + +// void OnReturnSetFailedList(vector& failedList) override { +// LOGE("start in client"); +// } +// }; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_CLIENT_H \ No newline at end of file diff --git a/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_stub.h b/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_stub.h new file mode 100644 index 000000000..00ee31581 --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_callback_stub.h @@ -0,0 +1,41 @@ +/* +* 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_STUB_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_STUB_H + +#include + +#include "i_cloud_disk_service_callback.h" +#include "iremote_stub.h" + +namespace OHOS::FileManagement::CloudDiskService { +using ServiceInterface = std::function; +class CloudDiskServiceCallbackStub : public IRemoteStub { +public: + CloudDiskServiceCallbackStub(); + virtual ~CloudDiskServiceCallbackStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + std::map opToInterfaceMap_; + + int32_t HandleOnChangeData(MessageParcel &data, MessageParcel &reply); + int32_t HandleOnReturnSetFailedList(MessageParcel &data, MessageParcel &reply); + int32_t HandleOnReturnGetResult(MessageParcel &data, MessageParcel &reply); +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_STUB_H \ No newline at end of file diff --git a/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_manager_impl.h b/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_manager_impl.h new file mode 100644 index 000000000..2b90c4ace --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/include/cloud_disk_service_manager_impl.h @@ -0,0 +1,49 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_MANAGER_IMPL_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_MANAGER_IMPL_H + +#include + +#include "nocopyable.h" + +#include "cloud_disk_service_manager.h" +#include "cloud_disk_service_callback_client.h" +#include "svc_death_recipient.h" + +namespace OHOS::FileManagement::CloudDiskService { +class CloudDiskServiceManagerImpl final : public CloudDiskServiceManager, public NoCopyable { +public: + static CloudDiskServiceManagerImpl &GetInstance(); + + int32_t RegisterSyncFolderChanges(const std::string& syncFolder, const std::shared_ptr callback) override; + int32_t UnregisterSyncFolderChanges(const std::string& syncFolder) override; + int32_t GetSyncFolderChanges(const std::string &syncFolder, uint64_t count, uint64_t startUsn, ChangesResult& changesResult) override; + int32_t SetFileSyncStates(const std::string& syncFolder, const std::vector& fileSyncStates, const std::shared_ptr callback) override; + int32_t GetFileSyncStates(const std::string& syncFolder, const std::vector& pathArray, const std::shared_ptr callback) override; + int32_t RegisterSyncFolder(int32_t userId, const std::string& bundleName, const std::string& path) override; + int32_t UnregisterSyncFolder(int32_t userId, const std::string& bundleName, const std::string& path) override; + +private: + void SetDeathRecipient(const sptr &remoteObject); + std::shared_ptr callback_; + sptr deathRecipient_; + std::mutex callbackMutex_; + std::atomic_flag isFirstCall_{false}; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_MANAGER_IMPL_H diff --git a/frameworks/native/clouddiskservice_kit_inner/include/service_proxy.h b/frameworks/native/clouddiskservice_kit_inner/include/service_proxy.h new file mode 100644 index 000000000..23b900812 --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/include/service_proxy.h @@ -0,0 +1,45 @@ +/* + * 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 OHOS_FILEMGMT_SERVICE_PROXY_H +#define OHOS_FILEMGMT_SERVICE_PROXY_H + +#include "icloud_disk_service.h" +#include "iremote_proxy.h" +#include "system_ability_load_callback_stub.h" + +namespace OHOS::FileManagement::CloudDiskService { +class ServiceProxy : public IRemoteProxy { +public: + static sptr GetInstance(); + static void InvaildInstance(); + + // class CloudDiskServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub { + // public: + // void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) override; + // void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; + + // std::condition_variable proxyConVar_; + // std::atomic isLoadSuccess_{false}; + // }; + +private: + // static inline std::mutex proxyMutex_; + static inline std::mutex instanceMutex_; + static inline sptr serviceProxy_; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_SERVICE_PROXY_H diff --git a/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp new file mode 100644 index 000000000..c74a0cd95 --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp @@ -0,0 +1,352 @@ +/* + * 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 "cloud_disk_common.h" + +#include + +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { + +// CloudDiskCommon &CloudDiskCommon::GetInstance() +// { +// static CloudDiskCommon instance; +// return instance; +// } + + +bool FileSyncState::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(path)) { + LOGE("failed to write path"); + return false; + } + + if (!parcel.WriteInt32(static_cast(state))) { + LOGE("failed to write state"); + return false; + } + + return true; +} + +FileSyncState *FileSyncState::Unmarshalling(Parcel &parcel) +{ + FileSyncState *info = new (std::nothrow) FileSyncState(); + if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { + LOGW("read from parcel failed"); + delete info; + info = nullptr; + } + return info; +} + +bool FileSyncState::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadString(path)) { + LOGE("failed to write path"); + return false; + } + + int32_t readState = 0; + if (!parcel.ReadInt32(readState)) { + LOGE("failed to write state"); + return false; + } + state = static_cast(readState); + + return true; +} + + +bool ChangeData::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteUint64(updateSequenceNumber)) { + LOGE("failed to write updateSequenceNumber"); + return false; + } + + if (!parcel.WriteString(fileId)) { + LOGE("failed to write fileId"); + return false; + } + + if (!parcel.WriteString(parentFileId)) { + LOGE("failed to write parentFileId"); + return false; + } + + if (!parcel.WriteString(relativePath)) { + LOGE("failed to write relativePath"); + return false; + } + + if (!parcel.WriteInt32(static_cast(operationType))) { + LOGE("failed to write operationType"); + return false; + } + + if (!parcel.WriteUint64(size)) { + LOGE("failed to write size"); + return false; + } + + if (!parcel.WriteUint64(mtime)) { + LOGE("failed to write mtime"); + return false; + } + + if (!parcel.WriteUint64(timeStamp)) { + LOGE("failed to write timeStamp"); + return false; + } + + return true; +} + +ChangeData *ChangeData::Unmarshalling(Parcel &parcel) +{ + ChangeData *info = new (std::nothrow) ChangeData(); + if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { + LOGW("read from parcel failed"); + delete info; + info = nullptr; + } + return info; +} + +bool ChangeData::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadUint64(updateSequenceNumber)) { + LOGE("failed to read updateSequenceNumber"); + return false; + } + + if (!parcel.ReadString(fileId)) { + LOGE("failed to read fileId"); + return false; + } + + if (!parcel.ReadString(parentFileId)) { + LOGE("failed to read parentFileId"); + return false; + } + + if (!parcel.ReadString(relativePath)) { + LOGE("failed to read relativePath"); + return false; + } + + int32_t readType = 0; + if (!parcel.ReadInt32(readType)) { + LOGE("failed to write operationType"); + return false; + } + operationType = static_cast(readType); + + if (!parcel.ReadUint64(size)) { + LOGE("failed to read size"); + return false; + } + + if (!parcel.ReadUint64(mtime)) { + LOGE("failed to read mtime"); + return false; + } + + if (!parcel.ReadUint64(timeStamp)) { + LOGE("failed to read timeStamp"); + return false; + } + + return true; +} + +bool ChangesResult::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteUint64(nextUsn)) { + LOGE("failed to write nextUsn"); + return false; + } + + if (!parcel.WriteInt32(static_cast(isEof))) { + LOGE("failed to write isEof"); + return false; + } + + if (!parcel.WriteInt32(static_cast(changesData.size()))) { + LOGE("failed to write changeData"); + return false; + } + + for(auto& item : changesData) { + item.Marshalling(parcel); + } + + return true; +} + +ChangesResult *ChangesResult::Unmarshalling(Parcel &parcel) +{ + ChangesResult *info = new (std::nothrow) ChangesResult(); + if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { + LOGW("read from parcel failed"); + delete info; + info = nullptr; + } + return info; +} + +bool ChangesResult::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadUint64(nextUsn)) { + LOGE("failed to read nextUSN"); + return false; + } + + int32_t readIsEof = 0; + if (!parcel.ReadInt32(readIsEof)) { + LOGE("failed to read isEof"); + return false; + } + isEof = readIsEof; + + int32_t size = 0; + if (!parcel.ReadInt32(size)) { + LOGE("failed to read changeData size"); + return false; + } + for(int32_t i = 0; i < size; ++i) { + ChangeData changeData; + changeData.ReadFromParcel(parcel); + changesData.push_back(changeData); + } + + return true; +} + +bool FailedList::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(path)) { + LOGE("failed to write path"); + return false; + } + + if (!parcel.WriteInt32(static_cast(error))) { + LOGE("failed to write error"); + return false; + } + + return true; +} + +FailedList *FailedList::Unmarshalling(Parcel &parcel) +{ + FailedList *info = new (std::nothrow) FailedList(); + if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { + LOGW("read from parcel failed"); + delete info; + info = nullptr; + } + return info; +} + +bool FailedList::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadString(path)) { + LOGE("failed to read path"); + return false; + } + + int32_t readError = 0; + if (!parcel.ReadInt32(readError)) { + LOGE("failed to write errno"); + return false; + } + error = static_cast(readError); + + return true; +} + +bool ResultList::Marshalling(Parcel &parcel) const +{ + + if (!parcel.WriteInt32(static_cast(isSuccess))) { + LOGE("failed to write isSuccess"); + return false; + } + + if (!parcel.WriteInt32(static_cast(state))) { + LOGE("failed to write state"); + return false; + } + + if (!parcel.WriteString(path)) { + LOGE("failed to write path"); + return false; + } + + if (!parcel.WriteInt32(static_cast(error))) { + LOGE("failed to write error"); + return false; + } + + return true; +} + +ResultList *ResultList::Unmarshalling(Parcel &parcel) +{ + ResultList *info = new (std::nothrow) ResultList(); + if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { + LOGW("read from parcel failed"); + delete info; + info = nullptr; + } + return info; +} + +bool ResultList::ReadFromParcel(Parcel &parcel) +{ + int32_t readIsSuccess = 0; + if (!parcel.ReadInt32(readIsSuccess)) { + LOGE("failed to write isSuccess"); + return false; + } + isSuccess= readIsSuccess; + + int32_t readState = 0; + if (!parcel.ReadInt32(readState)) { + LOGE("failed to write state"); + return false; + } + state = static_cast(readState); + + if (!parcel.ReadString(path)) { + LOGE("failed to write path"); + return false; + } + + int32_t readError = 0; + if (!parcel.ReadInt32(readError)) { + LOGE("failed to write state"); + return false; + } + error = static_cast(readError); + + return true; +} + +} // namespace OHOS::FileManagement::CloudDiskService diff --git a/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_client.cpp b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_client.cpp new file mode 100644 index 000000000..148c3423c --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_client.cpp @@ -0,0 +1,19 @@ +/* + * 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 "cloud_disk_service_callback_client.h" + +namespace OHOS::FileManagement::CloudDiskService { +} // namespace OHOS::FileManagement::CloudDiskService diff --git a/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_stub.cpp b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_stub.cpp new file mode 100644 index 000000000..e4f533e7b --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_stub.cpp @@ -0,0 +1,118 @@ +/* + * 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 "cloud_disk_service_callback_stub.h" +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; + +CloudDiskServiceCallbackStub::CloudDiskServiceCallbackStub() +{ + opToInterfaceMap_[SERVICE_CMD_ON_CHANGE_DATA] = [this](MessageParcel &data, MessageParcel &reply) { + return this->HandleOnChangeData(data, reply); + }; + opToInterfaceMap_[SERVICE_CMD_ON_RETURN_SET_FAILED_LIST] = [this](MessageParcel &data, MessageParcel &reply) { + return this->HandleOnReturnSetFailedList(data, reply); + }; + opToInterfaceMap_[SERVICE_CMD_ON_RETURN_GET_RESULT] = [this](MessageParcel &data, MessageParcel &reply) { + return this->HandleOnReturnGetResult(data, reply); + }; +} + +int32_t CloudDiskServiceCallbackStub::OnRemoteRequest(uint32_t code, + MessageParcel &data, + MessageParcel &reply, + MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + return E_SERVICE_DESCRIPTOR_IS_EMPTY; + } + auto interfaceIndex = opToInterfaceMap_.find(code); + if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) { + LOGE("Cannot response request %d: unknown tranction", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + auto memberFunc = interfaceIndex->second; + return memberFunc(data, reply); +} + +int32_t CloudDiskServiceCallbackStub::HandleOnChangeData(MessageParcel &data, MessageParcel &reply) +{ + std::vector changesData; + + auto size = data.ReadInt32(); + if (size > static_cast(VECTOR_MAX_SIZE)) { + return ERR_INVALID_DATA; + } + for (int i = 0; i < size; ++i) { + std::unique_ptr value(data.ReadParcelable()); + if(!value) { + LOGE("object of ChangeData is nullptr"); + return E_INVAL_ARG; + } + changesData.push_back(*value); + } + + OnChangeData(changesData); + LOGI("HandleOnChangeData end"); + return E_OK; +} + +int32_t CloudDiskServiceCallbackStub::HandleOnReturnSetFailedList(MessageParcel &data, MessageParcel &reply) +{ + std::vector failedList; + + auto size = data.ReadInt32(); + if (size > static_cast(VECTOR_MAX_SIZE)) { + return ERR_INVALID_DATA; + } + for (int i = 0; i < size; ++i) { + std::unique_ptr value(data.ReadParcelable()); + if(!value) { + LOGE("object of failedList is nullptr"); + return E_INVAL_ARG; + } + failedList.push_back(*value); + } + + OnReturnSetFailedList(failedList); + LOGI("HandleOnReturnSetFailedList end"); + return E_OK; +} + +int32_t CloudDiskServiceCallbackStub::HandleOnReturnGetResult(MessageParcel &data, MessageParcel &reply) +{ + std::vector resultList; + + auto size = data.ReadInt32(); + if (size > static_cast(VECTOR_MAX_SIZE)) { + return ERR_INVALID_DATA; + } + for (int i = 0; i < size; ++i) { + std::unique_ptr value(data.ReadParcelable()); + if(!value) { + LOGE("object of resultList is nullptr"); + return E_INVAL_ARG; + } + resultList.push_back(*value); + } + + OnReturnGetResult(resultList); + LOGI("HandleOnReturnGetResult end"); + return E_OK; +} +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager.cpp b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager.cpp new file mode 100644 index 000000000..808ff75d8 --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager.cpp @@ -0,0 +1,24 @@ +/* + * 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 "cloud_disk_service_manager_impl.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; +CloudDiskServiceManager &CloudDiskServiceManager::GetInstance() +{ + return CloudDiskServiceManagerImpl::GetInstance(); +} +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager_impl.cpp b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager_impl.cpp new file mode 100644 index 000000000..1f9cb668f --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager_impl.cpp @@ -0,0 +1,208 @@ +/* + * 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 "cloud_disk_service_manager_impl.h" + +// #include "dfsu_access_token_helper.h" +#include "cloud_disk_service_callback_client.h" +#include "cloud_file_utils.h" +#include "dfs_error.h" +#include "iservice_registry.h" +#include "service_proxy.h" +#include "system_ability_definition.h" +#include "utils_directory.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; +CloudDiskServiceManagerImpl &CloudDiskServiceManagerImpl::GetInstance() +{ + static CloudDiskServiceManagerImpl instance; + return instance; +} + +int32_t CloudDiskServiceManagerImpl::RegisterSyncFolderChanges(const std::string& syncFolder, + const std::shared_ptr callback) +{ +#ifdef SUPPORT_CLOUD_DISK_SERVICE + if (!callback) { + LOGE("callback is null"); + return E_INVAL_ARG; + } + auto serviceProxy = ServiceProxy::GetInstance(); + if (!serviceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + auto ret = serviceProxy->RegisterSyncFolderChangesInner(syncFolder, + sptr(new (std::nothrow) CloudDiskServiceCallbackClient(callback))); + { + unique_lock lock(callbackMutex_); + callback_ = callback; + } + SetDeathRecipient(serviceProxy->AsObject()); + LOGI("RegisterSyncFolderChanges ret %{public}d", ret); + return ret; +#else + return 0; +#endif +} + +int32_t CloudDiskServiceManagerImpl::UnregisterSyncFolderChanges(const std::string& syncFolder) +{ +#ifdef SUPPORT_CLOUD_DISK_SERVICE + auto serviceProxy = ServiceProxy::GetInstance(); + if (!serviceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + + auto ret = serviceProxy->UnregisterSyncFolderChangesInner(syncFolder); + { + unique_lock lock(callbackMutex_); + callback_ = nullptr; + } + SetDeathRecipient(serviceProxy->AsObject()); + LOGI("UnregisterSyncFolderChanges ret %{public}d", ret); + return ret; +#else + return 0; +#endif +} + +int32_t CloudDiskServiceManagerImpl::GetSyncFolderChanges(const std::string &syncFolder, uint64_t count, uint64_t startUsn, ChangesResult& changesResult) +{ +#ifdef SUPPORT_CLOUD_DISK_SERVICE + auto serviceProxy = ServiceProxy::GetInstance(); + if (!serviceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + + auto ret = serviceProxy->GetSyncFolderChangesInner(syncFolder, count, startUsn, changesResult); + SetDeathRecipient(serviceProxy->AsObject()); + LOGI("GetSyncFolderChangesInner ret %{public}d", ret); + return ret; +#else + return 0; +#endif +} + +int32_t CloudDiskServiceManagerImpl::SetFileSyncStates(const std::string& syncFolder, const std::vector& fileSyncStates, + const std::shared_ptr callback) +{ +#ifdef SUPPORT_CLOUD_DISK_SERVICE + LOGI("start SetXattr in impl"); + + if (!callback) { + LOGE("callback is null"); + return E_INVAL_ARG; + } + + auto serviceProxy = ServiceProxy::GetInstance(); + if (!serviceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + + auto ret = serviceProxy->SetFileSyncStatesInner(syncFolder, fileSyncStates, sptr(new (std::nothrow) CloudDiskServiceCallbackClient(callback))); + SetDeathRecipient(serviceProxy->AsObject()); + LOGI("SetFileSyncState, ret %{public}d", ret); + return ret; +#else + return 0; +#endif +} + +int32_t CloudDiskServiceManagerImpl::GetFileSyncStates(const std::string& syncFolder, const std::vector& pathArray, const std::shared_ptr callback) +{ +#ifdef SUPPORT_CLOUD_DISK_SERVICE + LOGI("start GetXattr in impl"); + + auto serviceProxy = ServiceProxy::GetInstance(); + if (!serviceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + + auto ret = serviceProxy->GetFileSyncStatesInner(syncFolder, pathArray, sptr(new (std::nothrow) CloudDiskServiceCallbackClient(callback))); + SetDeathRecipient(serviceProxy->AsObject()); + LOGI("GetFileSyncState, ret %{public}d", ret); + return ret; +#else + return 0; +#endif +} + +int32_t CloudDiskServiceManagerImpl::RegisterSyncFolder(int32_t userId, const std::string& bundleName, const std::string& path) +{ +#ifdef SUPPORT_CLOUD_DISK_SERVICE + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + LOGI("start RegisterSyncFolder in impl"); + auto serviceProxy = ServiceProxy::GetInstance(); + if (!serviceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + + auto ret = serviceProxy->RegisterSyncFolderInner(userId, bundleName, path); + SetDeathRecipient(serviceProxy->AsObject()); + LOGI("RegisterSyncFolder, ret %{public}d", ret); + return ret; +#else + return 0; +#endif +} + +int32_t CloudDiskServiceManagerImpl::UnregisterSyncFolder(int32_t userId, const std::string& bundleName, const std::string& path) +{ +#ifdef SUPPORT_CLOUD_DISK_SERVICE + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + LOGI("start UnregisterSyncFolder in impl"); + auto serviceProxy = ServiceProxy::GetInstance(); + if (!serviceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + + auto ret = serviceProxy->UnregisterSyncFolderInner(userId, bundleName, path); + SetDeathRecipient(serviceProxy->AsObject()); + LOGI("UnregisterSyncFolder, ret %{public}d", ret); + return ret; +#else + return 0; +#endif +} + +void CloudDiskServiceManagerImpl::SetDeathRecipient(const sptr &remoteObject) +{ + if (!isFirstCall_.test_and_set()) { + auto deathCallback = [this](const wptr &obj) { + LOGE("service died."); + ServiceProxy::InvaildInstance(); + if (callback_) { + callback_->OnDeathRecipient(); + } + isFirstCall_.clear(); + }; + deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback)); + if (!remoteObject->AddDeathRecipient(deathRecipient_)) { + LOGE("add death recipient failed"); + isFirstCall_.clear(); + } + } +} + +} // namespace OHOS::FileManagement::CloudDiskService diff --git a/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_task_manager.cpp b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_task_manager.cpp new file mode 100644 index 000000000..145c3a0b0 --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_task_manager.cpp @@ -0,0 +1,152 @@ +/* + * 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 "cloud_disk_service_task_manager.h" + +#include "iservice_registry.h" +#include "mem_mgr_client.h" +#include "parameters.h" +#include "system_ability_definition.h" +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { +using namespace std; + +const int32_t DELAY_TIME = 90000; // ms +const int32_t SYSTEM_LOAD_DELAY_TIME = 600000; // ms + +CloudDiskServiceTaskManager &CloudDiskServiceTaskManager::GetInstance() +{ + static CloudDiskServiceTaskManager instance; + return instance; +} + +CloudDiskServiceTaskManager::CloudDiskServiceTaskManager() : queue_("unloadTask") +{ +} + +void CloudDiskServiceTaskManager::StartTask(TaskKey key, TaskType task) +{ + CancelUnloadTask(); + std::lock_guard lock(taskMapsMutex_); + if (criticalStatus_ == false) { + int32_t ret = Memory::MemMgrClient::GetInstance().SetCritical(getpid(), + true, FILEMANAGEMENT_CLOUD_DISK_SERVICE_SA_ID); + if (ret == ERR_OK) { + criticalStatus_ = true; + } + } + auto iterator = taskMaps_.find(key); + if (iterator == taskMaps_.end()) { + taskMaps_[key] = static_cast(task); + return; + } + auto taskState = iterator->second | static_cast(task); + taskMaps_[key] = taskState; +} + +void CloudDiskServiceTaskManager::CompleteTask(TaskKey key, TaskType task) +{ + std::lock_guard lock(taskMapsMutex_); + auto iterator = taskMaps_.find(key); + if (iterator == taskMaps_.end()) { + LOGE("task is not started"); + } else { + taskMaps_[key] = iterator->second & ~static_cast(task); + if (taskMaps_[key] == 0) { + LOGI("start erase"); + taskMaps_.erase(key); + } + } + // if (taskMaps_.empty()) { + // DelayUnloadTask(true); + // } +} + +void CloudDiskServiceTaskManager::StartTask() +{ + std::lock_guard lock(taskMapsMutex_); + // if (taskMaps_.empty()) { + // DelayUnloadTask(false); + // } +} + +bool CloudDiskServiceTaskManager::HasTask(TaskKey key, TaskType task) +{ + std::lock_guard lock(taskMapsMutex_); + auto iterator = taskMaps_.find(key); + if (iterator != taskMaps_.end()) { + if (taskMaps_[key] & static_cast(task)) { + return true; + } + } + return false; +} + + +void CloudDiskServiceTaskManager::CancelUnloadTask() +{ + std::lock_guard lock(unloadTaskMutex_); + if (unloadTaskHandle_ == nullptr) { + return; + } + LOGD("cancel unload task"); + queue_.cancel(unloadTaskHandle_); + unloadTaskHandle_ = nullptr; +} + +// void CloudDiskServiceTaskManager::DelayUnloadTask(bool needSetCritical) +// { +// LOGI("delay unload task begin"); +// auto delayTime = DELAY_TIME; + +// if (needSetCritical == true && criticalStatus_ == true) { +// int32_t ret = Memory::MemMgrClient::GetInstance().SetCritical(getpid(), +// false, FILEMANAGEMENT_CLOUD_DISK_SERVICE_SA_ID); +// if (ret == ERR_OK) { +// criticalStatus_ = false; +// } +// } +// auto task = [this]() { +// LOGI("do unload task"); +// { +// std::lock_guard lock(unloadTaskMutex_); +// unloadTaskHandle_ = nullptr; +// } + +// auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); +// if (samgrProxy == nullptr) { +// LOGE("get samgr failed"); +// return; +// } +// // system::SetParameter(CLOUD_FILE_SERVICE_SA_STATUS_FLAG, CLOUD_FILE_SERVICE_SA_END); +// int32_t ret = samgrProxy->UnloadSystemAbility(FILEMANAGEMENT_CLOUD_DISK_SERVICE_SA_ID); +// if (ret != ERR_OK) { +// LOGE("remove system ability failed"); +// return; +// } +// }; + +// CancelUnloadTask(); +// std::lock_guard lock(unloadTaskMutex_); +// std::chrono::milliseconds ms(delayTime); +// auto us = std::chrono::duration_cast(ms); +// unloadTaskHandle_ = queue_.submit_h(task, ffrt::task_attr().delay(us.count())); +// } +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/clouddiskservice_kit_inner/src/service_proxy.cpp b/frameworks/native/clouddiskservice_kit_inner/src/service_proxy.cpp new file mode 100644 index 000000000..9fa81d389 --- /dev/null +++ b/frameworks/native/clouddiskservice_kit_inner/src/service_proxy.cpp @@ -0,0 +1,114 @@ +/* + * 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 "service_proxy.h" + +#include + +#include "cloud_disk_service_proxy.h" +#include "dfs_error.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; + +// constexpr int LOAD_SA_TIMEOUT_MS = 2000; + +sptr ServiceProxy::GetInstance() +{ + LOGD("getinstance"); + unique_lock lock(instanceMutex_); + if (serviceProxy_ != nullptr) { + if (serviceProxy_->AsObject() != nullptr && !serviceProxy_->AsObject()->IsObjectDead()) { + return serviceProxy_; + } + } + + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + LOGE("Samgr is nullptr"); + return nullptr; + } + + auto object = samgr->CheckSystemAbility(FILEMANAGEMENT_CLOUD_DISK_SERVICE_SA_ID); + if (object == nullptr) { + LOGE("CloudDiskService::Connect object == nullptr"); + return nullptr; + } + + serviceProxy_ = iface_cast(object); + if (serviceProxy_ == nullptr) { + LOGE("CloudDiskService::Connect service == nullptr"); + return nullptr; + } + + // sptr cloudDiskServiceLoadCallback = new CloudDiskServiceProxyLoadCallback(); + // if (cloudDiskServiceLoadCallback == nullptr) { + // LOGE("cloudDiskServiceLoadCallback is nullptr"); + // return nullptr; + // } + + // int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_DISK_SERVICE_SA_ID, cloudDiskServiceLoadCallback); + // if (ret != E_OK) { + // LOGE("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d", + // FILEMANAGEMENT_CLOUD_DISK_SERVICE_SA_ID, ret); + // return nullptr; + // } + + + // unique_lock proxyLock(proxyMutex_); + // auto waitStatus = cloudDiskServiceLoadCallback->proxyConVar_.wait_for( + // proxyLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), + // [cloudDiskServiceLoadCallback]() { return cloudDiskServiceLoadCallback->isLoadSuccess_.load(); }); + // if (!waitStatus) { + // LOGE("Load CloudDiskService SA timeout"); + // return nullptr; + // } + return serviceProxy_; +} + +void ServiceProxy::InvaildInstance() +{ + LOGI("invalid instance"); + unique_lock lock(instanceMutex_); + serviceProxy_ = nullptr; +} + +// void ServiceProxy::CloudDiskServiceProxyLoadCallback::OnLoadSystemAbilitySuccess( +// int32_t systemAbilityId, +// const sptr &remoteObject) +// { +// LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", systemAbilityId, +// (remoteObject == nullptr ? "false" : "true")); +// unique_lock lock(proxyMutex_); +// if (serviceProxy_ != nullptr) { +// LOGE("CloudDiskService SA proxy has been loaded"); +// } else { +// serviceProxy_ = iface_cast(remoteObject); +// } +// isLoadSuccess_.store(true); +// proxyConVar_.notify_one(); +// } + +// void ServiceProxy::CloudDiskServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +// { +// LOGI("Load CloudDiskService SA failed,systemAbilityId:%{public}d", systemAbilityId); +// unique_lock lock(proxyMutex_); +// serviceProxy_ = nullptr; +// isLoadSuccess_.store(false); +// proxyConVar_.notify_one(); +// } +} // namespace OHOS::FileManagement::CloudDiskService diff --git a/interfaces/inner_api/native/clouddiskservice_kit_inner/BUILD.gn b/interfaces/inner_api/native/clouddiskservice_kit_inner/BUILD.gn new file mode 100644 index 000000000..1f6f19702 --- /dev/null +++ b/interfaces/inner_api/native/clouddiskservice_kit_inner/BUILD.gn @@ -0,0 +1,102 @@ +# 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/dfs_service/distributedfile.gni") + +config("public_config") { + include_dirs = [ + ".", + "${distributedfile_path}/interfaces/inner_api/native/clouddiskservice_kit_inner", + ] +} + +config("private_config") { + include_dirs = [ + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/include", + "${distributedfile_path}/interfaces/inner_api/native/clouddiskservice_kit_inner", + "${distributedfile_path}/utils/log/include", + ] +} + +config("optimize-size") { + cflags = [ + "-fdata-sections", + "-ffunction-sections", + "-Oz", + ] + cflags_cc = [ + "-fvisibility-inlines-hidden", + "-Oz", + ] +} + +ohos_shared_library("clouddiskservice_kit_inner") { + branch_protector_ret = "pac_ret" + configs = [ ":optimize-size" ] + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + sources = [ + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_callback_stub.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager_impl.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_task_manager.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/service_proxy.cpp", + "${distributedfile_path}/utils/log/src/utils_log.cpp", + ] + + defines = [ + "LOG_DOMAIN=0xD004309", + "LOG_TAG=\"CLOUDDISKSERVICE_API\"", + ] + + if (dfs_service_feature_enable_cloud_disk) { + defines += [ "SUPPORT_CLOUD_DISK_SERVICE "] + } + + configs += [ ":private_config" ] + public_configs = [ + ":public_config", + "${services_path}/clouddiskservice:cloud_disk_service_public_config", + ] + + external_deps = [ + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:dataobs_manager", + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "memmgr:memmgrclient", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + deps = [ + "${services_path}/clouddiskservice:libcloud_disk_service_proxy", + "${utils_path}:libdistributedfiledentry", + "${utils_path}:libdistributedfileutils", + ] + + part_name = "dfs_service" + subsystem_name = "filemanagement" +} + diff --git a/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_common.h b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_common.h new file mode 100644 index 000000000..53a96bbf8 --- /dev/null +++ b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_common.h @@ -0,0 +1,110 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_COMMON_H +#define OHOS_FILEMGMT_CLOUD_DISK_COMMON_H + +#include +#include + +#include "parcel.h" + +namespace OHOS::FileManagement::CloudDiskService { + +/* +When adding new enumeration values, pay attention to the maximum enumeration value judgment of the getxattr method in clouddiskservice.cpp +*/ +enum class SyncState { + IDLE = 0, + SYNCING, + SYNC_SUCCESSED, + SYNC_FAILED, + SYNC_CANCELED, + SYNC_CONFLICTED, +}; + +enum class OperationType { + CREATE = 0, + DELETE, + MOVE_FROM, + MOVE_TO, + CLOSE_WRITE, + SYNC_FOLDER_INVALID, + OPERATION_MAX, +}; + +struct FileSyncState : public Parcelable{ + std::string path; + SyncState state; + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static FileSyncState *Unmarshalling(Parcel &parcel); +}; + +struct ChangeData : public Parcelable { + uint64_t updateSequenceNumber; + std::string fileId; + std::string parentFileId; + std::string relativePath; + OperationType operationType; + uint64_t size; + uint64_t mtime; + uint64_t timeStamp; + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static ChangeData *Unmarshalling(Parcel &parcel); +}; + +struct ChangesResult : public Parcelable { + uint64_t nextUsn; + bool isEof; + std::vector changesData; + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static ChangesResult *Unmarshalling(Parcel &parcel); +}; + +enum class ErrorReason { + INVALID_ARGUMENT = 0, + NO_SUCH_FILE, + NO_SPACE_LEFT, + OUT_OF_RANGE, +}; + +struct FailedList : public Parcelable { + std::string path; + ErrorReason error; + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static FailedList *Unmarshalling(Parcel &parcel); +}; + +struct ResultList : public FailedList { + bool isSuccess; + SyncState state; + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static ResultList *Unmarshalling(Parcel &parcel); +}; + +#define RETURN_ON_ERR(ret) \ + do { \ + int32_t res = ret; \ + if ((res) != E_OK) { \ + return res; \ + } \ + } while (0) +} // namespace OHOS::FileManagement::CloudDiskService +#endif // OHOS_FILEMGMT_CLOUD_DISK_COMMON_H diff --git a/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_callback.h b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_callback.h new file mode 100644 index 000000000..254134a9d --- /dev/null +++ b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_callback.h @@ -0,0 +1,32 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_H + +#include "cloud_disk_common.h" + +namespace OHOS::FileManagement::CloudDiskService { +class CloudDiskServiceCallback { +public: + virtual ~CloudDiskServiceCallback() = default; + virtual void OnChangeData(std::vector &changeData) = 0; + virtual void OnReturnSetFailedList(std::vector &FailedList) = 0; + virtual void OnReturnGetResult(std::vector &resultList) = 0; + virtual void OnDeathRecipient() {}; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_H \ No newline at end of file diff --git a/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_manager.h b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_manager.h new file mode 100644 index 000000000..c7cb36bcb --- /dev/null +++ b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_manager.h @@ -0,0 +1,39 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_MANAGER_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_MANAGER_H + +#include + +#include "cloud_disk_service_callback.h" + +namespace OHOS::FileManagement::CloudDiskService { +class CloudDiskServiceManager { +public: + static CloudDiskServiceManager &GetInstance(); + + virtual int32_t RegisterSyncFolderChanges(const std::string& syncFolder, + const std::shared_ptr callback) = 0; + virtual int32_t UnregisterSyncFolderChanges(const std::string& syncFolder) = 0; + virtual int32_t GetSyncFolderChanges(const std::string &syncFolder, uint64_t count, uint64_t startUsn, ChangesResult& changesResult) = 0; + virtual int32_t SetFileSyncStates(const std::string& syncFolder, const std::vector& fileSyncStates, const std::shared_ptr callback) = 0; + virtual int32_t GetFileSyncStates(const std::string& syncFolder, const std::vector& pathArray, const std::shared_ptr callback) = 0; + virtual int32_t RegisterSyncFolder(int32_t userId, const std::string& bundleName, const std::string& path) = 0; + virtual int32_t UnregisterSyncFolder(int32_t userId, const std::string& bundleName, const std::string& path) = 0; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_MANAGER_H diff --git a/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_task_manager.h b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_task_manager.h new file mode 100644 index 000000000..b65c3e841 --- /dev/null +++ b/interfaces/inner_api/native/clouddiskservice_kit_inner/cloud_disk_service_task_manager.h @@ -0,0 +1,71 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_TASK_MANAGER_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_TASK_MANAGER_H + +#include +#include +#include "ffrt_inner.h" + +namespace OHOS::FileManagement::CloudDiskService { +// static const std::string CLOUD_FILE_SERVICE_SA_STATUS_FLAG = "persist.kernel.medialibrarydata.stopflag"; +// static const std::string CLOUD_FILE_SERVICE_SA_START = "0"; +// static const std::string CLOUD_FILE_SERVICE_SA_END = "1"; +enum class TaskType : uint64_t { + REGISTER_TASK = 1, + UNREGISTER_TASK = 1 << 1, + GET_TASK = 1 << 2, +}; + +struct TaskKey { + std::string bundleName; + std::string syncChronousRootPath; + + bool operator ==(const TaskKey& other) const { + return bundleName == other.bundleName && + syncChronousRootPath == other.syncChronousRootPath; + } + + struct Hash { + size_t operator()(const TaskKey& key) const { + auto hasher = std::hash(); + return hasher(key.bundleName) ^ (hasher(key.syncChronousRootPath) << 1); + } + }; +}; + +class CloudDiskServiceTaskManager : public NoCopyable { +public: + static CloudDiskServiceTaskManager &GetInstance(); + ~CloudDiskServiceTaskManager() = default; + void StartTask(TaskKey key, TaskType task); + void CompleteTask(TaskKey key, TaskType task); + bool HasTask(TaskKey key, TaskType task); + void StartTask(); +private: + CloudDiskServiceTaskManager(); + void DelayUnloadTask(bool needSetCritical); + void CancelUnloadTask(); + + bool criticalStatus_ = true; + std::mutex taskMapsMutex_; + std::unordered_map taskMaps_; + ffrt::queue queue_; + ffrt::task_handle unloadTaskHandle_; + ffrt::mutex unloadTaskMutex_; +}; +} // namespace OHOS::FileManagement::CloudDiskService +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_TASK_MANAGER_H \ No newline at end of file diff --git a/interfaces/inner_api/native/clouddiskservice_kit_inner/i_cloud_disk_service_callback.h b/interfaces/inner_api/native/clouddiskservice_kit_inner/i_cloud_disk_service_callback.h new file mode 100644 index 000000000..97f4aa30c --- /dev/null +++ b/interfaces/inner_api/native/clouddiskservice_kit_inner/i_cloud_disk_service_callback.h @@ -0,0 +1,38 @@ +/* + * 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 OHOS_FILEMGMT_I_CLOUD_DISK_SERVICE_CALLBACK_H +#define OHOS_FILEMGMT_I_CLOUD_DISK_SERVICE_CALLBACK_H + +#include "iremote_broker.h" +#include "cloud_disk_service_callback.h" + +namespace OHOS::FileManagement::CloudDiskService { +class ICloudDiskServiceCallback : public CloudDiskServiceCallback, public IRemoteBroker { +public: + enum { + SERVICE_CMD_ON_CHANGE_DATA = 0, + SERVICE_CMD_ON_RETURN_SET_FAILED_LIST = 1, + SERVICE_CMD_ON_RETURN_GET_RESULT = 2, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Dfs.ICloudDiskServiceCallback") + +protected : + const int VECTOR_MAX_SIZE = 102400; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_I_CLOUD_DISK_SERVICE_CALLBACK_H \ No newline at end of file diff --git a/interfaces/inner_api/native/clouddiskservice_kit_inner/svc_death_recipient.h b/interfaces/inner_api/native/clouddiskservice_kit_inner/svc_death_recipient.h new file mode 100644 index 000000000..bd5b79a3f --- /dev/null +++ b/interfaces/inner_api/native/clouddiskservice_kit_inner/svc_death_recipient.h @@ -0,0 +1,41 @@ +/* + * 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 OHOS_FILEMGMT_SVC_DEATH_RECIPIENT_H +#define OHOS_FILEMGMT_SVC_DEATH_RECIPIENT_H + +#include + +#include "iremote_object.h" + +namespace OHOS::FileManagement::CloudDiskService { +class SvcDeathRecipient : public IRemoteObject::DeathRecipient { +public: + explicit SvcDeathRecipient(std::function &)> functor) : functor_(functor){}; + void OnRemoteDied(const wptr &object) override + { + if (object == nullptr) { + return; + } + object->RemoveDeathRecipient(this); + functor_(object); + } + +private: + std::function &)> functor_; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_SVC_DEATH_RECIPIENT_H \ No newline at end of file diff --git a/services/5207.json b/services/5207.json new file mode 100644 index 000000000..62f22ff23 --- /dev/null +++ b/services/5207.json @@ -0,0 +1,12 @@ +{ + "process": "clouddiskservice", + "systemability": [ + { + "name": 5207, + "libpath": "libclouddiskservice_sa.z.so", + "run-on-create": true, + "distributed": false, + "dump-level": 1 + } + ] +} \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn index 0eec280de..01f49084f 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -25,6 +25,11 @@ ohos_sa_profile("distributedfile_sa_profile") { part_name = "dfs_service" } +ohos_sa_profile("clouddiskservice_sa_profile") { + sources = [ "5207.json" ] + part_name = "dfs_service" +} + ohos_prebuilt_etc("distributedfile_etc") { source = "distributedfile.cfg" relative_install_dir = "init" @@ -32,6 +37,13 @@ ohos_prebuilt_etc("distributedfile_etc") { subsystem_name = "filemanagement" } +ohos_prebuilt_etc("clouddiskservice_etc") { + source = "clouddiskservice.cfg" + relative_install_dir = "init" + part_name = "dfs_service" + subsystem_name = "filemanagement" +} + ohos_prebuilt_etc("cloudfiledaemon_etc") { source = "cloudfiledaemon.cfg" relative_install_dir = "init" diff --git a/services/clouddiskservice.cfg b/services/clouddiskservice.cfg new file mode 100644 index 000000000..fb9c5e4d0 --- /dev/null +++ b/services/clouddiskservice.cfg @@ -0,0 +1,11 @@ +{ + "services": [ { + "name": "clouddiskservice", + "path": ["/system/bin/sa_main", "/system/profile/clouddiskservice.json"], + "uid": "6161", + "gid": ["6161", "dfs", "1006"], + "sandbox": 0, + "secon": "u:r:clouddiskservice:s0", + "apl": "system_basic" + }] +} \ No newline at end of file diff --git a/services/clouddiskservice/BUILD.gn b/services/clouddiskservice/BUILD.gn new file mode 100644 index 000000000..363fedd9c --- /dev/null +++ b/services/clouddiskservice/BUILD.gn @@ -0,0 +1,125 @@ +# 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/config/components/idl_tool/idl.gni") +import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +config("optimize-size") { + cflags = [ + "-fdata-sections", + "-ffunction-sections", + "-Oz", + ] + cflags_cc = [ + "-fvisibility-inlines-hidden", + "-Oz", + ] +} + +config("cloud_disk_service_public_config") { + include_dirs = [ "${target_gen_dir}" ] +} + +idl_gen_interface("cloud_disk_service") { + sources = [ "ICloudDiskService.idl" ] + sources_cpp = [ + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp", + "${distributedfile_path}/utils/log/src/utils_log.cpp", + ] + sub_include = [ + "${distributedfile_path}/utils/log/include", + "${innerkits_native_path}/clouddiskservice_kit_inner", + ] + configs = [] + + sequenceable_ext_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + + innerapi_tags = [ "platformsdk" ] + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + log_domainid = "0xD003900" + log_tag = "CloudDiskService" + subsystem_name = "filemanagement" + part_name = "dfs_service" +} + +ohos_shared_library("clouddiskservice_sa") { + branch_protector_ret = "pac_ret" + configs = [ ":optimize-size" ] + public_configs = [ ":cloud_disk_service_public_config" ] + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + include_dirs = [ + "ipc/include", + ] + + sources = [ + "ipc/src/cloud_disk_service.cpp", + "ipc/src/cloud_disk_service_callback_manager.cpp", + "ipc/src/cloud_disk_service_callback_proxy.cpp", + "ipc/src/cloud_disk_sync_folder.cpp", + "ipc/src/account_status_listener.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp", + "${distributedfile_path}/utils/log/src/utils_log.cpp", + ] + + output_values = get_target_outputs(":cloud_disk_service") + sources += filter_include(output_values, [ "*_stub.cpp" ]) + + defines = [ + "LOG_DOMAIN=0xD004308", + "LOG_TAG=\"CLOUDDISKSERVICE_SA\"", + ] + + deps = [ + ":cloud_disk_service", + ":libcloud_disk_service_proxy", + "${innerkits_native_path}/clouddiskservice_kit_inner:clouddiskservice_kit_inner", + "${utils_path}:libdistributedfiledentry", + "${utils_path}:libdistributedfileutils", + ] + + external_deps = [ + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ffrt:libffrt", + "e2fsprogs:libext2_uuid", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_single", + "samgr:samgr_proxy", + "safwk:system_ability_fwk", + "user_file_service:cloud_disk_manager_kit" + ] + + use_exceptions = true + part_name = "dfs_service" + subsystem_name = "filemanagement" +} diff --git a/services/clouddiskservice/ICloudDiskService.idl b/services/clouddiskservice/ICloudDiskService.idl new file mode 100644 index 000000000..a2b40a18b --- /dev/null +++ b/services/clouddiskservice/ICloudDiskService.idl @@ -0,0 +1,28 @@ +/* + * 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. + */ +sequenceable OHOS.IRemoteObject; +sequenceable cloud_disk_common..OHOS.FileManagement.CloudDiskService.ChangesResult; +sequenceable cloud_disk_common..OHOS.FileManagement.CloudDiskService.FileSyncState; + +interface OHOS.FileManagement.CloudDiskService.ICloudDiskService +{ + void RegisterSyncFolderChangesInner([in] String syncFolder, [in] IRemoteObject remoteobject); + void UnregisterSyncFolderChangesInner([in] String syncFolder); + ChangesResult GetSyncFolderChangesInner([in] String syncFolder, [in] unsigned long count, [in] unsigned long startUSN); + void SetFileSyncStatesInner([in] String syncFolder, [in] List fileSyncStates, [in] IRemoteObject remoteobject); + void GetFileSyncStatesInner([in] String syncFolder, [in] List pathArray, [in] IRemoteObject remoteobject); + void RegisterSyncFolderInner([in] int userId, [in] String bundleName, [in] String path); + void UnregisterSyncFolderInner([in] int userId, [in] String bundleName, [in] String path); +} diff --git a/services/clouddiskservice/ipc/include/account_status_listener.h b/services/clouddiskservice/ipc/include/account_status_listener.h new file mode 100644 index 000000000..c6604ec37 --- /dev/null +++ b/services/clouddiskservice/ipc/include/account_status_listener.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 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 CLOUD_DISK_SERVICE_ACCOUNT_STATUS_LISTENER_H +#define CLOUD_DISK_SERVICE_ACCOUNT_STATUS_LISTENER_H + +#include "cloud_file_utils.h" +#include "common_event_subscriber.h" +// #include "disk_monitor.h" +#include "dfsu_access_token_helper.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { +class AccountStatusListener { +public: + explicit AccountStatusListener() = default; + ~AccountStatusListener(); + void Start(); + void Stop(); + +private: + std::shared_ptr commonEventSubscriber_ = nullptr; +}; + +class AccountStatusSubscriber : public EventFwk::CommonEventSubscriber { +public: + AccountStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo); + ~AccountStatusSubscriber() override {} + void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; +}; +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS +#endif // CLOUD_DISK_SERVICE_ACCOUNT_STATUS_LISTENER_H \ No newline at end of file diff --git a/services/clouddiskservice/ipc/include/cloud_disk_service.h b/services/clouddiskservice/ipc/include/cloud_disk_service.h new file mode 100644 index 000000000..79f010b27 --- /dev/null +++ b/services/clouddiskservice/ipc/include/cloud_disk_service.h @@ -0,0 +1,72 @@ +/* + * 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 CLOUD_DISK_SERVICE_H +#define CLOUD_DISK_SERVICE_H + +#include +#include +#include + +#include "account_status_listener.h" +#include "cloud_disk_service_stub.h" +#include "icloud_disk_service.h" +#include "i_cloud_disk_service_callback.h" +#include "iremote_stub.h" +#include "nocopyable.h" +#include "refbase.h" +#include "system_ability.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { +enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; + +class CloudDiskService final : public SystemAbility, public CloudDiskServiceStub, protected NoCopyable { + DECLARE_SYSTEM_ABILITY(CloudDiskService); + +public: + explicit CloudDiskService(int32_t saID, bool runOnCreate = true); + virtual ~CloudDiskService() = default; + static CloudDiskService &GetInstance(); + + void OnStart() override; + void OnStop() override; + ErrCode RegisterSyncFolderChangesInner(const std::string& syncFolder, + const sptr& remoteObject) override; + ErrCode UnregisterSyncFolderChangesInner(const std::string& syncFolder) override; + ErrCode GetSyncFolderChangesInner(const std::string &syncFolder, uint64_t count, uint64_t startUsn, ChangesResult& changesResult) override; + ErrCode SetFileSyncStatesInner(const std::string& syncFolder, const std::vector& fileSyncStates, const sptr& remoteObject) override; + ErrCode GetFileSyncStatesInner(const std::string& syncFolder, const std::vector& pathArray, const sptr& remoteObject) override; + ErrCode RegisterSyncFolderInner(int32_t userId, const std::string& bundleName, const std::string& path) override; + ErrCode UnregisterSyncFolderInner(int32_t userId, const std::string& bundleName, const std::string& path) override; + + void RemoveXattr(std::string& path, const std::string& attrName); + +private: + CloudDiskService(); + ServiceRunningState state_ { ServiceRunningState::STATE_NOT_START }; + static sptr instance_; + static std::mutex instanceLock_; + bool registerToService_ { false }; + std::mutex callbackMutex_; + void PublishSA(); + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + std::shared_ptr accountStatusListener_ = nullptr; +}; +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS +#endif // CLOUD_DISK_SERVICE_H diff --git a/services/clouddiskservice/ipc/include/cloud_disk_service_callback_manager.h b/services/clouddiskservice/ipc/include/cloud_disk_service_callback_manager.h new file mode 100644 index 000000000..c50ffffc0 --- /dev/null +++ b/services/clouddiskservice/ipc/include/cloud_disk_service_callback_manager.h @@ -0,0 +1,45 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_MANAGER_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_MANAGER_H + +#include "nocopyable.h" + +#include "i_cloud_disk_service_callback.h" +#include "svc_death_recipient.h" + +namespace OHOS::FileManagement::CloudDiskService { +class CloudDiskServiceCallbackManager final : public NoCopyable { +public: + using TaskId = uint64_t; + static CloudDiskServiceCallbackManager &GetInstance(); + void RigisSyncFolderMap(std::string bundleName, uint32_t syncFolderIndex, const sptr &callback); + void UnregisSyncFolderMap(std::string bundleName, uint32_t syncFolderIndex); + void AddCallback(const std::string bundleName, const sptr &callback); + void OnChangeData(const uint32_t syncFolderIndex, std::vector &changeData); + + struct CallbackValue { + sptr callback; + std::vector syncFolderIndexs; + }; + std::map callbackAppMap_; // map -> callback + std::map> callbackIndexMap_; + std::mutex callbackAppMutex_; + std::mutex callbackIndexMutex_; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_MANAGER_H \ No newline at end of file diff --git a/services/clouddiskservice/ipc/include/cloud_disk_service_callback_proxy.h b/services/clouddiskservice/ipc/include/cloud_disk_service_callback_proxy.h new file mode 100644 index 000000000..fb84354f7 --- /dev/null +++ b/services/clouddiskservice/ipc/include/cloud_disk_service_callback_proxy.h @@ -0,0 +1,38 @@ +/* + * 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 OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_PROXY_H +#define OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_PROXY_H + +#include "iremote_proxy.h" + +#include "i_cloud_disk_service_callback.h" + +namespace OHOS::FileManagement::CloudDiskService { +class CloudDiskServiceCallbackProxy : public IRemoteProxy { +public: + explicit CloudDiskServiceCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~CloudDiskServiceCallbackProxy() override {} + + void OnChangeData(std::vector &changeData) override; + void OnReturnSetFailedList(std::vector &FailedList) override; + void OnReturnGetResult(std::vector &resultList) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // OHOS_FILEMGMT_CLOUD_DISK_SERVICE_CALLBACK_PROXY_H \ No newline at end of file diff --git a/services/clouddiskservice/ipc/include/cloud_disk_sync_folder.h b/services/clouddiskservice/ipc/include/cloud_disk_sync_folder.h new file mode 100644 index 000000000..e95abf69d --- /dev/null +++ b/services/clouddiskservice/ipc/include/cloud_disk_sync_folder.h @@ -0,0 +1,51 @@ +/* + * 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 CLOUD_DISK_SYNC_FOLDER_H +#define CLOUD_DISK_SYNC_FOLDER_H + +#include +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { + +struct SyncFolderValue { + std::string bundleName; + std::string path; +}; + +class CloudDiskSyncFolder { + +public: + static CloudDiskSyncFolder &GetInstance(); + void AddSyncFolder(const uint32_t& syncFolderIndex, const SyncFolderValue& syncFolderValue); + void DeleteSyncFolder(const uint32_t& syncFolderIndex); + int32_t GetSyncFolderSize(); + bool CheckSyncFolder(const uint32_t& syncFolderIndex); + bool GetSyncFolderByIndex(const uint32_t& syncFolderIndex, std::string& path); + bool GetIndexBySyncFolder(uint32_t& syncFolderIndex, const std::string& path); + std::unordered_map GetSyncFolderMap(); + std::vector syncFolderList; + +private: + std::unordered_map syncFolderMap; +}; +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS +#endif // CLOUD_DISK_SYNC_FOLDER_H diff --git a/services/clouddiskservice/ipc/src/account_status_listener.cpp b/services/clouddiskservice/ipc/src/account_status_listener.cpp new file mode 100644 index 000000000..b33601f89 --- /dev/null +++ b/services/clouddiskservice/ipc/src/account_status_listener.cpp @@ -0,0 +1,85 @@ +/* + * 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 "account_status_listener.h" + +#include + +#include "common_event_manager.h" +#include "common_event_support.h" +#include "cloud_disk_sync_folder.h" +#include "cloud_disk_comm.h" +#include "cloud_disk_manager.h" + +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { + +AccountStatusSubscriber::AccountStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) + : EventFwk::CommonEventSubscriber(subscribeInfo) +{ +} + +void AccountStatusSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData) +{ + auto action = eventData.GetWant().GetAction(); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_STARTED) { + LOGI("OnStartUser!"); + + std::vector syncFolders = + OHOS::FileManagement::CloudDiskManager::GetInstance().GetAllSyncFoldersForSa(); + for (auto item : syncFolders) { + SyncFolderValue syncFolderValue; + syncFolderValue.bundleName = item.bundleName_; + syncFolderValue.path = item.path_; + uint32_t syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(item.path_); + CloudDiskSyncFolder::GetInstance().AddSyncFolder(syncFolderIndex, syncFolderValue); + } + if (CloudDiskSyncFolder::GetInstance().GetSyncFolderSize() > 0) { + // 启动监听 + DiskMonitor::GetInstance().StartMonitor(eventData.GetCode()); + } + } +} + +AccountStatusListener::~AccountStatusListener() +{ + Stop(); +} + +void AccountStatusListener::Start() +{ + /* subscribe Account login, logout, Package remove and Datashare ready */ + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STARTED); + EventFwk::CommonEventSubscribeInfo info(matchingSkills); + commonEventSubscriber_ = std::make_shared(info); + auto subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_); + LOGI("Subscriber end, SubscribeResult = %{public}d", subRet); +} + +void AccountStatusListener::Stop() +{ + if (commonEventSubscriber_ != nullptr) { + EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_); + commonEventSubscriber_ = nullptr; + } +} + +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/services/clouddiskservice/ipc/src/cloud_disk_service.cpp b/services/clouddiskservice/ipc/src/cloud_disk_service.cpp new file mode 100644 index 000000000..29d9efabe --- /dev/null +++ b/services/clouddiskservice/ipc/src/cloud_disk_service.cpp @@ -0,0 +1,436 @@ +/* + * 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 "cloud_disk_service.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "cloud_disk_service_callback_manager.h" +#include "cloud_disk_service_task_manager.h" +// #include "cloud_disk_service_syncfolder.h" +#include "cloud_disk_sync_folder.h" +#include "dfs_error.h" +#include "ffrt_inner.h" +#include "iremote_object.h" +#include "system_ability_definition.h" +#include "utils_directory.h" +#include "utils_log.h" +#ifdef HICOLLIE_ENABLE +#include "xcollie_helper.h" +#endif + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { +using namespace std; + +const int32_t GET_FILE_SYNC_MAX = 100; +constexpr const char* FILE_SYNC_STATE = "user.clouddisk.filesyncstate"; + +namespace { +} +REGISTER_SYSTEM_ABILITY_BY_ID(CloudDiskService, FILEMANAGEMENT_CLOUD_DISK_SERVICE_SA_ID, true); + +CloudDiskService::CloudDiskService(int32_t saID, bool runOnCreate) : SystemAbility(saID, runOnCreate) +{ + accountStatusListener_ = make_shared(); +} + +CloudDiskService::CloudDiskService() +{ +} + +CloudDiskService &CloudDiskService::GetInstance() +{ + static CloudDiskService instance; + return instance; +} + +void CloudDiskService::PublishSA() +{ + LOGI("Begin to init"); + if (!registerToService_) { + bool ret = SystemAbility::Publish(this); + if (!ret) { + throw runtime_error("Failed to publish the clouddiskservice"); + } + registerToService_ = true; + } + LOGI("Init finished successfully"); +} + +void CloudDiskService::OnStart() +{ + LOGI("Begin to start service"); + if (state_ == ServiceRunningState::STATE_RUNNING) { + LOGI("CloudDiskService has already started"); + return; + } + + try { + PublishSA(); + AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID); + } catch (const exception &e) { + LOGE("%{public}s", e.what()); + } + + CloudDiskServiceTaskManager::GetInstance().StartTask(); + state_ = ServiceRunningState::STATE_RUNNING; + + LOGI("Start service successfully"); +} + +void CloudDiskService::OnStop() +{ + LOGI("Begin to stop"); + state_ = ServiceRunningState::STATE_NOT_START; + registerToService_ = false; + LOGI("Stop finished successfully"); +} + +static int32_t CheckPermissions(const string &permission, bool isSystemApp) +{ + if (!permission.empty() && !DfsuAccessTokenHelper::CheckCallerPermission(permission)) { + LOGE("permission denied"); + return E_PERMISSION_DENIED; + } + return E_OK; +} + +int32_t CloudDiskService::RegisterSyncFolderChangesInner(const std::string& syncFolder, + const sptr& remoteObject) +{ + LOGI("Begin RegisterSyncFolderChangesInner"); + LOGI("RegisterSyncFolderChangesInner start uri:%{public}s", syncFolder.c_str()); + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + // URI校验 + + if (remoteObject == nullptr) { + LOGE("remoteObject is nullptr"); + return E_INVAL_ARG; + } + + std::string bundleName = ""; + int32_t ret = DfsuAccessTokenHelper::GetCallerBundleName(bundleName); + if (ret != E_OK) { + LOGE("Get bundleName failed, ret:%{public}d", ret); + return ret; + } + + auto syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(syncFolder); + if (!CloudDiskSyncFolder::GetInstance().CheckSyncFolder(syncFolderIndex)) { + LOGE("SyncFolder is not exist"); + return E_PARAMS_; + } + SyncFolderValue syncFolderValue; + syncFolderValue.bundleName = bundleName; + syncFolderValue.path = syncFolder; + CloudDiskSyncFolder::GetInstance().AddSyncFolder(syncFolderIndex, syncFolderValue); + + TaskKey taskKey; + taskKey.bundleName = bundleName; + taskKey.syncChronousRootPath = syncFolder; + CloudDiskServiceTaskManager::GetInstance().StartTask(taskKey, TaskType::REGISTER_TASK); + + auto callback = iface_cast(remoteObject); + LOGI("callback is null : %{public}s", callback == nullptr? "true" : "false"); + + CloudDiskServiceCallbackManager::GetInstance().RigisSyncFolderMap(bundleName, syncFolderIndex, callback); + + CloudDiskServiceCallbackManager::GetInstance().AddCallback(bundleName, callback); + CloudDiskServiceTaskManager::GetInstance().CompleteTask(taskKey, TaskType::REGISTER_TASK); + LOGI("RegisterSyncFolderChangesInner end syncFolderIndex:%{public}u", syncFolderIndex); + LOGI("End RegisterSyncFolderChangesInner"); + return 0; +} + +int32_t CloudDiskService::UnregisterSyncFolderChangesInner(const std::string& syncFolder) +{ + LOGI("Begin UnregisterSyncFolderChangesInner"); + LOGI("UnregisterSyncFolderChangesInner start syncFolder:%{public}s", syncFolder.c_str()); + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + // URI校验 + + std::string bundleName = ""; + int32_t ret = DfsuAccessTokenHelper::GetCallerBundleName(bundleName); + if (ret != E_OK) { + LOGE("Get bundleName failed, ret:%{public}d", ret); + return ret; + } + + auto syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(syncFolder); + CloudDiskSyncFolder::GetInstance().DeleteSyncFolder(syncFolderIndex); + + CloudDiskServiceCallbackManager::GetInstance().UnregisSyncFolderMap(bundleName, syncFolderIndex); + + TaskKey taskKey; + taskKey.bundleName = bundleName; + taskKey.syncChronousRootPath = syncFolder; + CloudDiskServiceTaskManager::GetInstance().StartTask(taskKey, TaskType::REGISTER_TASK); + + CloudDiskServiceTaskManager::GetInstance().CompleteTask(taskKey, TaskType::REGISTER_TASK); + LOGI("End UnregisterSyncFolderChangesInner"); + return 0; +} + +int32_t CloudDiskService::GetSyncFolderChangesInner(const std::string &syncFolder, uint64_t count, uint64_t startUsn, ChangesResult& changesResult) +{ + LOGI("Begin GetSyncFolderChangesInner"); + LOGI("GetSyncFolderChangesInner start syncFolder:%{public}s", syncFolder.c_str()); + LOGI("GetSyncFolderChangesInner start count:%{public}ld", count); + LOGI("GetSyncFolderChangesInner start startUSN:%{public}ld", startUsn); + LOGI("GetSyncFolderChangesInner start changesResult.nextUsn:%{public}ld", changesResult.nextUsn); + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + // URI校验 + + auto syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(syncFolder); + if (!CloudDiskSyncFolder::GetInstance().CheckSyncFolder(syncFolderIndex)) { + LOGE("SyncFolder is not exist"); + return E_PARAMS_; + } + + TaskKey taskKey; + taskKey.bundleName = "test"; + taskKey.syncChronousRootPath = "test"; + CloudDiskServiceTaskManager::GetInstance().StartTask(taskKey, TaskType::REGISTER_TASK); + + // int32_t ret = CloudDiskServiceSyncFolder::GetSyncFolderChanges(DfsuAccessTokenHelper::GetUserId(), + // syncFolderIndex, startUsn, count, changesResult); + // if (ret != E_OK) { + // return ret; + // } + + // ChangeData changeData; + // changeData.updateSequenceNumber = 1; + // changeData.fileId = "1"; + // changeData.parentFileId = "1"; + // changeData.relativePath = "test"; + // changeData.operationType = OperationType::CREATE; + // changeData.size = 1; + // changeData.mtime = 10; + // changeData.timeStamp = 100; + // changesResult.nextUsn = 1; + // changesResult.isEof = true; + // changesResult.changesData.push_back(changeData); + + CloudDiskServiceTaskManager::GetInstance().CompleteTask(taskKey, TaskType::REGISTER_TASK); + LOGI("GetSyncFolderChangesInner end changesResult.nextUsn:%{public}ld", changesResult.nextUsn); + LOGI("End GetSyncFolderChangesInner"); + return 0; +} + +int32_t CloudDiskService::SetFileSyncStatesInner(const std::string& syncFolder, const std::vector& fileSyncStates, const sptr& remoteObject) +{ + LOGI("Begin SetXattrInner"); + FailedList failed; + std::vector failedList; + + auto syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(syncFolder); + if (!CloudDiskSyncFolder::GetInstance().CheckSyncFolder(syncFolderIndex) || fileSyncStates.empty() || fileSyncStates.size() > GET_FILE_SYNC_MAX) { + LOGE("Invalid parameter"); + return E_PARAMS_; + } + + auto callback = iface_cast(remoteObject); + for (auto& item : fileSyncStates) { + uint8_t state = static_cast(item.state); + if (state > static_cast(SyncState::SYNC_CONFLICTED)) { + LOGE("Invalid state for %{public}s", item.path.c_str()); + failed.path = item.path; + failed.error = ErrorReason::INVALID_ARGUMENT; // 待定 + failedList.push_back(failed); + continue; + } + if (setxattr(item.path.c_str(), FILE_SYNC_STATE, &state, sizeof(state), 0) != 0) { + LOGE("Failed to set xattr for file:%{public}s, err: %{public}d", item.path.c_str(), errno); + failed.path = item.path; + failed.error = ErrorReason::NO_SUCH_FILE; // 待定 + failedList.push_back(failed); + } + } + + callback->OnReturnSetFailedList(failedList); + + if (!failedList.empty()) { + return E_IPCSS; + } + + LOGI("End SetXattrInner"); + return 0; +} + +int32_t CloudDiskService::GetFileSyncStatesInner(const std::string& syncFolder, const std::vector& pathArray, const sptr& remoteObject) +{ + LOGI("Begin GetXattrInner"); + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + + auto syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(syncFolder); + if (!CloudDiskSyncFolder::GetInstance().CheckSyncFolder(syncFolderIndex) || pathArray.empty() || pathArray.size() > GET_FILE_SYNC_MAX) { + LOGE("Invalid parameter"); + return E_PARAMS_; + } + + auto callback = iface_cast(remoteObject); + std::vector resultLists; + ResultList resultList; + + for (auto& item : pathArray) { + resultList.isSuccess = false; + resultList.path = item; + auto xattrValueSize = getxattr(item.c_str(), FILE_SYNC_STATE, nullptr, 0); + if (xattrValueSize < 0) { + LOGE("getxattr failed, errno : %{public}d", errno); + resultList.error = ErrorReason::NO_SUCH_FILE; + resultLists.push_back(resultList); + continue; + } + std::unique_ptr xattrValue = std::make_unique((long)xattrValueSize + 1); + if (xattrValue == nullptr) { + LOGE("Failed to allocate memory for xattrValue, errno : %{public}d", errno); + resultList.error = ErrorReason::NO_SPACE_LEFT; + resultLists.push_back(resultList); + continue; + } + xattrValueSize = getxattr(item.c_str(), FILE_SYNC_STATE, xattrValue.get(), xattrValueSize); + if (xattrValueSize <= 0) { + LOGE("getxattr failed, errno : %{public}d", errno); + resultList.error = ErrorReason::NO_SUCH_FILE; + resultLists.push_back(resultList); + continue; + } + uint8_t rawState = static_cast(xattrValue[0]); + if (rawState > static_cast(SyncState::SYNC_CONFLICTED)) { + LOGE("get invalid number"); + resultList.error = ErrorReason::INVALID_ARGUMENT; + resultLists.push_back(resultList); + continue; + } + resultList.isSuccess = true; + resultList.state = static_cast(rawState); + resultLists.push_back(resultList); + } + + callback->OnReturnGetResult(resultLists); + + LOGI("End GetXattrInner"); + return 0; +} + +int32_t CloudDiskService::RegisterSyncFolderInner(int32_t userId, const std::string& bundleName, const std::string& path) +{ + LOGI("Begin GetSyncFolderChangesInner"); + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + // URI校验 + + TaskKey taskKey; + taskKey.bundleName = bundleName; + taskKey.syncChronousRootPath = path; + CloudDiskServiceTaskManager::GetInstance().StartTask(taskKey, TaskType::REGISTER_TASK); + + auto syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(path); + if (CloudDiskSyncFolder::GetInstance().CheckSyncFolder(syncFolderIndex)) { + LOGE("Syncfolder is exist"); + return E_PARAMS_; + } + + // int 32_t ret = CloudDiskServiceSyncFolder::RegisterSyncFolder(userId, syncFolderIndex, syncFolder); + // if (ret != E_OK) { + // return ret; + // } + + SyncFolderValue syncFolderValue; + syncFolderValue.bundleName = bundleName; + syncFolderValue.path = path; + CloudDiskSyncFolder::GetInstance().AddSyncFolder(syncFolderIndex, syncFolderValue); + if (CloudDiskSyncFolder::GetInstance().GetSyncFolderSize() == 1) { + // DiskMonitor::GetInstance().StartMonitor(userId); + } + + CloudDiskServiceTaskManager::GetInstance().CompleteTask(taskKey, TaskType::REGISTER_TASK); + LOGI("End GetSyncFolderChangesInner"); + return 0; +} + +void CloudDiskService::RemoveXattr(std::string& path, const std::string& attrName) +{ + DIR* dir = opendir(path.c_str()); + if (!dir) { + LOGE("Open failed, err:%{public}d", errno); + } + struct dirent* entry; + while ((entry = readdir(dir)) != nullptr) { + path = path + "/" + entry->d_name; + struct stat st; + if (lstat(path.c_str(), &st) == -1) { + LOGE("lstat failed"); + continue; + } + if (S_ISDIR(st.st_mode)) { + RemoveXattr(path, attrName); + } + if (S_ISREG(st.st_mode)) { + removexattr(path.c_str(), attrName.c_str()); + } + } +} + +int32_t CloudDiskService::UnregisterSyncFolderInner(int32_t userId, const std::string& bundleName, const std::string& path) +{ + LOGI("Begin UnregisterSyncFolderInner"); + // RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_DISK_SERVICE, true)); + // URI校验 + + TaskKey taskKey; + taskKey.bundleName = bundleName; + taskKey.syncChronousRootPath = path; + CloudDiskServiceTaskManager::GetInstance().StartTask(taskKey, TaskType::REGISTER_TASK); + + auto syncFolderIndex = CloudDisk::CloudFileUtils::DentryHash(path); + if (!CloudDiskSyncFolder::GetInstance().CheckSyncFolder(syncFolderIndex)) { + LOGE("Syncfolder is not exist"); + return E_PARAMS_; + } + + // int 32_t ret = CloudDiskServiceSyncFolder::UnRegisterSyncFolder(userId, path); + // if (ret != E_OK) { + // return ret; + // } + + CloudDiskSyncFolder::GetInstance().DeleteSyncFolder(syncFolderIndex); + if (CloudDiskSyncFolder::GetInstance().GetSyncFolderSize() == 0) { + // DiskMonitor::GetInstance().StopMonitor(); + } + + RemoveXattr(path, FILE_SYNC_STATE); + + CloudDiskServiceTaskManager::GetInstance().CompleteTask(taskKey, TaskType::REGISTER_TASK); + LOGI("End UnregisterSyncFolderInner"); + return 0; +} + +void CloudDiskService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + LOGI("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId); + accountStatusListener_->Start(); +} +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS diff --git a/services/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp b/services/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp new file mode 100644 index 000000000..5280d08c8 --- /dev/null +++ b/services/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp @@ -0,0 +1,104 @@ +/* + * 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 "cloud_disk_service_callback_manager.h" + +#include + +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; + +CloudDiskServiceCallbackManager &CloudDiskServiceCallbackManager::GetInstance() +{ + static CloudDiskServiceCallbackManager instance; + return instance; +} + +void CloudDiskServiceCallbackManager::AddCallback(const std::string bundleName, const sptr &callback) +{ + if (callbackAppMap_[bundleName].callback != nullptr) { + return; + } + if (callback == nullptr) { + LOGE("callback is nullptr"); + return; + } + callbackAppMap_[bundleName].callback = callback; + auto remoteObject = callback->AsObject(); + auto deathCb = [this, bundleName](const wptr &obj) { + LOGE("client died"); + std::vector syncFolderIndex; + auto it = callbackAppMap_.find(bundleName); + if (it != callbackAppMap_.end()) { + syncFolderIndex = it->second.syncFolderIndexs; + callbackAppMap_.erase(it); + } + for (auto item : syncFolderIndex) { + callbackIndexMap_.erase(item); + } + }; + auto death = sptr(new SvcDeathRecipient(deathCb)); + remoteObject->AddDeathRecipient(death); +} + +void CloudDiskServiceCallbackManager::OnChangeData(const uint32_t syncFolderIndex, std::vector &changeData) +{ + auto item = callbackIndexMap_.find(syncFolderIndex); + if (item == callbackIndexMap_.end() || !item->second) { + LOGE("callback is nullptr"); + return; + } + item->second->OnChangeData(changeData); +} + +void CloudDiskServiceCallbackManager::RigisSyncFolderMap(std::string bundleName, uint32_t syncFolderIndex, const sptr &callback) +{ + unique_lock lockApp(callbackAppMutex_); + unique_lock lockIndex(callbackIndexMutex_); + auto it = callbackAppMap_.find(bundleName); + if (it == callbackAppMap_.end()) { + CallbackValue callbackValue; + callbackValue.syncFolderIndexs.push_back(syncFolderIndex); + callbackAppMap_[bundleName] = callbackValue; + } else { + it->second.syncFolderIndexs.push_back(syncFolderIndex); + } + + auto item = callbackIndexMap_.find(syncFolderIndex); + if (item == callbackIndexMap_.end()) { + callbackIndexMap_[syncFolderIndex] = callback; + } +} + +void CloudDiskServiceCallbackManager::UnregisSyncFolderMap(std::string bundleName, uint32_t syncFolderIndex) +{ + unique_lock lock(callbackIndexMutex_); + auto it = callbackIndexMap_.find(syncFolderIndex); + if(it != callbackIndexMap_.end()) callbackIndexMap_.erase(it); + + auto item = callbackAppMap_.find(bundleName); + if (item == callbackAppMap_.end()) { + LOGE("no such app in callback"); + return; + } + auto& vec = item->second.syncFolderIndexs; + auto pos = std::find(vec.begin(), vec.end(), syncFolderIndex); + if (pos != vec.end()) { + vec.erase(pos); + } +} + +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/ipc/src/cloud_disk_service_callback_proxy.cpp b/services/clouddiskservice/ipc/src/cloud_disk_service_callback_proxy.cpp new file mode 100644 index 000000000..00ff74835 --- /dev/null +++ b/services/clouddiskservice/ipc/src/cloud_disk_service_callback_proxy.cpp @@ -0,0 +1,137 @@ +/* + * 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 "cloud_disk_service_callback_proxy.h" + +#include "cloud_file_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; + +void CloudDiskServiceCallbackProxy::OnChangeData(std::vector &changeData) +{ + LOGI("Start"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return; + } + + if (changeData.size() > static_cast(VECTOR_MAX_SIZE)) { + return; + } + + data.WriteInt32(changeData.size()); + for (auto item = changeData.begin(); item != changeData.end(); ++item) { + if (!data.WriteParcelable(&(*item))) { + return; + } + } + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return; + } + int32_t ret = remote->SendRequest(ICloudDiskServiceCallback::SERVICE_CMD_ON_CHANGE_DATA, data, reply, option); + if (ret != E_OK) { + LOGE("Failed to send out the requeset, ret:%{public}d", ret); + return; + } + LOGI("End"); + return; +} + +void CloudDiskServiceCallbackProxy::OnReturnSetFailedList(std::vector &failedList) +{ + LOGI("Start"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return; + } + + if (failedList.size() > static_cast(VECTOR_MAX_SIZE)) { + return; + } + data.WriteInt32(failedList.size()); + for (auto item = failedList.begin(); item != failedList.end(); ++item) { + if (!data.WriteParcelable(&(*item))) { + return; + } + } + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return; + } + int32_t ret = remote->SendRequest(ICloudDiskServiceCallback::SERVICE_CMD_ON_RETURN_SET_FAILED_LIST, data, reply, option); + if (ret != E_OK) { + LOGE("Failed to send out the requeset, ret:%{public}d", ret); + return; + } + LOGI("End"); + return; +} + +void CloudDiskServiceCallbackProxy::OnReturnGetResult(std::vector &resultList) +{ + LOGI("Start"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return; + } + + // if (resultList.size() > static_cast(VECTOR_MAX_SIZE) || failedList.size() > static_cast(VECTOR_MAX_SIZE)) { + if (resultList.size() > static_cast(VECTOR_MAX_SIZE)) { + return; + } + + data.WriteInt32(resultList.size()); + for (auto item = resultList.begin(); item != resultList.end(); ++item) { + if (!data.WriteParcelable(&(*item))) { + return; + } + } + // data.WriteInt32(failedList.size()); + // for (auto item = failedList.begin(); item != failedList.end(); ++item) { + // if (!data.WriteParcelable(&(*item))) { + // return; + // } + // } + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return; + } + int32_t ret = remote->SendRequest(ICloudDiskServiceCallback::SERVICE_CMD_ON_RETURN_GET_RESULT, data, reply, option); + if (ret != E_OK) { + LOGE("Failed to send out the requeset, ret:%{public}d", ret); + return; + } + LOGI("End"); + return; +} +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/ipc/src/cloud_disk_sync_folder.cpp b/services/clouddiskservice/ipc/src/cloud_disk_sync_folder.cpp new file mode 100644 index 000000000..6efbcee4c --- /dev/null +++ b/services/clouddiskservice/ipc/src/cloud_disk_sync_folder.cpp @@ -0,0 +1,92 @@ +/* + * 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 "cloud_disk_sync_folder.h" + +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDiskService { + +CloudDiskSyncFolder &CloudDiskSyncFolder::GetInstance() +{ + static CloudDiskSyncFolder instance; + return instance; +} + +void CloudDiskSyncFolder::AddSyncFolder(const uint32_t& syncFolderIndex, const SyncFolderValue& syncFolderValue) +{ + auto item = syncFolderMap.find(syncFolderIndex); + if (item == syncFolderMap.end()) { + syncFolderList.push_back(syncFolderValue.path); + syncFolderMap[syncFolderIndex] = syncFolderValue; + } +} + +void CloudDiskSyncFolder::DeleteSyncFolder(const uint32_t& syncFolderIndex) +{ + syncFolderList.clear(); + auto item = syncFolderMap.find(syncFolderIndex); + if (item != syncFolderMap.end()) { + syncFolderMap.erase(syncFolderIndex); + } + for (const auto& it : syncFolderMap) { + syncFolderList.push_back(it.second.path); + } +} + +int32_t CloudDiskSyncFolder::GetSyncFolderSize() +{ + return syncFolderMap.size(); +} + +bool CloudDiskSyncFolder::CheckSyncFolder(const uint32_t& syncFolderIndex) { + auto it = syncFolderMap.find(syncFolderIndex); + if (it == syncFolderMap.end()) { + return false; + } + return true; +} + +bool CloudDiskSyncFolder::GetSyncFolderByIndex(const uint32_t& syncFolderIndex, std::string& path) +{ + auto item = syncFolderMap.find(syncFolderIndex); + if (item == syncFolderMap.end()) { + return false; + } + path = item->second.path; + return true; +} + +bool CloudDiskSyncFolder::GetIndexBySyncFolder(uint32_t& syncFolderIndex, const std::string& path) +{ + for (const auto& item : syncFolderMap) { + if (item.second.path == path) { + syncFolderIndex = item.first; + return true; + } + } + return false; +} + +std::unordered_map CloudDiskSyncFolder::GetSyncFolderMap() +{ + return syncFolderMap; +} + +} // namespace CloudDiskService +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/include/dfsu_access_token_helper.h b/utils/system/include/dfsu_access_token_helper.h index 3d7398930..f5697e320 100644 --- a/utils/system/include/dfsu_access_token_helper.h +++ b/utils/system/include/dfsu_access_token_helper.h @@ -19,6 +19,7 @@ #include namespace OHOS::FileManagement { +inline const std::string PERM_CLOUD_DISK_SERVICE = "ohos.permission.ACCESS_CLOUD_DISK_INFO"; inline const std::string PERM_CLOUD_SYNC_MANAGER = "ohos.permission.CLOUDFILE_SYNC_MANAGER"; inline const std::string PERM_CLOUD_SYNC = "ohos.permission.CLOUDFILE_SYNC"; inline const std::string PERM_AUTH_URI = "ohos.permission.PROXY_AUTHORIZATION_URI"; -- Gitee