diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..4947287f7b5ccb5d1e8b7b2d3aa5d89f322c160d --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.md b/README.md index 1b2468c0540c7908114042f740c6afe65072d9b3..520b34b1391da4d7bce2dda8a024aaf6de6dd899 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,64 @@ -# storage_user_file_manger +# File Mangaer Service公共文件管理服务 -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} +- [简介](#section1158716411637) +- [目录](#section161941989596) +- [使用说明](#usage-guidelines) + - [创建文件](#get-audioasset) +- [相关仓](#section1533973044317) -#### 软件架构 -软件架构说明 +## 简介 -#### 安装教程 +**File Mangaer Service**公共文件管理服务仓库提供了公共文件管理接口。File Mangaer Service接口暂不对外部应用开放, 仅内部使用。 -1. xxxx -2. xxxx -3. xxxx +支持能力列举如下: +- 公共文件路径下媒体文件查询,创建 + 媒体文件包括图片,音频,视频。媒体文件通过相册方式呈现 + +- 公共文件路径下文档文件查询、创建 -#### 使用说明 + 文档文件包括其他文件,外部存储卡内的文件,文件以目录树方式呈现 -1. xxxx -2. xxxx -3. xxxx +**图 1** 公共文件管理架构图 +![](figures/file manager service.png "公共文件管理架构图") -#### 参与贡献 +## 目录 -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +仓目录结构如下: +``` +/foundation/storage/user_file_manger # fms组件代码 +├── figures # 插图文件 +├── serivce # 框架代码 +│   ├── etc # 内部接口实现 +│   ├── src # 内部接口实现 +├── interfaces # 接口代码 +│   ├── innerkits # 内部 Native 接口 +│   └── kits # 外部 JS 接口 +├── LICENSE # 证书文件 +├── ohos.build # 编译文件 +├── sa_profile # 服务配置文件 +└── services # 服务实现 +``` +## 使用说明 +### 创建文件 +1. + 接口流程 + 应用通过接口让媒体库创建文件,返回文件uri, 应用自己通过openfile打开uri,获取fd进行文件操作。 -#### 特技 + // file picker 流程 + // media_path通过filepicker获取待保存目录uri + import filemanager from '@ohos.filemanager' + let media_path = "xxxx" + let name = "xxxx" + filemanager.createFile(name,media_path) + .then((uri) => { + // success + }) + .catch(function(err) { + //类型错误,重名... + // fail + }) + ``` -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +## 相关仓 diff --git a/figures/file manager service.png b/figures/file manager service.png new file mode 100644 index 0000000000000000000000000000000000000000..8aa79a296704af54e3f1242e897f06d8ed0b1a6f Binary files /dev/null and b/figures/file manager service.png differ diff --git a/ohos.build b/ohos.build new file mode 100644 index 0000000000000000000000000000000000000000..ca16b7825bcd8b2864a8c59b66dac9188e82ca0f --- /dev/null +++ b/ohos.build @@ -0,0 +1,12 @@ +{ + "subsystem": "storage", + "parts": { + "filemanager": { + "module_list": [ + "//foundation/storage/services:fms", + "//foundation/storage/sa_profile:filemanager_service_sa_profile", + "//foundation/storage/interfaces/kits/js:filemanager" + ] + } + } +} diff --git a/sa_profile/4933.xml b/sa_profile/4933.xml new file mode 100644 index 0000000000000000000000000000000000000000..216eb6a8d7487075d7f01989d6c4040e3b6366f5 --- /dev/null +++ b/sa_profile/4933.xml @@ -0,0 +1,28 @@ + + + + fms_service + + /system/lib/libfms_server.z.so + + + 4933 + /system/lib/libfms_server.z.so + true + false + 1 + + diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..11b34990c58d10de761f9aeb2d62dd7bbae464f8 --- /dev/null +++ b/sa_profile/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (C) 2021 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/sa_profile/sa_profile.gni") + +ohos_sa_profile("filemanager_service_sa_profile") { + sources = [ "4933.xml" ] + + part_name = "filemanager" +} diff --git a/services/BUILD.gn b/services/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b28589074aaa2e1ab9067e17f1bfb11f67a036f3 --- /dev/null +++ b/services/BUILD.gn @@ -0,0 +1,119 @@ +# Copyright (c) 2021 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") +FMS_BASE_DIR = "//foundation/storage/services" + +group("fms") { + deps = [ + ":fms_service.rc", + ":fms_server", + ":fms_exe", + ] +} + +ohos_prebuilt_etc("fms_service.rc") { + if (use_musl) { + source = "etc/fms_service.cfg" + } else { + source = "etc/fms_service.rc" + } + relative_install_dir = "init" + subsystem_name = "storage" + part_name = "filemanager" +} + + +ohos_shared_library("fms_server") { + subsystem_name = "storage" + part_name = "filemanager" + + include_dirs = [ + "$FMS_BASE_DIR/include", + "$FMS_BASE_DIR/src/client", + "$FMS_BASE_DIR/src/server", + "$FMS_BASE_DIR/src/fileoper", + "$FMS_BASE_DIR/include/fileoper", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include/hilog", + ] + + sources = [ + "src/server/file_manager_service_stub.cpp", + "src/server/file_manager_service.cpp", + "src/fileoper/media_file_oper.cpp", + "src/fileoper/oper_factory.cpp", + "src/client/file_manager_proxy.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/medialibrary_data_ability:medialibrary_data_ability", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/base:base", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "aafwk_standard:ability_manager", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + ] +} + + +ohos_executable("fms_exe") { + install_enable = true + + sources = [ "exe/main.cpp" ] + + include_dirs = [ + "$FMS_BASE_DIR/include", + "$FMS_BASE_DIR/src/client", + "$FMS_BASE_DIR/src/server", + "$FMS_BASE_DIR/src/fileoper", + ] + + deps = [ + "//utils/native/base:utils", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + ":fms_server", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + + subsystem_name = "storage" + part_name = "filemanager" +} + + +# Unittest +group("test") { + testonly = true + + deps = [ "test:test" ] +} diff --git a/services/etc/fms_service.cfg b/services/etc/fms_service.cfg new file mode 100644 index 0000000000000000000000000000000000000000..705fd8971e15e1037cf41738c6a74f543cdf9336 --- /dev/null +++ b/services/etc/fms_service.cfg @@ -0,0 +1,14 @@ +{ + "jobs" : [{ + "name" : "boot", + "cmds" : [ + "start fms_service" + ] + } + ], + "services" : [{ + "name" : "fms_service", + "path" : ["/system/bin/sa_main", "/system/profile/fms_service.xml"] + } + ] +} diff --git a/services/etc/fms_service.rc b/services/etc/fms_service.rc new file mode 100644 index 0000000000000000000000000000000000000000..41f1e4d141e30319b75b0a9bfcf327649c68a687 --- /dev/null +++ b/services/etc/fms_service.rc @@ -0,0 +1,20 @@ +# Copyright (C) 2021 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. + +service fms_service /system/bin/sa_main /system/profile/fms_service.xml + class z_core + seclabel u:r:audiodistributedservice:s0 + +on boot + start fms_service + diff --git a/services/include/file_manager_service_const.h b/services/include/file_manager_service_const.h new file mode 100644 index 0000000000000000000000000000000000000000..5eb1218db734a976b5c98b9a5a475c001193f857 --- /dev/null +++ b/services/include/file_manager_service_const.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2021 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. + */ +#pragma once + +#include +namespace OHOS { +namespace FileManagerService { +enum FILE_OPER { + GET_ROOT, + MKDIR, + LIST_FILE, + CREATE_FILE +}; + +enum EQUIPMENT { + INTERNAL_CARD, + EXTERNAL_CARD +}; + +const int32_t CODE_MASK = 0xff; +const int32_t EQUIPMENT_SHIFT = 16; + +const int32_t SUCCESS = 0; +const int32_t FAIL = -1; +const int32_t E_NOEXIST = -2; // file not exist +const int32_t E_EMPTYFOLDER = -3; // folder empty +} // namespace FileManagerService +} // namespace OHOS diff --git a/services/include/log.h b/services/include/log.h new file mode 100644 index 0000000000000000000000000000000000000000..87bd8a21117e25658b5049bdbc176556b3747b3b --- /dev/null +++ b/services/include/log.h @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include +#ifndef FILE_SUBSYSTEM_DEV_ON_PC +#include "hilog/log.h" +#endif + +#undef LOG_DOMAIN +#undef LOG_TAG +#define LOG_DOMAIN 0xD002B00 +#define LOG_TAG "Storage:FMS" + +#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) + +#ifndef FILE_SUBSYSTEM_DEV_ON_PC +#ifndef OHOS_DEBUG +#define DECORATOR_HILOG(op, fmt, args...) \ + do { \ + op(LOG_CORE, fmt, ##args); \ + } while (0) +#else +#define DECORATOR_HILOG(op, fmt, args...) \ + do { \ + op(LOG_CORE, "{%s()-%s:%d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); \ + } while (0) +#endif + +#define DEBUG_LOG(fmt, args...) HILOG_DEBUG(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); +#define ERR_LOG(fmt, args...) HILOG_ERROR(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); +#define WARNING_LOG(fmt, args...) HILOG_WARN(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); +#define INFO_LOG(fmt, args...) HILOG_INFO(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); +#define FATAL_LOG(fmt, args...) HILOG_FATAL(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); + +#else +#define PCLOG(fmt, ...) \ + do { \ + const std::vector filter = { \ + "{public}", \ + "{private}", \ + }; \ + std::string str____(fmt); \ + for (auto &&pattern : filter) { \ + size_t pos = 0; \ + while (std::string::npos != (pos = str____.find(pattern))) { \ + str____.erase(pos, pattern.length()); \ + } \ + } \ + str____ += "\n"; \ + printf(str____.c_str(), ##__VA_ARGS__); \ + } while (0); + +#define DEBUG_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) +#define INFO_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) +#define WARNING_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) +#define ERR_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) +#define FATAL_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) +#endif + +#define OK 0 +#define INVALID_PARAM (-1) +#define INIT_FAIL (-2) +#define ERR (-3) +#define PERMISSION_DENIED (-4) diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80a65d19ad378b3014dc787e8edb335e3f27bb03 --- /dev/null +++ b/services/src/client/file_manager_proxy.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2021 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 "file_manager_proxy.h" + +#include "file_manager_service_const.h" +#include "file_info.h" +#include "file_manager_service_stub.h" +#include "log.h" + +using namespace std; + +namespace OHOS { +namespace FileManagerService { +int GetFileInfo(FileInfo &file, MessageParcel &reply) +{ + string path; + string name; + string type; + int64_t size = 0; + int64_t at = 0; + int64_t mt = 0; + + reply.ReadString(path); + reply.ReadString(type); + reply.ReadString(name); + reply.ReadInt64(size); + reply.ReadInt64(at); + reply.ReadInt64(mt); + file = FileInfo(name, path, type, size, at, mt); + return SUCCESS; +} + +FileManagerProxy::FileManagerProxy(const sptr &impl) + : IRemoteProxy(impl) { } + +int FileManagerProxy::CreateFile(string name, string path, string &uri) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + sptr remote = Remote(); + data.WriteString(name); + data.WriteString(path); + int err = remote->SendRequest(FILE_OPER::CREATE_FILE, data, reply, option); + if (err != ERR_NONE) { + ERR_LOG("FileManagerProxy::CreateFile send request fail %{public}d", err); + return err; + } + reply.ReadString(uri); + reply.ReadInt32(err); + return err; +} + +IFmsClient *IFmsClient::GetFmsInstance() +{ + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + ERR_LOG("samgr object is NULL."); + return nullptr; + } + sptr object = samgr->GetSystemAbility(FILE_MANAGER_SERVICE_ID); + if (object == nullptr) { + ERR_LOG("FileManager Service object is NULL."); + return nullptr; + } + static FileManagerProxy proxy(object); + return &proxy; +} + +int FileManagerProxy::ListFile(string path, int off, int count, vector &fileRes) +{ + int err; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + sptr remote = Remote(); + data.WriteString(path); + data.WriteInt32(off); + data.WriteInt32(count); + err = remote->SendRequest(FILE_OPER::LIST_FILE, data, reply, option); + if (err != ERR_NONE) { + ERR_LOG("FileManagerProxy::ListFile err %{public}d", err); + return err; + } + int fileInfoNum = 0; + reply.ReadInt32(fileInfoNum); + while (fileInfoNum) { + FileInfo file; + GetFileInfo(file, reply); + fileRes.emplace_back(file); + fileInfoNum--; + } + reply.ReadInt32(err); + return err; +} + +int FileManagerProxy::Mkdir(string name, string path) +{ + int err; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + sptr remote = Remote(); + data.WriteString(name); + data.WriteString(path); + err = remote->SendRequest(FILE_OPER::MKDIR, data, reply, option); + if (err != ERR_NONE) { + ERR_LOG("FileManagerProxy::mkdir err %{public}d", err); + return err; + } + reply.ReadInt32(err); + return err; +} +} // FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/client/file_manager_proxy.h b/services/src/client/file_manager_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..4df64c1aeb09b1b78632b1862727fb51d2e43b78 --- /dev/null +++ b/services/src/client/file_manager_proxy.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2021 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. + */ +#pragma once + +#include "file_manager_service_stub.h" +#include "ifms_client.h" +#include "iremote_proxy.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace FileManagerService { +class FileManagerProxy : public IRemoteProxy, public IFmsClient { +public: + explicit FileManagerProxy(const sptr &impl); + virtual ~FileManagerProxy() = default; + static IFmsClient* GetFmsInstance(); + int Mkdir(std::string name, std::string path) override; + int ListFile(std::string path, int off, int count, std::vector &fileRes) override; + int CreateFile(std::string name, std::string path, std::string &uri) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/client/ifms_client.h b/services/src/client/ifms_client.h new file mode 100644 index 0000000000000000000000000000000000000000..105411fb0c3ecf8e6884d59ebe1cc8e4dad15d6d --- /dev/null +++ b/services/src/client/ifms_client.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once +#include "file_info.h" + +namespace OHOS { +namespace FileManagerService { +class IFmsClient { +public: + virtual ~IFmsClient() {} + + static IFmsClient *GetFmsInstance(); + + virtual int Mkdir(std::string name, std::string path) = 0; + virtual int ListFile(std::string path, int off, int count, std::vector &fileRes) = 0; + virtual int CreateFile(std::string name, std::string path, std::string &uri) = 0; +}; +} // namespace FileManagerService { +} // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/file_info.h b/services/src/fileoper/file_info.h new file mode 100644 index 0000000000000000000000000000000000000000..9262ccc36464045acb4ac9aab68cc5756c60d00a --- /dev/null +++ b/services/src/fileoper/file_info.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include +#include +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace FileManagerService { +class FileInfo { +public: + FileInfo() {}; + FileInfo(const std::string &name, const std::string &path, const std::string &type, int64_t size, int64_t added_time, + int64_t modified_time) : path_(path), name_(name), type_(type), size_(size), + added_time_(added_time), modified_time_(modified_time) {} + std::string GetName() const + { + return name_; + } + std::string GetPath() const + { + return path_; + } + std::string GetType() const + { + return type_; + } + int64_t GetSize() const + { + return size_; + } + int64_t GetAdded_Time() const + { + return added_time_; + } + int64_t GetModified_time() const + { + return modified_time_; + } +private: + std::string path_; + std::string name_; + std::string type_; + int64_t size_; + int64_t added_time_; + int64_t modified_time_; +}; +} // OHOS +} // FileManager \ No newline at end of file diff --git a/services/src/fileoper/file_oper.h b/services/src/fileoper/file_oper.h new file mode 100644 index 0000000000000000000000000000000000000000..f8027a3ec675aafba92d3857d5c59e9e0a1fadf6 --- /dev/null +++ b/services/src/fileoper/file_oper.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace FileManagerService { +class FileOper { +public: + virtual ~FileOper() = default; + virtual int Mkdir(const std::string &name, const std::string &path) = 0; + virtual int ListFile(const std::string &path, int offset, int count, MessageParcel &data) = 0; + virtual int CreateFile(const std::string &name, const std::string &path, std::string &uri) = 0; + virtual int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) = 0; +}; +} // OHOS +} // FileManager \ No newline at end of file diff --git a/services/src/fileoper/media_file_oper.cpp b/services/src/fileoper/media_file_oper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e9734bdf909f3339b75df97ae89b1dbb8c298c1 --- /dev/null +++ b/services/src/fileoper/media_file_oper.cpp @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2021 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 "media_file_oper.h" + +#include + +#include "file_info.h" +#include "file_manager_service_const.h" +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" +#include "log.h" +#include "media_data_ability_const.h" +#include "medialibrary_data_ability.h" + +using namespace std; + +namespace OHOS { +namespace FileManagerService { +const std::vector mediaData = { + Media::MEDIA_DATA_DB_ID, + Media::MEDIA_DATA_DB_URI, + Media::MEDIA_DATA_DB_NAME + }; +const std::vector> mediaMetaMap = { + {0, "string"}, // 0 id + {1, "string"}, // 1 fileuri + {4, "string"}, // 4 type + {6, "string"}, // 6 name + {7, "int"}, // 7 size + {8, "int"}, // 8 at + // 9 mt + {9, "int"} + }; +int Insert(const string &name, const string &path, const string &type) +{ + Media::ValuesBucket values; + Media::MediaLibraryDataAbility mediaLibDb; + string abilityUri = Media::MEDIALIBRARY_DATA_URI; + string oper; + if (type == "album") { + oper = Media::MEDIA_ALBUMOPRN + "/" + Media::MEDIA_ALBUMOPRN_CREATEALBUM; + } else if (type == "file") { + oper = Media::MEDIA_FILEOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET; + } + Uri createUri(abilityUri + "/" + oper); + struct stat statInfo {}; + // need getfileStat info + // need MEDIA_DATA_DB_RELATIVE_PATH + // need MEDIA_DATA_DB_NAME + values.PutLong(Media::MEDIA_DATA_DB_DATE_ADDED, statInfo.st_ctime); + values.PutLong(Media::MEDIA_DATA_DB_DATE_MODIFIED, statInfo.st_mtime); + mediaLibDb.InitMediaLibraryRdbStore(); + return mediaLibDb.Insert(createUri, values); +} + +bool PushFileInfo(shared_ptr result, MessageParcel &reply) +{ + string id; + string uri; + result->GetString(mediaMetaMap[0].first, id); + result->GetString(mediaMetaMap[1].first, uri); + reply.WriteString(uri + "/" + id); + for (int i = 2; i < mediaMetaMap.size(); i++) { + if (mediaMetaMap[i].second == "string") { + string value; + result->GetString(mediaMetaMap[i].first, value); + reply.WriteString(value); + } else { + int64_t value; + result->GetLong(mediaMetaMap[i].first, value); + reply.WriteInt64(value); + } + } + return true; +} + +int GetFileInfoFromResult(shared_ptr result, MessageParcel &reply) +{ + int count = 0; + result->GetRowCount(count); + if (count == 0) { + ERR_LOG("GetFileInfoFromResult::AbsSharedResultSet null"); + return FAIL; + } + result->GoToFirstRow(); + reply.WriteInt32(count); + for (int i = 0; i < count; i++) { + PushFileInfo(result, reply); + result->GoToNextRow(); + } + return SUCCESS; +} + +bool GetRelativePath(const string &path, string &relativePath) +{ + NativeRdb::DataAbilityPredicates predicates; + string selection = Media::MEDIA_DATA_DB_ID + " LIKE ? "; + vector selectionArgs = { path }; + predicates.SetWhereClause(selection); + predicates.SetWhereArgs(selectionArgs); + Media::MediaLibraryDataAbility mediaLibDb; + mediaLibDb.InitMediaLibraryRdbStore(); + Uri uri(Media::MEDIALIBRARY_DATA_URI); + vector columns; + shared_ptr result = mediaLibDb.Query(uri, columns, predicates); + if (result == nullptr) { + return false; + } + int count = 0; + result->GetRowCount(count); + + if (count != 1) { + ERR_LOG("GetRelativePath::AbsSharedResultSet more than one uri"); + } + int32_t columnIndex; + int ret = result->GetColumnIndex(Media::MEDIA_DATA_DB_FILE_PATH, columnIndex); + if (ret != NativeRdb::E_OK) { + return false; + } + result->GoToFirstRow(); + ret = result->GetString(columnIndex, relativePath); + if (ret != NativeRdb::E_OK) { + return false; + } + return true; +} + +int MediaFileOper::CreateFile(const std::string &name, const std::string &path, std::string &uri) +{ + string type = "file"; + int index = Insert(name, path, type); + // media type need to check the path + if (index < 0) { + ERR_LOG("MediaFileOper:: Fail to create fail %{public}s %{public}s", name.c_str(), path.c_str()); + return index; + } + uri = Media::MEDIALIBRARY_FILE_URI; + uri += "/" + to_string(index); + return SUCCESS; +} + +int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) +{ + int errCode = SUCCESS; + // media process + switch (code) { + case FILE_OPER::MKDIR: { + string name = data.ReadString(); + string path = data.ReadString(); + errCode = Mkdir(name, path); + break; + } + case FILE_OPER::LIST_FILE: { + string path = data.ReadString(); + int off = data.ReadInt32(); + int count = data.ReadInt32(); + errCode = ListFile(path, off, count, reply); + // need reply fileInfo + break; + } + case FILE_OPER::CREATE_FILE: { + string name = data.ReadString(); + string path = data.ReadString(); + string uri; + errCode = CreateFile(name, path, uri); + reply.WriteString(uri); + break; + } + default: + break; + } + return errCode; +} + +int MediaFileOper::ListFile(const string &path, int offset, int count, MessageParcel &reply) +{ + // get the relative path from the path uri + string relativePath; + if (!GetRelativePath(path, relativePath)) { + ERR_LOG("MediaFileOper::path not exsit"); + return E_NOEXIST; + } + NativeRdb::DataAbilityPredicates predicates; + string selection = Media::MEDIA_DATA_DB_RELATIVE_PATH + " LIKE ? "; + vector selectionArgs = { relativePath }; + predicates.SetWhereClause(selection); + predicates.SetWhereArgs(selectionArgs); + Media::MediaLibraryDataAbility mediaLibDb; + mediaLibDb.InitMediaLibraryRdbStore(); + Uri uri(Media::MEDIALIBRARY_DATA_URI); + vector columns; + shared_ptr result = mediaLibDb.Query(uri, columns, predicates); + if (result == nullptr) { + ERR_LOG("MediaFileOper::ListFile folder is empty"); + return E_EMPTYFOLDER; + } + GetFileInfoFromResult(result, reply); + return SUCCESS; +} + +int MediaFileOper::Mkdir(const string &name, const string &path) +{ + string type = "album"; + Insert(name, path, type); + DEBUG_LOG("MediaFileOper::mkdir path %{public}s.", path.c_str()); + return SUCCESS; +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/media_file_oper.h b/services/src/fileoper/media_file_oper.h new file mode 100644 index 0000000000000000000000000000000000000000..76d84858ce489857a30b74e133ec21d0899d068f --- /dev/null +++ b/services/src/fileoper/media_file_oper.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include +#include "file_oper.h" + +namespace OHOS { +namespace FileManagerService { +class MediaFileOper : public FileOper { +public: + virtual ~MediaFileOper() = default; + int Mkdir(const std::string &name, const std::string &path) override; + int ListFile(const std::string &path, int offset, int count, MessageParcel &data) override; + int CreateFile(const std::string &name, const std::string &path, std::string &uri) override; + int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) override; +}; +} // OHOS +} // FileManager \ No newline at end of file diff --git a/services/src/fileoper/oper_factory.cpp b/services/src/fileoper/oper_factory.cpp new file mode 100644 index 0000000000000000000000000000000000000000..542d88585ee4e798650209fbad94121c86e942bf --- /dev/null +++ b/services/src/fileoper/oper_factory.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2021 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 "oper_factory.h" + +#include "file_oper.h" +#include "file_manager_service_const.h" +#include "log.h" +#include "media_file_oper.h" + +using namespace std; +namespace OHOS { +namespace FileManagerService { +FileOper* OperFactory::getFileOper(int equipmentId) +{ + FileOper* fp = nullptr; + DEBUG_LOG("OperFactory::getFileOper %{public}d.", equipmentId); + switch (equipmentId) { + case EQUIPMENT::INTERNAL_CARD: { + fp = new MediaFileOper(); + break; + } + case EQUIPMENT::EXTERNAL_CARD: { + // do Exteranl storage process; + // return ExternalOper() + break; + } + default: { + break; + } + } + return fp; +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/oper_factory.h b/services/src/fileoper/oper_factory.h new file mode 100644 index 0000000000000000000000000000000000000000..992e2ccbe2cb2ba463c835851ee105d71bb1cf4f --- /dev/null +++ b/services/src/fileoper/oper_factory.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include + +#include "file_oper.h" + +namespace OHOS { +namespace FileManagerService { +class OperFactory { +public: + FileOper* getFileOper(int equipmentId); +}; +} // OHOS +} // FileManager \ No newline at end of file diff --git a/services/src/server/file_manager_service.cpp b/services/src/server/file_manager_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a924a621d41fa5e6ccd50039e073420e309400b --- /dev/null +++ b/services/src/server/file_manager_service.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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 "file_manager_service.h" + +#include "iservice_registry.h" +#include "log.h" +#include "system_ability_definition.h" + + +namespace OHOS { +namespace FileManagerService { +REGISTER_SYSTEM_ABILITY_BY_ID(FileManagerService, FILE_MANAGER_SERVICE_ID, true); + +FileManagerService::FileManagerService(int32_t systemAbilityId, bool runOnCreate) + : SystemAbility(systemAbilityId, runOnCreate) { } +void FileManagerService::OnDump() { } + +void FileManagerService::OnStart() +{ + bool res = Publish(this); + if (!res) { + ERR_LOG("FileManagerService OnStart invalid"); + } +} + +void FileManagerService::OnStop() +{ + DEBUG_LOG("FileManagerService OnStop"); +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/server/file_manager_service.h b/services/src/server/file_manager_service.h new file mode 100644 index 0000000000000000000000000000000000000000..181d29d4d64ad34acaf10f88f689bd987053e7c4 --- /dev/null +++ b/services/src/server/file_manager_service.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include "file_manager_service_stub.h" +#include "iremote_stub.h" +#include "system_ability.h" + +namespace OHOS { +namespace FileManagerService { +class FileManagerService : public SystemAbility, public FileManagerServiceStub { + DECLARE_SYSTEM_ABILITY(FileManagerService); +public: + DISALLOW_COPY_AND_MOVE(FileManagerService); + explicit FileManagerService(int32_t systemAbilityId, bool runOnCreate = true); + virtual ~FileManagerService() = default; + void OnDump() override; + void OnStart() override; + void OnStop() override; +}; +} // namespace FileManagerService +} // namespace OHOS diff --git a/services/src/server/file_manager_service_stub.cpp b/services/src/server/file_manager_service_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d286a6f011fbb06fde4eeda6109981b430a58a26 --- /dev/null +++ b/services/src/server/file_manager_service_stub.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2021 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 "file_manager_service_stub.h" + +#include "file_manager_service_const.h" +#include "file_manager_service.h" +#include "log.h" +#include "oper_factory.h" + +using namespace std; + +namespace OHOS { +namespace FileManagerService { +int getEquipmentCode(uint32_t code) +{ + return (code >> EQUIPMENT_SHIFT) & CODE_MASK; +} +int getOperCode(uint32_t code) +{ + return code & CODE_MASK; +} +int FileManagerServiceStub::OperProcess(uint32_t code, MessageParcel &data, + MessageParcel &reply) +{ + int equipmentId = getEquipmentCode(code); + int operCode = getOperCode(code); + OperFactory factory = OperFactory(); + auto *fp = factory.getFileOper(equipmentId); + if (fp == nullptr) { + ERR_LOG("OnRemoteRequest inner error %{public}d", code); + return FAIL; + } + int errCode = fp->OperProcess(operCode, data, reply); + + delete fp; + return errCode; +} + +int FileManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + // to do checkpermission() + // do file process + int32_t errCode = OperProcess(code, data, reply); + reply.WriteInt32(errCode); + return errCode; +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/server/file_manager_service_stub.h b/services/src/server/file_manager_service_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..d0fbc97e062aec7a59a3530fe9c44b379fd28d52 --- /dev/null +++ b/services/src/server/file_manager_service_stub.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" +#include "oper_factory.h" + +namespace OHOS { +namespace FileManagerService { +class IFileManagerService : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"IFileManagerService"); +}; + +class FileManagerServiceStub : public IRemoteStub { +public: + int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply); + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; +}; +} // namespace FileManagerService +} // namespace OHOS diff --git a/services/test/BUILD.gn b/services/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ae241dfe151f551fa3532632509fce7fe895f176 --- /dev/null +++ b/services/test/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2021 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/test.gni") +FMS_BASE_DIR = "//foundation/storage/services" +ohos_unittest("fms_test") { + module_out_path = "storage/fms" + + sources = [ "fms_test.cpp" ] + + include_dirs = [ + "//third_party/json/include", + "//base/security/huks/interfaces/innerkits/huks_standard/main/include", + "$FMS_BASE_DIR/include", + "$FMS_BASE_DIR/src/client", + "$FMS_BASE_DIR/src/server", + ] + + configs = [ + "//build/config/compiler:exceptions", + ] + + deps = [ + "//foundation/storage/services:fms_server", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] +} + +group("test") { + testonly = true + + deps = [ ":fms_test" ] +} diff --git a/services/test/fms_test.cpp b/services/test/fms_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a339a9c6d1eb3bcdf2eabc95f5ccacec21631330 --- /dev/null +++ b/services/test/fms_test.cpp @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "file_manager_proxy.h" + +namespace { +using namespace std; +using namespace OHOS; +class FMSTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + cout << "FMS code test" << endl; + } + static void TearDownTestCase() {} +}; + +/** + * @tc.number: SUB_STORAGE_FMS_Proxy_GetFmsInstance_0000 + * @tc.name: fms_Proxy_GetFmsInstance_0000 + * @tc.desc: Test function of GetFmsInstance interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Proxy_GetFmsInstance_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Proxy_GetFmsInstance_0000" << endl; + try { + FileManagerService::FileManagerProxy proxy; + IFmsClient result = proxy.GetFmsInstance(); + EXPECT_NE(result, nullptr); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Proxy_GetFmsInstance_0000" << endl; +} + +/** + * @tc.number: SUB_STORAGE_FMS_Proxy_getFileInfo_0000 + * @tc.name: fms_Proxy_getFileInfo_0000 + * @tc.desc: Test function of getFileInfo interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Proxy_getFileInfo_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Proxy_getFileInfo_0000" << endl; + try { + FileManagerService::FileManagerProxy proxy; + FileInfo fileInfo; + MessageParcel messageParcel; + messageParcel.WriteString("FMS_Proxy_getFileInfo_0000"); + messageParcel.WriteString("./"); + messageParcel.WriteString("file"); + messageParcel.WriteString(0); + messageParcel.WriteString(0); + messageParcel.WriteString(0); + int result = proxy.getFileInfo(fileInfo, messageParcel); + EXPECT_EQ(result, 0); + EXPECT_NE(fileInfo, nullptr); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Proxy_getFileInfo_0000" << endl; +} + +/** + * @tc.number: SUB_STORAGE_FMS_Proxy_ListFile_0000 + * @tc.name: fms_Proxy_ListFile_0000 + * @tc.desc: Test function of ListFile interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Proxy_ListFile_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Proxy_ListFile_0000" << endl; + try { + FileManagerService::FileManagerProxy proxy; + vector fileList; + int result = proxy.ListFile("./", 0, 1, fileList); + EXPECT_EQ(result, 0); + EXPECT_NE(fileList, nullptr); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Proxy_ListFile_0000" << endl; +} + +/** + * @tc.number: SUB_STORAGE_FMS_Mediafile_Insert_0000 + * @tc.name: fms_Mediafile_Insert_0000 + * @tc.desc: Test function of Insert interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Mediafile_Insert_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Mediafile_Insert_0000" << endl; + try { + int result = MediaFileOper::Insert("FMS_Mediafile_Insert_0000", "./", "file"); + EXPECT_EQ(result, 0); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Mediafile_Insert_0000" << endl; +} + +/** + * @tc.number: SUB_STORAGE_FMS_Mediafile_CreateFile_0000 + * @tc.name: fms_Mediafile_CreateFile_0000 + * @tc.desc: Test function of CreateFile interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Mediafile_CreateFile_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Mediafile_CreateFile_0000" << endl; + try { + string path = "./"; + string url; + int result = MediaFileOper::CreateFile("FMS_Mediafile_CreateFile_0000", path, url); + EXPECT_EQ(result, 0); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Mediafile_CreateFile_0000" << endl; +} + +/** + * @tc.number: SUB_STORAGE_FMS_Mediafile_pushFileInfo_0000 + * @tc.name: fms_Mediafile_pushFileInfo_0000 + * @tc.desc: Test function of pushFileInfo interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Mediafile_pushFileInfo_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Mediafile_pushFileInfo_0000" << endl; + try { + FileInfo fileInfo = FileInfo("FMS_Mediafile_pushFileInfo_0000", "./", "file", 0, 0, 0); + MessageParcel messageParcel; + int result = MediaFileOper::pushFileInfo(fileInfo, messageParcel); + EXPECT_EQ(result, 0); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Mediafile_pushFileInfo_0000" << endl; +} + +/** + * @tc.number: SUB_STORAGE_FMS_Mediafile_ListFile_0000 + * @tc.name: fms_Mediafile_ListFile_0000 + * @tc.desc: Test function of ListFile interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Mediafile_ListFile_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Mediafile_ListFile_0000" << endl; + try { + MessageParcel messageParcel; + int result = MediaFileOper::ListFile("./", 0, 0, messageParcel); + EXPECT_EQ(result, 0); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Mediafile_ListFile_0000" << endl; +} + +/** + * @tc.number: SUB_STORAGE_FMS_Mediafile_mkdir_0000 + * @tc.name: fms_Mediafile_mkdir_0000 + * @tc.desc: Test function of mkdir interface. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: + */ +HWTEST_F(FMSTest, FMS_Mediafile_mkdir_0000, testing::ext::TestSize.Level1) +{ + cout << "FMSTest-begin FMS_Mediafile_mkdir_0000" << endl; + try { + int result = MediaFileOper::mkdir("FMS_Mediafile_mkdir_0000", "./"); + EXPECT_EQ(result, 1); + } catch (...) { + cout << "FMSTest-an exception occurred." << endl; + } + cout << "FMSTest-end FMS_Mediafile_mkdir_0000" << endl; +} +} // namespace \ No newline at end of file