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 #include "db_common.h"
17
18 #include <climits>
19 #include <cstdio>
20
21 #include "db_errno.h"
22 #include "platform_specific.h"
23 #include "hash.h"
24 #include "value_hash_calc.h"
25
26 namespace DistributedDB {
27 namespace {
RemoveFiles(const std::list<OS::FileAttr> & fileList,OS::FileType type)28 void RemoveFiles(const std::list<OS::FileAttr> &fileList, OS::FileType type)
29 {
30 for (const auto &item : fileList) {
31 if (item.fileType != type) {
32 continue;
33 }
34 int errCode = OS::RemoveFile(item.fileName.c_str());
35 if (errCode != E_OK) {
36 LOGE("Remove file failed:%d", errno);
37 }
38 }
39 }
40
RemoveDirectories(const std::list<OS::FileAttr> & fileList,OS::FileType type)41 void RemoveDirectories(const std::list<OS::FileAttr> &fileList, OS::FileType type)
42 {
43 for (auto item = fileList.rbegin(); item != fileList.rend(); ++item) {
44 if (item->fileType != type) {
45 continue;
46 }
47 int errCode = OS::RemoveDBDirectory(item->fileName);
48 if (errCode != 0) {
49 LOGE("Remove directory failed:%d", errno);
50 }
51 }
52 }
53 }
54
CreateDirectory(const std::string & directory)55 int DBCommon::CreateDirectory(const std::string &directory)
56 {
57 bool isExisted = OS::CheckPathExistence(directory);
58 if (!isExisted) {
59 int errCode = OS::MakeDBDirectory(directory);
60 if (errCode != E_OK) {
61 return errCode;
62 }
63 }
64 return E_OK;
65 }
66
StringToVector(const std::string & src,std::vector<uint8_t> & dst)67 void DBCommon::StringToVector(const std::string &src, std::vector<uint8_t> &dst)
68 {
69 dst.resize(src.size());
70 dst.assign(src.begin(), src.end());
71 }
72
VectorToString(const std::vector<uint8_t> & src,std::string & dst)73 void DBCommon::VectorToString(const std::vector<uint8_t> &src, std::string &dst)
74 {
75 dst.clear();
76 dst.assign(src.begin(), src.end());
77 }
78
VectorToHexString(const std::vector<uint8_t> & inVec,const std::string & separator)79 std::string DBCommon::VectorToHexString(const std::vector<uint8_t> &inVec, const std::string &separator)
80 {
81 std::string hexChar = "0123456789ABCDEF";
82 std::string outString;
83 for (auto &entry : inVec) {
84 outString.push_back(hexChar[entry >> 4]); // high 4 bit to one hex.
85 outString.push_back(hexChar[entry & 0x0F]); // low 4 bit to one hex.
86 outString += separator;
87 }
88 outString.erase(outString.size() - separator.size(), separator.size()); // remove needless separator at last
89 return outString;
90 }
91
PrintHexVector(const std::vector<uint8_t> & data,int line,const std::string & tag)92 void DBCommon::PrintHexVector(const std::vector<uint8_t> &data, int line, const std::string &tag)
93 {
94 const char *hex = "0123456789ABCDEF";
95 const size_t maxDataLength = 1024;
96 const int byteHexNum = 2;
97 size_t dataLength = data.size();
98
99 if (data.size() > maxDataLength) {
100 dataLength = maxDataLength;
101 }
102
103 char *buff = new (std::nothrow) char[dataLength * byteHexNum + 1]; // dual and add one for the end;
104 if (buff == nullptr) {
105 return;
106 }
107
108 for (std::vector<uint8_t>::size_type i = 0; i < dataLength; ++i) {
109 buff[byteHexNum * i] = hex[data[i] >> 4]; // high 4 bit to one hex.
110 buff[byteHexNum * i + 1] = hex[data[i] & 0x0F]; // low 4 bit to one hex.
111 }
112 buff[dataLength * byteHexNum] = '\0';
113
114 if (line == 0) {
115 LOGD("[%s] size:%zu -- %s", tag.c_str(), data.size(), buff);
116 } else {
117 LOGD("[%s][%d] size:%zu -- %s", tag.c_str(), line, data.size(), buff);
118 }
119
120 delete []buff;
121 return;
122 }
123
TransferHashString(const std::string & devName)124 std::string DBCommon::TransferHashString(const std::string &devName)
125 {
126 if (devName.empty()) {
127 return "";
128 }
129 std::vector<uint8_t> devVect(devName.begin(), devName.end());
130 std::vector<uint8_t> hashVect;
131 int errCode = CalcValueHash(devVect, hashVect);
132 if (errCode != E_OK) {
133 return "";
134 }
135
136 return std::string(hashVect.begin(), hashVect.end());
137 }
138
TransferStringToHex(const std::string & origStr)139 std::string DBCommon::TransferStringToHex(const std::string &origStr)
140 {
141 if (origStr.empty()) {
142 return "";
143 }
144 const char *hex = "0123456789abcdef";
145 std::string tmp;
146 for (auto item : origStr) {
147 unsigned char currentByte = static_cast<unsigned char>(item);
148 tmp.push_back(hex[currentByte >> 4]); // high 4 bit to one hex.
149 tmp.push_back(hex[currentByte & 0x0F]); // low 4 bit to one hex.
150 }
151 return tmp;
152 }
153
CalcValueHash(const std::vector<uint8_t> & value,std::vector<uint8_t> & hashValue)154 int DBCommon::CalcValueHash(const std::vector<uint8_t> &value, std::vector<uint8_t> &hashValue)
155 {
156 ValueHashCalc hashCalc;
157 int errCode = hashCalc.Initialize();
158 if (errCode != E_OK) {
159 return -E_INTERNAL_ERROR;
160 }
161
162 errCode = hashCalc.Update(value);
163 if (errCode != E_OK) {
164 return -E_INTERNAL_ERROR;
165 }
166
167 errCode = hashCalc.GetResult(hashValue);
168 if (errCode != E_OK) {
169 return -E_INTERNAL_ERROR;
170 }
171
172 return E_OK;
173 }
174
CreateStoreDirectory(const std::string & directory,const std::string & identifierName,const std::string & subDir,bool isCreate)175 int DBCommon::CreateStoreDirectory(const std::string &directory, const std::string &identifierName,
176 const std::string &subDir, bool isCreate)
177 {
178 std::string newDir = directory;
179 if (newDir.back() != '/') {
180 newDir += "/";
181 }
182
183 newDir += identifierName;
184 if (!isCreate) {
185 if (!OS::CheckPathExistence(newDir)) {
186 LOGE("Required path does not exist and won't create.");
187 return -E_INVALID_ARGS;
188 }
189 return E_OK;
190 }
191
192 if (directory.empty()) {
193 return -E_INVALID_ARGS;
194 }
195
196 int errCode = DBCommon::CreateDirectory(newDir);
197 if (errCode != E_OK) {
198 return errCode;
199 }
200
201 newDir += ("/" + subDir);
202 return DBCommon::CreateDirectory(newDir);
203 }
204
CopyFile(const std::string & srcFile,const std::string & dstFile)205 int DBCommon::CopyFile(const std::string &srcFile, const std::string &dstFile)
206 {
207 const int copyBlockSize = 4096;
208 std::vector<uint8_t> tmpBlock(copyBlockSize, 0);
209 int errCode;
210 FILE *fileIn = fopen(srcFile.c_str(), "rb");
211 if (fileIn == nullptr) {
212 LOGE("[Common:CpFile] open the source file error:%d", errno);
213 return -E_INVALID_FILE;
214 }
215 FILE *fileOut = fopen(dstFile.c_str(), "wb");
216 if (fileOut == nullptr) {
217 LOGE("[Common:CpFile] open the target file error:%d", errno);
218 errCode = -E_INVALID_FILE;
219 goto END;
220 }
221 for (;;) {
222 size_t readSize = fread(static_cast<void *>(tmpBlock.data()), 1, copyBlockSize, fileIn);
223 if (readSize < copyBlockSize) {
224 // not end and have error.
225 if (feof(fileIn) != 0 && ferror(fileIn) != 0) {
226 LOGE("Copy the file error:%d", errno);
227 errCode = -E_SYSTEM_API_FAIL;
228 break;
229 }
230 }
231
232 if (readSize != 0) {
233 size_t writeSize = fwrite(static_cast<void *>(tmpBlock.data()), 1, readSize, fileOut);
234 if (ferror(fileOut) != 0 || writeSize != readSize) {
235 LOGE("Write the data while copy:%d", errno);
236 errCode = -E_SYSTEM_API_FAIL;
237 break;
238 }
239 }
240
241 if (feof(fileIn) != 0) {
242 errCode = E_OK;
243 break;
244 }
245 }
246
247 END:
248 if (fileIn != nullptr) {
249 (void)fclose(fileIn);
250 fileIn = nullptr;
251 }
252 if (fileOut != nullptr) {
253 (void)fclose(fileOut);
254 fileOut = nullptr;
255 }
256 return errCode;
257 }
258
RemoveAllFilesOfDirectory(const std::string & dir,bool isNeedRemoveDir)259 int DBCommon::RemoveAllFilesOfDirectory(const std::string &dir, bool isNeedRemoveDir)
260 {
261 std::list<OS::FileAttr> fileList;
262 bool isExisted = OS::CheckPathExistence(dir);
263 if (!isExisted) {
264 return E_OK;
265 }
266 int errCode = OS::GetFileAttrFromPath(dir, fileList, true);
267 if (errCode != E_OK) {
268 return errCode;
269 }
270
271 RemoveFiles(fileList, OS::FileType::FILE);
272 RemoveDirectories(fileList, OS::FileType::PATH);
273 if (isNeedRemoveDir) {
274 // Pay attention to the order of deleting the directory
275 if (OS::CheckPathExistence(dir) && OS::RemoveDBDirectory(dir.c_str()) != 0) {
276 LOGI("Remove the directory error:%d", errno);
277 errCode = -E_SYSTEM_API_FAIL;
278 }
279 }
280
281 return errCode;
282 }
283
GenerateIdentifierId(const std::string & storeId,const std::string & appId,const std::string & userId)284 std::string DBCommon::GenerateIdentifierId(const std::string &storeId,
285 const std::string &appId, const std::string &userId)
286 {
287 return userId + "-" + appId + "-" + storeId;
288 }
289
GenerateDualTupleIdentifierId(const std::string & storeId,const std::string & appId)290 std::string DBCommon::GenerateDualTupleIdentifierId(const std::string &storeId, const std::string &appId)
291 {
292 return appId + "-" + storeId;
293 }
294
SetDatabaseIds(KvDBProperties & properties,const std::string & appId,const std::string & userId,const std::string & storeId)295 void DBCommon::SetDatabaseIds(KvDBProperties &properties, const std::string &appId, const std::string &userId,
296 const std::string &storeId)
297 {
298 properties.SetIdentifier(userId, appId, storeId);
299 std::string oriStoreDir;
300 std::string identifier = GenerateIdentifierId(storeId, appId, userId);
301 if (properties.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false)) {
302 oriStoreDir = storeId;
303 } else {
304 oriStoreDir = identifier;
305 }
306 std::string hashIdentifier = TransferHashString(identifier);
307 std::string hashDir = TransferHashString(oriStoreDir);
308 std::string hexHashDir = TransferStringToHex(hashDir);
309 properties.SetStringProp(KvDBProperties::IDENTIFIER_DIR, hexHashDir);
310 }
311
StringMasking(const std::string & oriStr,size_t remain)312 std::string DBCommon::StringMasking(const std::string &oriStr, size_t remain)
313 {
314 if (oriStr.size() > remain) {
315 return oriStr.substr(0, remain);
316 }
317 return oriStr;
318 }
319
GetDistributedTableName(const std::string & device,const std::string & tableName)320 std::string DBCommon::GetDistributedTableName(const std::string &device, const std::string &tableName)
321 {
322 std::string deviceHashHex = DBCommon::TransferStringToHex(DBCommon::TransferHashString(device));
323 return DBConstant::RELATIONAL_PREFIX + tableName + "_" + deviceHashHex;
324 }
325
GetDeviceFromName(const std::string & deviceTableName,std::string & deviceHash,std::string & tableName)326 void DBCommon::GetDeviceFromName(const std::string &deviceTableName, std::string &deviceHash, std::string &tableName)
327 {
328 std::size_t found = deviceTableName.rfind('_');
329 if (found != std::string::npos && found + 1 < deviceTableName.length() &&
330 found > DBConstant::RELATIONAL_PREFIX.length()) {
331 deviceHash = deviceTableName.substr(found + 1);
332 tableName = deviceTableName.substr(DBConstant::RELATIONAL_PREFIX.length(),
333 found - DBConstant::RELATIONAL_PREFIX.length());
334 }
335 }
336
CheckIsAlnumAndUnderscore(const std::string & text)337 bool DBCommon::CheckIsAlnumAndUnderscore(const std::string &text)
338 {
339 auto iter = std::find_if_not(text.begin(), text.end(), [](char c) {
340 return (std::isalnum(c) || c == '_');
341 });
342 return iter == text.end();
343 }
344 } // namespace DistributedDB
345