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 #define LOG_TAG "DirectoryManager"
16 #include "directory_manager.h"
17
18 #include <string>
19
20 #include "utils/constant.h"
21 #include "directory_ex.h"
22 #include "kvstore_context.h"
23 #include "log/log_print.h"
24 namespace OHOS::DistributedData {
25 using namespace DistributedKv;
26 std::map<PathType, std::string> ServerDirWorker::rootPathMap_ = {
27 { PATH_DE, "/data/misc_de/0/mdds" },
28 { PATH_CE, "/data/misc_ce/0/mdds" },
29 };
GetDir(ClientContext clientContext,PathType type)30 std::string ServerDirWorker::GetDir(ClientContext clientContext, PathType type)
31 {
32 if (rootPathMap_.find(type) == rootPathMap_.end()) {
33 return "";
34 }
35 return DirectoryManager::JoinPath({ rootPathMap_.at(type), clientContext.userId,
36 Constant::GetDefaultHarmonyAccountName(), clientContext.bundleName });
37 }
CreateDir(ClientContext clientContext,PathType type)38 bool ServerDirWorker::CreateDir(ClientContext clientContext, PathType type)
39 {
40 std::string directory = GetDir(clientContext, type);
41 bool ret = ForceCreateDirectory(directory);
42 if (!ret) {
43 ZLOGE("create directory[%s] failed, errstr=[%d].", directory.c_str(), errno);
44 return false;
45 }
46 return true;
47 }
GetInstance()48 ServerDirWorker &ServerDirWorker::GetInstance()
49 {
50 static ServerDirWorker instance;
51 return instance;
52 }
GetBackupDir(ClientContext clientContext,PathType type)53 std::string ServerDirWorker::GetBackupDir(ClientContext clientContext, PathType type)
54 {
55 if (rootPathMap_.find(type) == rootPathMap_.end()) {
56 return "";
57 }
58 return DirectoryManager::JoinPath({ rootPathMap_.at(type), clientContext.userId,
59 Constant::GetDefaultHarmonyAccountName(), clientContext.bundleName, "backup" });
60 }
GetMetaDir()61 std::string ServerDirWorker::GetMetaDir()
62 {
63 return DirectoryManager::JoinPath({ rootPathMap_.at(PATH_DE), "Meta" });
64 }
GetSecretKeyDir(ClientContext clientContext,PathType type)65 std::string ServerDirWorker::GetSecretKeyDir(ClientContext clientContext, PathType type)
66 {
67 return GetDir(clientContext, type);
68 }
GetDir(ClientContext clientContext,PathType type)69 std::string ClientDirWorker::GetDir(ClientContext clientContext, PathType type)
70 {
71 return clientContext.dataDir;
72 }
CreateDir(ClientContext clientContext,PathType type)73 bool ClientDirWorker::CreateDir(ClientContext clientContext, PathType type)
74 {
75 return true;
76 }
GetInstance()77 ClientDirWorker &ClientDirWorker::GetInstance()
78 {
79 static ClientDirWorker instance;
80 return instance;
81 }
GetBackupDir(ClientContext clientContext,PathType type)82 std::string ClientDirWorker::GetBackupDir(ClientContext clientContext, PathType type)
83 {
84 return DirectoryManager::JoinPath({ clientContext.dataDir, "backup" });
85 }
GetMetaDir()86 std::string ClientDirWorker::GetMetaDir()
87 {
88 return std::string("/data/service/el1/public/distributeddata/DistributedKvDataService/Meta/");
89 }
GetSecretKeyDir(ClientContext clientContext,PathType type)90 std::string ClientDirWorker::GetSecretKeyDir(ClientContext clientContext, PathType type)
91 {
92 return GetDir(clientContext, type);
93 }
94
CreatePath(const ClientContext & context,PathType type)95 std::string DirectoryManager::CreatePath(const ClientContext &context, PathType type)
96 {
97 return "";
98 }
99
GetInstance()100 DirectoryManager &DirectoryManager::GetInstance()
101 {
102 static DirectoryManager instance;
103 return instance;
104 }
105
GetStorePath(const StoreMetaData & metaData)106 std::string DirectoryManager::GetStorePath(const StoreMetaData &metaData)
107 {
108 return {};
109 }
110
GetStoreBackupPath(const StoreMetaData & metaData)111 std::string DirectoryManager::GetStoreBackupPath(const StoreMetaData &metaData)
112 {
113 return GetStorePath(metaData) + "/backup/";
114 }
115
GetMetaDataStorePath()116 std::string DirectoryManager::GetMetaDataStorePath()
117 {
118 return "/data/service/el0/0/database/ddms/metadata/";
119 }
AddParams(const Strategy & strategy)120 void DirectoryManager::AddParams(const Strategy &strategy)
121 {
122 patterns_[strategy.version] = strategy;
123 }
SetCurrentVersion(const std::string & version)124 void DirectoryManager::SetCurrentVersion(const std::string &version)
125 {
126 version_ = version;
127 }
128 } // namespace OHOS::DistributedData