From 6b102a56d490549d77ef94f580cac18061cc8391 Mon Sep 17 00:00:00 2001 From: maokelong Date: Thu, 28 Oct 2021 11:46:37 +0800 Subject: [PATCH 1/5] Upload the skeleton and the mount manager Signed-off-by: maokelong --- .clang-format | 162 ++++++++++++++++ LICENSE | 177 ++++++++++++++++++ distributedfile.gni | 18 ++ frameworks/native/BUILD.gn | 24 +++ interfaces/innerkits/native/deamon_proxy.h | 33 ++++ ohos.build | 17 ++ services/4901.xml | 30 +++ services/BUILD.gn | 35 ++++ services/distributedfile.cfg | 16 ++ services/distributedfiledeamon/BUILD.gn | 54 ++++++ .../include/ipc/deamon.h | 57 ++++++ .../include/ipc/deamon_stub.h | 37 ++++ .../include/ipc/i_deamon.h | 33 ++++ .../include/mountpoint/mount_manager.h | 42 +++++ .../include/mountpoint/mount_point.h | 49 +++++ .../distributedfiledeamon/src/ipc/deamon.cpp | 78 ++++++++ .../src/ipc/deamon_stub.cpp | 42 +++++ .../src/mountpoint/mount_manager.cpp | 82 ++++++++ .../src/mountpoint/mount_point.cpp | 85 +++++++++ utils/BUILD.gn | 70 +++++++ utils/log/include/utils_log.h | 44 +++++ utils/log/src/utils_log.cpp | 27 +++ utils/system/include/utils_directory.h | 40 ++++ utils/system/include/utils_mount_argument.h | 49 +++++ utils/system/include/utils_uid.h | 0 utils/system/src/utils_directory.cpp | 81 ++++++++ utils/system/src/utils_mount_argument.cpp | 112 +++++++++++ utils/system/src/utils_uid.cpp | 0 28 files changed, 1494 insertions(+) create mode 100644 .clang-format create mode 100644 LICENSE create mode 100755 distributedfile.gni create mode 100644 frameworks/native/BUILD.gn create mode 100644 interfaces/innerkits/native/deamon_proxy.h create mode 100755 ohos.build create mode 100755 services/4901.xml create mode 100755 services/BUILD.gn create mode 100644 services/distributedfile.cfg create mode 100755 services/distributedfiledeamon/BUILD.gn create mode 100644 services/distributedfiledeamon/include/ipc/deamon.h create mode 100644 services/distributedfiledeamon/include/ipc/deamon_stub.h create mode 100644 services/distributedfiledeamon/include/ipc/i_deamon.h create mode 100644 services/distributedfiledeamon/include/mountpoint/mount_manager.h create mode 100644 services/distributedfiledeamon/include/mountpoint/mount_point.h create mode 100644 services/distributedfiledeamon/src/ipc/deamon.cpp create mode 100644 services/distributedfiledeamon/src/ipc/deamon_stub.cpp create mode 100644 services/distributedfiledeamon/src/mountpoint/mount_manager.cpp create mode 100644 services/distributedfiledeamon/src/mountpoint/mount_point.cpp create mode 100755 utils/BUILD.gn create mode 100755 utils/log/include/utils_log.h create mode 100644 utils/log/src/utils_log.cpp create mode 100644 utils/system/include/utils_directory.h create mode 100644 utils/system/include/utils_mount_argument.h create mode 100644 utils/system/include/utils_uid.h create mode 100644 utils/system/src/utils_directory.cpp create mode 100644 utils/system/src/utils_mount_argument.cpp create mode 100644 utils/system/src/utils_uid.cpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..5820ac636 --- /dev/null +++ b/.clang-format @@ -0,0 +1,162 @@ +Language: Cpp +# BasedOnStyle: LLVM +# 访问说明符(public、private等)的偏移 +AccessModifierOffset: -4 +# 开括号(开圆括号、开尖括号、开方括号)后的对齐 +AlignAfterOpenBracket: Align +# 连续赋值时,等号对齐 +AlignConsecutiveAssignments: false +# 连续赋值时,变量名对齐 +AlignConsecutiveDeclarations: false +# 左对齐逃脱换行(使用反斜杠换行)的反斜杠 +AlignEscapedNewlinesLeft: true +# 水平对齐二元和三元表达式的操作数 +AlignOperands: true +# 对齐连续的尾随的注释 +AlignTrailingComments: true +# 允许函数声明的所有参数在放在下一行 +AllowAllParametersOfDeclarationOnNextLine: false +# 允许短的块放在同一行 +AllowShortBlocksOnASingleLine: false +# 允许短的case标签放在同一行 +AllowShortCaseLabelsOnASingleLine: false +# 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All +AllowShortFunctionsOnASingleLine: Empty +# 允许短的if语句保持在同一行 +AllowShortIfStatementsOnASingleLine: false +# 允许短的循环保持在同一行 +AllowShortLoopsOnASingleLine: false +# 总是在定义返回类型后换行(deprecated) +AlwaysBreakAfterDefinitionReturnType: None +# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数), +# AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义) +AlwaysBreakAfterReturnType: None +# 总是在多行string字面量前换行 +AlwaysBreakBeforeMultilineStrings: true +# 总是在template声明后换行 +AlwaysBreakTemplateDeclarations: true +# +SpaceAfterTemplateKeyword: false +# false表示函数实参要么都在同一行,要么都各自一行 +BinPackArguments: true +# false表示所有形参要么都在同一行,要么都各自一行 +BinPackParameters: false +# 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效 +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行) +BreakBeforeBinaryOperators: None +# 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似), +# Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似), +# Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom +# 注:这里认为语句块也属于函数 +BreakBeforeBraces: Custom +# 在三元运算符前换行 +BreakBeforeTernaryOperators: true +# 在构造函数的初始化列表的逗号前换行 +BreakConstructorInitializersBeforeComma: false +# 每行字符的限制,0表示没有限制 +ColumnLimit: 120 +# 描述具有特殊意义的注释的正则表达式,它不应该被分割为多行或以其它方式改变 +CommentPragmas: "^ IWYU pragma:" +# 构造函数的初始化列表要么都在同一行,要么都各自一行 +ConstructorInitializerAllOnOneLineOrOnePerLine: true +# 构造函数的初始化列表的缩进宽度 +ConstructorInitializerIndentWidth: 4 +# 延续的行的缩进宽度 +ContinuationIndentWidth: 4 +# 去除C++11的列表初始化的大括号{后和}前的空格 +Cpp11BracedListStyle: true +# 继承最常用的指针和引用的对齐方式 +DerivePointerAlignment: false +# 关闭格式化 +DisableFormat: false +# 自动检测函数的调用和定义是否被格式为每行一个参数(Experimental) +ExperimentalAutoDetectBinPacking: false +# 需要被解读为foreach循环而不是函数调用的宏 +ForEachMacros: [foreach, Q_FOREACH, BOOST_FOREACH] +# 对#include进行排序,匹配了某正则表达式的#include拥有对应的优先级,匹配不到的则默认优先级为INT_MAX(优先级越小排序越靠前), +# 可以定义负数优先级从而保证某些#include永远在最前面 +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|isl|json)/)' + Priority: 3 + - Regex: ".*" + Priority: 1 +# 缩进case标签 +IndentCaseLabels: true +# 缩进宽度 +IndentWidth: 4 +# 函数返回类型换行时,缩进函数声明或函数定义的函数名 +IndentWrappedFunctionNames: true +# 保留在块开始处的空行 +KeepEmptyLinesAtTheStartOfBlocks: true +# 开始一个块的宏的正则表达式 +MacroBlockBegin: "" +# 结束一个块的宏的正则表达式 +MacroBlockEnd: "" +# 连续空行的最大数量 +MaxEmptyLinesToKeep: 1 +# 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All +NamespaceIndentation: None +# 使用ObjC块时缩进宽度 +ObjCBlockIndentWidth: 4 +# 在ObjC的@property后添加一个空格 +ObjCSpaceAfterProperty: false +# 在ObjC的protocol列表前添加一个空格 +ObjCSpaceBeforeProtocolList: true +# 在call(后对函数调用换行的penalty +PenaltyBreakBeforeFirstCallParameter: 19 +# 在一个注释中引入换行的penalty +PenaltyBreakComment: 300 +# 第一次在<<前换行的penalty +PenaltyBreakFirstLessLess: 120 +# 在一个字符串字面量中引入换行的penalty +PenaltyBreakString: 1000 +# 对于每个在行字符数限制之外的字符的penalty +PenaltyExcessCharacter: 1000000 +# 将函数的返回类型放到它自己的行的penalty +PenaltyReturnTypeOnItsOwnLine: 60 +# 指针和引用的对齐: Left, Right, Middle +PointerAlignment: Right +# 允许重新排版注释 +ReflowComments: true +# 允许排序#include +SortIncludes: true +# 在C风格类型转换后添加空格 +SpaceAfterCStyleCast: false +# 在赋值运算符之前添加空格 +SpaceBeforeAssignmentOperators: true +# 开圆括号之前添加一个空格: Never, ControlStatements, Always +SpaceBeforeParens: ControlStatements +# 在空的圆括号中添加空格 +SpaceInEmptyParentheses: false +# 在尾随的评论前添加的空格数(只适用于//) +SpacesBeforeTrailingComments: 1 +# 在尖括号的<后和>前添加空格 +SpacesInAngles: false +# 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格 +SpacesInContainerLiterals: true +# 在C风格类型转换的括号中添加空格 +SpacesInCStyleCastParentheses: false +# 在圆括号的(后和)前添加空格 +SpacesInParentheses: false +# 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响 +SpacesInSquareBrackets: false +# 标准: Cpp03, Cpp11, Auto +Standard: Cpp11 +# tab宽度 +TabWidth: 4 +# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always +UseTab: Never \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..4947287f7 --- /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/distributedfile.gni b/distributedfile.gni new file mode 100755 index 000000000..60ee10292 --- /dev/null +++ b/distributedfile.gni @@ -0,0 +1,18 @@ +# 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. + +distributedfile_path = "//foundation/storage/distributed_file_manager" + +utils_path = "${distributedfile_path}/utils" +services_path = "${distributedfile_path}/services" +innerkits_native_path = "${distributedfile_path}/interfaces/innerkits/native" \ No newline at end of file diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn new file mode 100644 index 000000000..942bde711 --- /dev/null +++ b/frameworks/native/BUILD.gn @@ -0,0 +1,24 @@ +# 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") +import("//foundation/storage/distributed_file_manager/distributedfile.gni") + +ohos_shared_library("libdistributedfile_innerkits") { + include_dirs = [ "${innerkits_native_path}" ] + + sources = [ + ] + + part_name = "storage_distributed_file_manager" + subsystem_name = "storage" +} \ No newline at end of file diff --git a/interfaces/innerkits/native/deamon_proxy.h b/interfaces/innerkits/native/deamon_proxy.h new file mode 100644 index 000000000..953b5bf48 --- /dev/null +++ b/interfaces/innerkits/native/deamon_proxy.h @@ -0,0 +1,33 @@ +/* + * 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 "i_deamon.h" + +namespace OHOS { +namespace DistributedFile { +class DeamonProxy : public IRemoteProxy { +public: + explicit DeamonProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~DeamonProxy() {} + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/ohos.build b/ohos.build new file mode 100755 index 000000000..d9d007fb2 --- /dev/null +++ b/ohos.build @@ -0,0 +1,17 @@ +{ + "subsystem": "storage", + "parts": { + "storage_distributed_file_manager": { + "variants": [ + "wearable", + "phone" + ], + "module_list": [ + "//foundation/storage/distributed_file_manager/services/:services_target" + ], + "test_list": [ + "//foundation/storage/distributed_file_manager/services/distributedfiledeamon/test/unittest:unittest" + ] + } + } +} diff --git a/services/4901.xml b/services/4901.xml new file mode 100755 index 000000000..b499a9843 --- /dev/null +++ b/services/4901.xml @@ -0,0 +1,30 @@ + + + + distributedfiledeamon + + libdistributedfiledeamon.z.so + + + 4901 + libdistributedfiledeamon.z.so + + + true + false + 1 + + diff --git a/services/BUILD.gn b/services/BUILD.gn new file mode 100755 index 000000000..6c6dc669f --- /dev/null +++ b/services/BUILD.gn @@ -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. +import("//build/ohos.gni") +import("//build/ohos/sa_profile/sa_profile.gni") +import("//foundation/storage/distributed_file_manager/distributedfile.gni") + +ohos_sa_profile("distributedfile_sa_profile") { + sources = [ "4901.xml" ] + part_name = "storage_distributed_file_manager" +} + +ohos_prebuilt_etc("distributedfile_etc") { + source = "distributedfile.cfg" + relative_install_dir = "init" + part_name = "storage_distributed_file_manager" + subsystem_name = "storage" +} + +group("services_target") { + deps = [ + ":distributedfile_etc", + ":distributedfile_sa_profile", + "distributedfiledeamon:distributedfiledeamon_target", + ] +} diff --git a/services/distributedfile.cfg b/services/distributedfile.cfg new file mode 100644 index 000000000..7eef28530 --- /dev/null +++ b/services/distributedfile.cfg @@ -0,0 +1,16 @@ +{ + "jobs": [{ + "name": "boot", + "cmds": [ + "start distributedfiledeamon" + ] + }], + # 这里应引入对外部服务的依赖 + "services": [{ + "name": "distributedfiledeamon", + "path": ["/system/bin/sa_main", "/system/profile/distributedfiledeamon.xml"], + "uid": "root", + "gid": ["root", "system", "shell", "readproc", "media_rw", "inet"], + "caps": ["SYS_PTRACE", "KILL"] + }] +} \ No newline at end of file diff --git a/services/distributedfiledeamon/BUILD.gn b/services/distributedfiledeamon/BUILD.gn new file mode 100755 index 000000000..27f659ae1 --- /dev/null +++ b/services/distributedfiledeamon/BUILD.gn @@ -0,0 +1,54 @@ +# 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") +import("//foundation/storage/distributed_file_manager/distributedfile.gni") + +ohos_shared_library("libdistributedfiledeamon") { + include_dirs = [ + "include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + ] + + sources = [ + "src/ipc/deamon.cpp", + "src/ipc/deamon_stub.cpp", + "src/mountpoint/mount_manager.cpp", + "src/mountpoint/mount_point.cpp", + "src/device/device_info.cpp", + "src/device/device_manager_agent.cpp", + "src/network/network_agent_template.cpp", + "src/network/softbus/softbus_agent.cpp", + "src/network/softbus/softbus_session_dispatcher.cpp", + "src/network/softbus/softbus_session.cpp", + "src/network/kernel_talker.cpp", + "src/network/session_pool.cpp" + ] + + deps = [ + "${utils_path}:libdistributedfileutils", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + ] + external_deps = [ + "dsoftbus_standard:softbus_client", + "ipc:ipc_core", + "safwk:system_ability_fwk", + ] + configs = [ "${utils_path}:compiler_configs" ] + + part_name = "storage_distributed_file_manager" + subsystem_name = "storage" +} + +group("distributedfiledeamon_target") { + deps = [ ":libdistributedfiledeamon" ] +} diff --git a/services/distributedfiledeamon/include/ipc/deamon.h b/services/distributedfiledeamon/include/ipc/deamon.h new file mode 100644 index 000000000..abdd3a2a4 --- /dev/null +++ b/services/distributedfiledeamon/include/ipc/deamon.h @@ -0,0 +1,57 @@ +/* + * 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 "deamon_stub.h" + +#include + +#include "i_deamon.h" +#include "iremote_stub.h" +#include "system_ability.h" + +namespace OHOS { +namespace DistributedFile { +enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; + +class Deamon : public SystemAbility, public DeamonStub, protected NoCopyable { + DECLARE_SYSTEM_ABILITY(Deamon); + +public: + explicit Deamon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate){}; + ~Deamon() = default; + + void OnStart() override; + void OnStop() override; + ServiceRunningState QueryServiceState() const + { + return state_; + } + + int32_t EchoServerDemo(const std::string &echoStr) override; + +private: + Deamon(); + ServiceRunningState state_{ServiceRunningState::STATE_NOT_START}; + static sptr instance_; + static std::mutex instanceLock_; + bool registerToService_{false}; + + void PublishSA(); + void StartManagers(); +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/ipc/deamon_stub.h b/services/distributedfiledeamon/include/ipc/deamon_stub.h new file mode 100644 index 000000000..f408e9410 --- /dev/null +++ b/services/distributedfiledeamon/include/ipc/deamon_stub.h @@ -0,0 +1,37 @@ +/* + * 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 "i_deamon.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace DistributedFile { +class DeamonStub : public IRemoteStub { +public: + DeamonStub(); + ~DeamonStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + using DeamonInterface = int32_t (DeamonStub::*)(MessageParcel &data, MessageParcel &reply); + std::map opToInterfaceMap_; + + int32_t EchoServerDemoInner(MessageParcel &data, MessageParcel &reply); +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/ipc/i_deamon.h b/services/distributedfiledeamon/include/ipc/i_deamon.h new file mode 100644 index 000000000..f4d62aef6 --- /dev/null +++ b/services/distributedfiledeamon/include/ipc/i_deamon.h @@ -0,0 +1,33 @@ +/* + * 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 "iremote_broker.h" + +namespace OHOS { +namespace DistributedFile { +class IDeamon : public IRemoteBroker { +public: + enum { + DFS_DEAMON_CMD_ECHO = 0, + }; + virtual int32_t EchoServerDemo(const std::string &echoStr) = 0; + + static inline const std::string SERVICE_NAME{"ohos.storage.distributedfile.deamon"}; + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.storage.distributedfile.deamon") +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/mountpoint/mount_manager.h b/services/distributedfiledeamon/include/mountpoint/mount_manager.h new file mode 100644 index 000000000..97db5fcc9 --- /dev/null +++ b/services/distributedfiledeamon/include/mountpoint/mount_manager.h @@ -0,0 +1,42 @@ +/* + * 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 + +#include "mount_point.h" +#include "utils_singleton.h" + +namespace OHOS { +namespace DistributedFile { +class MountManager final : public Utils::Singleton { +public: + void Mount(std::unique_ptr mp); + void Umount(std::weak_ptr wmp); + + DECLARE_SINGLETON(MountManager); + +private: + void Start() override {} + void Stop() override {} + + std::mutex serializer_; + std::vector> mountPoints_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/mountpoint/mount_point.h b/services/distributedfiledeamon/include/mountpoint/mount_point.h new file mode 100644 index 000000000..d813b04b9 --- /dev/null +++ b/services/distributedfiledeamon/include/mountpoint/mount_point.h @@ -0,0 +1,49 @@ +/* + * 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 "nocopyable.h" +#include "utils_mount_argument.h" + +namespace OHOS { +namespace DistributedFile { +class MountPoint final : public NoCopyable { +public: + MountPoint(const Utils::MountArgument &mountArg); + ~MountPoint() = default; + + uint32_t GetID() const + { + return id_; + }; + + std::string ToString() const; + Utils::MountArgument GetMountArgument() const; + bool operator==(const MountPoint &rop) const; + +private: + friend class MountManager; + Utils::MountArgument mountArg_; + void Mount() const; + void Umount() const; + static std::atomic idGen_; + uint32_t id_{0}; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/ipc/deamon.cpp b/services/distributedfiledeamon/src/ipc/deamon.cpp new file mode 100644 index 000000000..1d97ae5d1 --- /dev/null +++ b/services/distributedfiledeamon/src/ipc/deamon.cpp @@ -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. + */ + +#include "ipc/deamon.h" + +#include "mountpoint/mount_manager.h" +#include "system_ability_definition.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +using namespace std; + +REGISTER_SYSTEM_ABILITY_BY_ID(Deamon, STORAGE_DISTRIBUTED_FILE_DEAMON_SA_ID, true); + +void Deamon::PublishSA() +{ + LOGI("Begin to init"); + if (!registerToService_) { + bool ret = SystemAbility::Publish(this); + if (!ret) { + throw runtime_error("Failed to publish the deamon"); + } + registerToService_ = true; + } + LOGI("Init finished successfully"); +} + +void Deamon::StartManagers() +{ + MountManager::GetInstance(); +} + +void Deamon::OnStart() +{ + LOGI("Begin to start service"); + if (state_ == ServiceRunningState::STATE_RUNNING) { + LOGD("Deamon has already started"); + return; + } + + try { + PublishSA(); + StartManagers(); + } catch (const std::exception &e) { + LOGE("%{public}s", e.what()); + } + + state_ = ServiceRunningState::STATE_RUNNING; + LOGI("Start service successfully"); +} + +void Deamon::OnStop() +{ + LOGI("Begin to stop"); + state_ = ServiceRunningState::STATE_NOT_START; + registerToService_ = false; + LOGI("Stop finished successfully"); +} + +int32_t Deamon::EchoServerDemo(const std::string &echoStr) +{ + return 0; +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/ipc/deamon_stub.cpp b/services/distributedfiledeamon/src/ipc/deamon_stub.cpp new file mode 100644 index 000000000..3698ec3d9 --- /dev/null +++ b/services/distributedfiledeamon/src/ipc/deamon_stub.cpp @@ -0,0 +1,42 @@ +/* + * 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 "ipc/deamon_stub.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +DeamonStub::DeamonStub() +{ + opToInterfaceMap_[DFS_DEAMON_CMD_ECHO] = &DeamonStub::EchoServerDemoInner; +} + +int32_t DeamonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + 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); + } + return (this->*(interfaceIndex->second))(data, reply); +} + +int32_t DeamonStub::EchoServerDemoInner(MessageParcel &data, MessageParcel &reply) +{ + LOGI("hello, world"); + return ERR_NONE; +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp b/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp new file mode 100644 index 000000000..afcd41e85 --- /dev/null +++ b/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp @@ -0,0 +1,82 @@ +/* + * 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 "mountpoint/mount_manager.h" + +#include +#include + +#include "device/device_manager_agent.h" +#include "utils_log.h" +#include "utils_mount_argument.h" + +namespace OHOS { +namespace DistributedFile { +using namespace std; + +MountManager::MountManager() +{ + Mount(make_unique(Utils::MountArgumentDescriptors::Alpha())); +} + +MountManager::~MountManager() +{ + try { + // Umount mountpoints in reverse order to eliminate dependencies + for_each(mountPoints_.rbegin(), mountPoints_.rend(), [this](auto &cur_mp) { Umount(cur_mp); }); + } catch (const std::exception &e) { + LOGE("%{public}s", e.what()); + } +} + +void MountManager::Mount(unique_ptr mp) +{ + lock_guard lock(serializer_); + + shared_ptr smp = move(mp); + + auto isExisted = [smp](const auto &cur_mp) { return *smp == *cur_mp; }; + if (!smp || any_of(mountPoints_.begin(), mountPoints_.end(), isExisted)) { + stringstream ss; + ss << "Mount an empty/existent mountpoint" << (smp ? smp->GetMountArgument().GetFullDst() : ""); + LOGE("%{public}s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + + smp->Mount(); + DeviceManagerAgent::GetInstance().JoinGroup(smp); + mountPoints_.push_back(smp); +} + +void MountManager::Umount(weak_ptr wmp) +{ + auto smp = wmp.lock(); + lock_guard lock(serializer_); + + decltype(mountPoints_.begin()) it; + auto isExisted = [smp](const auto &cur_mp) { return *smp == *cur_mp; }; + if (!smp || ((it = find_if(mountPoints_.begin(), mountPoints_.end(), isExisted)) == mountPoints_.end())) { + stringstream ss; + ss << "Umount an empty/non-existent mountpoint" << (smp ? smp->GetMountArgument().GetFullDst() : ""); + LOGE("%{public}s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + + smp->Umount(); + DeviceManagerAgent::GetInstance().QuitGroup(smp); + mountPoints_.erase(it); +} +} // namespace DistributedFile +} // namespace OHOS diff --git a/services/distributedfiledeamon/src/mountpoint/mount_point.cpp b/services/distributedfiledeamon/src/mountpoint/mount_point.cpp new file mode 100644 index 000000000..89a30fefc --- /dev/null +++ b/services/distributedfiledeamon/src/mountpoint/mount_point.cpp @@ -0,0 +1,85 @@ +/* + * 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 "mountpoint/mount_point.h" + +#include +#include + +#include "utils_directory.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +using namespace std; + +std::atomic MountPoint::idGen_; + +MountPoint::MountPoint(const Utils::MountArgument &mountArg) : mountArg_(mountArg) +{ + id_ = idGen_++; +} + +void MountPoint::Mount() const +{ + auto mntArg = GetMountArgument(); + + string src = mntArg.GetFullSrc(); + Utils::ForceCreateDirectory(src, S_IRWXU | S_IRWXG | S_IXOTH, Utils::UID_SYSTEM, Utils::UID_MEDIA_RW); + + string dst = mntArg.GetFullDst(); + Utils::ForceCreateDirectory(dst, S_IRWXU | S_IRWXG | S_IXOTH); + + string cache = mntArg.GetCachePath(); + Utils::ForceCreateDirectory(cache, S_IRWXU | S_IRWXG | S_IXOTH); + + unsigned long flags = mntArg.GetFlags(); + string options = mntArg.OptionsToString(); + int ret = mount(src.c_str(), dst.c_str(), "hmdfs", flags, options.c_str()); + if (ret == -1 && errno != EEXIST && errno != EBUSY) { + auto cond = system_category().default_error_condition(errno); + LOGE("Failed to mount: %{public}d %{public}s", cond.value(), cond.message().c_str()); + throw system_error(errno, system_category()); + } +} + +void MountPoint::Umount() const +{ + string dst = GetMountArgument().GetFullDst(); + if (umount2(dst.c_str(), MNT_DETACH) == -1) { + auto cond = system_category().default_error_condition(errno); + LOGE("Failed to umount: %{public}d %{public}s", cond.value(), cond.message().c_str()); + throw system_error(errno, system_category()); + } + + Utils::ForceRemoveDirectory(dst); +} + +bool MountPoint::operator==(const MountPoint &rop) const +{ + return mountArg_.GetFullDst() == rop.mountArg_.GetFullDst(); +} + +Utils::MountArgument MountPoint::GetMountArgument() const +{ + return mountArg_; +} + +std::string MountPoint::ToString() const +{ + return ""; +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/BUILD.gn b/utils/BUILD.gn new file mode 100755 index 000000000..c25662f4b --- /dev/null +++ b/utils/BUILD.gn @@ -0,0 +1,70 @@ +# 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") +import("//foundation/storage/distributed_file_manager/distributedfile.gni") + +config("compiler_configs") { + visibility = [ ":*" ] + + cflags = [ + "-Wall", + "-Werror", + "-Wdate-time", + "-Wfloat-equal", + "-Wshadow", + "-Wformat=2", + "-fvisibility=hidden", + "-ffunction-sections", + "-Os", + ] + configs = [ "//build/config/compiler:exceptions" ] + + cflags_cc = [ + "-fvisibility-inlines-hidden", + "-Os", + ] + + defines = [ + "LOG_TAG=\"distributedfile\"", + "LOG_DOMAIN=0xD001600", + "LOG_LEVEL=INFO", + ] +} + +config("utils_public_config") { + include_dirs = [ + "log/include", + "system/include", + "//utils/native/base/include", + ] +} + +ohos_shared_library("libdistributedfileutils") { + sources = [ + "log/src/utils_log.cpp", + "system/src/utils_directory.cpp", + "system/src/utils_mount_argument.cpp", + ] + + configs = [ "//build/config/compiler:exceptions" ] + public_configs = [ ":utils_public_config" ] + + public_deps = [ + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//utils/native/base:utils", + ] + + part_name = "storage_distributed_file_manager" + subsystem_name = "storage" +} diff --git a/utils/log/include/utils_log.h b/utils/log/include/utils_log.h new file mode 100755 index 000000000..a744c3bcf --- /dev/null +++ b/utils/log/include/utils_log.h @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2021 Huawei Device Co., Ltd. +d * 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 "hilog/log.h" + +namespace OHOS { +namespace DistributedFile { +#ifndef LOG_DOMAIN +#define LOG_DOMAIN 0xD001600 +#endif + +#ifndef LOG_TAG +#define LOG_TAG "distributedfile" +#endif + +static constexpr HiviewDFX::HiLogLabel APP_LABEL = {LOG_CORE, LOG_DOMAIN, LOG_TAG}; + +std::string GetFileNameFromFullPath(const char *str); +#define PRINT_LOG(Level, fmt, ...) \ + HiviewDFX::HiLog::Level(APP_LABEL, "[%{public}s:%{public}d->%{public}s] " fmt, \ + GetFileNameFromFullPath(__FILE__).c_str(), __LINE__, __FUNCTION__, ##__VA_ARGS__) + +#define LOGD(fmt, ...) PRINT_LOG(Debug, fmt, ##__VA_ARGS__) +#define LOGI(fmt, ...) PRINT_LOG(Info, fmt, ##__VA_ARGS__) +#define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) +#define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) +#define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/log/src/utils_log.cpp b/utils/log/src/utils_log.cpp new file mode 100644 index 000000000..2a375472e --- /dev/null +++ b/utils/log/src/utils_log.cpp @@ -0,0 +1,27 @@ +/* + * 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 "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +std::string GetFileNameFromFullPath(const char *str) +{ + std::string fullPath(str); + size_t pos = fullPath.find_last_of("/"); + return (pos == std::string::npos) ? std::string() : fullPath.substr(pos + 1); +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/include/utils_directory.h b/utils/system/include/utils_directory.h new file mode 100644 index 000000000..6f63c5312 --- /dev/null +++ b/utils/system/include/utils_directory.h @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2021 Huawei Device Co., Ltd. +d * 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 +#include + +#include "nocopyable.h" + +namespace OHOS { +namespace DistributedFile { +namespace Utils { +enum Uid { + UID_ROOT = 0, + UID_SYSTEM = 1000, + UID_MEDIA_RW = 1023, +}; + +void ForceCreateDirectory(const std::string &path); +void ForceCreateDirectory(const std::string &path, mode_t mode); +void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t gid); + +void ForceRemoveDirectory(const std::string &path); +} // namespace Utils +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/include/utils_mount_argument.h b/utils/system/include/utils_mount_argument.h new file mode 100644 index 000000000..16e1745b5 --- /dev/null +++ b/utils/system/include/utils_mount_argument.h @@ -0,0 +1,49 @@ +/* + * 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 DistributedFile { +namespace Utils { +struct MountArgument final { + int userId_{0}; + + std::string account_; + bool needInitDir_{false}; + bool useCache_{false}; + bool caseSensitive_{true}; + bool enableMergeView_{false}; + bool enableFixupOwnerShip_{false}; + bool enableOfflineStash_{true}; + bool externalFS_{false}; + + std::string GetFullSrc() const; + std::string GetFullDst() const; + std::string GetCtrlPath() const; + std::string GetCachePath() const; + std::string OptionsToString() const; + unsigned long GetFlags() const; +}; + +class MountArgumentDescriptors final { +public: + static MountArgument Alpha(int userId = 0); +}; +} // namespace Utils +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/include/utils_uid.h b/utils/system/include/utils_uid.h new file mode 100644 index 000000000..e69de29bb diff --git a/utils/system/src/utils_directory.cpp b/utils/system/src/utils_directory.cpp new file mode 100644 index 000000000..9a1c87dc8 --- /dev/null +++ b/utils/system/src/utils_directory.cpp @@ -0,0 +1,81 @@ +/* +* Copyright (c) 2021 Huawei Device Co., Ltd. +d * 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 "utils_directory.h" + +#include +#include +#include +#include + +#include "directory_ex.h" + +namespace OHOS { +namespace DistributedFile { +namespace Utils { +using namespace std; + +void ForceCreateDirectory(const string &path, function onSubDirCreated) +{ + string::size_type index = 0; + do { + string subPath; + index = path.find('/', index + 1); + if (index == string::npos) { + subPath = path; + } else { + subPath = path.substr(0, index); + } + + if (access(subPath.c_str(), F_OK) != 0) { + if (mkdir(subPath.c_str(), (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != 0 && errno != EEXIST) { + throw system_error(errno, system_category()); + } + onSubDirCreated(subPath); + } + } while (index != string::npos); +} + +void ForceCreateDirectory(const std::string &path) +{ + ForceCreateDirectory(path, nullptr); +} + +void ForceCreateDirectory(const std::string &path, mode_t mode) +{ + ForceCreateDirectory(path, [mode](const string &subPath) { + if (chmod(subPath.c_str(), mode) == -1) { + throw system_error(errno, system_category()); + } + }); +} + +void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t gid) +{ + ForceCreateDirectory(path, [mode, uid, gid](const string &subPath) { + if (chmod(subPath.c_str(), mode) == -1 || chown(subPath.c_str(), uid, gid) == -1) { + throw system_error(errno, system_category()); + } + }); +} + +void ForceRemoveDirectory(const std::string &path) +{ + if (!OHOS::ForceRemoveDirectory(path)) { + throw system_error(errno, system_category()); + } +} +} // namespace Utils +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/src/utils_mount_argument.cpp b/utils/system/src/utils_mount_argument.cpp new file mode 100644 index 000000000..7aa3dee71 --- /dev/null +++ b/utils/system/src/utils_mount_argument.cpp @@ -0,0 +1,112 @@ +/* +* Copyright (c) 2021 Huawei Device Co., Ltd. +d * 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 "utils_mount_argument.h" + +#include +#include + +namespace OHOS { +namespace DistributedFile { +namespace Utils { +using namespace std; + +string MountArgument::GetFullSrc() const +{ + stringstream ss; + ss << "/data/misc_ce/" << userId_ << "/hmdfs/storage"; + return ss.str(); +} + +string MountArgument::GetFullDst() const +{ + stringstream ss; + ss << "/mnt/hmdfs/" << userId_ << "/"; + return ss.str(); +} + +string MountArgument::GetCachePath() const +{ + stringstream ss; + ss << "/data/misc_ce/" << userId_ << "/hmdfs/cache/"; + return ss.str(); +} + +static uint64_t MocklispHash(const string &str) +{ + uint64_t res = 0; + constexpr int mocklispHashPos = 5; + /* Mocklisp hash function. */ + for (auto ch : str) { + res = (res << mocklispHashPos) - res + (uint64_t)ch; + } + return res; +} + +string MountArgument::GetCtrlPath() const +{ + auto dst = GetFullDst(); + auto res = MocklispHash(dst); + stringstream ss; + ss << "/sys/fs/hmdfs/" << res << "/cmd"; + return ss.str(); +} + +string MountArgument::OptionsToString() const +{ + stringstream ss; + ss << "local_dst=" << GetFullDst(); + if (useCache_) { + ss << ",cache_dir=" << GetCachePath(); + } + if (caseSensitive_) { + ss << ",sensitive"; + } + if (enableMergeView_) { + ss << ",merge"; + } + if (enableFixupOwnerShip_) { + ss << ",fixupownership"; + } + if (!enableOfflineStash_) { + ss << ",no_offline_stash"; + } + if (externalFS_) { + ss << ",external_fs"; + } + return ss.str(); +} + +unsigned long MountArgument::GetFlags() const +{ + return 0; +} + +MountArgument MountArgumentDescriptors::Alpha(int userId) +{ + MountArgument mountArgument = { + .account_ = "default", + .needInitDir_ = true, + .useCache_ = true, + .enableMergeView_ = true, + .enableFixupOwnerShip_ = true, + .enableOfflineStash_ = true, + .externalFS_ = false, + }; + mountArgument.userId_ = userId; + return mountArgument; +} +} // namespace Utils +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/src/utils_uid.cpp b/utils/system/src/utils_uid.cpp new file mode 100644 index 000000000..e69de29bb -- Gitee From a12ba19e34b07e41ac3494a7b1be43b7ba7269c1 Mon Sep 17 00:00:00 2001 From: c00382521 Date: Thu, 28 Oct 2021 11:55:32 +0800 Subject: [PATCH 2/5] upload the device/network manager Signed-off-by: c00382521 --- .../include/device/device_info.h | 49 ++++ .../include/device/device_manager_agent.h | 64 +++++ .../include/network/base_session.h | 35 +++ .../include/network/kernel_talker.h | 98 +++++++ .../include/network/network_agent_template.h | 69 +++++ .../include/network/session_pool.h | 43 +++ .../include/network/softbus/softbus_agent.h | 42 +++ .../include/network/softbus/softbus_session.h | 37 +++ .../softbus/softbus_session_dispatcher.h | 41 +++ .../network/softbus/softbus_session_name.h | 36 +++ .../src/device/device_info.cpp | 77 ++++++ .../src/device/device_manager_agent.cpp | 213 +++++++++++++++ .../src/network/kernel_talker.cpp | 257 ++++++++++++++++++ .../src/network/network_agent_template.cpp | 110 ++++++++ .../src/network/session_pool.cpp | 47 ++++ .../src/network/softbus/softbus_agent.cpp | 137 ++++++++++ .../src/network/softbus/softbus_session.cpp | 82 ++++++ .../softbus/softbus_session_dispatcher.cpp | 102 +++++++ .../test/unittest/BUILD.gn | 47 ++++ .../device/device_manager_agent_test.cpp | 61 +++++ utils/system/include/utils_dfs_thread.h | 135 +++++++++ utils/system/include/utils_singleton.h | 75 +++++ 22 files changed, 1857 insertions(+) create mode 100644 services/distributedfiledeamon/include/device/device_info.h create mode 100644 services/distributedfiledeamon/include/device/device_manager_agent.h create mode 100644 services/distributedfiledeamon/include/network/base_session.h create mode 100644 services/distributedfiledeamon/include/network/kernel_talker.h create mode 100644 services/distributedfiledeamon/include/network/network_agent_template.h create mode 100644 services/distributedfiledeamon/include/network/session_pool.h create mode 100644 services/distributedfiledeamon/include/network/softbus/softbus_agent.h create mode 100644 services/distributedfiledeamon/include/network/softbus/softbus_session.h create mode 100644 services/distributedfiledeamon/include/network/softbus/softbus_session_dispatcher.h create mode 100644 services/distributedfiledeamon/include/network/softbus/softbus_session_name.h create mode 100644 services/distributedfiledeamon/src/device/device_info.cpp create mode 100644 services/distributedfiledeamon/src/device/device_manager_agent.cpp create mode 100644 services/distributedfiledeamon/src/network/kernel_talker.cpp create mode 100644 services/distributedfiledeamon/src/network/network_agent_template.cpp create mode 100644 services/distributedfiledeamon/src/network/session_pool.cpp create mode 100644 services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp create mode 100644 services/distributedfiledeamon/src/network/softbus/softbus_session.cpp create mode 100644 services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp create mode 100644 services/distributedfiledeamon/test/unittest/BUILD.gn create mode 100644 services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp create mode 100644 utils/system/include/utils_dfs_thread.h create mode 100644 utils/system/include/utils_singleton.h diff --git a/services/distributedfiledeamon/include/device/device_info.h b/services/distributedfiledeamon/include/device/device_info.h new file mode 100644 index 000000000..7714406a0 --- /dev/null +++ b/services/distributedfiledeamon/include/device/device_info.h @@ -0,0 +1,49 @@ +/* + * 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 "dm_device_info.h" + +namespace OHOS { +namespace DistributedFile { +class DeviceInfo final { +public: + DeviceInfo() = default; + ~DeviceInfo() = default; + explicit DeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo); + explicit DeviceInfo(DeviceInfo &&nodeInfo); + DeviceInfo &operator=(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo); + + /** + * @note Remove the concept iid later + */ + void SetIid(uint64_t iid); + void SetCid(const std::string cid); + + uint64_t GetIid() const; + const std::string &GetCid() const; + +private: + std::atomic initIidFlag_{false}; + std::atomic initCidFlag_{false}; + + uint64_t iid_{0}; + std::string cid_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/device/device_manager_agent.h b/services/distributedfiledeamon/include/device/device_manager_agent.h new file mode 100644 index 000000000..4337512af --- /dev/null +++ b/services/distributedfiledeamon/include/device/device_manager_agent.h @@ -0,0 +1,64 @@ +/* + * 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 +#include + +#include "device_info.h" +#include "device_manager.h" +#include "mountpoint/mount_point.h" +#include "network/network_agent_template.h" +#include "utils_singleton.h" + +namespace OHOS { +namespace DistributedFile { +class DeviceManagerAgent final : public OHOS::DistributedHardware::DmInitCallback, + public OHOS::DistributedHardware::DeviceStateCallback, + public std::enable_shared_from_this, + public Utils::Singleton { + DECLARE_SINGLETON(DeviceManagerAgent); + +public: + void JoinGroup(std::weak_ptr mp); + void QuitGroup(std::weak_ptr mp); + + void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override {} + + void OnRemoteDied() override; + + DeviceInfo &GetLocalDeviceInfo(); + std::vector GetRemoteDevicesInfo(); + +private: + void InitLocalIid(); + void InitLocalNodeInfo(); + void RegisterToExternalDm(); + void UnregisterFromExternalDm(); + void Start() override; + void Stop() override; + + // We use a mutex instead of a shared_mutex to serialize online/offline procedures + std::mutex mpToNetworksMutex_; + std::map> mpToNetworks_; + DeviceInfo localDeviceInfo_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/base_session.h b/services/distributedfiledeamon/include/network/base_session.h new file mode 100644 index 000000000..d12aed2d3 --- /dev/null +++ b/services/distributedfiledeamon/include/network/base_session.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 + +namespace OHOS { +namespace DistributedFile { +constexpr int KEY_SIZE_MAX = 32; + +class BaseSession { +public: + virtual ~BaseSession() = default; + virtual bool IsFromServer() const = 0; + virtual std::string GetCid() const = 0; + virtual int32_t GetHandle() const = 0; + virtual std::array GetKey() const = 0; + virtual void Release() const = 0; + virtual void DisableSessionListener() const = 0; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/kernel_talker.h b/services/distributedfiledeamon/include/network/kernel_talker.h new file mode 100644 index 000000000..de1eadf3a --- /dev/null +++ b/services/distributedfiledeamon/include/network/kernel_talker.h @@ -0,0 +1,98 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + +#include "mountpoint/mount_point.h" +#include "network/base_session.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +constexpr int CID_MAX_LEN = 64; +struct NotifyParam { + int32_t notify; + int32_t fd; + uint64_t remoteIid; + uint16_t udpPort; + uint8_t deviceType; + int32_t flag; + int32_t reserved; + char remoteCid[CID_MAX_LEN]; +} __attribute__((packed)); + +class KernelTalker final : protected NoCopyable { +public: + explicit KernelTalker(std::weak_ptr mountPoint, std::function callback) + : mountPoint_(mountPoint), GetSessionCallback_(callback) + { + } + KernelTalker() = default; + ~KernelTalker() = default; + + void SinkSessionTokernel(std::shared_ptr session); + void SinkInitCmdToKernel(uint64_t iid); + void SinkOfflineCmdToKernel(std::string cid); + std::unordered_set GetKernelSesions(); + + void CreatePollThread(); + void WaitForPollThreadExited(); + +private: + template + void SetCmd(T &cmd) + { + auto spt = mountPoint_.lock(); + if (spt == nullptr) { + LOGE("mountPoint is not exist! bad weak_ptr"); + return; // ! 抛异常 + } + std::string ctrlPath = spt->GetMountArgument().GetCtrlPath(); + LOGI("cmd path:%{public}s", ctrlPath.c_str()); + std::lock_guard lock(cmdMutex_); + int file = open(ctrlPath.c_str(), O_RDWR); + if (file < 0) { + LOGE("Open node file error. %{public}d", file); + return; + } + int err = write(file, &cmd, sizeof(T)); + if (err < 0) { + LOGE("write return err. %{public}d", err); + } + close(file); + return; + } + + void PollRun(); + void HandleAllNotify(int fd); + void NotifyHandler(NotifyParam ¶m); + + std::weak_ptr mountPoint_; + std::mutex cmdMutex_; + std::atomic isRunning_{true}; + std::unique_ptr pollThread_{nullptr}; + std::function GetSessionCallback_{nullptr}; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/network_agent_template.h b/services/distributedfiledeamon/include/network/network_agent_template.h new file mode 100644 index 000000000..a37d25428 --- /dev/null +++ b/services/distributedfiledeamon/include/network/network_agent_template.h @@ -0,0 +1,69 @@ +/* + * 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 +#include + +#include "utils_dfs_thread.h" +#include "device/device_info.h" +#include "mountpoint/mount_point.h" +#include "network/kernel_talker.h" +#include "network/session_pool.h" + +namespace OHOS { +namespace DistributedFile { +class NetworkAgentTemplate { +public: + explicit NetworkAgentTemplate(std::weak_ptr mountPoint) + : mountPoint_(mountPoint), + kernerlTalker_(mountPoint, [&](NotifyParam ¶m) { GetSessionProcess(param); }), + sessionPool_(kernerlTalker_) + { + } + virtual ~NetworkAgentTemplate(); + void Start(); + void Stop(); + void ConnectOnlineDevices(); + void ConnectDeviceAsync(const DeviceInfo &info); + void DisconnectDevice(const DeviceInfo &info); + void AcceptSession(std::shared_ptr session); + +protected: + virtual void JoinDomain() = 0; + virtual void QuitDomain() = 0; + virtual void StopTopHalf() = 0; + virtual void StopBottomHalf() = 0; + virtual std::shared_ptr OpenSession(const DeviceInfo &info) = 0; + virtual void CloseSession(std::shared_ptr session) = 0; + + std::weak_ptr mountPoint_; + +private: + void HandleAllNotify(int fd); + void NotifyHandler(NotifyParam ¶m); + void GetSessionProcess(NotifyParam ¶m); + void GetSesion(const std::string &cid); + + std::mutex taskMut_; + std::list tasks_; + KernelTalker kernerlTalker_; + SessionPool sessionPool_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/session_pool.h b/services/distributedfiledeamon/include/network/session_pool.h new file mode 100644 index 000000000..f1a65db3a --- /dev/null +++ b/services/distributedfiledeamon/include/network/session_pool.h @@ -0,0 +1,43 @@ +/* + * 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 +#include + +#include "network/base_session.h" +#include "network/kernel_talker.h" + +namespace OHOS { +namespace DistributedFile { +class SessionPool final : protected NoCopyable { +public: + explicit SessionPool(KernelTalker &talker) : talker_(talker) {} + ~SessionPool() = default; + void HoldSession(std::shared_ptr session); + void RefreshSessionPoolBasedOnKernel(); + +private: + std::recursive_mutex sessionPoolLock_; + std::list> usrSpaceSessionPool_; + KernelTalker &talker_; + + void AddSessionToPool(std::shared_ptr session); +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_agent.h b/services/distributedfiledeamon/include/network/softbus/softbus_agent.h new file mode 100644 index 000000000..392c40113 --- /dev/null +++ b/services/distributedfiledeamon/include/network/softbus/softbus_agent.h @@ -0,0 +1,42 @@ +/* + * 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 "network/network_agent_template.h" +#include + +namespace OHOS { +namespace DistributedFile { +class SoftbusAgent final : public NetworkAgentTemplate, public std::enable_shared_from_this { +public: + explicit SoftbusAgent(std::weak_ptr mountPoint); + ~SoftbusAgent() = default; + int OnSessionOpened(const int sessionId, const int result); + void OnSessionClosed(int sessionId); + +protected: + void JoinDomain() override; + void QuitDomain() override; + void StopTopHalf() override; + void StopBottomHalf() override; + std::shared_ptr OpenSession(const DeviceInfo &info) override; + void CloseSession(std::shared_ptr session) override; + +private: + std::string sessionName_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_session.h b/services/distributedfiledeamon/include/network/softbus/softbus_session.h new file mode 100644 index 000000000..554d05004 --- /dev/null +++ b/services/distributedfiledeamon/include/network/softbus/softbus_session.h @@ -0,0 +1,37 @@ +/* + * 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 "network/base_session.h" + +namespace OHOS { +namespace DistributedFile { +class SoftbusSession final : public BaseSession { +public: + SoftbusSession(int sessionId) : sessionId_(sessionId) {} + ~SoftbusSession() = default; + bool IsFromServer() const override; + std::string GetCid() const override; + int32_t GetHandle() const override; + std::array GetKey() const override; + void Release() const override; + void DisableSessionListener() const override; + +private: + int sessionId_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_session_dispatcher.h b/services/distributedfiledeamon/include/network/softbus/softbus_session_dispatcher.h new file mode 100644 index 000000000..dbf12c48f --- /dev/null +++ b/services/distributedfiledeamon/include/network/softbus/softbus_session_dispatcher.h @@ -0,0 +1,41 @@ +/* + * 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 +#include + +namespace OHOS { +namespace DistributedFile { +class SoftbusAgent; +class SoftbusSessionDispatcher final { +public: + SoftbusSessionDispatcher() = delete; + ~SoftbusSessionDispatcher() = delete; + static void RegisterSessionListener(const std::string busName, std::weak_ptr); + static void UnregisterSessionListener(const std::string busName, std::weak_ptr); + static std::weak_ptr GetAgent(int sessionId); + static int OnSessionOpened(int sessionId, int result); + static void OnSessionClosed(int sessionId); + +private: + static std::mutex softbusAgentMutex_; + static std::map> busNameToAgent_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_session_name.h b/services/distributedfiledeamon/include/network/softbus/softbus_session_name.h new file mode 100644 index 000000000..c7c04149f --- /dev/null +++ b/services/distributedfiledeamon/include/network/softbus/softbus_session_name.h @@ -0,0 +1,36 @@ +/* + * 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 DistributedFile { +class SoftbusSessionName final { +public: + explicit SoftbusSessionName(std::string path) : path_(path) {} + ~SoftbusSessionName() = default; + std::string ToString() + { + return prefix + path_; + } + +private: + const std::string prefix = "DistributedFileService"; + std::string path_; +}; +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/device/device_info.cpp b/services/distributedfiledeamon/src/device/device_info.cpp new file mode 100644 index 000000000..2a378a14a --- /dev/null +++ b/services/distributedfiledeamon/src/device/device_info.cpp @@ -0,0 +1,77 @@ +/* + * 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 "device/device_info.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +using namespace std; + +DeviceInfo::DeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo) +{ + cid_ = string(nodeInfo.deviceId); +} + +DeviceInfo &DeviceInfo::operator=(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo) +{ + cid_ = string(nodeInfo.deviceId); + return *this; +} + +DeviceInfo::DeviceInfo(DeviceInfo &&nodeInfo) +{ + initIidFlag_.store(nodeInfo.initIidFlag_.load()); + initCidFlag_.store(nodeInfo.initCidFlag_.load()); + iid_ = nodeInfo.iid_; + cid_ = nodeInfo.cid_; +} + +void DeviceInfo::SetIid(uint64_t iid) +{ + if (initIidFlag_ == false) { + iid_ = iid; + initIidFlag_ = true; + } else { + LOGI("Iid is already initializing"); + } +} + +void DeviceInfo::SetCid(const std::string cid) +{ + if (initCidFlag_ == false) { + cid_ = cid; + initCidFlag_ = true; + } else { + LOGI("Iid is already initializing"); + } +} + +uint64_t DeviceInfo::GetIid() const +{ + if (initIidFlag_ == false) { + // TODO 抛异常 + } + return iid_; +} +const std::string &DeviceInfo::GetCid() const +{ + if (initCidFlag_ == false) { + // TODO 抛异常 + } + return cid_; +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/device/device_manager_agent.cpp b/services/distributedfiledeamon/src/device/device_manager_agent.cpp new file mode 100644 index 000000000..fa1db6ab3 --- /dev/null +++ b/services/distributedfiledeamon/src/device/device_manager_agent.cpp @@ -0,0 +1,213 @@ +/* + * 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 "device/device_manager_agent.h" +#include "network/softbus/softbus_agent.h" + +#include +#include + +#include "ipc/i_deamon.h" +#include "softbus_bus_center.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +using namespace std; + +DeviceManagerAgent::DeviceManagerAgent() {} + +DeviceManagerAgent::~DeviceManagerAgent() +{ + StopInstance(); +} + +void DeviceManagerAgent::Start() +{ + RegisterToExternalDm(); // TODO Catch? + InitLocalNodeInfo(); +} + +void DeviceManagerAgent::Stop() +{ + UnregisterFromExternalDm(); +} + +void DeviceManagerAgent::JoinGroup(std::weak_ptr mp) +{ + auto smp = mp.lock(); + if (!smp) { + stringstream ss("Failed to join group: Received empty mountpoint"); + LOGE("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + + unique_lock lock(mpToNetworksMutex_); + auto agent = make_shared(mp); + auto [ignored, inserted] = mpToNetworks_.insert({smp->GetID(), agent}); + if (!inserted) { + stringstream ss; + ss << "Failed to join group: Mountpoint existed" << smp->ToString(); + throw runtime_error(ss.str()); + } + + agent->Start(); +} + +void DeviceManagerAgent::QuitGroup(std::weak_ptr mp) +{ + auto smp = mp.lock(); + if (!smp) { + stringstream ss("Failed to quit group: Received empty mountpoint"); + LOGE("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + + unique_lock lock(mpToNetworksMutex_); + auto it = mpToNetworks_.find(smp->GetID()); + if (it == mpToNetworks_.end()) { + stringstream ss; + ss << "Failed to quit group: Mountpoint didn't exist " << smp->ToString(); + throw runtime_error(ss.str()); + } + + it->second->Stop(); + mpToNetworks_.erase(smp->GetID()); +} + +void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) +{ + LOGI("OnDeviceOnline begin"); + auto dm = &DeviceManagerAgent::GetInstance(); + unique_lock lock(dm->mpToNetworksMutex_); + for (auto &&networkAgent : dm->mpToNetworks_) { + DeviceInfo info(deviceInfo); + networkAgent.second->ConnectDeviceAsync(info); + } + LOGI("OnDeviceOnline end"); +} + +void DeviceManagerAgent::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) +{ + LOGI("OnDeviceOffline begin"); + auto dm = &DeviceManagerAgent::GetInstance(); + + unique_lock lock(dm->mpToNetworksMutex_); + for (auto &&networkAgent : dm->mpToNetworks_) { + DeviceInfo info(deviceInfo); + networkAgent.second->DisconnectDevice(info); + } + LOGI("OnDeviceOffline end"); +} + +void DeviceManagerAgent::OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) +{ + LOGI("OnDeviceInfoChanged"); +} + +void DeviceManagerAgent::InitLocalNodeInfo() +{ + NodeBasicInfo tmpNodeInfo; + int errCode = GetLocalNodeDeviceInfo(IDeamon::SERVICE_NAME.c_str(), &tmpNodeInfo); + if (errCode != 0) { + stringstream ss; + ss << "Failed to get local cid: error code reads " << errCode; + LOGW("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + localDeviceInfo_.SetCid(string(tmpNodeInfo.networkId)); + InitLocalIid(); +} + +void DeviceManagerAgent::OnRemoteDied() +{ + LOGI("device manager service died"); + RegisterToExternalDm(); // ! TODO +} + +void DeviceManagerAgent::InitLocalIid() +{ + localDeviceInfo_.SetIid(0x12345678); // TODO 随机产生 +} + +DeviceInfo &DeviceManagerAgent::GetLocalDeviceInfo() +{ + return localDeviceInfo_; +} + +std::vector DeviceManagerAgent::GetRemoteDevicesInfo() +{ + string extra = ""; + string pkgName = IDeamon::SERVICE_NAME; + vector deviceList; + + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + int errCode = deviceManager.GetTrustedDeviceList(pkgName, extra, deviceList); + if (errCode) { + stringstream ss; + ss << "Failed to get info of remote devices: the error code reads " << errCode; + LOGE("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + + vector res; + for (const auto &item : deviceList) { + res.push_back(DeviceInfo(item)); + } + return res; +} + +void DeviceManagerAgent::RegisterToExternalDm() +{ + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + string pkgName = IDeamon::SERVICE_NAME; + int errCode = deviceManager.InitDeviceManager(pkgName, shared_from_this()); + if (errCode) { + stringstream ss; + ss << "Failed to InitDeviceManager: the error code reads " << errCode; + LOGE("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + string extra = ""; + errCode = deviceManager.RegisterDevStateCallback(pkgName, extra, shared_from_this()); + if (errCode) { + stringstream ss; + ss << "Failed to RegisterDevStateCallback: the error code reads " << errCode; + LOGE("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } +} + +void DeviceManagerAgent::UnregisterFromExternalDm() +{ + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + string pkgName = IDeamon::SERVICE_NAME; + int errCode = deviceManager.UnRegisterDevStateCallback(pkgName); + if (errCode) { + stringstream ss; + ss << "Failed to UnRegisterDevStateCallback: the error code reads " << errCode; + LOGE("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } + errCode = deviceManager.UnInitDeviceManager(pkgName); + if (errCode) { + stringstream ss; + ss << "Failed to UnInitDeviceManager: the error code reads " << errCode; + LOGE("%s", ss.str().c_str()); + throw runtime_error(ss.str()); + } +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/kernel_talker.cpp b/services/distributedfiledeamon/src/network/kernel_talker.cpp new file mode 100644 index 000000000..b32365815 --- /dev/null +++ b/services/distributedfiledeamon/src/network/kernel_talker.cpp @@ -0,0 +1,257 @@ +/* + * 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 "network/kernel_talker.h" +#include "device/device_manager_agent.h" +#include "securec.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +constexpr int KEY_MAX_LEN = 32; +constexpr int HMDFS_ACCOUNT_HASH_MAX_LEN = 21; +constexpr int POLL_TIMEOUT_MS = 200; +constexpr int NONE_EVENT = -1; +constexpr int READ_EVENT = 1; +constexpr int TIME_OUT_EVENT = 0; + +struct InitParam { + int32_t cmd; + uint64_t localIid; + uint8_t currentAccout[HMDFS_ACCOUNT_HASH_MAX_LEN]; +} __attribute__((packed)); + +struct UpdateSocketParam { + int32_t cmd; + int32_t newfd; + uint64_t localIid; + uint8_t status; + uint8_t protocol; + uint16_t udpPort; + uint8_t deviceType; + uint8_t masterKey[KEY_MAX_LEN]; + char cid[CID_MAX_LEN]; + int32_t linkType; + int32_t binderFd; +} __attribute__((packed)); + +struct OfflineParam { + int32_t cmd; + char remoteCid[CID_MAX_LEN]; +} __attribute__((packed)); + +enum { + CMD_INIT = 0, + CMD_UPDATE_SOCKET, + CMD_OFF_LINE, + CMD_SET_ACCOUNT, + CMD_OFF_LINE_ALL, + CMD_UPDATE_CAPABILITY, + CMD_GET_P2P_SESSION_FAIL, + CMD_DELETE_CONNECTION, + CMD_CNT +}; + +enum { + SOCKET_STAT_ACCEPT = 0, + SOCKET_STAT_OPEN, +}; + +enum { + TCP_TRANSPORT_PROTO = 0, + UDP_TRANSPORT_PROTO, +}; + +enum Notify { + NOTIFY_HS_DONE = 0, + NOTIFY_OFFLINE, + NOTIFY_OFFLINE_IID, + NOTIFY_GET_SESSION, + NOTIFY_GOT_UDP_PORT, + NOTIFY_NONE, + NOTIFY_DISCONNECT, + NOTIFY_D2DP_FAILED, + NOTIFY_CNT, +}; + +void KernelTalker::SinkSessionTokernel(std::shared_ptr session) +{ + int socketFd = session->GetHandle(); + auto masterkey = session->GetKey(); + auto cid = session->GetCid(); + LOGD("sink session to kernel success, cid:%{public}s, socketFd:%{public}d, key[0]:%{public}x", cid.c_str(), + socketFd, *(uint32_t *)masterkey.data()); + + uint8_t status = (session->IsFromServer() ? SOCKET_STAT_ACCEPT : SOCKET_STAT_OPEN); + + UpdateSocketParam cmd = { + .cmd = CMD_UPDATE_SOCKET, + .newfd = socketFd, + .localIid = DeviceManagerAgent::GetInstance().GetLocalDeviceInfo().GetIid(), + .status = status, + .protocol = TCP_TRANSPORT_PROTO, + .linkType = 0, + .binderFd = -1, + }; + if (memcpy_s(cmd.masterKey, KEY_MAX_LEN, masterkey.data(), KEY_MAX_LEN) != EOK) { + return; // !抛异常 + } + + if (memcpy_s(cmd.cid, CID_MAX_LEN, cid.c_str(), CID_MAX_LEN)) { + return; // !抛异常 + } + session->DisableSessionListener(); + SetCmd(cmd); +} + +void KernelTalker::SinkInitCmdToKernel(uint64_t iid) +{ + InitParam cmd = { + .cmd = CMD_INIT, + .localIid = iid, + }; + + SetCmd(cmd); +} + +void KernelTalker::SinkOfflineCmdToKernel(std::string cid) +{ + OfflineParam cmd = { + .cmd = CMD_OFF_LINE, + }; + + if (cid.length() < CID_MAX_LEN) { + return; // ! 抛异常 + } + + if (memcpy_s(cmd.remoteCid, CID_MAX_LEN, cid.c_str(), CID_MAX_LEN) != EOK) { + return; // ! 抛异常 + } + SetCmd(cmd); +} + +std::unordered_set KernelTalker::GetKernelSesions() +{ + return {}; +} + +void KernelTalker::CreatePollThread() +{ + isRunning_ = true; + if (pollThread_ != nullptr) { + LOGE("pollTread is not null"); + return; + } + pollThread_ = std::make_unique(&KernelTalker::PollRun, this); + LOGI("Create pollThread OK"); +} + +void KernelTalker::WaitForPollThreadExited() +{ + isRunning_ = false; + if (pollThread_ == nullptr) { + LOGE("pollTread is null"); + return; + } + + if (pollThread_->joinable()) { + LOGI("pollThread->joinable is true"); + pollThread_->join(); + } + pollThread_ = nullptr; + LOGI("pollTread exit ok"); +} + +void KernelTalker::PollRun() +{ + struct pollfd fileFd; + int cmdFd = -1; + + LOGI("entry"); + auto spt = mountPoint_.lock(); + if (spt == nullptr) { + LOGE("mountPoint is not exist! bad weak_ptr"); + return; // ! 抛异常 + } + std::string ctrlPath = spt->GetMountArgument().GetCtrlPath(); + cmdFd = open(ctrlPath.c_str(), O_RDWR); + if (cmdFd < 0) { + LOGE("Open node file error %{public}d", errno); + return; // ! 待审视,此处不能抛异常,用户态还没有通知到内核时,这个文件可能就不存在 + } + + LOGI("Open node file success"); + + while (isRunning_) { + fileFd.fd = cmdFd; + fileFd.events = POLLPRI; + fileFd.revents = 0; + int ret = poll(&fileFd, 1, POLL_TIMEOUT_MS); + switch (ret) { + case NONE_EVENT: + LOGI("none event, poll exit"); + break; + case TIME_OUT_EVENT: + break; + case READ_EVENT: + HandleAllNotify(cmdFd); + break; + default: + LOGI("poll exit"); + } + } + close(cmdFd); + LOGI("exit"); + return; +} + +void KernelTalker::HandleAllNotify(int fd) +{ + NotifyParam param; + + while (isRunning_) { + lseek(fd, 0, SEEK_SET); + param.notify = NOTIFY_NONE; + int readSize = read(fd, ¶m, sizeof(NotifyParam)); + if ((readSize < (int)sizeof(NotifyParam)) || (param.notify == NOTIFY_NONE)) { + return; + } + NotifyHandler(param); + } +} + +void KernelTalker::NotifyHandler(NotifyParam ¶m) +{ + int cmd = param.notify; + std::string cidStr(param.remoteCid, CID_MAX_LEN); + switch (cmd) { + case NOTIFY_HS_DONE: + LOGI("NOTIFY_HS_DONE, remote cid %{public}s", cidStr.c_str()); + break; + case NOTIFY_OFFLINE: + case NOTIFY_OFFLINE_IID: + LOGI("%{public}s, remote cid %{public}s", (cmd == NOTIFY_OFFLINE) ? "NOTIFY_OFFLINE" : "NOTIFY_OFFLINE_IID", + cidStr.c_str()); + break; + case NOTIFY_GET_SESSION: + GetSessionCallback_(param); + break; + default: + LOGI("cmd %{public}d not support now", cmd); + break; + } +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/network_agent_template.cpp b/services/distributedfiledeamon/src/network/network_agent_template.cpp new file mode 100644 index 000000000..8a9fe7d7a --- /dev/null +++ b/services/distributedfiledeamon/src/network/network_agent_template.cpp @@ -0,0 +1,110 @@ +/* + * 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 "network/network_agent_template.h" +#include "device/device_manager_agent.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +namespace { +constexpr int MAX_RETRY_COUNT = 7; +constexpr int SLEEP_TIME = 1500; +} // namespace + +NetworkAgentTemplate::~NetworkAgentTemplate() {} + +void NetworkAgentTemplate::Start() +{ + JoinDomain(); // TODO 考虑下软总线是否有可能还没有起来 + ConnectOnlineDevices(); + kernerlTalker_.CreatePollThread(); +} + +void NetworkAgentTemplate::Stop() +{ + StopTopHalf(); + StopBottomHalf(); + kernerlTalker_.WaitForPollThreadExited(); +} + +void NetworkAgentTemplate::ConnectDeviceAsync(const DeviceInfo &info) +{ + kernerlTalker_.SinkInitCmdToKernel(info.GetIid()); + + std::unique_lock taskLock(taskMut_); + tasks_.emplace_back(); + tasks_.back().RunLoopFlexible( + [&info, this](uint64_t &sleepTime) { + auto session = OpenSession(info); + if (session == nullptr) { + LOGE("open session fail, retry, cid:%{public}s", info.GetCid().c_str()); + return false; + } + LOGI("open session success, cid:%{public}s", info.GetCid().c_str()); + return true; + }, + SLEEP_TIME, MAX_RETRY_COUNT); +} + +void NetworkAgentTemplate::ConnectOnlineDevices() +{ + auto infos = DeviceManagerAgent::GetInstance().GetRemoteDevicesInfo(); + LOGI("Have %{public}d devices Online", infos.size()); + for (const auto &info : infos) { + ConnectDeviceAsync(info); + } +} + +void NetworkAgentTemplate::DisconnectDevice(const DeviceInfo &info) +{ + LOGI("DeviceOffline, cid:%{public}s", info.GetCid().c_str()); + kernerlTalker_.SinkOfflineCmdToKernel(info.GetCid()); + sessionPool_.RefreshSessionPoolBasedOnKernel(); +} + +void NetworkAgentTemplate::AcceptSession(std::shared_ptr session) +{ + std::unique_lock taskLock(taskMut_); + tasks_.emplace_back(); + tasks_.back().Run([=] { + auto cid = session->GetCid(); + LOGI("AcceptSession thread run, cid:%{public}s", cid.c_str()); + sessionPool_.HoldSession(session); + return true; + }); +} + +void NetworkAgentTemplate::GetSessionProcess(NotifyParam ¶m) +{ + std::string cidStr(param.remoteCid, CID_MAX_LEN); + LOGI("NOTIFY_GET_SESSION, old fd %{public}d, remote cid %{public}s", param.fd, cidStr.c_str()); + sessionPool_.RefreshSessionPoolBasedOnKernel(); + GetSesion(cidStr); +} + +void NetworkAgentTemplate::GetSesion(const std::string &cid) +{ + DeviceInfo deviceInfo; + deviceInfo.SetCid(cid); + auto session = OpenSession(deviceInfo); + if (session == nullptr) { + LOGE("open session fail, retry, cid:%{public}s", cid.c_str()); + return; + } + LOGI("open session success, cid:%{public}s", cid.c_str()); +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/session_pool.cpp b/services/distributedfiledeamon/src/network/session_pool.cpp new file mode 100644 index 000000000..5ff256f75 --- /dev/null +++ b/services/distributedfiledeamon/src/network/session_pool.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 "network/session_pool.h" + +namespace OHOS { +namespace DistributedFile { +void SessionPool::HoldSession(std::shared_ptr session) +{ + std::lock_guard lock(sessionPoolLock_); + talker_.SinkSessionTokernel(session); + AddSessionToPool(session); + RefreshSessionPoolBasedOnKernel(); +} + +void SessionPool::RefreshSessionPoolBasedOnKernel() +{ + std::lock_guard lock(sessionPoolLock_); + auto kernelSessions = talker_.GetKernelSesions(); + for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) { + if (kernelSessions.count((*iter)->GetHandle() == 0)) { + // (*iter)->Release(); + // iter = usrSpaceSessionPool_.erase(iter); // ! 待GetKernelSessions实现后放开 + } else { + ++iter; + } + } +} + +void SessionPool::AddSessionToPool(std::shared_ptr session) +{ + usrSpaceSessionPool_.push_back(session); +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp b/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp new file mode 100644 index 000000000..85a9251b3 --- /dev/null +++ b/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp @@ -0,0 +1,137 @@ +/* + * 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 "network/softbus/softbus_agent.h" + +#include + +#include "ipc/i_deamon.h" +#include "network/softbus/softbus_session.h" +#include "network/softbus/softbus_session_dispatcher.h" +#include "network/softbus/softbus_session_name.h" +#include "session.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +using namespace std; + +SoftbusAgent::SoftbusAgent(std::weak_ptr mountPoint) : NetworkAgentTemplate(mountPoint) +{ + auto spt = mountPoint.lock(); + if (spt == nullptr) { + LOGE("mountPoint is not exist! bad weak_ptr"); + sessionName_ = ""; + return; + } + + std::string path = spt->GetMountArgument().GetFullDst(); + SoftbusSessionName sessionName(path); + sessionName_ = sessionName.ToString(); +} + +void SoftbusAgent::JoinDomain() +{ + ISessionListener sessionListener = { + .OnSessionOpened = SoftbusSessionDispatcher::OnSessionOpened, + .OnSessionClosed = SoftbusSessionDispatcher::OnSessionClosed, + .OnBytesReceived = nullptr, + .OnMessageReceived = nullptr, + .OnStreamReceived = nullptr, + }; + + SoftbusSessionDispatcher::RegisterSessionListener(sessionName_, shared_from_this()); + int ret = ::CreateSessionServer(IDeamon::SERVICE_NAME.c_str(), sessionName_.c_str(), &sessionListener); + if (ret != 0) { // ! 错误码 + std::stringstream ss; + ss << "Failed to CreateSessionServer, errno:" << ret; + LOGE("%{public}s, sessionName:%{public}s", ss.str().c_str(), sessionName_.c_str()); + throw std::runtime_error(ss.str()); + } + LOGD("Succeed to JoinDomain, busName:%{public}s", sessionName_.c_str()); +} + +void SoftbusAgent::QuitDomain() +{ + int ret = ::RemoveSessionServer(IDeamon::SERVICE_NAME.c_str(), sessionName_.c_str()); + if (ret != 0) { // ! 错误码 + std::stringstream ss; + ss << "Failed to RemoveSessionServer, errno:" << ret; + LOGE("%{public}s", ss.str().c_str()); + throw std::runtime_error(ss.str()); + } + + SoftbusSessionDispatcher::UnregisterSessionListener(sessionName_.c_str(), shared_from_this()); + LOGD("Succeed to QuitDomain, busName:%{public}s", sessionName_.c_str()); +} +void SoftbusAgent::StopTopHalf() +{ + QuitDomain(); +} +void SoftbusAgent::StopBottomHalf() {} +std::shared_ptr SoftbusAgent::OpenSession(const DeviceInfo &info) +{ + SessionAttribute attr; + attr.dataType = TYPE_BYTES; + attr.unique = true; + + LOGD("Start to Open Session, cid:%{public}s", info.GetCid().c_str()); + + int sessionId = + ::OpenSession(sessionName_.c_str(), sessionName_.c_str(), info.GetCid().c_str(), "hmdfs_wifiGroup", &attr); + if (sessionId < 0) { + LOGE("Failed to open session, cid:%{public}s, sessionId:%{public}d", info.GetCid().c_str(), sessionId); + return nullptr; + } + return std::make_shared(sessionId); +} + +void SoftbusAgent::CloseSession(std::shared_ptr session) +{ + if (session == nullptr) { + LOGE("Failed to close session, error:invalid session"); + return; + } + session->Release(); +} + +int SoftbusAgent::OnSessionOpened(const int sessionId, const int result) +{ + auto session = std::make_shared(sessionId); + auto cid = session->GetCid(); + if (!session->IsFromServer()) { + if (result != 0) { + // !是否加OpenSession重试? + LOGE("open session failed, result:%{public}d", result); + return 0; + } + } + int socket_fd = session->GetHandle(); + LOGI( + "accept sesion, sessionid:%{public}d, Is %{public}s Side, fd %{public}d, from cid %{public}s, result " + "%{public}d", + sessionId, (session->IsFromServer() == true) ? "Server" : "Client", socket_fd, cid.c_str(), result); + AcceptSession(session); + return 0; +} + +void SoftbusAgent::OnSessionClosed(int sessionId) +{ + auto session = std::make_shared(sessionId); + auto cid = session->GetCid(); + LOGI("Session to %{public}s closed by unknown reason, Is %{public}s Side", cid.c_str(), + (session->IsFromServer() == true) ? "Server" : "Client"); +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp b/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp new file mode 100644 index 000000000..8c75790c7 --- /dev/null +++ b/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp @@ -0,0 +1,82 @@ +/* + * 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 "network/softbus/softbus_session.h" + +#include "dfs_session.h" +#include "session.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +using namespace std; + +constexpr int32_t SOFTBUS_OK = 0; +constexpr int32_t INVALID_SOCKET_FD = -1; +constexpr int32_t DEVICE_ID_SIZE_MAX = 65; + +bool SoftbusSession::IsFromServer() const +{ + return (::GetSessionSide(sessionId_) == IS_SERVER) ? true : false; +} + +string SoftbusSession::GetCid() const +{ + char perDevId[DEVICE_ID_SIZE_MAX] = ""; + int ret = ::GetPeerDeviceId(sessionId_, perDevId, sizeof(perDevId)); + if (ret != SOFTBUS_OK) { + LOGE("get my peer device id failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_); + return {}; // ! 抛异常 + } + return string(perDevId); +} + +int32_t SoftbusSession::GetHandle() const +{ + int32_t socket_fd; + int32_t ret = ::GetSessionHandle(sessionId_, &socket_fd); + if (ret != SOFTBUS_OK) { + LOGE("get session socket fd failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_); + return INVALID_SOCKET_FD; + } + return socket_fd; +} + +std::array SoftbusSession::GetKey() const +{ + array key; + int32_t ret = ::GetSessionKey(sessionId_, key.data(), key.size()); + if (ret != SOFTBUS_OK) { + LOGE("get session key failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_); + return {}; // ! 抛异常 + } + return key; +} + +void SoftbusSession::Release() const +{ + ::CloseSession(sessionId_); +} + +void SoftbusSession::DisableSessionListener() const +{ + int32_t ret = ::DisableSessionListener(sessionId_); + if (ret != SOFTBUS_OK) { + LOGE("disableSessionlistener failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_); + return; // !抛异常 + } +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp b/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp new file mode 100644 index 000000000..e8416a10f --- /dev/null +++ b/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp @@ -0,0 +1,102 @@ +/* + * 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 "network/softbus/softbus_session_dispatcher.h" + +#include + +#include "network/softbus/softbus_agent.h" +#include "session.h" +#include "utils_log.h" + +namespace OHOS { +namespace DistributedFile { +constexpr int32_t SESSION_NAME_SIZE_MAX = 256; + +std::mutex SoftbusSessionDispatcher::softbusAgentMutex_; +std::map> SoftbusSessionDispatcher::busNameToAgent_; + +void SoftbusSessionDispatcher::RegisterSessionListener(const std::string busName, + std::weak_ptr softbusAgent) +{ + if (busName == "") { + std::stringstream ss; + ss << "Failed to register session to softbus"; + LOGE("%{public}s", ss.str().c_str()); + throw std::runtime_error(ss.str()); + } + std::lock_guard lock(softbusAgentMutex_); + auto agent = busNameToAgent_.find(busName); + if (agent != busNameToAgent_.end()) { + std::stringstream ss; + ss << "this softbusAgent is not exist, busName: " << busName.c_str(); + LOGE("%{public}s", ss.str().c_str()); + throw std::runtime_error(ss.str()); + } else { + busNameToAgent_.insert(std::make_pair(busName, softbusAgent)); + } + LOGD("RegisterSessionListener SUCCESS, busName:%{public}s", busName.c_str()); +} +void SoftbusSessionDispatcher::UnregisterSessionListener(const std::string busName, std::weak_ptr) +{ + std::lock_guard lock(softbusAgentMutex_); + auto agent = busNameToAgent_.find(busName); + if (agent != busNameToAgent_.end()) { + busNameToAgent_.erase(busName); + } else { + std::stringstream ss; + ss << "this softbusAgent is not exist, busName: " << busName.c_str(); + LOGE("%{public}s", ss.str().c_str()); + throw std::runtime_error(ss.str()); + } + LOGD("UnregisterSessionListener SUCCESS, busName:%{public}s", busName.c_str()); +} +std::weak_ptr SoftbusSessionDispatcher::GetAgent(int sessionId) +{ + char peeSessionName[SESSION_NAME_SIZE_MAX]; + int ret = GetPeerSessionName(sessionId, peeSessionName, sizeof(peeSessionName)); + if (ret != 0) { // ! TODO 魔术字 + LOGE("Get my peer session name failed, session id is %{public}d.", sessionId); + return {}; // ! TODO + } + auto agent = busNameToAgent_.find(std::string(peeSessionName)); + if (agent != busNameToAgent_.end()) { + LOGD("Get softbus Agent Success, busName:%{public}s", peeSessionName); + return agent->second; + } + LOGE("Get Session Agent fail, not exist! sessionId:%{public}d, busName:%{public}s", sessionId, peeSessionName); + return {}; // ! TODO +} +int SoftbusSessionDispatcher::OnSessionOpened(int sessionId, int result) +{ + auto agent = GetAgent(sessionId); + if (auto spt = agent.lock()) { + return spt->OnSessionOpened(sessionId, result); + } else { + LOGE("session not exist!, session id is %{public}d", sessionId); + return -1; + } +} +void SoftbusSessionDispatcher::OnSessionClosed(int sessionId) +{ + auto agent = GetAgent(sessionId); + if (auto spt = agent.lock()) { + spt->OnSessionClosed(sessionId); + } else { + LOGE("session not exist!, session id is %{public}d", sessionId); + } +} +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/test/unittest/BUILD.gn b/services/distributedfiledeamon/test/unittest/BUILD.gn new file mode 100644 index 000000000..7958f7835 --- /dev/null +++ b/services/distributedfiledeamon/test/unittest/BUILD.gn @@ -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. +import("//build/test.gni") +import("//foundation/storage/distributed_file_manager/distributedfile.gni") + +module_output_path = "storage_distributed_file_manager/distributedfiledeamon" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "../../include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + ] +} + +ohos_unittest("deviceManagerAgentTest") { + module_out_path = module_output_path + + sources = [ + ] + + sources += [ "device/device_manager_agent_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gtest_main", + "${utils_path}:libdistributedfileutils", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + ] +} + +group("unittest") { + testonly = true + deps = [":deviceManagerAgentTest"] +} \ No newline at end of file diff --git a/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp b/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp new file mode 100644 index 000000000..55d15c0ab --- /dev/null +++ b/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 XXXX 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 "device/device_manager_agent.h" +#include + +using namespace testing::ext; + +class deviceManagerAgentTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void deviceManagerAgentTest::SetUpTestCase(void) +{ + // input testsuit setup step,setup invoked before all testcases +} + +void deviceManagerAgentTest::TearDownTestCase(void) +{ + // input testsuit teardown step,teardown invoked after all testcases +} + +void deviceManagerAgentTest::SetUp(void) +{ + // input testcase setup step,setup invoked before each testcases +} + +void deviceManagerAgentTest::TearDown(void) +{ + // input testcase teardown step,teardown invoked after each testcases +} + +/** + * @tc.name: integer_sub_001 + * @tc.desc: Verify the sub function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(deviceManagerAgentTest, test_001, TestSize.Level1) +{ + // step 1:调用函数获取结果 + int actual = 4; + // Step 2:使用断言比较预期与实际结果 + EXPECT_EQ(4, actual); +} \ No newline at end of file diff --git a/utils/system/include/utils_dfs_thread.h b/utils/system/include/utils_dfs_thread.h new file mode 100644 index 000000000..3b728e0c9 --- /dev/null +++ b/utils/system/include/utils_dfs_thread.h @@ -0,0 +1,135 @@ +/* + * 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 "utils_log.h" +#include +#include +#include +#include +#include + +namespace OHOS { +namespace DistributedFile { +namespace Utils { +class DfsThread { +public: + DfsThread() = default; + DfsThread(const DfsThread &) = delete; + DfsThread &operator=(const DfsThread &) = delete; + + ~DfsThread() + { + Stop(); + } + + template + bool Run(_Fn &&_Fx, _Args &&..._Ax) + { + std::unique_lock lock(threadMutex_); + if (thread_ != nullptr) { + return false; + } + running_ = true; + thread_ = std::make_unique(std::forward<_Fn>(_Fx), std::forward<_Args>(_Ax)...); + return true; + } + + bool RunLoop(std::function task, uint64_t interval, uint32_t retryTimes = UINT32_MAX) + { + std::unique_lock lock(threadMutex_); + if (thread_ != nullptr) { + return false; + } + running_ = true; + thread_ = std::make_unique([this, task, interval, retryTimes] { + uint32_t times = retryTimes; + LOGD("DfsThread: entering loop"); + while ((!task()) && (times > 0)) { + times--; + std::unique_lock lock(sleepMutex_); + bool stop = + sleepCv_.wait_for(lock, std::chrono::milliseconds(interval), [this]() { return !this->running_; }); + if (stop) { // is stopped + break; + } + } + LOGD("DfsThread: leaving loop"); + }); + return true; + } + + bool RunLoopFlexible(std::function task, uint64_t interval, uint32_t retryTimes = UINT32_MAX) + { + std::unique_lock lock(threadMutex_); + if (thread_ != nullptr) { + return false; + } + running_ = true; + thread_ = std::make_unique([this, task, interval, retryTimes] { + uint32_t times = retryTimes; + uint64_t duration = interval; + LOGD("DfsThread: entering flexible loop"); + while ((!task(duration)) && (times > 0)) { + times--; + std::unique_lock lock(sleepMutex_); + bool stop = + sleepCv_.wait_for(lock, std::chrono::milliseconds(duration), [this]() { return !this->running_; }); + if (stop) { // is stopped + break; + } + } + LOGD("DfsThread: leaving flexible loop"); + }); + return true; + } + + bool Stop() + { + std::unique_lock lockThread(threadMutex_); + if (thread_ == nullptr) { + return true; + } + { + std::unique_lock lockSleep(sleepMutex_); + running_ = false; + sleepCv_.notify_one(); + } + LOGD("wait thread to stop"); + if (thread_->joinable()) { + thread_->join(); + } + thread_ = nullptr; + return true; + } + + bool operator==(std::thread::id id) + { + if (thread_ == nullptr) { + return false; + } + return thread_->get_id() == id; + } + +private: + std::atomic_bool running_{false}; + std::mutex threadMutex_{}; + std::unique_ptr thread_{nullptr}; + std::mutex sleepMutex_{}; + std::condition_variable sleepCv_{}; +}; +} // namespace Utils +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/include/utils_singleton.h b/utils/system/include/utils_singleton.h new file mode 100644 index 000000000..012f80465 --- /dev/null +++ b/utils/system/include/utils_singleton.h @@ -0,0 +1,75 @@ +/* + * 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 "nocopyable.h" +#include +#include +#include + +namespace OHOS { +namespace DistributedFile { +namespace Utils { +#define DECLARE_SINGLETON(MyClass) \ +public: \ + ~MyClass(); \ + \ +private: \ + friend Singleton; \ + MyClass(); + +template +class Singleton : public NoCopyable { +public: + static T &GetInstance(); + static void StopInstance(); + +protected: + /** + * @note We depend on the IPC manager to serialize the start and the stop procedure + */ + virtual void Start() = 0; + + /** + * @note Be very careful when freeing memory! Threads may call stop and other member functions simultaneously + */ + virtual void Stop() = 0; +}; + +template +T &Singleton::GetInstance() +{ + static T *instance = nullptr; + static std::once_flag once; + std::call_once(once, [pinstance{&instance}]() mutable { + (*pinstance) = new T(); + (*pinstance)->Start(); + }); + return *instance; +} + +template +void Singleton::StopInstance() +{ + static std::once_flag once; + std::call_once(once, []() { + auto instance = &GetInstance(); + instance->Stop(); + }); +} +} // namespace Utils +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file -- Gitee From 76f370d7200cc3907231489de30e7b8470b10aa2 Mon Sep 17 00:00:00 2001 From: c00382521 Date: Mon, 1 Nov 2021 20:58:12 +0800 Subject: [PATCH 3/5] modify singleton implementation & code optimization Signed-off-by: c00382521 --- ohos.build | 3 +- .../include/device/device_info.h | 6 +- .../include/device/device_manager_agent.h | 12 +-- .../src/device/device_info.cpp | 12 ++- .../src/device/device_manager_agent.cpp | 10 +-- .../distributedfiledeamon/src/ipc/deamon.cpp | 4 +- .../src/mountpoint/mount_manager.cpp | 6 +- .../src/mountpoint/mount_point.cpp | 4 +- .../src/network/kernel_talker.cpp | 16 ++-- .../src/network/network_agent_template.cpp | 15 ++-- .../src/network/session_pool.cpp | 10 ++- .../src/network/softbus/softbus_agent.cpp | 22 ++--- .../src/network/softbus/softbus_session.cpp | 2 +- .../softbus/softbus_session_dispatcher.cpp | 34 ++++---- .../test/unittest/BUILD.gn | 23 +++-- .../device/device_manager_agent_test.cpp | 20 +++-- test/moduletest/BUILD.gn | 56 +++++++++++++ .../distributedfiledeamon_service_test.cpp | 83 +++++++++++++++++++ utils/system/include/utils_singleton.h | 25 ++++-- utils/system/src/utils_directory.cpp | 8 +- 20 files changed, 269 insertions(+), 102 deletions(-) create mode 100644 test/moduletest/BUILD.gn create mode 100644 test/moduletest/src/distributedfiledeamon_service_test.cpp diff --git a/ohos.build b/ohos.build index d9d007fb2..54ebf6425 100755 --- a/ohos.build +++ b/ohos.build @@ -10,7 +10,8 @@ "//foundation/storage/distributed_file_manager/services/:services_target" ], "test_list": [ - "//foundation/storage/distributed_file_manager/services/distributedfiledeamon/test/unittest:unittest" + "//foundation/storage/distributed_file_manager/services/distributedfiledeamon/test/unittest:unittest", + "//foundation/storage/distributed_file_manager/test/moduletest:moduletest" ] } } diff --git a/services/distributedfiledeamon/include/device/device_info.h b/services/distributedfiledeamon/include/device/device_info.h index 7714406a0..f0b591785 100644 --- a/services/distributedfiledeamon/include/device/device_info.h +++ b/services/distributedfiledeamon/include/device/device_info.h @@ -25,9 +25,9 @@ class DeviceInfo final { public: DeviceInfo() = default; ~DeviceInfo() = default; - explicit DeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo); - explicit DeviceInfo(DeviceInfo &&nodeInfo); - DeviceInfo &operator=(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo); + explicit DeviceInfo(const DistributedHardware::DmDeviceInfo &nodeInfo); + explicit DeviceInfo(const DeviceInfo &nodeInfo); + DeviceInfo &operator=(const DistributedHardware::DmDeviceInfo &nodeInfo); /** * @note Remove the concept iid later diff --git a/services/distributedfiledeamon/include/device/device_manager_agent.h b/services/distributedfiledeamon/include/device/device_manager_agent.h index 4337512af..cf135b2a3 100644 --- a/services/distributedfiledeamon/include/device/device_manager_agent.h +++ b/services/distributedfiledeamon/include/device/device_manager_agent.h @@ -27,8 +27,8 @@ namespace OHOS { namespace DistributedFile { -class DeviceManagerAgent final : public OHOS::DistributedHardware::DmInitCallback, - public OHOS::DistributedHardware::DeviceStateCallback, +class DeviceManagerAgent final : public DistributedHardware::DmInitCallback, + public DistributedHardware::DeviceStateCallback, public std::enable_shared_from_this, public Utils::Singleton { DECLARE_SINGLETON(DeviceManagerAgent); @@ -37,10 +37,10 @@ public: void JoinGroup(std::weak_ptr mp); void QuitGroup(std::weak_ptr mp); - void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; - void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; - void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; - void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override {} + void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override; + void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override {} void OnRemoteDied() override; diff --git a/services/distributedfiledeamon/src/device/device_info.cpp b/services/distributedfiledeamon/src/device/device_info.cpp index 2a378a14a..7805d9938 100644 --- a/services/distributedfiledeamon/src/device/device_info.cpp +++ b/services/distributedfiledeamon/src/device/device_info.cpp @@ -20,23 +20,21 @@ namespace OHOS { namespace DistributedFile { using namespace std; -DeviceInfo::DeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo) +DeviceInfo::DeviceInfo(const DistributedHardware::DmDeviceInfo &nodeInfo) { cid_ = string(nodeInfo.deviceId); } -DeviceInfo &DeviceInfo::operator=(const OHOS::DistributedHardware::DmDeviceInfo &nodeInfo) +DeviceInfo &DeviceInfo::operator=(const DistributedHardware::DmDeviceInfo &nodeInfo) { cid_ = string(nodeInfo.deviceId); return *this; } -DeviceInfo::DeviceInfo(DeviceInfo &&nodeInfo) +DeviceInfo::DeviceInfo(const DeviceInfo &nodeInfo) : iid_(nodeInfo.iid_), cid_(nodeInfo.cid_) { initIidFlag_.store(nodeInfo.initIidFlag_.load()); initCidFlag_.store(nodeInfo.initCidFlag_.load()); - iid_ = nodeInfo.iid_; - cid_ = nodeInfo.cid_; } void DeviceInfo::SetIid(uint64_t iid) @@ -49,7 +47,7 @@ void DeviceInfo::SetIid(uint64_t iid) } } -void DeviceInfo::SetCid(const std::string cid) +void DeviceInfo::SetCid(const string cid) { if (initCidFlag_ == false) { cid_ = cid; @@ -66,7 +64,7 @@ uint64_t DeviceInfo::GetIid() const } return iid_; } -const std::string &DeviceInfo::GetCid() const +const string &DeviceInfo::GetCid() const { if (initCidFlag_ == false) { // TODO 抛异常 diff --git a/services/distributedfiledeamon/src/device/device_manager_agent.cpp b/services/distributedfiledeamon/src/device/device_manager_agent.cpp index fa1db6ab3..f6f372ab8 100644 --- a/services/distributedfiledeamon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledeamon/src/device/device_manager_agent.cpp @@ -45,7 +45,7 @@ void DeviceManagerAgent::Stop() UnregisterFromExternalDm(); } -void DeviceManagerAgent::JoinGroup(std::weak_ptr mp) +void DeviceManagerAgent::JoinGroup(weak_ptr mp) { auto smp = mp.lock(); if (!smp) { @@ -66,7 +66,7 @@ void DeviceManagerAgent::JoinGroup(std::weak_ptr mp) agent->Start(); } -void DeviceManagerAgent::QuitGroup(std::weak_ptr mp) +void DeviceManagerAgent::QuitGroup(weak_ptr mp) { auto smp = mp.lock(); if (!smp) { @@ -90,7 +90,7 @@ void DeviceManagerAgent::QuitGroup(std::weak_ptr mp) void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) { LOGI("OnDeviceOnline begin"); - auto dm = &DeviceManagerAgent::GetInstance(); + auto dm = DeviceManagerAgent::GetInstance(); unique_lock lock(dm->mpToNetworksMutex_); for (auto &&networkAgent : dm->mpToNetworks_) { DeviceInfo info(deviceInfo); @@ -102,7 +102,7 @@ void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo void DeviceManagerAgent::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) { LOGI("OnDeviceOffline begin"); - auto dm = &DeviceManagerAgent::GetInstance(); + auto dm = DeviceManagerAgent::GetInstance(); unique_lock lock(dm->mpToNetworksMutex_); for (auto &&networkAgent : dm->mpToNetworks_) { @@ -147,7 +147,7 @@ DeviceInfo &DeviceManagerAgent::GetLocalDeviceInfo() return localDeviceInfo_; } -std::vector DeviceManagerAgent::GetRemoteDevicesInfo() +vector DeviceManagerAgent::GetRemoteDevicesInfo() { string extra = ""; string pkgName = IDeamon::SERVICE_NAME; diff --git a/services/distributedfiledeamon/src/ipc/deamon.cpp b/services/distributedfiledeamon/src/ipc/deamon.cpp index 1d97ae5d1..ce8ad5d69 100644 --- a/services/distributedfiledeamon/src/ipc/deamon.cpp +++ b/services/distributedfiledeamon/src/ipc/deamon.cpp @@ -54,7 +54,7 @@ void Deamon::OnStart() try { PublishSA(); StartManagers(); - } catch (const std::exception &e) { + } catch (const exception &e) { LOGE("%{public}s", e.what()); } @@ -70,7 +70,7 @@ void Deamon::OnStop() LOGI("Stop finished successfully"); } -int32_t Deamon::EchoServerDemo(const std::string &echoStr) +int32_t Deamon::EchoServerDemo(const string &echoStr) { return 0; } diff --git a/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp b/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp index afcd41e85..7f2287da4 100644 --- a/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp +++ b/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp @@ -36,7 +36,7 @@ MountManager::~MountManager() try { // Umount mountpoints in reverse order to eliminate dependencies for_each(mountPoints_.rbegin(), mountPoints_.rend(), [this](auto &cur_mp) { Umount(cur_mp); }); - } catch (const std::exception &e) { + } catch (const exception &e) { LOGE("%{public}s", e.what()); } } @@ -56,7 +56,7 @@ void MountManager::Mount(unique_ptr mp) } smp->Mount(); - DeviceManagerAgent::GetInstance().JoinGroup(smp); + DeviceManagerAgent::GetInstance()->JoinGroup(smp); mountPoints_.push_back(smp); } @@ -75,7 +75,7 @@ void MountManager::Umount(weak_ptr wmp) } smp->Umount(); - DeviceManagerAgent::GetInstance().QuitGroup(smp); + DeviceManagerAgent::GetInstance()->QuitGroup(smp); mountPoints_.erase(it); } } // namespace DistributedFile diff --git a/services/distributedfiledeamon/src/mountpoint/mount_point.cpp b/services/distributedfiledeamon/src/mountpoint/mount_point.cpp index 89a30fefc..8506db207 100644 --- a/services/distributedfiledeamon/src/mountpoint/mount_point.cpp +++ b/services/distributedfiledeamon/src/mountpoint/mount_point.cpp @@ -25,7 +25,7 @@ namespace OHOS { namespace DistributedFile { using namespace std; -std::atomic MountPoint::idGen_; +atomic MountPoint::idGen_; MountPoint::MountPoint(const Utils::MountArgument &mountArg) : mountArg_(mountArg) { @@ -77,7 +77,7 @@ Utils::MountArgument MountPoint::GetMountArgument() const return mountArg_; } -std::string MountPoint::ToString() const +string MountPoint::ToString() const { return ""; } diff --git a/services/distributedfiledeamon/src/network/kernel_talker.cpp b/services/distributedfiledeamon/src/network/kernel_talker.cpp index b32365815..223d92baf 100644 --- a/services/distributedfiledeamon/src/network/kernel_talker.cpp +++ b/services/distributedfiledeamon/src/network/kernel_talker.cpp @@ -20,6 +20,8 @@ namespace OHOS { namespace DistributedFile { +using namespace std; + constexpr int KEY_MAX_LEN = 32; constexpr int HMDFS_ACCOUNT_HASH_MAX_LEN = 21; constexpr int POLL_TIMEOUT_MS = 200; @@ -86,7 +88,7 @@ enum Notify { NOTIFY_CNT, }; -void KernelTalker::SinkSessionTokernel(std::shared_ptr session) +void KernelTalker::SinkSessionTokernel(shared_ptr session) { int socketFd = session->GetHandle(); auto masterkey = session->GetKey(); @@ -99,7 +101,7 @@ void KernelTalker::SinkSessionTokernel(std::shared_ptr session) UpdateSocketParam cmd = { .cmd = CMD_UPDATE_SOCKET, .newfd = socketFd, - .localIid = DeviceManagerAgent::GetInstance().GetLocalDeviceInfo().GetIid(), + .localIid = DeviceManagerAgent::GetInstance()->GetLocalDeviceInfo().GetIid(), .status = status, .protocol = TCP_TRANSPORT_PROTO, .linkType = 0, @@ -126,7 +128,7 @@ void KernelTalker::SinkInitCmdToKernel(uint64_t iid) SetCmd(cmd); } -void KernelTalker::SinkOfflineCmdToKernel(std::string cid) +void KernelTalker::SinkOfflineCmdToKernel(string cid) { OfflineParam cmd = { .cmd = CMD_OFF_LINE, @@ -142,7 +144,7 @@ void KernelTalker::SinkOfflineCmdToKernel(std::string cid) SetCmd(cmd); } -std::unordered_set KernelTalker::GetKernelSesions() +unordered_set KernelTalker::GetKernelSesions() { return {}; } @@ -154,7 +156,7 @@ void KernelTalker::CreatePollThread() LOGE("pollTread is not null"); return; } - pollThread_ = std::make_unique(&KernelTalker::PollRun, this); + pollThread_ = make_unique(&KernelTalker::PollRun, this); LOGI("Create pollThread OK"); } @@ -185,7 +187,7 @@ void KernelTalker::PollRun() LOGE("mountPoint is not exist! bad weak_ptr"); return; // ! 抛异常 } - std::string ctrlPath = spt->GetMountArgument().GetCtrlPath(); + string ctrlPath = spt->GetMountArgument().GetCtrlPath(); cmdFd = open(ctrlPath.c_str(), O_RDWR); if (cmdFd < 0) { LOGE("Open node file error %{public}d", errno); @@ -235,7 +237,7 @@ void KernelTalker::HandleAllNotify(int fd) void KernelTalker::NotifyHandler(NotifyParam ¶m) { int cmd = param.notify; - std::string cidStr(param.remoteCid, CID_MAX_LEN); + string cidStr(param.remoteCid, CID_MAX_LEN); switch (cmd) { case NOTIFY_HS_DONE: LOGI("NOTIFY_HS_DONE, remote cid %{public}s", cidStr.c_str()); diff --git a/services/distributedfiledeamon/src/network/network_agent_template.cpp b/services/distributedfiledeamon/src/network/network_agent_template.cpp index 8a9fe7d7a..7515e1aca 100644 --- a/services/distributedfiledeamon/src/network/network_agent_template.cpp +++ b/services/distributedfiledeamon/src/network/network_agent_template.cpp @@ -19,6 +19,7 @@ namespace OHOS { namespace DistributedFile { +using namespace std; namespace { constexpr int MAX_RETRY_COUNT = 7; constexpr int SLEEP_TIME = 1500; @@ -44,10 +45,10 @@ void NetworkAgentTemplate::ConnectDeviceAsync(const DeviceInfo &info) { kernerlTalker_.SinkInitCmdToKernel(info.GetIid()); - std::unique_lock taskLock(taskMut_); + unique_lock taskLock(taskMut_); tasks_.emplace_back(); tasks_.back().RunLoopFlexible( - [&info, this](uint64_t &sleepTime) { + [info{DeviceInfo(info)}, this](uint64_t &sleepTime) { // ! 不能使用this,待解决 auto session = OpenSession(info); if (session == nullptr) { LOGE("open session fail, retry, cid:%{public}s", info.GetCid().c_str()); @@ -61,7 +62,7 @@ void NetworkAgentTemplate::ConnectDeviceAsync(const DeviceInfo &info) void NetworkAgentTemplate::ConnectOnlineDevices() { - auto infos = DeviceManagerAgent::GetInstance().GetRemoteDevicesInfo(); + auto infos = DeviceManagerAgent::GetInstance()->GetRemoteDevicesInfo(); LOGI("Have %{public}d devices Online", infos.size()); for (const auto &info : infos) { ConnectDeviceAsync(info); @@ -75,9 +76,9 @@ void NetworkAgentTemplate::DisconnectDevice(const DeviceInfo &info) sessionPool_.RefreshSessionPoolBasedOnKernel(); } -void NetworkAgentTemplate::AcceptSession(std::shared_ptr session) +void NetworkAgentTemplate::AcceptSession(shared_ptr session) { - std::unique_lock taskLock(taskMut_); + unique_lock taskLock(taskMut_); tasks_.emplace_back(); tasks_.back().Run([=] { auto cid = session->GetCid(); @@ -89,13 +90,13 @@ void NetworkAgentTemplate::AcceptSession(std::shared_ptr session) void NetworkAgentTemplate::GetSessionProcess(NotifyParam ¶m) { - std::string cidStr(param.remoteCid, CID_MAX_LEN); + string cidStr(param.remoteCid, CID_MAX_LEN); LOGI("NOTIFY_GET_SESSION, old fd %{public}d, remote cid %{public}s", param.fd, cidStr.c_str()); sessionPool_.RefreshSessionPoolBasedOnKernel(); GetSesion(cidStr); } -void NetworkAgentTemplate::GetSesion(const std::string &cid) +void NetworkAgentTemplate::GetSesion(const string &cid) { DeviceInfo deviceInfo; deviceInfo.SetCid(cid); diff --git a/services/distributedfiledeamon/src/network/session_pool.cpp b/services/distributedfiledeamon/src/network/session_pool.cpp index 5ff256f75..bd792fcfe 100644 --- a/services/distributedfiledeamon/src/network/session_pool.cpp +++ b/services/distributedfiledeamon/src/network/session_pool.cpp @@ -17,9 +17,11 @@ namespace OHOS { namespace DistributedFile { -void SessionPool::HoldSession(std::shared_ptr session) +using namespace std; + +void SessionPool::HoldSession(shared_ptr session) { - std::lock_guard lock(sessionPoolLock_); + lock_guard lock(sessionPoolLock_); talker_.SinkSessionTokernel(session); AddSessionToPool(session); RefreshSessionPoolBasedOnKernel(); @@ -27,7 +29,7 @@ void SessionPool::HoldSession(std::shared_ptr session) void SessionPool::RefreshSessionPoolBasedOnKernel() { - std::lock_guard lock(sessionPoolLock_); + lock_guard lock(sessionPoolLock_); auto kernelSessions = talker_.GetKernelSesions(); for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) { if (kernelSessions.count((*iter)->GetHandle() == 0)) { @@ -39,7 +41,7 @@ void SessionPool::RefreshSessionPoolBasedOnKernel() } } -void SessionPool::AddSessionToPool(std::shared_ptr session) +void SessionPool::AddSessionToPool(shared_ptr session) { usrSpaceSessionPool_.push_back(session); } diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp b/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp index 85a9251b3..2e882758c 100644 --- a/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp +++ b/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp @@ -27,7 +27,7 @@ namespace OHOS { namespace DistributedFile { using namespace std; -SoftbusAgent::SoftbusAgent(std::weak_ptr mountPoint) : NetworkAgentTemplate(mountPoint) +SoftbusAgent::SoftbusAgent(weak_ptr mountPoint) : NetworkAgentTemplate(mountPoint) { auto spt = mountPoint.lock(); if (spt == nullptr) { @@ -36,7 +36,7 @@ SoftbusAgent::SoftbusAgent(std::weak_ptr mountPoint) : NetworkAgentT return; } - std::string path = spt->GetMountArgument().GetFullDst(); + string path = spt->GetMountArgument().GetFullDst(); SoftbusSessionName sessionName(path); sessionName_ = sessionName.ToString(); } @@ -54,10 +54,10 @@ void SoftbusAgent::JoinDomain() SoftbusSessionDispatcher::RegisterSessionListener(sessionName_, shared_from_this()); int ret = ::CreateSessionServer(IDeamon::SERVICE_NAME.c_str(), sessionName_.c_str(), &sessionListener); if (ret != 0) { // ! 错误码 - std::stringstream ss; + stringstream ss; ss << "Failed to CreateSessionServer, errno:" << ret; LOGE("%{public}s, sessionName:%{public}s", ss.str().c_str(), sessionName_.c_str()); - throw std::runtime_error(ss.str()); + throw runtime_error(ss.str()); } LOGD("Succeed to JoinDomain, busName:%{public}s", sessionName_.c_str()); } @@ -66,10 +66,10 @@ void SoftbusAgent::QuitDomain() { int ret = ::RemoveSessionServer(IDeamon::SERVICE_NAME.c_str(), sessionName_.c_str()); if (ret != 0) { // ! 错误码 - std::stringstream ss; + stringstream ss; ss << "Failed to RemoveSessionServer, errno:" << ret; LOGE("%{public}s", ss.str().c_str()); - throw std::runtime_error(ss.str()); + throw runtime_error(ss.str()); } SoftbusSessionDispatcher::UnregisterSessionListener(sessionName_.c_str(), shared_from_this()); @@ -80,7 +80,7 @@ void SoftbusAgent::StopTopHalf() QuitDomain(); } void SoftbusAgent::StopBottomHalf() {} -std::shared_ptr SoftbusAgent::OpenSession(const DeviceInfo &info) +shared_ptr SoftbusAgent::OpenSession(const DeviceInfo &info) { SessionAttribute attr; attr.dataType = TYPE_BYTES; @@ -94,10 +94,10 @@ std::shared_ptr SoftbusAgent::OpenSession(const DeviceInfo &info) LOGE("Failed to open session, cid:%{public}s, sessionId:%{public}d", info.GetCid().c_str(), sessionId); return nullptr; } - return std::make_shared(sessionId); + return make_shared(sessionId); } -void SoftbusAgent::CloseSession(std::shared_ptr session) +void SoftbusAgent::CloseSession(shared_ptr session) { if (session == nullptr) { LOGE("Failed to close session, error:invalid session"); @@ -108,7 +108,7 @@ void SoftbusAgent::CloseSession(std::shared_ptr session) int SoftbusAgent::OnSessionOpened(const int sessionId, const int result) { - auto session = std::make_shared(sessionId); + auto session = make_shared(sessionId); auto cid = session->GetCid(); if (!session->IsFromServer()) { if (result != 0) { @@ -128,7 +128,7 @@ int SoftbusAgent::OnSessionOpened(const int sessionId, const int result) void SoftbusAgent::OnSessionClosed(int sessionId) { - auto session = std::make_shared(sessionId); + auto session = make_shared(sessionId); auto cid = session->GetCid(); LOGI("Session to %{public}s closed by unknown reason, Is %{public}s Side", cid.c_str(), (session->IsFromServer() == true) ? "Server" : "Client"); diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp b/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp index 8c75790c7..41153f00e 100644 --- a/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp +++ b/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp @@ -54,7 +54,7 @@ int32_t SoftbusSession::GetHandle() const return socket_fd; } -std::array SoftbusSession::GetKey() const +array SoftbusSession::GetKey() const { array key; int32_t ret = ::GetSessionKey(sessionId_, key.data(), key.size()); diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp b/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp index e8416a10f..493749335 100644 --- a/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp +++ b/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp @@ -23,47 +23,49 @@ namespace OHOS { namespace DistributedFile { +using namespace std; + constexpr int32_t SESSION_NAME_SIZE_MAX = 256; -std::mutex SoftbusSessionDispatcher::softbusAgentMutex_; -std::map> SoftbusSessionDispatcher::busNameToAgent_; +mutex SoftbusSessionDispatcher::softbusAgentMutex_; +map> SoftbusSessionDispatcher::busNameToAgent_; -void SoftbusSessionDispatcher::RegisterSessionListener(const std::string busName, - std::weak_ptr softbusAgent) +void SoftbusSessionDispatcher::RegisterSessionListener(const string busName, + weak_ptr softbusAgent) { if (busName == "") { - std::stringstream ss; + stringstream ss; ss << "Failed to register session to softbus"; LOGE("%{public}s", ss.str().c_str()); - throw std::runtime_error(ss.str()); + throw runtime_error(ss.str()); } - std::lock_guard lock(softbusAgentMutex_); + lock_guard lock(softbusAgentMutex_); auto agent = busNameToAgent_.find(busName); if (agent != busNameToAgent_.end()) { - std::stringstream ss; + stringstream ss; ss << "this softbusAgent is not exist, busName: " << busName.c_str(); LOGE("%{public}s", ss.str().c_str()); - throw std::runtime_error(ss.str()); + throw runtime_error(ss.str()); } else { - busNameToAgent_.insert(std::make_pair(busName, softbusAgent)); + busNameToAgent_.insert(make_pair(busName, softbusAgent)); } LOGD("RegisterSessionListener SUCCESS, busName:%{public}s", busName.c_str()); } -void SoftbusSessionDispatcher::UnregisterSessionListener(const std::string busName, std::weak_ptr) +void SoftbusSessionDispatcher::UnregisterSessionListener(const string busName, weak_ptr) { - std::lock_guard lock(softbusAgentMutex_); + lock_guard lock(softbusAgentMutex_); auto agent = busNameToAgent_.find(busName); if (agent != busNameToAgent_.end()) { busNameToAgent_.erase(busName); } else { - std::stringstream ss; + stringstream ss; ss << "this softbusAgent is not exist, busName: " << busName.c_str(); LOGE("%{public}s", ss.str().c_str()); - throw std::runtime_error(ss.str()); + throw runtime_error(ss.str()); } LOGD("UnregisterSessionListener SUCCESS, busName:%{public}s", busName.c_str()); } -std::weak_ptr SoftbusSessionDispatcher::GetAgent(int sessionId) +weak_ptr SoftbusSessionDispatcher::GetAgent(int sessionId) { char peeSessionName[SESSION_NAME_SIZE_MAX]; int ret = GetPeerSessionName(sessionId, peeSessionName, sizeof(peeSessionName)); @@ -71,7 +73,7 @@ std::weak_ptr SoftbusSessionDispatcher::GetAgent(int sessionId) LOGE("Get my peer session name failed, session id is %{public}d.", sessionId); return {}; // ! TODO } - auto agent = busNameToAgent_.find(std::string(peeSessionName)); + auto agent = busNameToAgent_.find(string(peeSessionName)); if (agent != busNameToAgent_.end()) { LOGD("Get softbus Agent Success, busName:%{public}s", peeSessionName); return agent->second; diff --git a/services/distributedfiledeamon/test/unittest/BUILD.gn b/services/distributedfiledeamon/test/unittest/BUILD.gn index 7958f7835..19b7a0d78 100644 --- a/services/distributedfiledeamon/test/unittest/BUILD.gn +++ b/services/distributedfiledeamon/test/unittest/BUILD.gn @@ -19,12 +19,12 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ - "../../include", - "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "${services_path}/distributedfiledeamon/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", ] } -ohos_unittest("deviceManagerAgentTest") { +ohos_unittest("DeviceManagerAgentTest") { module_out_path = module_output_path sources = [ @@ -32,16 +32,23 @@ ohos_unittest("deviceManagerAgentTest") { sources += [ "device/device_manager_agent_test.cpp" ] - configs = [ ":module_private_config" ] + configs = [ + ":module_private_config", + "${utils_path}:compiler_configs" + ] deps = [ - "//third_party/googletest:gtest_main", - "${utils_path}:libdistributedfileutils", - "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//third_party/googletest:gtest_main", + "${utils_path}:libdistributedfileutils", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + ] + + defines = [ + "private=public" ] } group("unittest") { testonly = true - deps = [":deviceManagerAgentTest"] + deps = [":DeviceManagerAgentTest"] } \ No newline at end of file diff --git a/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp b/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp index 55d15c0ab..d32f12c06 100644 --- a/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp +++ b/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp @@ -16,9 +16,12 @@ #include "device/device_manager_agent.h" #include +namespace OHOS { +namespace DistributedFile { +namespace Test { using namespace testing::ext; -class deviceManagerAgentTest : public testing::Test { +class DeviceManagerAgentTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); @@ -26,22 +29,22 @@ public: void TearDown(); }; -void deviceManagerAgentTest::SetUpTestCase(void) +void DeviceManagerAgentTest::SetUpTestCase(void) { // input testsuit setup step,setup invoked before all testcases } -void deviceManagerAgentTest::TearDownTestCase(void) +void DeviceManagerAgentTest::TearDownTestCase(void) { // input testsuit teardown step,teardown invoked after all testcases } -void deviceManagerAgentTest::SetUp(void) +void DeviceManagerAgentTest::SetUp(void) { // input testcase setup step,setup invoked before each testcases } -void deviceManagerAgentTest::TearDown(void) +void DeviceManagerAgentTest::TearDown(void) { // input testcase teardown step,teardown invoked after each testcases } @@ -52,10 +55,13 @@ void deviceManagerAgentTest::TearDown(void) * @tc.type: FUNC * @tc.require: Issue Number */ -HWTEST_F(deviceManagerAgentTest, test_001, TestSize.Level1) +HWTEST_F(DeviceManagerAgentTest, test_001, TestSize.Level1) { // step 1:调用函数获取结果 int actual = 4; // Step 2:使用断言比较预期与实际结果 EXPECT_EQ(4, actual); -} \ No newline at end of file +} +} // namespace Test +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn new file mode 100644 index 000000000..6a3ec1cee --- /dev/null +++ b/test/moduletest/BUILD.gn @@ -0,0 +1,56 @@ +# 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") +import("//foundation/storage/distributed_file_manager/distributedfile.gni") + +module_output_path = "storage_distributed_file_manager/distributedfiledeamon" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "${services_path}/distributedfiledeamon/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + ] +} + +ohos_moduletest("DistributedFileDeamonServiceTest") { + module_out_path = module_output_path + + sources = [ + "${services_path}/distributedfiledeamon/src/mountpoint/mount_point.cpp" + ] + + sources += [ "src/distributedfiledeamon_service_test.cpp" ] + + configs = [ + ":module_private_config", + "${utils_path}:compiler_configs" + ] + + defines = [ + "private=public" + ] + + deps = [ + "//third_party/googletest:gtest_main", + "//third_party/googletest:gmock", + "${utils_path}:libdistributedfileutils", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + ] +} + +group("moduletest") { + testonly = true + deps = [":DistributedFileDeamonServiceTest"] +} \ No newline at end of file diff --git a/test/moduletest/src/distributedfiledeamon_service_test.cpp b/test/moduletest/src/distributedfiledeamon_service_test.cpp new file mode 100644 index 000000000..f4a6fb122 --- /dev/null +++ b/test/moduletest/src/distributedfiledeamon_service_test.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021 XXXX 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 "device/device_manager_agent.h" +#include "mountpoint/mount_point.h" +#include "utils_directory.h" +#include "utils_log.h" +namespace OHOS { +namespace DistributedFile { +namespace Test { +using namespace testing::ext; +using namespace std; + +class DistributedFileDeamonServiceTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DistributedFileDeamonServiceTest::SetUpTestCase(void) +{ + // input testsuit setup step,setup invoked before all testcases +} + +void DistributedFileDeamonServiceTest::TearDownTestCase(void) +{ + // input testsuit teardown step,teardown invoked after all testcases +} + +void DistributedFileDeamonServiceTest::SetUp(void) +{ + // input testcase setup step,setup invoked before each testcases +} + +void DistributedFileDeamonServiceTest::TearDown(void) +{ + // input testcase teardown step,teardown invoked after each testcases +} + +/** + * @tc.name: mount_test_001 + * @tc.desc: Verify the mount/umount function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DistributedFileDeamonServiceTest, mount_umount_test_001, TestSize.Level1) +{ + auto mp = make_unique(OHOS::DistributedFile::Utils::MountArgumentDescriptors::Alpha(9527)); + + shared_ptr smp = move(mp); + + try { + smp->Mount(); + smp->Umount(); + LOGE("testcase run OK"); + } catch (const exception &e) { + LOGE("%{public}s", e.what()); + EXPECT_EQ(0, 1); + } + EXPECT_EQ(0, 0); +} +} // namespace Test +} // namespace DistributedFile +} // namespace OHOS \ No newline at end of file diff --git a/utils/system/include/utils_singleton.h b/utils/system/include/utils_singleton.h index 012f80465..d20817fd4 100644 --- a/utils/system/include/utils_singleton.h +++ b/utils/system/include/utils_singleton.h @@ -34,7 +34,7 @@ private: \ template class Singleton : public NoCopyable { public: - static T &GetInstance(); + static std::shared_ptr GetInstance(); static void StopInstance(); protected: @@ -49,16 +49,25 @@ protected: virtual void Stop() = 0; }; +/** + * @brief + * + * @tparam T + * @return T& + * + * @note We use call_once to ensure the atomicity of new() and start() + * @note Memory leaking of T is exactly what we want. Now T will be available along the program's life-time + */ template -T &Singleton::GetInstance() +std::shared_ptr Singleton::GetInstance() { - static T *instance = nullptr; + static std::shared_ptr *dummy = nullptr; static std::once_flag once; - std::call_once(once, [pinstance{&instance}]() mutable { - (*pinstance) = new T(); - (*pinstance)->Start(); + std::call_once(once, []() mutable { + dummy = new std::shared_ptr(new T()); + (*dummy)->Start(); }); - return *instance; + return *dummy; } template @@ -66,7 +75,7 @@ void Singleton::StopInstance() { static std::once_flag once; std::call_once(once, []() { - auto instance = &GetInstance(); + auto instance = GetInstance(); instance->Stop(); }); } diff --git a/utils/system/src/utils_directory.cpp b/utils/system/src/utils_directory.cpp index 9a1c87dc8..282711f8d 100644 --- a/utils/system/src/utils_directory.cpp +++ b/utils/system/src/utils_directory.cpp @@ -47,12 +47,12 @@ void ForceCreateDirectory(const string &path, function onS } while (index != string::npos); } -void ForceCreateDirectory(const std::string &path) +void ForceCreateDirectory(const string &path) { ForceCreateDirectory(path, nullptr); } -void ForceCreateDirectory(const std::string &path, mode_t mode) +void ForceCreateDirectory(const string &path, mode_t mode) { ForceCreateDirectory(path, [mode](const string &subPath) { if (chmod(subPath.c_str(), mode) == -1) { @@ -61,7 +61,7 @@ void ForceCreateDirectory(const std::string &path, mode_t mode) }); } -void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t gid) +void ForceCreateDirectory(const string &path, mode_t mode, uid_t uid, gid_t gid) { ForceCreateDirectory(path, [mode, uid, gid](const string &subPath) { if (chmod(subPath.c_str(), mode) == -1 || chown(subPath.c_str(), uid, gid) == -1) { @@ -70,7 +70,7 @@ void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t }); } -void ForceRemoveDirectory(const std::string &path) +void ForceRemoveDirectory(const string &path) { if (!OHOS::ForceRemoveDirectory(path)) { throw system_error(errno, system_category()); -- Gitee From 248feda4a88098465981573c4a3b308fac31f5fd Mon Sep 17 00:00:00 2001 From: c00382521 Date: Wed, 3 Nov 2021 16:03:39 +0800 Subject: [PATCH 4/5] modify cfg &&Sync Call disableSessionListener Signed-off-by: c00382521 --- services/distributedfile.cfg | 6 +++++- .../distributedfiledeamon/src/network/kernel_talker.cpp | 1 - .../src/network/softbus/softbus_agent.cpp | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/services/distributedfile.cfg b/services/distributedfile.cfg index 7eef28530..4d9b924c4 100644 --- a/services/distributedfile.cfg +++ b/services/distributedfile.cfg @@ -4,8 +4,12 @@ "cmds": [ "start distributedfiledeamon" ] + },{ + "name" : "init", + "cmds" : [ + "mount tmpfs tmpfs /mnt" + ] }], - # 这里应引入对外部服务的依赖 "services": [{ "name": "distributedfiledeamon", "path": ["/system/bin/sa_main", "/system/profile/distributedfiledeamon.xml"], diff --git a/services/distributedfiledeamon/src/network/kernel_talker.cpp b/services/distributedfiledeamon/src/network/kernel_talker.cpp index 223d92baf..6f54a6c3d 100644 --- a/services/distributedfiledeamon/src/network/kernel_talker.cpp +++ b/services/distributedfiledeamon/src/network/kernel_talker.cpp @@ -114,7 +114,6 @@ void KernelTalker::SinkSessionTokernel(shared_ptr session) if (memcpy_s(cmd.cid, CID_MAX_LEN, cid.c_str(), CID_MAX_LEN)) { return; // !抛异常 } - session->DisableSessionListener(); SetCmd(cmd); } diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp b/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp index 2e882758c..08cd1732d 100644 --- a/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp +++ b/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp @@ -122,6 +122,7 @@ int SoftbusAgent::OnSessionOpened(const int sessionId, const int result) "accept sesion, sessionid:%{public}d, Is %{public}s Side, fd %{public}d, from cid %{public}s, result " "%{public}d", sessionId, (session->IsFromServer() == true) ? "Server" : "Client", socket_fd, cid.c_str(), result); + session->DisableSessionListener(); AcceptSession(session); return 0; } -- Gitee From 35444a1e93c8ba4bf655ed95740b66c26cf1be33 Mon Sep 17 00:00:00 2001 From: c00382521 Date: Wed, 17 Nov 2021 12:16:54 +0800 Subject: [PATCH 5/5] Rename deamon to daemon && Modification of some review comments Signed-off-by: c00382521 --- .../native/{deamon_proxy.h => daemon_proxy.h} | 19 +++++----- ohos.build | 2 +- services/4901.xml | 6 ++-- services/BUILD.gn | 2 +- services/distributedfile.cfg | 6 ++-- .../BUILD.gn | 10 +++--- .../include/device/device_info.h | 12 ++++--- .../include/device/device_manager_agent.h | 8 +++-- .../include/ipc/daemon.h} | 25 +++++++------ .../include/ipc/daemon_stub.h} | 20 ++++++----- .../include/ipc/i_daemon.h} | 16 +++++---- .../include/mountpoint/mount_manager.h | 8 +++-- .../include/mountpoint/mount_point.h | 8 +++-- .../include/network/base_session.h | 8 +++-- .../include/network/kernel_talker.h | 8 +++-- .../include/network/network_agent_template.h | 12 ++++--- .../include/network/session_pool.h | 8 +++-- .../include/network/softbus/softbus_agent.h | 8 +++-- .../include/network/softbus/softbus_session.h | 8 +++-- .../softbus/softbus_session_dispatcher.h | 10 ++++-- .../network/softbus/softbus_session_name.h | 8 +++-- .../src/device/device_info.cpp | 8 +++-- .../src/device/device_manager_agent.cpp | 20 ++++++----- .../src/ipc/daemon.cpp} | 20 ++++++----- .../src/ipc/daemon_stub.cpp} | 12 ++++--- .../src/mountpoint/mount_manager.cpp | 4 ++- .../src/mountpoint/mount_point.cpp | 2 ++ .../src/network/kernel_talker.cpp | 2 ++ .../src/network/network_agent_template.cpp | 6 ++-- .../src/network/session_pool.cpp | 2 ++ .../src/network/softbus/softbus_agent.cpp | 10 +++--- .../src/network/softbus/softbus_session.cpp | 2 ++ .../softbus/softbus_session_dispatcher.cpp | 4 ++- .../test/unittest/BUILD.gn | 4 +-- .../device/device_manager_agent_test.cpp | 2 ++ test/moduletest/BUILD.gn | 12 +++---- .../distributedfiledeamon_service_test.cpp | 14 ++++---- utils/log/include/utils_log.h | 35 +++++++++++-------- utils/log/src/utils_log.cpp | 2 ++ utils/system/include/utils_dfs_thread.h | 8 +++-- utils/system/include/utils_directory.h | 33 +++++++++-------- utils/system/include/utils_mount_argument.h | 8 +++-- utils/system/include/utils_singleton.h | 8 +++-- utils/system/src/utils_directory.cpp | 27 +++++++------- utils/system/src/utils_mount_argument.cpp | 27 +++++++------- 45 files changed, 301 insertions(+), 183 deletions(-) rename interfaces/innerkits/native/{deamon_proxy.h => daemon_proxy.h} (66%) rename services/{distributedfiledeamon => distributedfiledaemon}/BUILD.gn (89%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/device/device_info.h (86%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/device/device_manager_agent.h (93%) rename services/{distributedfiledeamon/include/ipc/deamon.h => distributedfiledaemon/include/ipc/daemon.h} (74%) rename services/{distributedfiledeamon/include/ipc/deamon_stub.h => distributedfiledaemon/include/ipc/daemon_stub.h} (72%) rename services/{distributedfiledeamon/include/ipc/i_deamon.h => distributedfiledaemon/include/ipc/i_daemon.h} (80%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/mountpoint/mount_manager.h (89%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/mountpoint/mount_point.h (90%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/base_session.h (89%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/kernel_talker.h (95%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/network_agent_template.h (91%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/session_pool.h (90%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/softbus/softbus_agent.h (91%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/softbus/softbus_session.h (89%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/softbus/softbus_session_dispatcher.h (88%) rename services/{distributedfiledeamon => distributedfiledaemon}/include/network/softbus/softbus_session_name.h (86%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/device/device_info.cpp (90%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/device/device_manager_agent.cpp (94%) rename services/{distributedfiledeamon/src/ipc/deamon.cpp => distributedfiledaemon/src/ipc/daemon.cpp} (79%) rename services/{distributedfiledeamon/src/ipc/deamon_stub.cpp => distributedfiledaemon/src/ipc/daemon_stub.cpp} (80%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/mountpoint/mount_manager.cpp (97%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/mountpoint/mount_point.cpp (98%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/network/kernel_talker.cpp (99%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/network/network_agent_template.cpp (98%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/network/session_pool.cpp (97%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/network/softbus/softbus_agent.cpp (95%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/network/softbus/softbus_session.cpp (98%) rename services/{distributedfiledeamon => distributedfiledaemon}/src/network/softbus/softbus_session_dispatcher.cpp (98%) rename services/{distributedfiledeamon => distributedfiledaemon}/test/unittest/BUILD.gn (95%) rename services/{distributedfiledeamon => distributedfiledaemon}/test/unittest/device/device_manager_agent_test.cpp (97%) diff --git a/interfaces/innerkits/native/deamon_proxy.h b/interfaces/innerkits/native/daemon_proxy.h similarity index 66% rename from interfaces/innerkits/native/deamon_proxy.h rename to interfaces/innerkits/native/daemon_proxy.h index 953b5bf48..013803ef3 100644 --- a/interfaces/innerkits/native/deamon_proxy.h +++ b/interfaces/innerkits/native/daemon_proxy.h @@ -12,22 +12,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#pragma once +#ifndef DAEMON_PROXY_H +#define DAEMON_PROXY_H #include -#include "i_deamon.h" +#include "i_daemon.h" namespace OHOS { +namespace Storage { namespace DistributedFile { -class DeamonProxy : public IRemoteProxy { +class DaemonProxy : public IRemoteProxy { public: - explicit DeamonProxy(const sptr &impl) : IRemoteProxy(impl) {} - ~DeamonProxy() {} + explicit DaemonProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~DaemonProxy() {} private: - static inline BrokerDelegator delegator_; + static inline BrokerDelegator delegator_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // DAEMON_PROXY_H \ No newline at end of file diff --git a/ohos.build b/ohos.build index 54ebf6425..575dc9bba 100755 --- a/ohos.build +++ b/ohos.build @@ -10,7 +10,7 @@ "//foundation/storage/distributed_file_manager/services/:services_target" ], "test_list": [ - "//foundation/storage/distributed_file_manager/services/distributedfiledeamon/test/unittest:unittest", + "//foundation/storage/distributed_file_manager/services/distributedfiledaemon/test/unittest:unittest", "//foundation/storage/distributed_file_manager/test/moduletest:moduletest" ] } diff --git a/services/4901.xml b/services/4901.xml index b499a9843..7d140a704 100755 --- a/services/4901.xml +++ b/services/4901.xml @@ -14,13 +14,13 @@ * limitations under the License. --> - distributedfiledeamon + distributedfiledaemon - libdistributedfiledeamon.z.so + libdistributedfiledaemon.z.so 4901 - libdistributedfiledeamon.z.so + libdistributedfiledaemon.z.so true diff --git a/services/BUILD.gn b/services/BUILD.gn index 6c6dc669f..91e21af8e 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -30,6 +30,6 @@ group("services_target") { deps = [ ":distributedfile_etc", ":distributedfile_sa_profile", - "distributedfiledeamon:distributedfiledeamon_target", + "distributedfiledaemon:distributedfiledaemon_target", ] } diff --git a/services/distributedfile.cfg b/services/distributedfile.cfg index 4d9b924c4..bc21a7068 100644 --- a/services/distributedfile.cfg +++ b/services/distributedfile.cfg @@ -2,7 +2,7 @@ "jobs": [{ "name": "boot", "cmds": [ - "start distributedfiledeamon" + "start distributedfiledaemon" ] },{ "name" : "init", @@ -11,8 +11,8 @@ ] }], "services": [{ - "name": "distributedfiledeamon", - "path": ["/system/bin/sa_main", "/system/profile/distributedfiledeamon.xml"], + "name": "distributedfiledaemon", + "path": ["/system/bin/sa_main", "/system/profile/distributedfiledaemon.xml"], "uid": "root", "gid": ["root", "system", "shell", "readproc", "media_rw", "inet"], "caps": ["SYS_PTRACE", "KILL"] diff --git a/services/distributedfiledeamon/BUILD.gn b/services/distributedfiledaemon/BUILD.gn similarity index 89% rename from services/distributedfiledeamon/BUILD.gn rename to services/distributedfiledaemon/BUILD.gn index 27f659ae1..c18dc7558 100755 --- a/services/distributedfiledeamon/BUILD.gn +++ b/services/distributedfiledaemon/BUILD.gn @@ -13,15 +13,15 @@ import("//build/ohos.gni") import("//foundation/storage/distributed_file_manager/distributedfile.gni") -ohos_shared_library("libdistributedfiledeamon") { +ohos_shared_library("libdistributedfiledaemon") { include_dirs = [ "include", "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", ] sources = [ - "src/ipc/deamon.cpp", - "src/ipc/deamon_stub.cpp", + "src/ipc/daemon.cpp", + "src/ipc/daemon_stub.cpp", "src/mountpoint/mount_manager.cpp", "src/mountpoint/mount_point.cpp", "src/device/device_info.cpp", @@ -49,6 +49,6 @@ ohos_shared_library("libdistributedfiledeamon") { subsystem_name = "storage" } -group("distributedfiledeamon_target") { - deps = [ ":libdistributedfiledeamon" ] +group("distributedfiledaemon_target") { + deps = [ ":libdistributedfiledaemon" ] } diff --git a/services/distributedfiledeamon/include/device/device_info.h b/services/distributedfiledaemon/include/device/device_info.h similarity index 86% rename from services/distributedfiledeamon/include/device/device_info.h rename to services/distributedfiledaemon/include/device/device_info.h index f0b591785..2a340d56c 100644 --- a/services/distributedfiledeamon/include/device/device_info.h +++ b/services/distributedfiledaemon/include/device/device_info.h @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once +#ifndef DEVICE_INFO_H +#define DEVICE_INFO_H #include #include @@ -20,6 +21,7 @@ #include "dm_device_info.h" namespace OHOS { +namespace Storage { namespace DistributedFile { class DeviceInfo final { public: @@ -32,8 +34,8 @@ public: /** * @note Remove the concept iid later */ - void SetIid(uint64_t iid); - void SetCid(const std::string cid); + void SetIid(const uint64_t iid); + void SetCid(const std::string &cid); uint64_t GetIid() const; const std::string &GetCid() const; @@ -46,4 +48,6 @@ private: std::string cid_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // DEVICE_INFO_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h similarity index 93% rename from services/distributedfiledeamon/include/device/device_manager_agent.h rename to services/distributedfiledaemon/include/device/device_manager_agent.h index cf135b2a3..aa3948fb2 100644 --- a/services/distributedfiledeamon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once +#ifndef DEVICE_MANAGER_AGENT_H +#define DEVICE_MANAGER_AGENT_H #include #include @@ -26,6 +27,7 @@ #include "utils_singleton.h" namespace OHOS { +namespace Storage { namespace DistributedFile { class DeviceManagerAgent final : public DistributedHardware::DmInitCallback, public DistributedHardware::DeviceStateCallback, @@ -61,4 +63,6 @@ private: DeviceInfo localDeviceInfo_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // DEVICE_MANAGER_AGENT_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/ipc/deamon.h b/services/distributedfiledaemon/include/ipc/daemon.h similarity index 74% rename from services/distributedfiledeamon/include/ipc/deamon.h rename to services/distributedfiledaemon/include/ipc/daemon.h index abdd3a2a4..986cc7a3f 100644 --- a/services/distributedfiledeamon/include/ipc/deamon.h +++ b/services/distributedfiledaemon/include/ipc/daemon.h @@ -13,26 +13,27 @@ * limitations under the License. */ -#pragma once - -#include "deamon_stub.h" +#ifndef DAEMON_H +#define DAEMON_H #include -#include "i_deamon.h" +#include "daemon_stub.h" +#include "i_daemon.h" #include "iremote_stub.h" #include "system_ability.h" namespace OHOS { +namespace Storage { namespace DistributedFile { enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; -class Deamon : public SystemAbility, public DeamonStub, protected NoCopyable { - DECLARE_SYSTEM_ABILITY(Deamon); +class Daemon final: public SystemAbility, public DaemonStub, protected NoCopyable { + DECLARE_SYSTEM_ABILITY(Daemon); public: - explicit Deamon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate){}; - ~Deamon() = default; + explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate){}; + virtual ~Daemon() = default; void OnStart() override; void OnStop() override; @@ -44,9 +45,9 @@ public: int32_t EchoServerDemo(const std::string &echoStr) override; private: - Deamon(); + Daemon(); ServiceRunningState state_{ServiceRunningState::STATE_NOT_START}; - static sptr instance_; + static sptr instance_; static std::mutex instanceLock_; bool registerToService_{false}; @@ -54,4 +55,6 @@ private: void StartManagers(); }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // DAEMON_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/ipc/deamon_stub.h b/services/distributedfiledaemon/include/ipc/daemon_stub.h similarity index 72% rename from services/distributedfiledeamon/include/ipc/deamon_stub.h rename to services/distributedfiledaemon/include/ipc/daemon_stub.h index f408e9410..7ebf25ecd 100644 --- a/services/distributedfiledeamon/include/ipc/deamon_stub.h +++ b/services/distributedfiledaemon/include/ipc/daemon_stub.h @@ -12,26 +12,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once +#ifndef DAEMON_STUB_H +#define DAEMON_STUB_H #include -#include "i_deamon.h" +#include "i_daemon.h" #include "iremote_stub.h" namespace OHOS { +namespace Storage { namespace DistributedFile { -class DeamonStub : public IRemoteStub { +class DaemonStub : public IRemoteStub { public: - DeamonStub(); - ~DeamonStub() = default; + DaemonStub(); + virtual ~DaemonStub() = default; int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; private: - using DeamonInterface = int32_t (DeamonStub::*)(MessageParcel &data, MessageParcel &reply); - std::map opToInterfaceMap_; + using DaemonInterface = int32_t (DaemonStub::*)(MessageParcel &data, MessageParcel &reply); + std::map opToInterfaceMap_; int32_t EchoServerDemoInner(MessageParcel &data, MessageParcel &reply); }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // DAEMON_STUB_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/ipc/i_deamon.h b/services/distributedfiledaemon/include/ipc/i_daemon.h similarity index 80% rename from services/distributedfiledeamon/include/ipc/i_deamon.h rename to services/distributedfiledaemon/include/ipc/i_daemon.h index f4d62aef6..fce520673 100644 --- a/services/distributedfiledeamon/include/ipc/i_deamon.h +++ b/services/distributedfiledaemon/include/ipc/i_daemon.h @@ -13,21 +13,25 @@ * limitations under the License. */ -#pragma once +#ifndef I_DAEMON_H +#define I_DAEMON_H #include "iremote_broker.h" namespace OHOS { +namespace Storage { namespace DistributedFile { -class IDeamon : public IRemoteBroker { +class IDaemon : public IRemoteBroker { public: enum { - DFS_DEAMON_CMD_ECHO = 0, + DFS_DAEMON_CMD_ECHO = 0, }; virtual int32_t EchoServerDemo(const std::string &echoStr) = 0; - static inline const std::string SERVICE_NAME{"ohos.storage.distributedfile.deamon"}; - DECLARE_INTERFACE_DESCRIPTOR(u"ohos.storage.distributedfile.deamon") + static inline const std::string SERVICE_NAME{"ohos.storage.distributedfile.daemon"}; + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.storage.distributedfile.daemon") }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // I_DAEMON_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/mountpoint/mount_manager.h b/services/distributedfiledaemon/include/mountpoint/mount_manager.h similarity index 89% rename from services/distributedfiledeamon/include/mountpoint/mount_manager.h rename to services/distributedfiledaemon/include/mountpoint/mount_manager.h index 97db5fcc9..41b015650 100644 --- a/services/distributedfiledeamon/include/mountpoint/mount_manager.h +++ b/services/distributedfiledaemon/include/mountpoint/mount_manager.h @@ -13,7 +13,8 @@ * limitations under the License. */ -#pragma once +#ifndef MOUNT_MANAGER_H +#define MOUNT_MANAGER_H #include #include @@ -23,6 +24,7 @@ #include "utils_singleton.h" namespace OHOS { +namespace Storage { namespace DistributedFile { class MountManager final : public Utils::Singleton { public: @@ -39,4 +41,6 @@ private: std::vector> mountPoints_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // MOUNT_MANAGER_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/mountpoint/mount_point.h b/services/distributedfiledaemon/include/mountpoint/mount_point.h similarity index 90% rename from services/distributedfiledeamon/include/mountpoint/mount_point.h rename to services/distributedfiledaemon/include/mountpoint/mount_point.h index d813b04b9..59019a953 100644 --- a/services/distributedfiledeamon/include/mountpoint/mount_point.h +++ b/services/distributedfiledaemon/include/mountpoint/mount_point.h @@ -13,7 +13,8 @@ * limitations under the License. */ -#pragma once +#ifndef MOUNT_POINT_H +#define MOUNT_POINT_H #include #include @@ -22,6 +23,7 @@ #include "utils_mount_argument.h" namespace OHOS { +namespace Storage { namespace DistributedFile { class MountPoint final : public NoCopyable { public: @@ -46,4 +48,6 @@ private: uint32_t id_{0}; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // MOUNT_POINT_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/base_session.h b/services/distributedfiledaemon/include/network/base_session.h similarity index 89% rename from services/distributedfiledeamon/include/network/base_session.h rename to services/distributedfiledaemon/include/network/base_session.h index d12aed2d3..db1f3aea3 100644 --- a/services/distributedfiledeamon/include/network/base_session.h +++ b/services/distributedfiledaemon/include/network/base_session.h @@ -13,11 +13,13 @@ * limitations under the License. */ -#pragma once +#ifndef BASE_SESSION_H +#define BASE_SESSION_H #include namespace OHOS { +namespace Storage { namespace DistributedFile { constexpr int KEY_SIZE_MAX = 32; @@ -32,4 +34,6 @@ public: virtual void DisableSessionListener() const = 0; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // BASE_SESSION_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/kernel_talker.h b/services/distributedfiledaemon/include/network/kernel_talker.h similarity index 95% rename from services/distributedfiledeamon/include/network/kernel_talker.h rename to services/distributedfiledaemon/include/network/kernel_talker.h index de1eadf3a..c33a95ffa 100644 --- a/services/distributedfiledeamon/include/network/kernel_talker.h +++ b/services/distributedfiledaemon/include/network/kernel_talker.h @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once +#ifndef KERNEL_TALKER_H +#define KERNEL_TALKER_H #include #include @@ -29,6 +30,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { constexpr int CID_MAX_LEN = 64; struct NotifyParam { @@ -95,4 +97,6 @@ private: std::function GetSessionCallback_{nullptr}; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // KERNEL_TALKER_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/network_agent_template.h b/services/distributedfiledaemon/include/network/network_agent_template.h similarity index 91% rename from services/distributedfiledeamon/include/network/network_agent_template.h rename to services/distributedfiledaemon/include/network/network_agent_template.h index a37d25428..0f9485fae 100644 --- a/services/distributedfiledeamon/include/network/network_agent_template.h +++ b/services/distributedfiledaemon/include/network/network_agent_template.h @@ -13,20 +13,22 @@ * limitations under the License. */ -#pragma once +#ifndef NETWORK_AGENT_TEMPLATE_H +#define NETWORK_AGENT_TEMPLATE_H #include #include #include #include -#include "utils_dfs_thread.h" #include "device/device_info.h" #include "mountpoint/mount_point.h" #include "network/kernel_talker.h" #include "network/session_pool.h" +#include "utils_dfs_thread.h" namespace OHOS { +namespace Storage { namespace DistributedFile { class NetworkAgentTemplate { public: @@ -36,7 +38,7 @@ public: sessionPool_(kernerlTalker_) { } - virtual ~NetworkAgentTemplate(); + virtual ~NetworkAgentTemplate() {} void Start(); void Stop(); void ConnectOnlineDevices(); @@ -66,4 +68,6 @@ private: SessionPool sessionPool_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // NETWORK_AGENT_TEMPLATE_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/session_pool.h b/services/distributedfiledaemon/include/network/session_pool.h similarity index 90% rename from services/distributedfiledeamon/include/network/session_pool.h rename to services/distributedfiledaemon/include/network/session_pool.h index f1a65db3a..52226b186 100644 --- a/services/distributedfiledeamon/include/network/session_pool.h +++ b/services/distributedfiledaemon/include/network/session_pool.h @@ -13,7 +13,8 @@ * limitations under the License. */ -#pragma once +#ifndef SESSION_POOL_H +#define SESSION_POOL_H #include #include @@ -24,6 +25,7 @@ #include "network/kernel_talker.h" namespace OHOS { +namespace Storage { namespace DistributedFile { class SessionPool final : protected NoCopyable { public: @@ -40,4 +42,6 @@ private: void AddSessionToPool(std::shared_ptr session); }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // SESSION_POOL_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_agent.h b/services/distributedfiledaemon/include/network/softbus/softbus_agent.h similarity index 91% rename from services/distributedfiledeamon/include/network/softbus/softbus_agent.h rename to services/distributedfiledaemon/include/network/softbus/softbus_agent.h index 392c40113..c3065aff3 100644 --- a/services/distributedfiledeamon/include/network/softbus/softbus_agent.h +++ b/services/distributedfiledaemon/include/network/softbus/softbus_agent.h @@ -13,12 +13,14 @@ * limitations under the License. */ -#pragma once +#ifndef SOFTBUS_AGENT_H +#define SOFTBUS_AGENT_H #include "network/network_agent_template.h" #include namespace OHOS { +namespace Storage { namespace DistributedFile { class SoftbusAgent final : public NetworkAgentTemplate, public std::enable_shared_from_this { public: @@ -39,4 +41,6 @@ private: std::string sessionName_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // SOFTBUS_AGENT_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_session.h b/services/distributedfiledaemon/include/network/softbus/softbus_session.h similarity index 89% rename from services/distributedfiledeamon/include/network/softbus/softbus_session.h rename to services/distributedfiledaemon/include/network/softbus/softbus_session.h index 554d05004..0d6d30eef 100644 --- a/services/distributedfiledeamon/include/network/softbus/softbus_session.h +++ b/services/distributedfiledaemon/include/network/softbus/softbus_session.h @@ -13,11 +13,13 @@ * limitations under the License. */ -#pragma once +#ifndef SOFTBUS_SESSION_H +#define SOFTBUS_SESSION_H #include "network/base_session.h" namespace OHOS { +namespace Storage { namespace DistributedFile { class SoftbusSession final : public BaseSession { public: @@ -34,4 +36,6 @@ private: int sessionId_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // SOFTBUS_SESSION_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_session_dispatcher.h b/services/distributedfiledaemon/include/network/softbus/softbus_session_dispatcher.h similarity index 88% rename from services/distributedfiledeamon/include/network/softbus/softbus_session_dispatcher.h rename to services/distributedfiledaemon/include/network/softbus/softbus_session_dispatcher.h index dbf12c48f..fc7e1d9cd 100644 --- a/services/distributedfiledeamon/include/network/softbus/softbus_session_dispatcher.h +++ b/services/distributedfiledaemon/include/network/softbus/softbus_session_dispatcher.h @@ -13,7 +13,8 @@ * limitations under the License. */ -#pragma once +#ifndef SOFTBUS_SESSION_DISPATCHER_H +#define SOFTBUS_SESSION_DISPATCHER_H #include #include @@ -21,6 +22,7 @@ #include namespace OHOS { +namespace Storage { namespace DistributedFile { class SoftbusAgent; class SoftbusSessionDispatcher final { @@ -28,7 +30,7 @@ public: SoftbusSessionDispatcher() = delete; ~SoftbusSessionDispatcher() = delete; static void RegisterSessionListener(const std::string busName, std::weak_ptr); - static void UnregisterSessionListener(const std::string busName, std::weak_ptr); + static void UnregisterSessionListener(const std::string busName); static std::weak_ptr GetAgent(int sessionId); static int OnSessionOpened(int sessionId, int result); static void OnSessionClosed(int sessionId); @@ -38,4 +40,6 @@ private: static std::map> busNameToAgent_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // SOFTBUS_SESSION_DISPATCHER_H \ No newline at end of file diff --git a/services/distributedfiledeamon/include/network/softbus/softbus_session_name.h b/services/distributedfiledaemon/include/network/softbus/softbus_session_name.h similarity index 86% rename from services/distributedfiledeamon/include/network/softbus/softbus_session_name.h rename to services/distributedfiledaemon/include/network/softbus/softbus_session_name.h index c7c04149f..87d3b4de3 100644 --- a/services/distributedfiledeamon/include/network/softbus/softbus_session_name.h +++ b/services/distributedfiledaemon/include/network/softbus/softbus_session_name.h @@ -13,11 +13,13 @@ * limitations under the License. */ -#pragma once +#ifndef SOFTBUS_SESSION_NAME_H +#define SOFTBUS_SESSION_NAME_H #include namespace OHOS { +namespace Storage { namespace DistributedFile { class SoftbusSessionName final { public: @@ -33,4 +35,6 @@ private: std::string path_; }; } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // SOFTBUS_SESSION_NAME_H \ No newline at end of file diff --git a/services/distributedfiledeamon/src/device/device_info.cpp b/services/distributedfiledaemon/src/device/device_info.cpp similarity index 90% rename from services/distributedfiledeamon/src/device/device_info.cpp rename to services/distributedfiledaemon/src/device/device_info.cpp index 7805d9938..293ece7b7 100644 --- a/services/distributedfiledeamon/src/device/device_info.cpp +++ b/services/distributedfiledaemon/src/device/device_info.cpp @@ -17,6 +17,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -37,7 +38,7 @@ DeviceInfo::DeviceInfo(const DeviceInfo &nodeInfo) : iid_(nodeInfo.iid_), cid_(n initCidFlag_.store(nodeInfo.initCidFlag_.load()); } -void DeviceInfo::SetIid(uint64_t iid) +void DeviceInfo::SetIid(const uint64_t iid) { if (initIidFlag_ == false) { iid_ = iid; @@ -47,13 +48,13 @@ void DeviceInfo::SetIid(uint64_t iid) } } -void DeviceInfo::SetCid(const string cid) +void DeviceInfo::SetCid(const string &cid) { if (initCidFlag_ == false) { cid_ = cid; initCidFlag_ = true; } else { - LOGI("Iid is already initializing"); + LOGI("Cid is already initializing"); } } @@ -72,4 +73,5 @@ const string &DeviceInfo::GetCid() const return cid_; } } // namespace DistributedFile +} // namespace Storages } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp similarity index 94% rename from services/distributedfiledeamon/src/device/device_manager_agent.cpp rename to services/distributedfiledaemon/src/device/device_manager_agent.cpp index f6f372ab8..84e57a38e 100644 --- a/services/distributedfiledeamon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -19,11 +19,12 @@ #include #include -#include "ipc/i_deamon.h" +#include "ipc/i_daemon.h" #include "softbus_bus_center.h" #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -120,7 +121,7 @@ void DeviceManagerAgent::OnDeviceChanged(const DistributedHardware::DmDeviceInfo void DeviceManagerAgent::InitLocalNodeInfo() { NodeBasicInfo tmpNodeInfo; - int errCode = GetLocalNodeDeviceInfo(IDeamon::SERVICE_NAME.c_str(), &tmpNodeInfo); + int errCode = GetLocalNodeDeviceInfo(IDaemon::SERVICE_NAME.c_str(), &tmpNodeInfo); if (errCode != 0) { stringstream ss; ss << "Failed to get local cid: error code reads " << errCode; @@ -150,7 +151,7 @@ DeviceInfo &DeviceManagerAgent::GetLocalDeviceInfo() vector DeviceManagerAgent::GetRemoteDevicesInfo() { string extra = ""; - string pkgName = IDeamon::SERVICE_NAME; + string pkgName = IDaemon::SERVICE_NAME; vector deviceList; auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); @@ -172,9 +173,9 @@ vector DeviceManagerAgent::GetRemoteDevicesInfo() void DeviceManagerAgent::RegisterToExternalDm() { auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); - string pkgName = IDeamon::SERVICE_NAME; + string pkgName = IDaemon::SERVICE_NAME; int errCode = deviceManager.InitDeviceManager(pkgName, shared_from_this()); - if (errCode) { + if (errCode != 0) { stringstream ss; ss << "Failed to InitDeviceManager: the error code reads " << errCode; LOGE("%s", ss.str().c_str()); @@ -182,7 +183,7 @@ void DeviceManagerAgent::RegisterToExternalDm() } string extra = ""; errCode = deviceManager.RegisterDevStateCallback(pkgName, extra, shared_from_this()); - if (errCode) { + if (errCode != 0) { stringstream ss; ss << "Failed to RegisterDevStateCallback: the error code reads " << errCode; LOGE("%s", ss.str().c_str()); @@ -193,16 +194,16 @@ void DeviceManagerAgent::RegisterToExternalDm() void DeviceManagerAgent::UnregisterFromExternalDm() { auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); - string pkgName = IDeamon::SERVICE_NAME; + string pkgName = IDaemon::SERVICE_NAME; int errCode = deviceManager.UnRegisterDevStateCallback(pkgName); - if (errCode) { + if (errCode != 0) { stringstream ss; ss << "Failed to UnRegisterDevStateCallback: the error code reads " << errCode; LOGE("%s", ss.str().c_str()); throw runtime_error(ss.str()); } errCode = deviceManager.UnInitDeviceManager(pkgName); - if (errCode) { + if (errCode != 0) { stringstream ss; ss << "Failed to UnInitDeviceManager: the error code reads " << errCode; LOGE("%s", ss.str().c_str()); @@ -210,4 +211,5 @@ void DeviceManagerAgent::UnregisterFromExternalDm() } } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/ipc/deamon.cpp b/services/distributedfiledaemon/src/ipc/daemon.cpp similarity index 79% rename from services/distributedfiledeamon/src/ipc/deamon.cpp rename to services/distributedfiledaemon/src/ipc/daemon.cpp index ce8ad5d69..2db0236d3 100644 --- a/services/distributedfiledeamon/src/ipc/deamon.cpp +++ b/services/distributedfiledaemon/src/ipc/daemon.cpp @@ -13,41 +13,42 @@ * limitations under the License. */ -#include "ipc/deamon.h" +#include "ipc/daemon.h" #include "mountpoint/mount_manager.h" #include "system_ability_definition.h" #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; -REGISTER_SYSTEM_ABILITY_BY_ID(Deamon, STORAGE_DISTRIBUTED_FILE_DEAMON_SA_ID, true); +REGISTER_SYSTEM_ABILITY_BY_ID(Daemon, STORAGE_DISTRIBUTED_FILE_DAEMON_SA_ID, true); -void Deamon::PublishSA() +void Daemon::PublishSA() { LOGI("Begin to init"); if (!registerToService_) { bool ret = SystemAbility::Publish(this); if (!ret) { - throw runtime_error("Failed to publish the deamon"); + throw runtime_error("Failed to publish the daemon"); } registerToService_ = true; } LOGI("Init finished successfully"); } -void Deamon::StartManagers() +void Daemon::StartManagers() { MountManager::GetInstance(); } -void Deamon::OnStart() +void Daemon::OnStart() { LOGI("Begin to start service"); if (state_ == ServiceRunningState::STATE_RUNNING) { - LOGD("Deamon has already started"); + LOGD("Daemon has already started"); return; } @@ -62,7 +63,7 @@ void Deamon::OnStart() LOGI("Start service successfully"); } -void Deamon::OnStop() +void Daemon::OnStop() { LOGI("Begin to stop"); state_ = ServiceRunningState::STATE_NOT_START; @@ -70,9 +71,10 @@ void Deamon::OnStop() LOGI("Stop finished successfully"); } -int32_t Deamon::EchoServerDemo(const string &echoStr) +int32_t Daemon::EchoServerDemo(const string &echoStr) { return 0; } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/ipc/deamon_stub.cpp b/services/distributedfiledaemon/src/ipc/daemon_stub.cpp similarity index 80% rename from services/distributedfiledeamon/src/ipc/deamon_stub.cpp rename to services/distributedfiledaemon/src/ipc/daemon_stub.cpp index 3698ec3d9..b05455f02 100644 --- a/services/distributedfiledeamon/src/ipc/deamon_stub.cpp +++ b/services/distributedfiledaemon/src/ipc/daemon_stub.cpp @@ -13,17 +13,18 @@ * limitations under the License. */ -#include "ipc/deamon_stub.h" +#include "ipc/daemon_stub.h" #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { -DeamonStub::DeamonStub() +DaemonStub::DaemonStub() { - opToInterfaceMap_[DFS_DEAMON_CMD_ECHO] = &DeamonStub::EchoServerDemoInner; + opToInterfaceMap_[DFS_DAEMON_CMD_ECHO] = &DaemonStub::EchoServerDemoInner; } -int32_t DeamonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +int32_t DaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { auto interfaceIndex = opToInterfaceMap_.find(code); if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) { @@ -33,10 +34,11 @@ int32_t DeamonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageP return (this->*(interfaceIndex->second))(data, reply); } -int32_t DeamonStub::EchoServerDemoInner(MessageParcel &data, MessageParcel &reply) +int32_t DaemonStub::EchoServerDemoInner(MessageParcel &data, MessageParcel &reply) { LOGI("hello, world"); return ERR_NONE; } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp b/services/distributedfiledaemon/src/mountpoint/mount_manager.cpp similarity index 97% rename from services/distributedfiledeamon/src/mountpoint/mount_manager.cpp rename to services/distributedfiledaemon/src/mountpoint/mount_manager.cpp index 7f2287da4..b0197e068 100644 --- a/services/distributedfiledeamon/src/mountpoint/mount_manager.cpp +++ b/services/distributedfiledaemon/src/mountpoint/mount_manager.cpp @@ -23,6 +23,7 @@ #include "utils_mount_argument.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -79,4 +80,5 @@ void MountManager::Umount(weak_ptr wmp) mountPoints_.erase(it); } } // namespace DistributedFile -} // namespace OHOS +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/mountpoint/mount_point.cpp b/services/distributedfiledaemon/src/mountpoint/mount_point.cpp similarity index 98% rename from services/distributedfiledeamon/src/mountpoint/mount_point.cpp rename to services/distributedfiledaemon/src/mountpoint/mount_point.cpp index 8506db207..b77e02ad5 100644 --- a/services/distributedfiledeamon/src/mountpoint/mount_point.cpp +++ b/services/distributedfiledaemon/src/mountpoint/mount_point.cpp @@ -22,6 +22,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -82,4 +83,5 @@ string MountPoint::ToString() const return ""; } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/kernel_talker.cpp b/services/distributedfiledaemon/src/network/kernel_talker.cpp similarity index 99% rename from services/distributedfiledeamon/src/network/kernel_talker.cpp rename to services/distributedfiledaemon/src/network/kernel_talker.cpp index 6f54a6c3d..acb5e42f1 100644 --- a/services/distributedfiledeamon/src/network/kernel_talker.cpp +++ b/services/distributedfiledaemon/src/network/kernel_talker.cpp @@ -19,6 +19,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -255,4 +256,5 @@ void KernelTalker::NotifyHandler(NotifyParam ¶m) } } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/network_agent_template.cpp b/services/distributedfiledaemon/src/network/network_agent_template.cpp similarity index 98% rename from services/distributedfiledeamon/src/network/network_agent_template.cpp rename to services/distributedfiledaemon/src/network/network_agent_template.cpp index 7515e1aca..3a2ed16b4 100644 --- a/services/distributedfiledeamon/src/network/network_agent_template.cpp +++ b/services/distributedfiledaemon/src/network/network_agent_template.cpp @@ -18,14 +18,13 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; namespace { constexpr int MAX_RETRY_COUNT = 7; constexpr int SLEEP_TIME = 1500; -} // namespace - -NetworkAgentTemplate::~NetworkAgentTemplate() {} +} void NetworkAgentTemplate::Start() { @@ -108,4 +107,5 @@ void NetworkAgentTemplate::GetSesion(const string &cid) LOGI("open session success, cid:%{public}s", cid.c_str()); } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/session_pool.cpp b/services/distributedfiledaemon/src/network/session_pool.cpp similarity index 97% rename from services/distributedfiledeamon/src/network/session_pool.cpp rename to services/distributedfiledaemon/src/network/session_pool.cpp index bd792fcfe..6824636dd 100644 --- a/services/distributedfiledeamon/src/network/session_pool.cpp +++ b/services/distributedfiledaemon/src/network/session_pool.cpp @@ -16,6 +16,7 @@ #include "network/session_pool.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -46,4 +47,5 @@ void SessionPool::AddSessionToPool(shared_ptr session) usrSpaceSessionPool_.push_back(session); } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_agent.cpp similarity index 95% rename from services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp rename to services/distributedfiledaemon/src/network/softbus/softbus_agent.cpp index 08cd1732d..59d988ac4 100644 --- a/services/distributedfiledeamon/src/network/softbus/softbus_agent.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_agent.cpp @@ -16,7 +16,7 @@ #include -#include "ipc/i_deamon.h" +#include "ipc/i_daemon.h" #include "network/softbus/softbus_session.h" #include "network/softbus/softbus_session_dispatcher.h" #include "network/softbus/softbus_session_name.h" @@ -24,6 +24,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -52,7 +53,7 @@ void SoftbusAgent::JoinDomain() }; SoftbusSessionDispatcher::RegisterSessionListener(sessionName_, shared_from_this()); - int ret = ::CreateSessionServer(IDeamon::SERVICE_NAME.c_str(), sessionName_.c_str(), &sessionListener); + int ret = ::CreateSessionServer(IDaemon::SERVICE_NAME.c_str(), sessionName_.c_str(), &sessionListener); if (ret != 0) { // ! 错误码 stringstream ss; ss << "Failed to CreateSessionServer, errno:" << ret; @@ -64,7 +65,7 @@ void SoftbusAgent::JoinDomain() void SoftbusAgent::QuitDomain() { - int ret = ::RemoveSessionServer(IDeamon::SERVICE_NAME.c_str(), sessionName_.c_str()); + int ret = ::RemoveSessionServer(IDaemon::SERVICE_NAME.c_str(), sessionName_.c_str()); if (ret != 0) { // ! 错误码 stringstream ss; ss << "Failed to RemoveSessionServer, errno:" << ret; @@ -72,7 +73,7 @@ void SoftbusAgent::QuitDomain() throw runtime_error(ss.str()); } - SoftbusSessionDispatcher::UnregisterSessionListener(sessionName_.c_str(), shared_from_this()); + SoftbusSessionDispatcher::UnregisterSessionListener(sessionName_.c_str()); LOGD("Succeed to QuitDomain, busName:%{public}s", sessionName_.c_str()); } void SoftbusAgent::StopTopHalf() @@ -135,4 +136,5 @@ void SoftbusAgent::OnSessionClosed(int sessionId) (session->IsFromServer() == true) ? "Server" : "Client"); } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_session.cpp similarity index 98% rename from services/distributedfiledeamon/src/network/softbus/softbus_session.cpp rename to services/distributedfiledaemon/src/network/softbus/softbus_session.cpp index 41153f00e..92264a0a7 100644 --- a/services/distributedfiledeamon/src/network/softbus/softbus_session.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_session.cpp @@ -20,6 +20,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -79,4 +80,5 @@ void SoftbusSession::DisableSessionListener() const } } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_session_dispatcher.cpp similarity index 98% rename from services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp rename to services/distributedfiledaemon/src/network/softbus/softbus_session_dispatcher.cpp index 493749335..d1c1faf42 100644 --- a/services/distributedfiledeamon/src/network/softbus/softbus_session_dispatcher.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_session_dispatcher.cpp @@ -22,6 +22,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { using namespace std; @@ -51,7 +52,7 @@ void SoftbusSessionDispatcher::RegisterSessionListener(const string busName, } LOGD("RegisterSessionListener SUCCESS, busName:%{public}s", busName.c_str()); } -void SoftbusSessionDispatcher::UnregisterSessionListener(const string busName, weak_ptr) +void SoftbusSessionDispatcher::UnregisterSessionListener(const string busName) { lock_guard lock(softbusAgentMutex_); auto agent = busNameToAgent_.find(busName); @@ -101,4 +102,5 @@ void SoftbusSessionDispatcher::OnSessionClosed(int sessionId) } } } // namespace DistributedFile +} // namespace Storages } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledeamon/test/unittest/BUILD.gn b/services/distributedfiledaemon/test/unittest/BUILD.gn similarity index 95% rename from services/distributedfiledeamon/test/unittest/BUILD.gn rename to services/distributedfiledaemon/test/unittest/BUILD.gn index 19b7a0d78..e6df95ed9 100644 --- a/services/distributedfiledeamon/test/unittest/BUILD.gn +++ b/services/distributedfiledaemon/test/unittest/BUILD.gn @@ -13,13 +13,13 @@ import("//build/test.gni") import("//foundation/storage/distributed_file_manager/distributedfile.gni") -module_output_path = "storage_distributed_file_manager/distributedfiledeamon" +module_output_path = "storage_distributed_file_manager/distributedfiledaemon" config("module_private_config") { visibility = [ ":*" ] include_dirs = [ - "${services_path}/distributedfiledeamon/include", + "${services_path}/distributedfiledaemon/include", "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", ] } diff --git a/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp b/services/distributedfiledaemon/test/unittest/device/device_manager_agent_test.cpp similarity index 97% rename from services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp rename to services/distributedfiledaemon/test/unittest/device/device_manager_agent_test.cpp index d32f12c06..20613477f 100644 --- a/services/distributedfiledeamon/test/unittest/device/device_manager_agent_test.cpp +++ b/services/distributedfiledaemon/test/unittest/device/device_manager_agent_test.cpp @@ -17,6 +17,7 @@ #include namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Test { using namespace testing::ext; @@ -64,4 +65,5 @@ HWTEST_F(DeviceManagerAgentTest, test_001, TestSize.Level1) } } // namespace Test } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 6a3ec1cee..26350e2a5 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -13,25 +13,25 @@ import("//build/test.gni") import("//foundation/storage/distributed_file_manager/distributedfile.gni") -module_output_path = "storage_distributed_file_manager/distributedfiledeamon" +module_output_path = "storage_distributed_file_manager/distributedfiledaemon" config("module_private_config") { visibility = [ ":*" ] include_dirs = [ - "${services_path}/distributedfiledeamon/include", + "${services_path}/distributedfiledaemon/include", "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", ] } -ohos_moduletest("DistributedFileDeamonServiceTest") { +ohos_moduletest("DistributedFileDaemonServiceTest") { module_out_path = module_output_path sources = [ - "${services_path}/distributedfiledeamon/src/mountpoint/mount_point.cpp" + "${services_path}/distributedfiledaemon/src/mountpoint/mount_point.cpp" ] - sources += [ "src/distributedfiledeamon_service_test.cpp" ] + sources += [ "src/distributedfiledaemon_service_test.cpp" ] configs = [ ":module_private_config", @@ -52,5 +52,5 @@ ohos_moduletest("DistributedFileDeamonServiceTest") { group("moduletest") { testonly = true - deps = [":DistributedFileDeamonServiceTest"] + deps = [":DistributedFileDaemonServiceTest"] } \ No newline at end of file diff --git a/test/moduletest/src/distributedfiledeamon_service_test.cpp b/test/moduletest/src/distributedfiledeamon_service_test.cpp index f4a6fb122..7329e67d8 100644 --- a/test/moduletest/src/distributedfiledeamon_service_test.cpp +++ b/test/moduletest/src/distributedfiledeamon_service_test.cpp @@ -23,12 +23,13 @@ #include "utils_directory.h" #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Test { using namespace testing::ext; using namespace std; -class DistributedFileDeamonServiceTest : public testing::Test { +class DistributedFileDaemonServiceTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); @@ -36,22 +37,22 @@ public: void TearDown(); }; -void DistributedFileDeamonServiceTest::SetUpTestCase(void) +void DistributedFileDaemonServiceTest::SetUpTestCase(void) { // input testsuit setup step,setup invoked before all testcases } -void DistributedFileDeamonServiceTest::TearDownTestCase(void) +void DistributedFileDaemonServiceTest::TearDownTestCase(void) { // input testsuit teardown step,teardown invoked after all testcases } -void DistributedFileDeamonServiceTest::SetUp(void) +void DistributedFileDaemonServiceTest::SetUp(void) { // input testcase setup step,setup invoked before each testcases } -void DistributedFileDeamonServiceTest::TearDown(void) +void DistributedFileDaemonServiceTest::TearDown(void) { // input testcase teardown step,teardown invoked after each testcases } @@ -62,7 +63,7 @@ void DistributedFileDeamonServiceTest::TearDown(void) * @tc.type: FUNC * @tc.require: Issue Number */ -HWTEST_F(DistributedFileDeamonServiceTest, mount_umount_test_001, TestSize.Level1) +HWTEST_F(DistributedFileDaemonServiceTest, mount_umount_test_001, TestSize.Level1) { auto mp = make_unique(OHOS::DistributedFile::Utils::MountArgumentDescriptors::Alpha(9527)); @@ -80,4 +81,5 @@ HWTEST_F(DistributedFileDeamonServiceTest, mount_umount_test_001, TestSize.Level } } // namespace Test } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/utils/log/include/utils_log.h b/utils/log/include/utils_log.h index a744c3bcf..8da302ecc 100755 --- a/utils/log/include/utils_log.h +++ b/utils/log/include/utils_log.h @@ -1,24 +1,27 @@ /* -* Copyright (c) 2021 Huawei Device Co., Ltd. -d * 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 + * 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. + */ + +#ifndef UTILS_LOG_H +#define UTILS_LOG_H #include #include "hilog/log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { #ifndef LOG_DOMAIN #define LOG_DOMAIN 0xD001600 @@ -41,4 +44,6 @@ std::string GetFileNameFromFullPath(const char *str); #define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) #define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // UTILS_LOG_H \ No newline at end of file diff --git a/utils/log/src/utils_log.cpp b/utils/log/src/utils_log.cpp index 2a375472e..37deb39c6 100644 --- a/utils/log/src/utils_log.cpp +++ b/utils/log/src/utils_log.cpp @@ -16,6 +16,7 @@ #include "utils_log.h" namespace OHOS { +namespace Storage { namespace DistributedFile { std::string GetFileNameFromFullPath(const char *str) { @@ -24,4 +25,5 @@ std::string GetFileNameFromFullPath(const char *str) return (pos == std::string::npos) ? std::string() : fullPath.substr(pos + 1); } } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/utils/system/include/utils_dfs_thread.h b/utils/system/include/utils_dfs_thread.h index 3b728e0c9..593ec79fd 100644 --- a/utils/system/include/utils_dfs_thread.h +++ b/utils/system/include/utils_dfs_thread.h @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once +#ifndef UTILS_DFS_THREAD_H +#define UTILS_DFS_THREAD_H #include "utils_log.h" #include @@ -22,6 +23,7 @@ #include namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Utils { class DfsThread { @@ -132,4 +134,6 @@ private: }; } // namespace Utils } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // UTILS_DFS_THREAD_H \ No newline at end of file diff --git a/utils/system/include/utils_directory.h b/utils/system/include/utils_directory.h index 6f63c5312..48d89abcf 100644 --- a/utils/system/include/utils_directory.h +++ b/utils/system/include/utils_directory.h @@ -1,18 +1,20 @@ /* -* Copyright (c) 2021 Huawei Device Co., Ltd. -d * 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. -*/ + * 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 +#ifndef UTILS_DIRECTORY_H +#define UTILS_DIRECTORY_H #include #include @@ -22,6 +24,7 @@ d * you may not use this file except in compliance with the License. #include "nocopyable.h" namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Utils { enum Uid { @@ -37,4 +40,6 @@ void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t void ForceRemoveDirectory(const std::string &path); } // namespace Utils } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // UTILS_DIRECTORY_H \ No newline at end of file diff --git a/utils/system/include/utils_mount_argument.h b/utils/system/include/utils_mount_argument.h index 16e1745b5..a57a07738 100644 --- a/utils/system/include/utils_mount_argument.h +++ b/utils/system/include/utils_mount_argument.h @@ -13,11 +13,13 @@ * limitations under the License. */ -#pragma once +#ifndef UTILS_MOUNT_ARGUMENT_H +#define UTILS_MOUNT_ARGUMENT_H #include namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Utils { struct MountArgument final { @@ -46,4 +48,6 @@ public: }; } // namespace Utils } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // UTILS_MOUNT_ARGUMENT_H \ No newline at end of file diff --git a/utils/system/include/utils_singleton.h b/utils/system/include/utils_singleton.h index d20817fd4..570497556 100644 --- a/utils/system/include/utils_singleton.h +++ b/utils/system/include/utils_singleton.h @@ -13,7 +13,8 @@ * limitations under the License. */ -#pragma once +#ifndef UTILS_SINGLETON_H +#define UTILS_SINGLETON_H #include "nocopyable.h" #include @@ -21,6 +22,7 @@ #include namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Utils { #define DECLARE_SINGLETON(MyClass) \ @@ -81,4 +83,6 @@ void Singleton::StopInstance() } } // namespace Utils } // namespace DistributedFile -} // namespace OHOS \ No newline at end of file +} // namespace Storage +} // namespace OHOS +#endif // UTILS_SINGLETON_H \ No newline at end of file diff --git a/utils/system/src/utils_directory.cpp b/utils/system/src/utils_directory.cpp index 282711f8d..7c62a2a40 100644 --- a/utils/system/src/utils_directory.cpp +++ b/utils/system/src/utils_directory.cpp @@ -1,16 +1,17 @@ /* -* Copyright (c) 2021 Huawei Device Co., Ltd. -d * 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. -*/ + * 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 "utils_directory.h" @@ -22,6 +23,7 @@ d * you may not use this file except in compliance with the License. #include "directory_ex.h" namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Utils { using namespace std; @@ -78,4 +80,5 @@ void ForceRemoveDirectory(const string &path) } } // namespace Utils } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/utils/system/src/utils_mount_argument.cpp b/utils/system/src/utils_mount_argument.cpp index 7aa3dee71..abde578d5 100644 --- a/utils/system/src/utils_mount_argument.cpp +++ b/utils/system/src/utils_mount_argument.cpp @@ -1,16 +1,17 @@ /* -* Copyright (c) 2021 Huawei Device Co., Ltd. -d * 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. -*/ + * 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 "utils_mount_argument.h" @@ -18,6 +19,7 @@ d * you may not use this file except in compliance with the License. #include namespace OHOS { +namespace Storage { namespace DistributedFile { namespace Utils { using namespace std; @@ -109,4 +111,5 @@ MountArgument MountArgumentDescriptors::Alpha(int userId) } } // namespace Utils } // namespace DistributedFile +} // namespace Storage } // namespace OHOS \ No newline at end of file -- Gitee