diff --git a/interfaces/innerkits/storage_manager/native/istorage_manager.h b/interfaces/innerkits/storage_manager/native/istorage_manager.h index 814bfe95..d1e6a395 100644 --- a/interfaces/innerkits/storage_manager/native/istorage_manager.h +++ b/interfaces/innerkits/storage_manager/native/istorage_manager.h @@ -69,6 +69,10 @@ public: const std::vector &secret) = 0; virtual int32_t InactiveUserKey(uint32_t userId) = 0; virtual int32_t UpdateKeyContext(uint32_t userId) = 0; + + // app file share api + virtual int32_t CreateShareFile(std::string uri, int32_t tokenID, int32_t flag) = 0; + virtual int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) = 0; enum { PREPARE_ADD_USER = 1, @@ -105,6 +109,8 @@ public: SET_VOL_DESC, FORMAT, GET_DISK_BY_ID, + CREATE_SHARE_FILE, + DELETE_SHARE_FILE, }; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.StorageManager.IStorageManager"); diff --git a/services/storage_daemon/BUILD.gn b/services/storage_daemon/BUILD.gn index 765311b3..bef4d843 100644 --- a/services/storage_daemon/BUILD.gn +++ b/services/storage_daemon/BUILD.gn @@ -102,7 +102,11 @@ ohos_executable("storage_daemon") { ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", "access_token:libaccesstoken_sdk", + "app_file_service:fileshare_native", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "init:libbegetutil", diff --git a/services/storage_daemon/include/ipc/istorage_daemon.h b/services/storage_daemon/include/ipc/istorage_daemon.h index 067d82e4..cfbecfc0 100644 --- a/services/storage_daemon/include/ipc/istorage_daemon.h +++ b/services/storage_daemon/include/ipc/istorage_daemon.h @@ -46,6 +46,8 @@ public: ACTIVE_USER_KEY, INACTIVE_USER_KEY, UPDATE_KEY_CONTEXT, + CREATE_SHARE_FILE, + DELETE_SHARE_FILE, }; enum { @@ -82,6 +84,10 @@ public: virtual int32_t InactiveUserKey(uint32_t userId) = 0; virtual int32_t UpdateKeyContext(uint32_t userId) = 0; + // app file share api + virtual int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) = 0; + virtual int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.StorageDaemon"); }; } // STORAGE_DAEMON diff --git a/services/storage_daemon/include/ipc/storage_daemon.h b/services/storage_daemon/include/ipc/storage_daemon.h index 4002fc95..bb57afd3 100644 --- a/services/storage_daemon/include/ipc/storage_daemon.h +++ b/services/storage_daemon/include/ipc/storage_daemon.h @@ -53,6 +53,10 @@ public: const std::vector &secret) override; virtual int32_t InactiveUserKey(uint32_t userId) override; virtual int32_t UpdateKeyContext(uint32_t userId) override; + + // app file share api + virtual int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) override; + virtual int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) override; }; } // StorageDaemon } // OHOS diff --git a/services/storage_daemon/include/ipc/storage_daemon_proxy.h b/services/storage_daemon/include/ipc/storage_daemon_proxy.h index 4b959d0e..3f8e90d2 100644 --- a/services/storage_daemon/include/ipc/storage_daemon_proxy.h +++ b/services/storage_daemon/include/ipc/storage_daemon_proxy.h @@ -53,6 +53,10 @@ public: virtual int32_t InactiveUserKey(uint32_t userId) override; virtual int32_t UpdateKeyContext(uint32_t userId) override; + // app file share api + virtual int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) override; + virtual int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) override; + private: static inline BrokerDelegator delegator_; int32_t SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/services/storage_daemon/include/ipc/storage_daemon_stub.h b/services/storage_daemon/include/ipc/storage_daemon_stub.h index 1ab9ceac..91f186cb 100644 --- a/services/storage_daemon/include/ipc/storage_daemon_stub.h +++ b/services/storage_daemon/include/ipc/storage_daemon_stub.h @@ -52,6 +52,10 @@ private: int32_t HandleActiveUserKey(MessageParcel &data, MessageParcel &reply); int32_t HandleInactiveUserKey(MessageParcel &data, MessageParcel &reply); int32_t HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply); + + // app file share api + int32_t HandleCreateShareFile(MessageParcel &data, MessageParcel &reply); + int32_t HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply); }; } // StorageDaemon } // OHOS diff --git a/services/storage_daemon/include/user/mount_manager.h b/services/storage_daemon/include/user/mount_manager.h index f091dcc8..7790cc52 100644 --- a/services/storage_daemon/include/user/mount_manager.h +++ b/services/storage_daemon/include/user/mount_manager.h @@ -55,6 +55,8 @@ private: int32_t HmdfsTwiceMount(int32_t userId, std::string relativePath); int32_t HmdfsUMount(int32_t userId); int32_t HmdfsUMount(int32_t userId, std::string relativePath); + int32_t SharefsMount(int32_t userId); + int32_t SharefsUMount(int32_t userId); int32_t HmdfsTwiceUMount(int32_t userId, std::string relativePath); int32_t LocalMount(int32_t userId); int32_t LocalUMount(int32_t userId); diff --git a/services/storage_daemon/include/utils/mount_argument_utils.h b/services/storage_daemon/include/utils/mount_argument_utils.h index 2919dd79..6480188c 100644 --- a/services/storage_daemon/include/utils/mount_argument_utils.h +++ b/services/storage_daemon/include/utils/mount_argument_utils.h @@ -33,6 +33,9 @@ struct MountArgument final { std::string GetFullSrc() const; std::string GetFullDst() const; + std::string GetShareSrc() const; + std::string GetShareDst() const; + std::string GetUserIdPara() const; std::string GetCommFullPath() const; std::string GetCachePath() const; std::string GetCtrlPath() const; diff --git a/services/storage_daemon/ipc/src/storage_daemon.cpp b/services/storage_daemon/ipc/src/storage_daemon.cpp index 71ba28b1..fe9c2edf 100644 --- a/services/storage_daemon/ipc/src/storage_daemon.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon.cpp @@ -17,12 +17,14 @@ #include "crypto/key_manager.h" #include "disk/disk_manager.h" +#include "file_share.h" #include "file_sharing/file_sharing.h" #include "storage_service_errno.h" #include "storage_service_log.h" #include "user/user_manager.h" #include "volume/volume_manager.h" + namespace OHOS { namespace StorageDaemon { int32_t StorageDaemon::Shutdown() @@ -152,5 +154,15 @@ int32_t StorageDaemon::UpdateKeyContext(uint32_t userId) { return KeyManager::GetInstance()->UpdateKeyContext(userId); } + +int32_t StorageDaemon::CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) +{ + return AppFileService::FileShare::CreateShareFile(uri, tokenId, flag); +} + +int32_t StorageDaemon::DeleteShareFile(int32_t tokenId, std::vectorsharePathList) +{ + return AppFileService::FileShare::DeleteShareFile(tokenId, sharePathList); +} } // StorageDaemon } // OHOS diff --git a/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp b/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp index 09e399f8..123e07f0 100644 --- a/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp @@ -461,6 +461,68 @@ int32_t StorageDaemonProxy::UpdateKeyContext(uint32_t userId) return reply.ReadInt32(); } +int32_t StorageDaemonProxy::CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + + if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) { + return E_WRITE_DESCRIPTOR_ERR; + } + + if (!data.WriteString(uri)) { + return E_WRITE_PARCEL_ERR; + } + + if (!data.WriteUint32(tokenId)) { + return E_WRITE_PARCEL_ERR; + } + + if (!data.WriteUint32(flag)) { + return E_WRITE_PARCEL_ERR; + } + + int err = SendRequest(CREATE_SHARE_FILE, data, reply, option); + if (err != E_OK) { + return err; + } + + return reply.ReadInt32(); +} + +int32_t StorageDaemonProxy::DeleteShareFile(int32_t tokenId, std::vectorsharePathList) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + + if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) { + return E_WRITE_DESCRIPTOR_ERR; + } + + if (!data.WriteUint32(tokenId)) { + return E_WRITE_PARCEL_ERR; + } + + size_t length = sharePathList.size(); + if (!data.WriteUint32(length)) { + return E_WRITE_PARCEL_ERR; + } + + for (auto &sharePath : sharePathList) { + if (!data.WriteString(sharePath)) { + return E_WRITE_PARCEL_ERR; + } + } + + int err = SendRequest(DELETE_SHARE_FILE, data, reply, option); + if (err != E_OK) { + return err; + } + + return reply.ReadInt32(); +} int32_t StorageDaemonProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { sptr remote = Remote(); diff --git a/services/storage_daemon/ipc/src/storage_daemon_stub.cpp b/services/storage_daemon/ipc/src/storage_daemon_stub.cpp index cb9d3f5a..67f27a03 100644 --- a/services/storage_daemon/ipc/src/storage_daemon_stub.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon_stub.cpp @@ -90,6 +90,12 @@ int32_t StorageDaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, case UPDATE_KEY_CONTEXT: err = HandleUpdateKeyContext(data, reply); break; + case CREATE_SHARE_FILE: + err = HandleCreateShareFile(data, reply); + break; + case DELETE_SHARE_FILE: + err = HandleDeleteShareFile(data, reply); + break; default: { LOGI(" use IPCObjectStub default OnRemoteRequest"); err = IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -335,5 +341,33 @@ int32_t StorageDaemonStub::HandleUpdateKeyContext(MessageParcel &data, MessagePa return E_OK; } + +int32_t StorageDaemonStub::HandleCreateShareFile(MessageParcel &data, MessageParcel &reply) +{ + std::string uri = data.ReadString(); + int32_t tokenId = data.ReadInt32(); + int32_t flag = data.ReadUint32(); + int err = CreateShareFile(uri, tokenId, flag); + if (!reply.WriteInt32(err)) { + return E_WRITE_REPLY_ERR; + } + return E_OK; +} + +int32_t StorageDaemonStub::HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply) +{ + int32_t tokenId = data.ReadInt32(); + int32_t length = data.ReadInt32(); + std::vector sharePathList; + for (int32_t i = 0; i < length; i++) { + std::string path = data.ReadString(); + sharePathList.emplace_back(path); + } + int err = DeleteShareFile(tokenId, sharePathList); + if (!reply.WriteInt32(err)) { + return E_WRITE_REPLY_ERR; + } + return E_OK; +} } // StorageDaemon } // OHOS diff --git a/services/storage_daemon/ipc/test/BUILD.gn b/services/storage_daemon/ipc/test/BUILD.gn index c212cb47..7f13f906 100644 --- a/services/storage_daemon/ipc/test/BUILD.gn +++ b/services/storage_daemon/ipc/test/BUILD.gn @@ -63,7 +63,11 @@ ohos_unittest("storage_daemon_test") { ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", "access_token:libaccesstoken_sdk", + "app_file_service:fileshare_native", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "init:libbegetutil", diff --git a/services/storage_daemon/ipc/test/storage_daemon_service_mock.h b/services/storage_daemon/ipc/test/storage_daemon_service_mock.h index ec5f4e5a..15dec40b 100644 --- a/services/storage_daemon/ipc/test/storage_daemon_service_mock.h +++ b/services/storage_daemon/ipc/test/storage_daemon_service_mock.h @@ -131,6 +131,16 @@ public: { return E_OK; } + + virtual int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) override + { + return E_OK; + } + + virtual int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) override + { + return E_OK; + } }; } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h b/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h index 1062ee73..8f64a569 100644 --- a/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h +++ b/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h @@ -46,6 +46,8 @@ public: MOCK_METHOD3(ActiveUserKey, int32_t (uint32_t, const std::vector &, const std::vector &)); MOCK_METHOD1(InactiveUserKey, int32_t (uint32_t)); MOCK_METHOD1(UpdateKeyContext, int32_t (uint32_t)); + MOCK_METHOD3(CreateShareFile, int32_t (std::string, int32_t, int32_t)); + MOCK_METHOD2(DeleteShareFile, int32_t (int32_t, std::vector)); }; } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_daemon/storage_daemon.cfg b/services/storage_daemon/storage_daemon.cfg index 63df0a0d..63f9cf73 100644 --- a/services/storage_daemon/storage_daemon.cfg +++ b/services/storage_daemon/storage_daemon.cfg @@ -5,6 +5,7 @@ "start storage_daemon", "mount configfs none /config", "chown foundation system /config/hmdfs", + "chown foundation system /config/sharefs", "mkdir /mnt/external 0711 root file_manager" ] } diff --git a/services/storage_daemon/user/src/mount_manager.cpp b/services/storage_daemon/user/src/mount_manager.cpp index 9315024a..5ba3b83a 100644 --- a/services/storage_daemon/user/src/mount_manager.cpp +++ b/services/storage_daemon/user/src/mount_manager.cpp @@ -37,7 +37,8 @@ const std::string HMDFS_SYS_CAP = "const.distributed_file_property.enabled"; const int32_t HMDFS_VAL_LEN = 6; const int32_t HMDFS_TRUE_LEN = 5; MountManager::MountManager() - : hmdfsDirVec_{{"/data/service/el2/%d/hmdfs", 0711, OID_SYSTEM, OID_SYSTEM}, + : hmdfsDirVec_{{"/data/service/el2/%d/share", 0711, OID_SYSTEM, OID_SYSTEM}, + {"/data/service/el2/%d/hmdfs", 0711, OID_SYSTEM, OID_SYSTEM}, {"/data/service/el2/%d/hmdfs/account", 0711, OID_SYSTEM, OID_SYSTEM}, {"/data/service/el2/%d/hmdfs/account/files", 02771, OID_USER_DATA_RW, OID_USER_DATA_RW}, {"/data/service/el2/%d/hmdfs/account/data", 0711, OID_SYSTEM, OID_SYSTEM}, @@ -50,6 +51,8 @@ MountManager::MountManager() {"/data/service/el2/%d/hmdfs/account/services", 0771, OID_DFS_SHARE, OID_DFS_SHARE}}, virtualDir_{{"/storage/media/%d", 0711, OID_USER_DATA_RW, OID_USER_DATA_RW}, {"/storage/media/%d/local", 0711, OID_USER_DATA_RW, OID_USER_DATA_RW}, + {"/mnt/share/", 0711, OID_ROOT, OID_ROOT}, + {"/mnt/share/%d/", 0711, OID_ROOT, OID_ROOT}, {"/mnt/hmdfs/", 0711, OID_ROOT, OID_ROOT}, {"/mnt/hmdfs/%d/", 0711, OID_ROOT, OID_ROOT}, {"/mnt/hmdfs/%d/account", 0711, OID_ROOT, OID_ROOT}, @@ -80,6 +83,30 @@ int32_t MountManager::HmdfsTwiceMount(int32_t userId, std::string relativePath) return E_OK; } +int32_t MountManager::SharefsMount(int32_t userId) +{ + Utils::MountArgument sharefsMntArgs(Utils::MountArgumentDescriptors::Alpha(userId, "")); + int ret = Mount(sharefsMntArgs.GetShareSrc(), sharefsMntArgs.GetShareDst(), "sharefs", + sharefsMntArgs.GetFlags(), sharefsMntArgs.GetUserIdPara().c_str()); + if (ret != 0 && errno != EEXIST && errno != EBUSY) { + LOGE("failed to mount sharefs, err %{public}d", errno); + return E_MOUNT; + } + return E_OK; +} + +int32_t MountManager::SharefsUMount(int32_t userId) +{ + Utils::MountArgument sharefsMntArgs(Utils::MountArgumentDescriptors::Alpha(userId, "")); + int32_t ret = UMount2(sharefsMntArgs.GetShareDst().c_str(), MNT_DETACH); + if (ret != E_OK) { + LOGE("umount sharefs, errno %{public}d, sharefs dst %{public}s", errno, + sharefsMntArgs.GetShareDst().c_str()); + return E_UMOUNT; + } + return E_OK; +} + int32_t MountManager::HmdfsMount(int32_t userId, std::string relativePath) { Utils::MountArgument hmdfsMntArgs(Utils::MountArgumentDescriptors::Alpha(userId, relativePath)); @@ -176,17 +203,28 @@ int32_t MountManager::LocalMount(int32_t userId) int32_t MountManager::MountByUser(int32_t userId) { + int ret = E_OK; if (CreateVirtualDirs(userId) != E_OK) { LOGE("create hmdfs virtual dir error"); return E_PREPARE_DIR; } if (!SupportHmdfs()) { - return LocalMount(userId); + ret = LocalMount(userId); } else { - return HmdfsMount(userId); + ret = HmdfsMount(userId); } + if (!ret) { + LOGE("hmdfs mount error"); + return ret; + } + + ret = SharefsMount(userId); + if (!ret) { + LOGE("sharefs mount error"); + return ret; + } return E_OK; } @@ -201,20 +239,26 @@ int32_t MountManager::UmountByUser(int32_t userId) int32_t count = 0; while (count < UMOUNT_RETRY_TIMES) { int32_t err = E_OK; + err = SharefsUMount(userId); if (!SupportHmdfs()) { err = LocalUMount(userId); } else { err = HmdfsUMount(userId); } - if (err == E_OK) { - break; - } else if (errno == EBUSY) { + if (errno == EBUSY) { count++; continue; } else { - LOGE("failed to umount, errno %{public}d", errno); + LOGE("failed to umount hmdfs, errno %{public}d", errno); return E_UMOUNT; } + if (err != E_OK && err != EBUSY) { + LOGE("failed to umount sharefs, errno %{public}d", errno); + return E_UMOUNT; + } + if (err == E_OK) { + break; + } } return E_OK; diff --git a/services/storage_daemon/utils/mount_argument_utils.cpp b/services/storage_daemon/utils/mount_argument_utils.cpp index f09add46..8ad4244d 100644 --- a/services/storage_daemon/utils/mount_argument_utils.cpp +++ b/services/storage_daemon/utils/mount_argument_utils.cpp @@ -27,6 +27,8 @@ namespace { static const std::string BASE_MOUNT_POINT = "/mnt/hmdfs/"; static const std::string SYSFS_HMDFS_PATH = "/sys/fs/hmdfs/"; static const std::string COMM_DATA_POINT = "/storage/media/"; + static const std::string SHAREFS_DATA_POINT = "/data/service/el2/"; + static const std::string SHAREFS_BASE_MOUNT_POINT = "/mnt/share/"; } // namespace string MountArgument::GetFullSrc() const @@ -45,6 +47,30 @@ string MountArgument::GetFullDst() const return ss.str(); } +string MountArgument::GetShareSrc() const +{ + stringstream ss; + ss << SHAREFS_DATA_POINT << userId_ << "/share"; + + return ss.str(); +} + +string MountArgument::GetUserIdPara() const +{ + stringstream ss; + ss << "user_id=" << userId_; + + return ss.str(); +} + +string MountArgument::GetShareDst() const +{ + stringstream ss; + ss << SHAREFS_BASE_MOUNT_POINT << userId_; + + return ss.str(); +} + string MountArgument::GetCommFullPath() const { stringstream ss; diff --git a/services/storage_manager/include/ipc/storage_manager.h b/services/storage_manager/include/ipc/storage_manager.h index b2a649f0..0ef24239 100644 --- a/services/storage_manager/include/ipc/storage_manager.h +++ b/services/storage_manager/include/ipc/storage_manager.h @@ -83,6 +83,10 @@ public: const std::vector &secret) override; int32_t InactiveUserKey(uint32_t userId) override; int32_t UpdateKeyContext(uint32_t userId) override; + + // app file share api + int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) override; + int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) override; private: StorageManager(); static sptr instance_; diff --git a/services/storage_manager/include/ipc/storage_manager_proxy.h b/services/storage_manager/include/ipc/storage_manager_proxy.h index fab9f425..0cac34ff 100644 --- a/services/storage_manager/include/ipc/storage_manager_proxy.h +++ b/services/storage_manager/include/ipc/storage_manager_proxy.h @@ -70,6 +70,10 @@ public: int32_t InactiveUserKey(uint32_t userId) override; int32_t UpdateKeyContext(uint32_t userId) override; + // app file share api + int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) override; + int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) override; + private: static inline BrokerDelegator delegator_; int32_t SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/services/storage_manager/include/ipc/storage_manager_stub.h b/services/storage_manager/include/ipc/storage_manager_stub.h index a24469a0..aab2f7aa 100644 --- a/services/storage_manager/include/ipc/storage_manager_stub.h +++ b/services/storage_manager/include/ipc/storage_manager_stub.h @@ -62,6 +62,10 @@ private: int32_t HandleActiveUserKey(MessageParcel &data, MessageParcel &reply); int32_t HandleInactiveUserKey(MessageParcel &data, MessageParcel &reply); int32_t HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply); + + // app file share api + int32_t HandleCreateShareFile(MessageParcel &data, MessageParcel &reply); + int32_t HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply); }; } // StorageManager } // OHOS diff --git a/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h b/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h index f449e04d..c4ce7798 100644 --- a/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h +++ b/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h @@ -56,6 +56,10 @@ public: int32_t UpdateKeyContext(uint32_t userId); int32_t ResetSdProxy(); + + // app file share api + int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag); + int32_t DeleteShareFile(int32_t tokenId, std::vector sharePathList); private: sptr storageDaemon_; sptr deathRecipient_ = nullptr; diff --git a/services/storage_manager/ipc/src/storage_manager.cpp b/services/storage_manager/ipc/src/storage_manager.cpp index c9127b1b..1704304e 100644 --- a/services/storage_manager/ipc/src/storage_manager.cpp +++ b/services/storage_manager/ipc/src/storage_manager.cpp @@ -23,6 +23,7 @@ #include "account_subscriber/account_subscriber.h" #include "crypto/filesystem_crypto.h" #include "disk/disk_manager_service.h" +#include "storage_daemon_communication/storage_daemon_communication.h" #include "storage_service_errno.h" #include "storage_service_log.h" #include "system_ability_definition.h" @@ -306,5 +307,19 @@ int32_t StorageManager::UpdateKeyContext(uint32_t userId) int32_t err = fsCrypto->UpdateKeyContext(userId); return err; } + +int32_t StorageManager::CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) +{ + std::shared_ptr sdCommunication; + sdCommunication = DelayedSingleton::GetInstance(); + return sdCommunication->CreateShareFile(uri, tokenId, flag); +} + +int32_t StorageManager::DeleteShareFile(int32_t tokenId, std::vector sharePathList) +{ + std::shared_ptr sdCommunication; + sdCommunication = DelayedSingleton::GetInstance(); + return sdCommunication->DeleteShareFile(tokenId, sharePathList); +} } } diff --git a/services/storage_manager/ipc/src/storage_manager_proxy.cpp b/services/storage_manager/ipc/src/storage_manager_proxy.cpp index b3a23d19..6fb2aaa9 100644 --- a/services/storage_manager/ipc/src/storage_manager_proxy.cpp +++ b/services/storage_manager/ipc/src/storage_manager_proxy.cpp @@ -851,6 +851,69 @@ int32_t StorageManagerProxy::GetDiskById(std::string diskId, Disk &disk) return reply.ReadInt32(); } +int32_t StorageManagerProxy::CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + + if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) { + return E_WRITE_DESCRIPTOR_ERR; + } + + if (!data.WriteString(uri)) { + return E_WRITE_PARCEL_ERR; + } + + if (!data.WriteUint32(tokenId)) { + return E_WRITE_PARCEL_ERR; + } + + if (!data.WriteUint32(flag)) { + return E_WRITE_PARCEL_ERR; + } + + int err = SendRequest(CREATE_SHARE_FILE, data, reply, option); + if (err != E_OK) { + return err; + } + + return reply.ReadInt32(); +} + +int32_t StorageManagerProxy::DeleteShareFile(int32_t tokenId, std::vectorsharePathList) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + + if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) { + return E_WRITE_DESCRIPTOR_ERR; + } + + if (!data.WriteUint32(tokenId)) { + return E_WRITE_PARCEL_ERR; + } + + size_t length = sharePathList.size(); + if (!data.WriteUint32(length)) { + return E_WRITE_PARCEL_ERR; + } + + for (auto &sharePath : sharePathList) { + if (!data.WriteString(sharePath)) { + return E_WRITE_PARCEL_ERR; + } + } + + int err = SendRequest(DELETE_SHARE_FILE, data, reply, option); + if (err != E_OK) { + return err; + } + + return reply.ReadInt32(); +} + int32_t StorageManagerProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { diff --git a/services/storage_manager/ipc/src/storage_manager_stub.cpp b/services/storage_manager/ipc/src/storage_manager_stub.cpp index 64c5f0b7..c6b547a1 100644 --- a/services/storage_manager/ipc/src/storage_manager_stub.cpp +++ b/services/storage_manager/ipc/src/storage_manager_stub.cpp @@ -148,6 +148,12 @@ int32_t StorageManagerStub::OnRemoteRequest(uint32_t code, case UPDATE_KEY_CONTEXT: err = HandleUpdateKeyContext(data, reply); break; + case CREATE_SHARE_FILE: + err = HandleCreateShareFile(data, reply); + break; + case DELETE_SHARE_FILE: + err = HandleDeleteShareFile(data, reply); + break; default: { LOGI("use IPCObjectStub default OnRemoteRequest"); err = IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -715,5 +721,39 @@ int32_t StorageManagerStub::HandleUpdateKeyContext(MessageParcel &data, MessageP return E_OK; } + +int32_t StorageManagerStub::HandleCreateShareFile(MessageParcel &data, MessageParcel &reply) +{ + if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) { + return E_PERMISSION_DENIED; + } + std::string uri = data.ReadString(); + int32_t tokenId = data.ReadInt32(); + int32_t flag = data.ReadUint32(); + int err = CreateShareFile(uri, tokenId, flag); + if (!reply.WriteInt32(err)) { + return E_WRITE_REPLY_ERR; + } + return E_OK; +} + +int32_t StorageManagerStub::HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply) +{ + if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) { + return E_PERMISSION_DENIED; + } + int32_t tokenId = data.ReadInt32(); + int32_t length = data.ReadInt32(); + std::vector sharePathList; + for (int32_t i = 0; i < length; i++) { + std::string path = data.ReadString(); + sharePathList.emplace_back(path); + } + int err = DeleteShareFile(tokenId, sharePathList); + if (!reply.WriteInt32(err)) { + return E_WRITE_REPLY_ERR; + } + return E_OK; +} } // StorageManager } // OHOS diff --git a/services/storage_manager/ipc/test/BUILD.gn b/services/storage_manager/ipc/test/BUILD.gn index fbf4243d..0d165c86 100644 --- a/services/storage_manager/ipc/test/BUILD.gn +++ b/services/storage_manager/ipc/test/BUILD.gn @@ -50,6 +50,8 @@ ohos_unittest("storage_manager_proxy_test") { external_deps = [ "ability_base:base", "ability_base:want", + "ability_base:zuri", + "app_file_service:fileshare_native", "common_event_service:cesfwk_innerkits", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", @@ -96,9 +98,13 @@ ohos_unittest("storage_manager_stub_test") { ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", + "app_file_service:fileshare_native", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "init:libbegetutil", @@ -144,7 +150,11 @@ ohos_unittest("storage_manager_stub_nonpermission_test") { ] external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", "access_token:libaccesstoken_sdk", + "app_file_service:fileshare_native", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "init:libbegetutil", diff --git a/services/storage_manager/ipc/test/storage_manager_service_mock.h b/services/storage_manager/ipc/test/storage_manager_service_mock.h index dbe4cb18..a4a20fce 100644 --- a/services/storage_manager/ipc/test/storage_manager_service_mock.h +++ b/services/storage_manager/ipc/test/storage_manager_service_mock.h @@ -210,6 +210,16 @@ public: { return E_OK; } + + virtual int32_t CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) override + { + return E_OK; + } + + virtual int32_t DeleteShareFile(int32_t tokenId, std::vectorsharePathList) override + { + return E_OK; + } }; } // namespace StorageManager } // namespace OHOS diff --git a/services/storage_manager/ipc/test/storage_manager_stub_mock.h b/services/storage_manager/ipc/test/storage_manager_stub_mock.h index 88af84a9..ab2944a2 100644 --- a/services/storage_manager/ipc/test/storage_manager_stub_mock.h +++ b/services/storage_manager/ipc/test/storage_manager_stub_mock.h @@ -61,6 +61,8 @@ public: MOCK_METHOD3(ActiveUserKey, int32_t(uint32_t, const std::vector &, const std::vector &)); MOCK_METHOD1(InactiveUserKey, int32_t(uint32_t)); MOCK_METHOD1(UpdateKeyContext, int32_t(uint32_t)); + MOCK_METHOD3(CreateShareFile, int32_t (std::string, int32_t, int32_t)); + MOCK_METHOD2(DeleteShareFile, int32_t (int32_t, std::vector)); }; } // namespace StorageManager } // namespace OHOS diff --git a/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp b/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp index a54f1bf9..73ad992c 100644 --- a/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp +++ b/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp @@ -266,5 +266,27 @@ void SdDeathRecipient::OnRemoteDied(const wptr &remote) { DelayedSingleton::GetInstance()->ResetSdProxy(); } + +int32_t StorageDaemonCommunication::CreateShareFile(std::string uri, int32_t tokenId, int32_t flag) +{ + LOGI("enter"); + int32_t err = Connect(); + if (err != E_OK) { + LOGE("Connect failed"); + return err; + } + return storageDaemon_->CreateShareFile(uri, tokenId, flag); +} + +int32_t StorageDaemonCommunication::DeleteShareFile(int32_t tokenId, std::vectorsharePathList) +{ + LOGI("enter"); + int32_t err = Connect(); + if (err != E_OK) { + LOGE("Connect failed"); + return err; + } + return storageDaemon_->DeleteShareFile(tokenId, sharePathList); +} } // namespace StorageManager } // namespace OHOS