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 const std::string HEX_CHAR_MAP = "0123456789abcdef";
54 const std::string CAP_HEX_CHAR_MAP = "0123456789ABCDEF";
55 }
56
CreateDirectory(const std::string & directory)57 int DBCommon::CreateDirectory(const std::string &directory)
58 {
59 bool isExisted = OS::CheckPathExistence(directory);
60 if (!isExisted) {
61 int errCode = OS::MakeDBDirectory(directory);
62 if (errCode != E_OK) {
63 return errCode;
64 }
65 }
66 return E_OK;
67 }
68
StringToVector(const std::string & src,std::vector<uint8_t> & dst)69 void DBCommon::StringToVector(const std::string &src, std::vector<uint8_t> &dst)
70 {
71 dst.resize(src.size());
72 dst.assign(src.begin(), src.end());
73 }
74
VectorToString(const std::vector<uint8_t> & src,std::string & dst)75 void DBCommon::VectorToString(const std::vector<uint8_t> &src, std::string &dst)
76 {
77 dst.clear();
78 dst.assign(src.begin(), src.end());
79 }
80
VectorToHexString(const std::vector<uint8_t> & inVec,const std::string & separator)81 std::string DBCommon::VectorToHexString(const std::vector<uint8_t> &inVec, const std::string &separator)
82 {
83 std::string outString;
84 for (auto &entry : inVec) {
85 outString.push_back(CAP_HEX_CHAR_MAP[entry >> 4]); // high 4 bits to one hex.
86 outString.push_back(CAP_HEX_CHAR_MAP[entry & 0x0F]); // low 4 bits to one hex.
87 outString += separator;
88 }
89 outString.erase(outString.size() - separator.size(), separator.size()); // remove needless separator at last
90 return outString;
91 }
92
PrintHexVector(const std::vector<uint8_t> & data,int line,const std::string & tag)93 void DBCommon::PrintHexVector(const std::vector<uint8_t> &data, int line, const std::string &tag)
94 {
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] = CAP_HEX_CHAR_MAP[data[i] >> 4]; // high 4 bits to one hex.
110 buff[byteHexNum * i + 1] = CAP_HEX_CHAR_MAP[data[i] & 0x0F]; // low 4 bits 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
145 std::string tmp;
146 for (auto item : origStr) {
147 unsigned char currentByte = static_cast<unsigned char>(item);
148 tmp.push_back(HEX_CHAR_MAP[currentByte >> 4]); // high 4 bits to one hex.
149 tmp.push_back(HEX_CHAR_MAP[currentByte & 0x0F]); // low 4 bits 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,int32_t instanceId)284 std::string DBCommon::GenerateIdentifierId(const std::string &storeId,
285 const std::string &appId, const std::string &userId, int32_t instanceId)
286 {
287 std::string id = userId + "-" + appId + "-" + storeId;
288 if (instanceId != 0) {
289 id += "-" + std::to_string(instanceId);
290 }
291 return id;
292 }
293
GenerateDualTupleIdentifierId(const std::string & storeId,const std::string & appId)294 std::string DBCommon::GenerateDualTupleIdentifierId(const std::string &storeId, const std::string &appId)
295 {
296 return appId + "-" + storeId;
297 }
298
SetDatabaseIds(KvDBProperties & properties,const std::string & appId,const std::string & userId,const std::string & storeId,int32_t instanceId)299 void DBCommon::SetDatabaseIds(KvDBProperties &properties, const std::string &appId, const std::string &userId,
300 const std::string &storeId, int32_t instanceId)
301 {
302 properties.SetIdentifier(userId, appId, storeId, instanceId);
303 std::string oriStoreDir;
304 // IDENTIFIER_DIR no need cal with instanceId
305 std::string identifier = GenerateIdentifierId(storeId, appId, userId);
306 if (properties.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false)) {
307 oriStoreDir = storeId;
308 } else {
309 oriStoreDir = identifier;
310 }
311 std::string hashIdentifier = TransferHashString(identifier);
312 std::string hashDir = TransferHashString(oriStoreDir);
313 std::string hexHashDir = TransferStringToHex(hashDir);
314 properties.SetStringProp(KvDBProperties::IDENTIFIER_DIR, hexHashDir);
315 }
316
StringMasking(const std::string & oriStr,size_t remain)317 std::string DBCommon::StringMasking(const std::string &oriStr, size_t remain)
318 {
319 if (oriStr.size() > remain) {
320 return oriStr.substr(0, remain);
321 }
322 return oriStr;
323 }
324
GetDistributedTableName(const std::string & device,const std::string & tableName)325 std::string DBCommon::GetDistributedTableName(const std::string &device, const std::string &tableName)
326 {
327 std::string deviceHashHex = DBCommon::TransferStringToHex(DBCommon::TransferHashString(device));
328 return DBConstant::RELATIONAL_PREFIX + tableName + "_" + deviceHashHex;
329 }
330
GetDeviceFromName(const std::string & deviceTableName,std::string & deviceHash,std::string & tableName)331 void DBCommon::GetDeviceFromName(const std::string &deviceTableName, std::string &deviceHash, std::string &tableName)
332 {
333 std::size_t found = deviceTableName.rfind('_');
334 if (found != std::string::npos && found + 1 < deviceTableName.length() &&
335 found > DBConstant::RELATIONAL_PREFIX.length()) {
336 deviceHash = deviceTableName.substr(found + 1);
337 tableName = deviceTableName.substr(DBConstant::RELATIONAL_PREFIX.length(),
338 found - DBConstant::RELATIONAL_PREFIX.length());
339 }
340 }
341
TrimSpace(const std::string & input)342 std::string DBCommon::TrimSpace(const std::string &input)
343 {
344 std::string res;
345 res.reserve(input.length());
346 bool isPreSpace = true;
347 for (char c : input) {
348 if (std::isspace(c)) {
349 isPreSpace = true;
350 } else {
351 if (!res.empty() && isPreSpace) {
352 res += ' ';
353 }
354 res += c;
355 isPreSpace = false;
356 }
357 }
358 res.shrink_to_fit();
359 return res;
360 }
361
362 namespace {
CharIn(char c,const std::string & pattern)363 bool CharIn(char c, const std::string &pattern)
364 {
365 return std::any_of(pattern.begin(), pattern.end(), [c] (char p) {
366 return c == p;
367 });
368 }
369 }
370
HasConstraint(const std::string & sql,const std::string & keyWord,const std::string & prePattern,const std::string & nextPattern)371 bool DBCommon::HasConstraint(const std::string &sql, const std::string &keyWord, const std::string &prePattern,
372 const std::string &nextPattern)
373 {
374 size_t pos = 0;
375 while ((pos = sql.find(keyWord, pos)) != std::string::npos) {
376 if (pos - 1 >= 0 && CharIn(sql[pos - 1], prePattern) && ((pos + keyWord.length() == sql.length()) ||
377 ((pos + keyWord.length() < sql.length()) && CharIn(sql[pos + keyWord.length()], nextPattern)))) {
378 return true;
379 }
380 pos++;
381 }
382 return false;
383 }
384
IsSameCipher(CipherType srcType,CipherType inputType)385 bool DBCommon::IsSameCipher(CipherType srcType, CipherType inputType)
386 {
387 // At present, the default type is AES-256-GCM.
388 // So when src is default and input is AES-256-GCM,
389 // or when src is AES-256-GCM and input is default,
390 // we think they are the same type.
391 if (srcType == inputType ||
392 ((srcType == CipherType::DEFAULT || srcType == CipherType::AES_256_GCM) &&
393 (inputType == CipherType::DEFAULT || inputType == CipherType::AES_256_GCM))) {
394 return true;
395 }
396 return false;
397 }
398
CheckIsAlnumAndUnderscore(const std::string & text)399 bool DBCommon::CheckIsAlnumAndUnderscore(const std::string &text)
400 {
401 auto iter = std::find_if_not(text.begin(), text.end(), [](char c) {
402 return (std::isalnum(c) || c == '_');
403 });
404 return iter == text.end();
405 }
406 } // namespace DistributedDB
407