From ad820db6b170d5c9da9019e0e2775633f796e425 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 9 May 2022 20:12:10 +0800 Subject: [PATCH 1/4] add directory manager Signed-off-by: Sven Wang --- conf/config.json | 24 +- .../distributeddatafwk/include/itypes_util.h | 6 + .../distributeddatafwk/src/itypes_util.cpp | 23 +- .../app/src/kvstore_app_manager.h | 1 - .../app/src/kvstore_data_service.h | 1 + .../route_head_handler_impl.cpp | 8 +- .../session_manager/route_head_handler_impl.h | 6 +- .../distributeddataservice/service/BUILD.gn | 1 + .../service/bootstrap/src/bootstrap.cpp | 11 +- .../service/config/include/config_factory.h | 1 + .../config/include/model/directory_config.h | 1 - .../service/config/src/config_factory.cpp | 5 + .../config/src/model/directory_config.cpp | 14 +- .../directory/include/directory_manager.h | 97 +++---- .../directory/include/kvstore_context.h | 27 -- .../directory/src/directory_manager.cpp | 242 +++++++++++++----- .../service/test/directory_manager_test.cpp | 169 ++++++++++-- 17 files changed, 422 insertions(+), 215 deletions(-) delete mode 100644 services/distributeddataservice/service/directory/include/kvstore_context.h diff --git a/conf/config.json b/conf/config.json index 8b241c7c2..0a351e213 100644 --- a/conf/config.json +++ b/conf/config.json @@ -63,27 +63,17 @@ ] }, "directory": { - "workers": [ - "appWorker", - "serverWorker" - ], - "metaPath": "/data/service/el0/0/database/ddms/metadata/", "strategy": [ { - "version": "0", - "holder": "ddms", - "path": "/data/{security}/0/mdds/{userId}/{bundleName}", - "metaPath": "/data/misc_de/0/mdds/Meta" - }, - { - "version": "1", - "holder": "ddms", - "path": "/data/service/{security}/0/database/ddms/{userId}/{bundleName}" + "version": 0, + "pattern": "/data/{security}/0/mdds/{userId}/default/{bundleName}", + "metaPath": "/data/misc_de/0/mdds/Meta", + "autoCreate": true }, { - "version": "2", - "hodler": "caller", - "path": "/data/{type}/{security}/{userId}/{packageName}/database/{bundleName}" + "version": 50331651, + "pattern": "/data/{type}/{area}/{userId}/database/{bundleName}/{hapName}/{store}", + "metaPath": "/data/service/el1/public/database/distributeddata/meta" } ] } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/itypes_util.h b/frameworks/innerkitsimpl/distributeddatafwk/include/itypes_util.h index f2e51b898..72f21d894 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/itypes_util.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/itypes_util.h @@ -29,6 +29,9 @@ public: static bool Marshalling(MessageParcel &data); static bool Unmarshalling(MessageParcel &data); + static bool Marshalling(int32_t input, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, int32_t &output); + static bool Marshalling(const std::string &input, MessageParcel &data); static bool Unmarshalling(MessageParcel &data, std::string &output); @@ -59,6 +62,9 @@ public: static bool Marshalling(const Options &input, MessageParcel &data); static bool Unmarshalling(MessageParcel &data, Options &output); + static bool Marshalling(const sptr &input, MessageParcel &data); + static bool Unmarshalling(MessageParcel &data, sptr &output); + static int64_t GetTotalSize(const std::vector &entries); static int64_t GetTotalSize(const std::vector &entries); diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp index 8350e2556..da7fc6531 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp @@ -16,7 +16,7 @@ #define LOG_TAG "ITypesUtil" #include "itypes_util.h" - +#include "iremote_object.h" #include "autils/constant.h" #include "log_print.h" @@ -31,6 +31,16 @@ bool ITypesUtil::Unmarshalling(MessageParcel &data) return true; } +bool ITypesUtil::Marshalling(int32_t input, MessageParcel &data) +{ + return data.WriteInt32(input); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, int32_t &output) +{ + return data.ReadInt32(output); +} + bool ITypesUtil::Marshalling(const std::string &input, MessageParcel &data) { return data.WriteString(input); @@ -387,6 +397,17 @@ bool ITypesUtil::Unmarshalling(MessageParcel &data, Options &output) return true; } +bool ITypesUtil::Marshalling(const sptr &input, MessageParcel &data) +{ + return data.WriteRemoteObject(input); +} + +bool ITypesUtil::Unmarshalling(MessageParcel &data, sptr &output) +{ + output = data.ReadRemoteObject(); + return true; +} + int64_t ITypesUtil::GetTotalSize(const std::vector &entries) { int64_t bufferSize = 1; diff --git a/services/distributeddataservice/app/src/kvstore_app_manager.h b/services/distributeddataservice/app/src/kvstore_app_manager.h index a25160c19..4900b0a1f 100644 --- a/services/distributeddataservice/app/src/kvstore_app_manager.h +++ b/services/distributeddataservice/app/src/kvstore_app_manager.h @@ -29,7 +29,6 @@ #include "single_kvstore_impl.h" #include "types.h" - namespace OHOS { namespace DistributedKv { class KvStoreAppManager { diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index f3782f392..15b9df869 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -28,6 +28,7 @@ #include "kvstore_device_listener.h" #include "kvstore_impl.h" #include "kvstore_user_manager.h" +#include "metadata/store_meta_data.h" #include "reporter.h" #include "security/security.h" #include "single_kvstore_impl.h" diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index 0c7b6c72b..a328ba109 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -141,12 +141,12 @@ bool RouteHeadHandlerImpl::PackDataBody(uint8_t *data, uint32_t totalLen) { uint8_t *ptr = data; SessionDevicePair *devicePair = reinterpret_cast(ptr); - auto ret = strcpy_s(devicePair->sourceDeviceId, DEVICE_ID_SIZE_MAX, session_.sourceDeviceId.c_str()); + auto ret = strcpy_s(devicePair->sourceId, MAX_DEVICE_ID, session_.sourceDeviceId.c_str()); if (ret != 0) { ZLOGE("strcpy for source device id failed"); return false; } - ret = strcpy_s(devicePair->targetDeviceId, DEVICE_ID_SIZE_MAX, session_.targetDeviceId.c_str()); + ret = strcpy_s(devicePair->targetId, MAX_DEVICE_ID, session_.targetDeviceId.c_str()); if (ret != 0) { ZLOGE("strcpy for target device id failed"); return false; @@ -248,8 +248,8 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen return false; } const SessionDevicePair *devicePair = reinterpret_cast(ptr); - session_.sourceDeviceId.append(devicePair->sourceDeviceId, DEVICE_ID_SIZE_MAX - 1); - session_.targetDeviceId.append(devicePair->targetDeviceId, DEVICE_ID_SIZE_MAX - 1); + session_.sourceDeviceId = std::string(devicePair->sourceId, strnlen(devicePair->sourceId, MAX_DEVICE_ID)); + session_.targetDeviceId = std::string(devicePair->targetId, strnlen(devicePair->targetId, MAX_DEVICE_ID)); ptr += sizeof(SessionDevicePair); leftSize -= sizeof(SessionDevicePair); diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h index 3deb9ed9e..5c85f93a5 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h @@ -37,10 +37,10 @@ struct RouteHead { uint32_t dataLen; }; -constexpr const int32_t DEVICE_ID_SIZE_MAX = 65; +constexpr const int32_t MAX_DEVICE_ID = 65; struct SessionDevicePair { - char sourceDeviceId[DEVICE_ID_SIZE_MAX]; - char targetDeviceId[DEVICE_ID_SIZE_MAX]; + char sourceId[MAX_DEVICE_ID]; + char targetId[MAX_DEVICE_ID]; }; struct SessionUserPair { diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 098d7bfa8..641bb1b71 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -54,6 +54,7 @@ ohos_shared_library("distributeddatasvc") { ] external_deps = [ + "access_token:libaccesstoken_sdk", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", ] diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 1a2a45f99..2dfedbf7c 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -74,14 +74,15 @@ void Bootstrap::LoadNetworks() } void Bootstrap::LoadDirectory() { - auto *global = ConfigFactory::GetInstance().GetGlobalConfig(); - if (global == nullptr || global->directory == nullptr) { + auto *config = ConfigFactory::GetInstance().GetDirectoryConfig(); + if (config == nullptr) { return; } - for (const auto &strategy : global->directory->strategy) { - DirectoryManager::GetInstance().AddParams(strategy); + std::vector strategies(config->strategy.size()); + for (int i = 0; i < config->strategy.size(); ++i) { + strategies[i] = config->strategy[i]; } - DirectoryManager::GetInstance().SetCurrentVersion(global->directory->currentStrategyVersion); + DirectoryManager::GetInstance().Initialize(strategies); } } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/service/config/include/config_factory.h b/services/distributeddataservice/service/config/include/config_factory.h index 7d3a4fc8c..55fc0f309 100644 --- a/services/distributeddataservice/service/config/include/config_factory.h +++ b/services/distributeddataservice/service/config/include/config_factory.h @@ -28,6 +28,7 @@ public: API_EXPORT NetworkConfig *GetNetworkConfig(); API_EXPORT CheckerConfig *GetCheckerConfig(); API_EXPORT GlobalConfig *GetGlobalConfig(); + API_EXPORT DirectoryConfig *GetDirectoryConfig(); private: static constexpr const char *CONF_PATH = "/system/etc/distributeddata/conf"; ConfigFactory(); diff --git a/services/distributeddataservice/service/config/include/model/directory_config.h b/services/distributeddataservice/service/config/include/model/directory_config.h index 1244eb622..baa7b05c6 100644 --- a/services/distributeddataservice/service/config/include/model/directory_config.h +++ b/services/distributeddataservice/service/config/include/model/directory_config.h @@ -25,7 +25,6 @@ public: bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; }; - std::string currentStrategyVersion; std::vector strategy; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; diff --git a/services/distributeddataservice/service/config/src/config_factory.cpp b/services/distributeddataservice/service/config/src/config_factory.cpp index 117c495ba..5fe9724be 100644 --- a/services/distributeddataservice/service/config/src/config_factory.cpp +++ b/services/distributeddataservice/service/config/src/config_factory.cpp @@ -67,5 +67,10 @@ GlobalConfig *ConfigFactory::GetGlobalConfig() { return &config_; } + +DirectoryConfig *ConfigFactory::GetDirectoryConfig() +{ + return config_.directory; +} } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/service/config/src/model/directory_config.cpp b/services/distributeddataservice/service/config/src/model/directory_config.cpp index bd8eb3856..5ef254e78 100644 --- a/services/distributeddataservice/service/config/src/model/directory_config.cpp +++ b/services/distributeddataservice/service/config/src/model/directory_config.cpp @@ -19,34 +19,30 @@ namespace DistributedData { bool DirectoryConfig::DirectoryStrategy::Marshal(json &node) const { SetValue(node[GET_NAME(version)], version); - SetValue(node[GET_NAME(holder)], holder); - SetValue(node[GET_NAME(path)], path); + SetValue(node[GET_NAME(pattern)], pattern); SetValue(node[GET_NAME(metaPath)], metaPath); + SetValue(node[GET_NAME(autoCreate)], autoCreate); return true; } bool DirectoryConfig::DirectoryStrategy::Unmarshal(const json &node) { GetValue(node, GET_NAME(version), version); - GetValue(node, GET_NAME(holder), holder); - GetValue(node, GET_NAME(path), path); + GetValue(node, GET_NAME(pattern), pattern); GetValue(node, GET_NAME(metaPath), metaPath); + GetValue(node, GET_NAME(autoCreate), autoCreate); return true; } bool DirectoryConfig::Marshal(json &node) const { - SetValue(node[GET_NAME(currentStrategyVersion)], currentStrategyVersion); SetValue(node[GET_NAME(strategy)], strategy); return true; } bool DirectoryConfig::Unmarshal(const json &node) { - bool ret = true; - ret = GetValue(node, GET_NAME(currentStrategyVersion), currentStrategyVersion) && ret; - ret = GetValue(node, GET_NAME(strategy), strategy) && ret; - return ret; + return GetValue(node, GET_NAME(strategy), strategy); } } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/service/directory/include/directory_manager.h b/services/distributeddataservice/service/directory/include/directory_manager.h index 08c13c7cf..09d4a91e6 100644 --- a/services/distributeddataservice/service/directory/include/directory_manager.h +++ b/services/distributeddataservice/service/directory/include/directory_manager.h @@ -18,78 +18,51 @@ #include #include -#include "visibility.h" -#include "kvstore_context.h" + #include "metadata/store_meta_data.h" +#include "visibility.h" namespace OHOS::DistributedData { -enum PathType { - PATH_DE, - PATH_CE, -}; -class DirWorker { -public: - DirWorker() = default; - virtual std::string GetDir(ClientContext clientContext, PathType type) = 0; - virtual bool CreateDir(ClientContext clientContext, PathType type) = 0; - virtual std::string GetBackupDir(ClientContext clientContext, PathType type) = 0; - virtual std::string GetSecretKeyDir(ClientContext clientContext, PathType type) = 0; - virtual std::string GetMetaDir() = 0; -}; - -class ServerDirWorker : public DirWorker { -public: - static ServerDirWorker &GetInstance(); - - std::string GetDir(ClientContext clientContext, PathType type) override; - bool CreateDir(ClientContext clientContext, PathType type) override; - std::string GetBackupDir(ClientContext clientContext, PathType type) override; - std::string GetSecretKeyDir(ClientContext clientContext, PathType type) override; - std::string GetMetaDir() override; - -public: - static std::map rootPathMap_; -}; - -class ClientDirWorker : public DirWorker { -public: - static ClientDirWorker &GetInstance(); - - std::string GetDir(ClientContext clientContext, PathType type) override; - bool CreateDir(ClientContext clientContext, PathType type) override; - std::string GetBackupDir(ClientContext clientContext, PathType type) override; - std::string GetSecretKeyDir(ClientContext clientContext, PathType type) override; - std::string GetMetaDir() override; -}; - class DirectoryManager { public: + static constexpr uint32_t INVALID_VERSION = 0xFFFFFFFF; struct Strategy { - std::string version; - std::string holder; - std::string path; + bool autoCreate = false; + uint32_t version = 0; + std::string pattern; std::string metaPath; }; API_EXPORT static DirectoryManager &GetInstance(); - API_EXPORT std::string CreatePath(const ClientContext &context, PathType type); - API_EXPORT std::string GetStorePath(const StoreMetaData &metaData); - API_EXPORT std::string GetStoreBackupPath(const StoreMetaData &metaData); - API_EXPORT std::string GetMetaDataStorePath(); - - API_EXPORT void AddParams(const Strategy &strategy); - API_EXPORT void SetCurrentVersion(const std::string &version); - - inline static std::string JoinPath(std::initializer_list stringList) - { - std::string tmpPath; - for (const std::string &str : stringList) { - tmpPath += (str + "/"); - } - return tmpPath; - } + API_EXPORT std::string GetStorePath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION); + API_EXPORT std::string GetSecretKeyPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION); + API_EXPORT std::string GetStoreBackupPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION); + API_EXPORT std::string GetMetaStorePath(uint32_t version = INVALID_VERSION); + API_EXPORT std::vector GetVersions(); + API_EXPORT void Initialize(const std::vector &strategies); private: - std::map patterns_; - std::string version_; + using Action = std::string (DirectoryManager::*)(const StoreMetaData &) const; + struct StrategyImpl { + bool autoCreate = false; + uint32_t version; + std::string metaPath; + std::vector path; + std::vector pipes; + }; + + DirectoryManager(); + std::string GetType(const StoreMetaData &metaData) const; + std::string GetStore(const StoreMetaData &metaData) const; + std::string GetSecurity(const StoreMetaData &metaData) const; + std::string GetArea(const StoreMetaData &metaData) const; + std::string GetUserId(const StoreMetaData &metaData) const; + std::string GetBundleName(const StoreMetaData &metaData) const; + std::string GetHapName(const StoreMetaData &metaData) const; + std::vector Split(const std::string &source, const std::string &pattern) const; + int32_t GetVersionIndex(uint32_t version) const; + std::string GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath = "") const; + bool CreateDirectory(const std::string &path) const; + const std::map actions_; + std::vector strategies_; }; } // namespace OHOS::DistributedData #endif // DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H diff --git a/services/distributeddataservice/service/directory/include/kvstore_context.h b/services/distributeddataservice/service/directory/include/kvstore_context.h deleted file mode 100644 index f312d57bb..000000000 --- a/services/distributeddataservice/service/directory/include/kvstore_context.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 DISTRIBUTEDDATAMGR_KVSTORE_CONTEXT_H -#define DISTRIBUTEDDATAMGR_KVSTORE_CONTEXT_H -#include - -namespace OHOS::DistributedData { -struct ClientContext { - std::string userId {}; - std::string bundleName {}; - std::string dataDir {}; -}; -} // namespace OHOS::DistributedData -#endif // DISTRIBUTEDDATAMGR_KVSTORE_CONTEXT_H diff --git a/services/distributeddataservice/service/directory/src/directory_manager.cpp b/services/distributeddataservice/service/directory/src/directory_manager.cpp index 9796444d1..b6cd22f29 100644 --- a/services/distributeddataservice/service/directory/src/directory_manager.cpp +++ b/services/distributeddataservice/service/directory/src/directory_manager.cpp @@ -15,116 +15,222 @@ #define LOG_TAG "DirectoryManager" #include "directory_manager.h" +#include +#include + #include -#include "utils/constant.h" -#include "directory_ex.h" -#include "kvstore_context.h" +#include "accesstoken_kit.h" #include "log/log_print.h" +#include "types.h" +#include "unistd.h" namespace OHOS::DistributedData { -using namespace DistributedKv; -std::map ServerDirWorker::rootPathMap_ = { - { PATH_DE, "/data/misc_de/0/mdds" }, - { PATH_CE, "/data/misc_ce/0/mdds" }, -}; -std::string ServerDirWorker::GetDir(ClientContext clientContext, PathType type) -{ - if (rootPathMap_.find(type) == rootPathMap_.end()) { - return ""; - } - return DirectoryManager::JoinPath( - { rootPathMap_.at(type), clientContext.userId, "default", clientContext.bundleName }); -} -bool ServerDirWorker::CreateDir(ClientContext clientContext, PathType type) +using OHOS::DistributedKv::SecurityLevel; +using namespace OHOS::Security::AccessToken; +DirectoryManager::DirectoryManager() + : actions_({ { "{security}", &DirectoryManager::GetSecurity }, { "{store}", &DirectoryManager::GetStore }, + { "{type}", &DirectoryManager::GetType }, { "{area}", &DirectoryManager::GetArea }, + { "{userId}", &DirectoryManager::GetUserId }, { "{bundleName}", &DirectoryManager::GetBundleName }, + { "{hapName}", &DirectoryManager::GetHapName } }) { - std::string directory = GetDir(clientContext, type); - bool ret = ForceCreateDirectory(directory); - if (!ret) { - ZLOGE("create directory[%s] failed, errstr=[%d].", directory.c_str(), errno); - return false; - } - return true; } -ServerDirWorker &ServerDirWorker::GetInstance() + +DirectoryManager &DirectoryManager::GetInstance() { - static ServerDirWorker instance; + static DirectoryManager instance; return instance; } -std::string ServerDirWorker::GetBackupDir(ClientContext clientContext, PathType type) + +std::string DirectoryManager::GetStorePath(const StoreMetaData &metaData, uint32_t version) { - if (rootPathMap_.find(type) == rootPathMap_.end()) { - return ""; - } - return DirectoryManager::JoinPath({ rootPathMap_.at(type), clientContext.userId, - "default", clientContext.bundleName, "backup" }); + return GenPath(metaData, version, ""); } -std::string ServerDirWorker::GetMetaDir() + +std::string DirectoryManager::GetStoreBackupPath(const StoreMetaData &metaData, uint32_t version) { - return DirectoryManager::JoinPath({ rootPathMap_.at(PATH_DE), "Meta" }); + return GenPath(metaData, version, "backup"); } -std::string ServerDirWorker::GetSecretKeyDir(ClientContext clientContext, PathType type) + +std::string DirectoryManager::GetSecretKeyPath(const StoreMetaData &metaData, uint32_t version) { - return GetDir(clientContext, type); + return GenPath(metaData, version, "secret"); } -std::string ClientDirWorker::GetDir(ClientContext clientContext, PathType type) + +std::string DirectoryManager::GetMetaStorePath(uint32_t version) { - return clientContext.dataDir; + int32_t index = GetVersionIndex(version); + if (index < 0) { + return ""; + } + return strategies_[index].metaPath; } -bool ClientDirWorker::CreateDir(ClientContext clientContext, PathType type) -{ - return true; + +void DirectoryManager::Initialize(const std::vector &strategies) +{ + strategies_.resize(strategies.size()); + for (int i = 0; i < strategies.size(); ++i) { + const Strategy &strategy = strategies[i]; + StrategyImpl &impl = strategies_[i]; + impl.autoCreate = strategy.autoCreate; + impl.version = strategy.version; + impl.metaPath = strategy.metaPath; + impl.path = Split(strategy.pattern, "/"); + impl.pipes.clear(); + for (auto &value : impl.path) { + auto it = actions_.find(value); + impl.pipes.push_back(it == actions_.end() ? nullptr : it->second); + } + } + + std::sort(strategies_.begin(), strategies_.end(), + [](const StrategyImpl &curr, const StrategyImpl &prev) { return curr.version > prev.version; }); } -ClientDirWorker &ClientDirWorker::GetInstance() + +std::string DirectoryManager::GetType(const StoreMetaData &metaData) const { - static ClientDirWorker instance; - return instance; + if (AccessTokenKit::GetTokenTypeFlag(metaData.tokenId) == TOKEN_NATIVE) { + return "service"; + } + return "app"; } -std::string ClientDirWorker::GetBackupDir(ClientContext clientContext, PathType type) + +std::string DirectoryManager::GetStore(const StoreMetaData &metaData) const { - return DirectoryManager::JoinPath({ clientContext.dataDir, "backup" }); + if (metaData.storeType < 10) { + return "kvdb"; + } + // rdb use empty session + return ""; } -std::string ClientDirWorker::GetMetaDir() -{ - return std::string("/data/service/el1/public/distributeddata/DistributedKvDataService/Meta/"); + +std::string DirectoryManager::GetSecurity(const StoreMetaData &metaData) const +{ + switch (metaData.securityLevel) { + case SecurityLevel::NO_LABEL: + if ((metaData.bundleName != metaData.appId) || (metaData.appType != "harmony")) { + break; + } + case SecurityLevel::S0: + case SecurityLevel::S1: + return "misc_de"; + } + return "misc_ce"; } -std::string ClientDirWorker::GetSecretKeyDir(ClientContext clientContext, PathType type) + +std::string DirectoryManager::GetArea(const StoreMetaData &metaData) const { - return GetDir(clientContext, type); + return std::string("el") + std::to_string(metaData.area); } -std::string DirectoryManager::CreatePath(const ClientContext &context, PathType type) +std::string DirectoryManager::GetUserId(const StoreMetaData &metaData) const { - (void) context; - (void) type; - return ""; + if (AccessTokenKit::GetTokenTypeFlag(metaData.tokenId) == TOKEN_NATIVE) { + return "public"; + } + return metaData.user; } -DirectoryManager &DirectoryManager::GetInstance() +std::string DirectoryManager::GetBundleName(const StoreMetaData &metaData) const { - static DirectoryManager instance; - return instance; + if (metaData.instanceId == 0) { + return metaData.bundleName; + } + return metaData.bundleName + "_" + std::to_string(metaData.instanceId); } -std::string DirectoryManager::GetStorePath(const StoreMetaData &metaData) +std::string DirectoryManager::GetHapName(const StoreMetaData &metaData) const { - return metaData.dataDir; + return metaData.hapName; +} + +std::vector DirectoryManager::Split(const std::string &source, const std::string &pattern) const +{ + std::vector values; + std::string::size_type pos = 0; + std::string::size_type nextPos = 0; + while (nextPos != std::string::npos) { + nextPos = source.find(pattern, pos); + if (nextPos == pos) { + pos = pos + pattern.size(); + continue; + } + values.push_back(source.substr(pos, nextPos - pos)); + pos = nextPos + pattern.size(); + } + return values; } -std::string DirectoryManager::GetStoreBackupPath(const StoreMetaData &metaData) +int32_t DirectoryManager::GetVersionIndex(uint32_t version) const { - return GetStorePath(metaData) + "/backup/"; + for (int i = 0; i < strategies_.size(); ++i) { + if (version >= strategies_[i].version) { + return i; + } + } + return int32_t(strategies_.size()) - 1; } -std::string DirectoryManager::GetMetaDataStorePath() +std::vector DirectoryManager::GetVersions() { - return "/data/service/el1/public/distributeddata/meta/"; + std::vector versions; + for (int i = 0; i < strategies_.size(); ++i) { + versions[i] = strategies_[i].version; + } + return versions; } -void DirectoryManager::AddParams(const Strategy &strategy) + +std::string DirectoryManager::GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath) const { - patterns_[strategy.version] = strategy; + int32_t index = GetVersionIndex(version); + if (index < 0) { + return ""; + } + std::string path; + auto &strategy = strategies_[index]; + for (int i = 0; i < strategy.pipes.size(); ++i) { + std::string section; + if (strategy.pipes[i] == nullptr) { + section = strategy.path[i]; + } else { + section = (this->*(strategy.pipes[i]))(metaData); + } + if (section.empty()) { + continue; + } + path += "/" + section; + } + if (!exPath.empty()) { + path += "/" + exPath; + } + if (strategy.autoCreate) { + CreateDirectory(path); + } + return path; } -void DirectoryManager::SetCurrentVersion(const std::string &version) + +bool DirectoryManager::CreateDirectory(const std::string &path) const { - version_ = version; + if (access(path.c_str(), F_OK) == 0) { + return true; + } + + std::string::size_type index = 0; + do { + std::string subPath; + index = path.find('/', index + 1); + if (index == std::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_IRWXG | S_IROTH | S_IXOTH)) != 0) { + return false; + } + } + } while (index != std::string::npos); + + return access(path.c_str(), F_OK) == 0; } } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/service/test/directory_manager_test.cpp b/services/distributeddataservice/service/test/directory_manager_test.cpp index e4dfdb133..5173eab26 100644 --- a/services/distributeddataservice/service/test/directory_manager_test.cpp +++ b/services/distributeddataservice/service/test/directory_manager_test.cpp @@ -16,23 +16,60 @@ #include "directory_manager.h" #include -#include -#include -#include -#include #include "types.h" +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" using namespace testing::ext; using namespace OHOS::DistributedData; using namespace OHOS::DistributedKv; using namespace OHOS; +using namespace OHOS::Security::AccessToken; class DirectoryManagerTest : public testing::Test { public: - static void SetUpTestCase() {} + static void SetUpTestCase() + { + HapInfoParams info = { + .userID = 100, .bundleName = "ohos.test.demo", .instIndex = 0, .appIDDesc = "ohos.test.demo" + }; + HapPolicyParams policy = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = { + { + .permissionName = "ohos.permission.test", + .bundleName = "ohos.test.demo", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "open the door", + .descriptionId = 1 + } + }, + .permStateList = { + { + .permissionName = "ohos.permission.test", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 } + } + } + }; + AccessTokenKit::AllocHapToken(info, policy); + } static void TearDownTestCase() {} - void SetUp() { } - void TearDown() { } + void SetUp() + { + tokenParam_.processName = "foundation"; + tokenParam_.aplStr = "system_core"; + } + void TearDown() {} + +protected: + NativeTokenInfoParams tokenParam_{0}; }; /** @@ -42,33 +79,131 @@ public: * @tc.require: * @tc.author: baoyayong */ -HWTEST_F(DirectoryManagerTest, GetStoragePath01, TestSize.Level0) +HWTEST_F(DirectoryManagerTest, GetBundleStorePath, TestSize.Level0) { StoreMetaData metaData; - metaData.user = "10"; + metaData.user = "100"; + metaData.bundleName = "ohos.test.demo"; + metaData.dataDir = "/data/app/el1/100/database/ohos.test.demo/kvdb"; + metaData.securityLevel = SecurityLevel::S2; + metaData.area = 1; + metaData.tokenId = AccessTokenKit::GetHapTokenID(10, "ohos.test.demo", 0); + HapTokenInfo tokenInfo; + AccessTokenKit::GetHapTokenInfo(metaData.tokenId, tokenInfo); + metaData.appId = tokenInfo.appID; + auto path = DirectoryManager::GetInstance().GetStorePath(metaData); + EXPECT_EQ(path, metaData.dataDir); +} + +/** +* @tc.name: GetHapStorePath +* @tc.desc: test get db dir +* @tc.type: FUNC +* @tc.require: +* @tc.author: baoyayong +*/ +HWTEST_F(DirectoryManagerTest, GetHapStorePath, TestSize.Level0) +{ + StoreMetaData metaData; + metaData.user = "100"; metaData.bundleName = "com.sample.helloworld"; - metaData.dataDir = "/data/app/el1/10/com.sample.helloworld"; + metaData.hapName = "example.hap"; + metaData.dataDir = "/data/app/el2/100/database/com.sample.helloworld/example.hap/kvdb"; + metaData.securityLevel = SecurityLevel::S2; + metaData.area = 2; + auto path = DirectoryManager::GetInstance().GetStorePath(metaData); + EXPECT_EQ(path, metaData.dataDir); +} + +/** +* @tc.name: GetSaStorePath +* @tc.desc: test get db dir +* @tc.type: FUNC +* @tc.require: +* @tc.author: baoyayong +*/ +HWTEST_F(DirectoryManagerTest, GetSaStorePath, TestSize.Level0) +{ + StoreMetaData metaData; + metaData.user = "0"; + metaData.bundleName = "bundle_manager_service"; + metaData.appId = "bundle_manager_service"; + metaData.dataDir = "/data/service/el1/public/database/bundle_manager_service/kvdb"; metaData.securityLevel = SecurityLevel::S2; + metaData.area = 1; + metaData.tokenId = GetAccessTokenId(&tokenParam_); auto path = DirectoryManager::GetInstance().GetStorePath(metaData); EXPECT_EQ(path, metaData.dataDir); } /** -* @tc.name: GetStorageBackupPath01 +* @tc.name: GetSaStorePath +* @tc.desc: test get db dir +* @tc.type: FUNC +* @tc.require: +* @tc.author: baoyayong +*/ +HWTEST_F(DirectoryManagerTest, GetAppStorePathV0, TestSize.Level0) +{ + StoreMetaData metaData; + metaData.user = "0"; + metaData.bundleName = "bundle_manager_service"; + metaData.appId = "bundle_manager_service"; + metaData.appType = "harmony"; + metaData.dataDir = "/data/misc_ce/0/mdds/0/default/bundle_manager_service"; + metaData.securityLevel = SecurityLevel::S2; + metaData.area = 1; + metaData.tokenId = -1; + auto path = DirectoryManager::GetInstance().GetStorePath(metaData, 0); + EXPECT_EQ(path, metaData.dataDir); +} + + +/** +* @tc.name: GetRdbBackupPath * @tc.desc: test get db backup dir * @tc.type: FUNC * @tc.require: * @tc.author: baoyayong */ -HWTEST_F(DirectoryManagerTest, GetStorageBackupPath01, TestSize.Level0) +HWTEST_F(DirectoryManagerTest, GetRDBBackupPath, TestSize.Level0) { StoreMetaData metaData; metaData.user = "10"; - metaData.bundleName = "com.sample.helloworld"; - metaData.dataDir = "/data/app/el1/10/com.sample.helloworld"; + metaData.bundleName = "ohos.test.demo"; + metaData.dataDir = "/data/app/el1/10/database/ohos.test.demo"; + metaData.securityLevel = SecurityLevel::S2; + metaData.area = 1; + metaData.storeType = 10; + metaData.tokenId = AccessTokenKit::GetHapTokenID(10, "ohos.test.demo", 0); + HapTokenInfo tokenInfo; + AccessTokenKit::GetHapTokenInfo(metaData.tokenId, tokenInfo); + metaData.appId = tokenInfo.appID; + auto path = DirectoryManager::GetInstance().GetStoreBackupPath(metaData); + EXPECT_EQ(path, metaData.dataDir + "/backup"); +} +/** +* @tc.name: GetRdbBackupPath +* @tc.desc: test get db backup dir +* @tc.type: FUNC +* @tc.require: +* @tc.author: baoyayong +*/ +HWTEST_F(DirectoryManagerTest, GetKVDBBackupPath, TestSize.Level0) +{ + StoreMetaData metaData; + metaData.user = "10"; + metaData.bundleName = "ohos.test.demo"; + metaData.dataDir = "/data/app/el1/10/database/ohos.test.demo/kvdb"; metaData.securityLevel = SecurityLevel::S2; + metaData.area = 1; + metaData.storeType = 0; + metaData.tokenId = AccessTokenKit::GetHapTokenID(10, "ohos.test.demo", 0); + HapTokenInfo tokenInfo; + AccessTokenKit::GetHapTokenInfo(metaData.tokenId, tokenInfo); + metaData.appId = tokenInfo.appID; auto path = DirectoryManager::GetInstance().GetStoreBackupPath(metaData); - EXPECT_EQ(path, metaData.dataDir + "/backup/"); + EXPECT_EQ(path, metaData.dataDir + "/backup"); } /** @@ -80,6 +215,6 @@ HWTEST_F(DirectoryManagerTest, GetStorageBackupPath01, TestSize.Level0) */ HWTEST_F(DirectoryManagerTest, GetStorageMetaPath01, TestSize.Level0) { - auto path = DirectoryManager::GetInstance().GetMetaDataStorePath(); - EXPECT_EQ(path, "/data/service/el1/public/distributeddata/meta/"); + auto path = DirectoryManager::GetInstance().GetMetaStorePath(); + EXPECT_EQ(path, "/data/service/el1/public/database/distributeddata/meta"); } -- Gitee From 800d07c662951ebdad32857023583fb7bb1bfbb7 Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Mon, 9 May 2022 21:44:35 +0800 Subject: [PATCH 2/4] update format Signed-off-by: Sven Wang --- .../app/src/kvstore_user_manager.h | 2 +- .../include/metadata/secret_key_meta_data.h | 2 +- .../include/metadata/store_meta_data.h | 3 +++ .../metadata/secret_key_meta_data.cpp | 5 +++-- .../framework/metadata/store_meta_data.cpp | 18 ++++++++++++++---- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_user_manager.h b/services/distributeddataservice/app/src/kvstore_user_manager.h index da7ad860a..2897b59c5 100644 --- a/services/distributeddataservice/app/src/kvstore_user_manager.h +++ b/services/distributeddataservice/app/src/kvstore_user_manager.h @@ -27,7 +27,7 @@ namespace OHOS { namespace DistributedKv { class KvStoreUserManager { public: -using StoreMetaData = DistributedData::StoreMetaData; + using StoreMetaData = DistributedData::StoreMetaData; explicit KvStoreUserManager(const std::string &userId); virtual ~KvStoreUserManager(); diff --git a/services/distributeddataservice/framework/include/metadata/secret_key_meta_data.h b/services/distributeddataservice/framework/include/metadata/secret_key_meta_data.h index 0d62d7ab1..521ebbe68 100644 --- a/services/distributeddataservice/framework/include/metadata/secret_key_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/secret_key_meta_data.h @@ -21,7 +21,7 @@ namespace DistributedData { struct API_EXPORT SecretKeyMetaData final : public Serializable { std::vector time {}; std::vector sKey {}; - int32_t kvStoreType = 0; + int32_t storeType = 0; API_EXPORT SecretKeyMetaData(); API_EXPORT ~SecretKeyMetaData(); API_EXPORT bool Marshal(json &node) const override; diff --git a/services/distributeddataservice/framework/include/metadata/store_meta_data.h b/services/distributeddataservice/framework/include/metadata/store_meta_data.h index 4cd19a1cf..455368c18 100644 --- a/services/distributeddataservice/framework/include/metadata/store_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/store_meta_data.h @@ -32,11 +32,14 @@ struct API_EXPORT StoreMetaData final : public Serializable { bool isEncrypt = false; int32_t storeType = -1; int32_t securityLevel = 0; + int32_t area = 0; int32_t uid = -1; uint32_t tokenId = 0; + uint32_t instanceId = 0; std::string appId = ""; std::string appType = ""; std::string bundleName = ""; + std::string hapName = ""; std::string dataDir = ""; std::string deviceId = ""; std::string schema = ""; diff --git a/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp b/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp index ff218fba3..41599d383 100644 --- a/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/secret_key_meta_data.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "metadata/secret_key_meta_data.h" + #include "utils/constant.h" namespace OHOS { namespace DistributedData { @@ -27,14 +28,14 @@ bool SecretKeyMetaData::Marshal(json &node) const { SetValue(node[GET_NAME(time)], time); SetValue(node[GET_NAME(sKey)], sKey); - SetValue(node[GET_NAME(kvStoreType)], kvStoreType); + SetValue(node[GET_NAME(storeType)], storeType); return true; } bool SecretKeyMetaData::Unmarshal(const json &node) { GetValue(node, GET_NAME(time), time); GetValue(node, GET_NAME(sKey), sKey); - GetValue(node, GET_NAME(kvStoreType), kvStoreType); + GetValue(node, GET_NAME(storeType), storeType); return true; } diff --git a/services/distributeddataservice/framework/metadata/store_meta_data.cpp b/services/distributeddataservice/framework/metadata/store_meta_data.cpp index f10ed436c..434e15c8c 100644 --- a/services/distributeddataservice/framework/metadata/store_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/store_meta_data.cpp @@ -30,11 +30,14 @@ bool StoreMetaData::Marshal(json &node) const SetValue(node[GET_NAME(isDirty)], isDirty); SetValue(node[GET_NAME(storeType)], storeType); SetValue(node[GET_NAME(securityLevel)], securityLevel); + SetValue(node[GET_NAME(area)], area); SetValue(node[GET_NAME(uid)], uid); SetValue(node[GET_NAME(tokenId)], tokenId); + SetValue(node[GET_NAME(instanceId)], instanceId); SetValue(node[GET_NAME(appId)], appId); SetValue(node[GET_NAME(appType)], appType); SetValue(node[GET_NAME(bundleName)], bundleName); + SetValue(node[GET_NAME(hapName)], hapName); SetValue(node[GET_NAME(dataDir)], dataDir); SetValue(node[GET_NAME(deviceId)], deviceId); SetValue(node[GET_NAME(schema)], schema); @@ -58,11 +61,14 @@ bool StoreMetaData::Unmarshal(const json &node) GetValue(node, GET_NAME(isEncrypt), isEncrypt); GetValue(node, GET_NAME(storeType), storeType); GetValue(node, GET_NAME(securityLevel), securityLevel); + GetValue(node, GET_NAME(area), area); GetValue(node, GET_NAME(uid), uid); GetValue(node, GET_NAME(tokenId), tokenId); + GetValue(node, GET_NAME(instanceId), instanceId); GetValue(node, GET_NAME(appId), appId); GetValue(node, GET_NAME(appType), appType); GetValue(node, GET_NAME(bundleName), bundleName); + GetValue(node, GET_NAME(hapName), hapName); GetValue(node, GET_NAME(dataDir), dataDir); GetValue(node, GET_NAME(deviceId), deviceId); GetValue(node, GET_NAME(schema), schema); @@ -106,9 +112,10 @@ bool StoreMetaData::operator==(const StoreMetaData &metaData) const if (!((isEncrypt && metaData.isEncrypt) || (!isEncrypt && !metaData.isEncrypt))) { return false; } - return (version == metaData.version && storeType == metaData.storeType && securityLevel == metaData.securityLevel - && uid == metaData.uid && tokenId != metaData.tokenId && appId == metaData.appId - && appType == metaData.appId && bundleName == metaData.bundleName && dataDir == metaData.dataDir); + return (version == metaData.version && storeType == metaData.storeType && + securityLevel == metaData.securityLevel && area == metaData.area && uid == metaData.uid && + tokenId != metaData.tokenId && instanceId == metaData.instanceId && appId == metaData.appId && + appType == metaData.appId && bundleName == metaData.bundleName && dataDir == metaData.dataDir); } std::string StoreMetaData::GetKey(const std::initializer_list &fields) @@ -122,7 +129,10 @@ std::string StoreMetaData::GetKey(const std::initializer_list &fiel std::string StoreMetaData::GetKey() { - return GetKey({ deviceId, user, "default", bundleName, storeId }); + if (instanceId == 0) { + return GetKey({ deviceId, user, "default", bundleName, storeId }); + } + return GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) }); } std::string StoreMetaData::GetPrefix(const std::initializer_list &fields) -- Gitee From df443af96b38d6866e19bd63ba5854d5714e7e4e Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Tue, 10 May 2022 10:40:27 +0800 Subject: [PATCH 3/4] fixed code style Signed-off-by: Sven Wang --- frameworks/common/test/lru_bucket_test.cpp | 6 +++--- .../service/bootstrap/src/bootstrap.cpp | 2 +- .../service/directory/src/directory_manager.cpp | 11 ++++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/frameworks/common/test/lru_bucket_test.cpp b/frameworks/common/test/lru_bucket_test.cpp index 3c294da7b..f0965da85 100644 --- a/frameworks/common/test/lru_bucket_test.cpp +++ b/frameworks/common/test/lru_bucket_test.cpp @@ -39,7 +39,7 @@ protected: { bucket_.ResetCapacity(0); bucket_.ResetCapacity(TEST_CAPACITY); - for (int i = 0; i < TEST_CAPACITY; ++i) { + for (size_t i = 0; i < TEST_CAPACITY; ++i) { std::string key = std::string("test_") + std::to_string(i); TestValue value = {key, key, "case"}; bucket_.Set(key, value); @@ -80,7 +80,7 @@ HWTEST_F(LRUBucketTest, insert, TestSize.Level0) HWTEST_F(LRUBucketTest, cap_one_insert, TestSize.Level0) { bucket_.ResetCapacity(1); - for (int i = 0; i <= TEST_CAPACITY; ++i) { + for (size_t i = 0; i <= TEST_CAPACITY; ++i) { std::string key = std::string("test_") + std::to_string(i); TestValue value = {key, key, "find"}; bucket_.Set(key, value); @@ -100,7 +100,7 @@ HWTEST_F(LRUBucketTest, cap_one_insert, TestSize.Level0) HWTEST_F(LRUBucketTest, cap_zero_insert, TestSize.Level0) { bucket_.ResetCapacity(0); - for (int i = 0; i <= TEST_CAPACITY; ++i) { + for (size_t i = 0; i <= TEST_CAPACITY; ++i) { std::string key = std::string("test_") + std::to_string(i); TestValue value = {key, key, "find"}; bucket_.Set(key, value); diff --git a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp index 2dfedbf7c..0f66984f9 100644 --- a/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp +++ b/services/distributeddataservice/service/bootstrap/src/bootstrap.cpp @@ -79,7 +79,7 @@ void Bootstrap::LoadDirectory() return; } std::vector strategies(config->strategy.size()); - for (int i = 0; i < config->strategy.size(); ++i) { + for (size_t i = 0; i < config->strategy.size(); ++i) { strategies[i] = config->strategy[i]; } DirectoryManager::GetInstance().Initialize(strategies); diff --git a/services/distributeddataservice/service/directory/src/directory_manager.cpp b/services/distributeddataservice/service/directory/src/directory_manager.cpp index b6cd22f29..bf3bc5d74 100644 --- a/services/distributeddataservice/service/directory/src/directory_manager.cpp +++ b/services/distributeddataservice/service/directory/src/directory_manager.cpp @@ -68,7 +68,7 @@ std::string DirectoryManager::GetMetaStorePath(uint32_t version) void DirectoryManager::Initialize(const std::vector &strategies) { strategies_.resize(strategies.size()); - for (int i = 0; i < strategies.size(); ++i) { + for (size_t i = 0; i < strategies.size(); ++i) { const Strategy &strategy = strategies[i]; StrategyImpl &impl = strategies_[i]; impl.autoCreate = strategy.autoCreate; @@ -110,7 +110,8 @@ std::string DirectoryManager::GetSecurity(const StoreMetaData &metaData) const if ((metaData.bundleName != metaData.appId) || (metaData.appType != "harmony")) { break; } - case SecurityLevel::S0: + [[fallthrough]]; + case SecurityLevel::S0: [[fallthrough]]; case SecurityLevel::S1: return "misc_de"; } @@ -162,7 +163,7 @@ std::vector DirectoryManager::Split(const std::string &source, cons int32_t DirectoryManager::GetVersionIndex(uint32_t version) const { - for (int i = 0; i < strategies_.size(); ++i) { + for (size_t i = 0; i < strategies_.size(); ++i) { if (version >= strategies_[i].version) { return i; } @@ -173,7 +174,7 @@ int32_t DirectoryManager::GetVersionIndex(uint32_t version) const std::vector DirectoryManager::GetVersions() { std::vector versions; - for (int i = 0; i < strategies_.size(); ++i) { + for (size_t i = 0; i < strategies_.size(); ++i) { versions[i] = strategies_[i].version; } return versions; @@ -187,7 +188,7 @@ std::string DirectoryManager::GenPath(const StoreMetaData &metaData, uint32_t ve } std::string path; auto &strategy = strategies_[index]; - for (int i = 0; i < strategy.pipes.size(); ++i) { + for (size_t i = 0; i < strategy.pipes.size(); ++i) { std::string section; if (strategy.pipes[i] == nullptr) { section = strategy.path[i]; -- Gitee From fc979c7ffc67c0abd1919d10f341842d630f8eac Mon Sep 17 00:00:00 2001 From: Sven Wang Date: Tue, 10 May 2022 14:56:22 +0800 Subject: [PATCH 4/4] update description Signed-off-by: Sven Wang --- .../service/test/directory_manager_test.cpp | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/services/distributeddataservice/service/test/directory_manager_test.cpp b/services/distributeddataservice/service/test/directory_manager_test.cpp index 5173eab26..0182c277b 100644 --- a/services/distributeddataservice/service/test/directory_manager_test.cpp +++ b/services/distributeddataservice/service/test/directory_manager_test.cpp @@ -31,7 +31,7 @@ public: static void SetUpTestCase() { HapInfoParams info = { - .userID = 100, .bundleName = "ohos.test.demo", .instIndex = 0, .appIDDesc = "ohos.test.demo" + .userID = 100, .bundleName = "ohos.test.demo", .instIndex = 0, .appIDDesc = "ohos.test.demo_09AEF01D" }; HapPolicyParams policy = { .apl = APL_NORMAL, @@ -60,7 +60,11 @@ public: }; AccessTokenKit::AllocHapToken(info, policy); } - static void TearDownTestCase() {} + static void TearDownTestCase() + { + auto tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0); + AccessTokenKit::DeleteToken(tokenId); + } void SetUp() { tokenParam_.processName = "foundation"; @@ -69,15 +73,15 @@ public: void TearDown() {} protected: - NativeTokenInfoParams tokenParam_{0}; + NativeTokenInfoParams tokenParam_{ 0 }; }; /** -* @tc.name: GetStoragePath01 -* @tc.desc: test get db dir +* @tc.name: GetBundleStorePath +* @tc.desc: test get kv db dir * @tc.type: FUNC * @tc.require: -* @tc.author: baoyayong +* @tc.author: Sven Wang */ HWTEST_F(DirectoryManagerTest, GetBundleStorePath, TestSize.Level0) { @@ -97,10 +101,10 @@ HWTEST_F(DirectoryManagerTest, GetBundleStorePath, TestSize.Level0) /** * @tc.name: GetHapStorePath -* @tc.desc: test get db dir +* @tc.desc: test get kv db dir * @tc.type: FUNC * @tc.require: -* @tc.author: baoyayong +* @tc.author: Sven Wang */ HWTEST_F(DirectoryManagerTest, GetHapStorePath, TestSize.Level0) { @@ -117,10 +121,10 @@ HWTEST_F(DirectoryManagerTest, GetHapStorePath, TestSize.Level0) /** * @tc.name: GetSaStorePath -* @tc.desc: test get db dir +* @tc.desc: test get kv db dir * @tc.type: FUNC * @tc.require: -* @tc.author: baoyayong +* @tc.author: Sven Wang */ HWTEST_F(DirectoryManagerTest, GetSaStorePath, TestSize.Level0) { @@ -137,13 +141,13 @@ HWTEST_F(DirectoryManagerTest, GetSaStorePath, TestSize.Level0) } /** -* @tc.name: GetSaStorePath +* @tc.name: GetSaStorePathV0 * @tc.desc: test get db dir * @tc.type: FUNC * @tc.require: -* @tc.author: baoyayong +* @tc.author: Sven Wang */ -HWTEST_F(DirectoryManagerTest, GetAppStorePathV0, TestSize.Level0) +HWTEST_F(DirectoryManagerTest, GetSaStorePathV0, TestSize.Level0) { StoreMetaData metaData; metaData.user = "0"; @@ -158,13 +162,12 @@ HWTEST_F(DirectoryManagerTest, GetAppStorePathV0, TestSize.Level0) EXPECT_EQ(path, metaData.dataDir); } - /** * @tc.name: GetRdbBackupPath -* @tc.desc: test get db backup dir +* @tc.desc: test get rdb backup dir * @tc.type: FUNC * @tc.require: -* @tc.author: baoyayong +* @tc.author: Sven Wang */ HWTEST_F(DirectoryManagerTest, GetRDBBackupPath, TestSize.Level0) { @@ -183,8 +186,8 @@ HWTEST_F(DirectoryManagerTest, GetRDBBackupPath, TestSize.Level0) EXPECT_EQ(path, metaData.dataDir + "/backup"); } /** -* @tc.name: GetRdbBackupPath -* @tc.desc: test get db backup dir +* @tc.name: GetKVDBBackupPath +* @tc.desc: test get kv db backup dir * @tc.type: FUNC * @tc.require: * @tc.author: baoyayong @@ -207,13 +210,13 @@ HWTEST_F(DirectoryManagerTest, GetKVDBBackupPath, TestSize.Level0) } /** -* @tc.name: GetStorageMetaPath01 -* @tc.desc: test get db meta dir +* @tc.name: GetStorageMetaPath +* @tc.desc: test get meta store dir * @tc.type: FUNC * @tc.require: -* @tc.author: baoyayong +* @tc.author: Sven Wang */ -HWTEST_F(DirectoryManagerTest, GetStorageMetaPath01, TestSize.Level0) +HWTEST_F(DirectoryManagerTest, GetStorageMetaPath, TestSize.Level0) { auto path = DirectoryManager::GetInstance().GetMetaStorePath(); EXPECT_EQ(path, "/data/service/el1/public/database/distributeddata/meta"); -- Gitee