1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H 17 #define DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H 18 19 #include <map> 20 #include <memory> 21 #include "visibility.h" 22 #include "kvstore_context.h" 23 #include "metadata/store_meta_data.h" 24 namespace OHOS::DistributedData { 25 enum PathType { 26 PATH_DE, 27 PATH_CE, 28 }; 29 class DirWorker { 30 public: 31 DirWorker() = default; 32 virtual std::string GetDir(ClientContext clientContext, PathType type) = 0; 33 virtual bool CreateDir(ClientContext clientContext, PathType type) = 0; 34 virtual std::string GetBackupDir(ClientContext clientContext, PathType type) = 0; 35 virtual std::string GetSecretKeyDir(ClientContext clientContext, PathType type) = 0; 36 virtual std::string GetMetaDir() = 0; 37 }; 38 39 class ServerDirWorker : public DirWorker { 40 public: 41 static ServerDirWorker &GetInstance(); 42 43 std::string GetDir(ClientContext clientContext, PathType type) override; 44 bool CreateDir(ClientContext clientContext, PathType type) override; 45 std::string GetBackupDir(ClientContext clientContext, PathType type) override; 46 std::string GetSecretKeyDir(ClientContext clientContext, PathType type) override; 47 std::string GetMetaDir() override; 48 49 public: 50 static std::map<PathType, std::string> rootPathMap_; 51 }; 52 53 class ClientDirWorker : public DirWorker { 54 public: 55 static ClientDirWorker &GetInstance(); 56 57 std::string GetDir(ClientContext clientContext, PathType type) override; 58 bool CreateDir(ClientContext clientContext, PathType type) override; 59 std::string GetBackupDir(ClientContext clientContext, PathType type) override; 60 std::string GetSecretKeyDir(ClientContext clientContext, PathType type) override; 61 std::string GetMetaDir() override; 62 }; 63 64 class DirectoryManager { 65 public: 66 struct Strategy { 67 std::string version; 68 std::string holder; 69 std::string path; 70 std::string metaPath; 71 }; 72 API_EXPORT static DirectoryManager &GetInstance(); 73 API_EXPORT std::string CreatePath(const ClientContext &context, PathType type); 74 API_EXPORT std::string GetStorePath(const StoreMetaData &metaData); 75 API_EXPORT std::string GetStoreBackupPath(const StoreMetaData &metaData); 76 API_EXPORT std::string GetMetaDataStorePath(); 77 78 API_EXPORT void AddParams(const Strategy &strategy); 79 API_EXPORT void SetCurrentVersion(const std::string &version); 80 JoinPath(std::initializer_list<std::string> stringList)81 inline static std::string JoinPath(std::initializer_list<std::string> stringList) 82 { 83 std::string tmpPath; 84 for (const std::string &str : stringList) { 85 tmpPath += (str + "/"); 86 } 87 return tmpPath; 88 } 89 private: 90 std::map<std::string, Strategy> patterns_; 91 std::string version_; 92 }; 93 } // namespace OHOS::DistributedKv 94 #endif // DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H 95