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     };
34     API_EXPORT static DirectoryManager &GetInstance();
35     API_EXPORT std::string GetStorePath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION);
36     API_EXPORT std::string GetSecretKeyPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION);
37     API_EXPORT std::string GetStoreBackupPath(const StoreMetaData &metaData, uint32_t version = INVALID_VERSION);
38     API_EXPORT std::string GetMetaStorePath(uint32_t version = INVALID_VERSION);
39     API_EXPORT std::string GetMetaBackupPath(uint32_t version = INVALID_VERSION);
40     API_EXPORT std::vector<uint32_t> GetVersions();
41     API_EXPORT void Initialize(const std::vector<Strategy> &strategies);
42     API_EXPORT bool CreateDirectory(const std::string &path) const;
43 
44 private:
45     using Action = std::string (DirectoryManager::*)(const StoreMetaData &) const;
46     struct StrategyImpl {
47         bool autoCreate = false;
48         uint32_t version;
49         std::string metaPath;
50         std::vector<std::string> path;
51         std::vector<Action> pipes;
52     };
53 
54     DirectoryManager();
55     std::string GetType(const StoreMetaData &metaData) const;
56     std::string GetStore(const StoreMetaData &metaData) const;
57     std::string GetSecurity(const StoreMetaData &metaData) const;
58     std::string GetArea(const StoreMetaData &metaData) const;
59     std::string GetUserId(const StoreMetaData &metaData) const;
60     std::string GetBundleName(const StoreMetaData &metaData) const;
61     std::string GetHapName(const StoreMetaData &metaData) const;
62     std::string GetCustomDir(const StoreMetaData &metaData) const;
63     std::vector<std::string> Split(const std::string &source, const std::string &pattern) const;
64     int32_t GetVersionIndex(uint32_t version) const;
65     std::string GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath = "") const;
66     const std::map<std::string, Action> actions_;
67     std::vector<StrategyImpl> strategies_;
68 };
69 } // namespace OHOS::DistributedData
70 #endif // DISTRIBUTEDDATAMGR_DIRECTORY_MANAGER_H
71