• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include "metadata/store_meta_data.h"
23 #include "visibility.h"
24 namespace OHOS::DistributedData {
25 class DirectoryManager {
26 public:
27     static constexpr uint32_t INVALID_VERSION = 0xFFFFFFFF;
28     struct Strategy {
29         bool autoCreate = false;
30         uint32_t version = 0;
31         std::string pattern;
32         std::string metaPath;
33         std::string clonePath;
34     };
35     struct StoreType {
36         std::vector<int32_t> range;
37         std::string type;
38     };
39     API_EXPORT static DirectoryManager &GetInstance();
40     API_EXPORT std::string GetStorePath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION);
41     API_EXPORT std::string GetSecretKeyPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION);
42     API_EXPORT std::string GetStoreBackupPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION);
43     API_EXPORT std::string GetClonePath(const std::string &userId, uint32_t version = INVALID_VERSION);
44     API_EXPORT std::string GetMetaStorePath(uint32_t version = INVALID_VERSION);
45     API_EXPORT std::string GetMetaBackupPath(uint32_t version = INVALID_VERSION);
46     API_EXPORT std::vector<uint32_t> GetVersions();
47     API_EXPORT void Initialize(const std::vector<Strategy> &strategies, const std::vector<StoreType> &storeTypes);
48     API_EXPORT bool CreateDirectory(const std::string &path) const;
49 
50 private:
51     using Action = std::string (DirectoryManager::*)(const StoreMetaData &) const;
52     struct StrategyImpl {
53         bool autoCreate = false;
54         uint32_t version;
55         std::string metaPath;
56         std::string clonePath;
57         std::vector<std::string> path;
58         std::vector<Action> pipes;
59     };
60 
61     DirectoryManager();
62     std::string GetType(const StoreMetaData &metaData) const;
63     std::string GetStore(const StoreMetaData &metaData) const;
64     std::string GetSecurity(const StoreMetaData &metaData) const;
65     std::string GetArea(const StoreMetaData &metaData) const;
66     std::string GetUserId(const StoreMetaData &metaData) const;
67     std::string GetBundleName(const StoreMetaData &metaData) const;
68     std::string GetHapName(const StoreMetaData &metaData) const;
69     std::string GetCustomDir(const StoreMetaData &metaData) const;
70     std::vector<std::string> Split(const std::string &source, const std::string &pattern) const;
71     int32_t GetVersionIndex(uint32_t version) const;
72     std::string GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath = "") const;
73     const std::map<std::string, Action> actions_;
74     std::vector<StrategyImpl> strategies_;
75     std::vector<StoreType> storeTypes_;
76 };
77 } // namespace OHOS::DistributedData
78 #endif // DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H
79