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 "runtime_context.h"
25 #include "value_hash_calc.h"
26
27 namespace DistributedDB {
28 namespace {
RemoveFiles(const std::list<OS::FileAttr> & fileList,OS::FileType type)29 void RemoveFiles(const std::list<OS::FileAttr> &fileList, OS::FileType type)
30 {
31 for (const auto &item : fileList) {
32 if (item.fileType != type) {
33 continue;
34 }
35 int errCode = OS::RemoveFile(item.fileName);
36 if (errCode != E_OK) {
37 LOGE("Remove file failed:%d", errno);
38 }
39 }
40 }
41
RemoveDirectories(const std::list<OS::FileAttr> & fileList,OS::FileType type)42 void RemoveDirectories(const std::list<OS::FileAttr> &fileList, OS::FileType type)
43 {
44 for (auto item = fileList.rbegin(); item != fileList.rend(); ++item) {
45 if (item->fileType != type) {
46 continue;
47 }
48 int errCode = OS::RemoveDBDirectory(item->fileName);
49 if (errCode != 0) {
50 LOGE("Remove directory failed:%d", errno);
51 }
52 }
53 }
54 const std::string HEX_CHAR_MAP = "0123456789abcdef";
55 const std::string CAP_HEX_CHAR_MAP = "0123456789ABCDEF";
56 }
57
CreateDirectory(const std::string & directory)58 int DBCommon::CreateDirectory(const std::string &directory)
59 {
60 bool isExisted = OS::CheckPathExistence(directory);
61 if (!isExisted) {
62 int errCode = OS::MakeDBDirectory(directory);
63 if (errCode != E_OK) {
64 return errCode;
65 }
66 }
67 return E_OK;
68 }
69
StringToVector(const std::string & src,std::vector<uint8_t> & dst)70 void DBCommon::StringToVector(const std::string &src, std::vector<uint8_t> &dst)
71 {
72 dst.resize(src.size());
73 dst.assign(src.begin(), src.end());
74 }
75
VectorToString(const std::vector<uint8_t> & src,std::string & dst)76 void DBCommon::VectorToString(const std::vector<uint8_t> &src, std::string &dst)
77 {
78 dst.clear();
79 dst.assign(src.begin(), src.end());
80 }
81
VectorToHexString(const std::vector<uint8_t> & inVec,const std::string & separator)82 std::string DBCommon::VectorToHexString(const std::vector<uint8_t> &inVec, const std::string &separator)
83 {
84 std::string outString;
85 for (auto &entry : inVec) {
86 outString.push_back(CAP_HEX_CHAR_MAP[entry >> 4]); // high 4 bits to one hex.
87 outString.push_back(CAP_HEX_CHAR_MAP[entry & 0x0F]); // low 4 bits to one hex.
88 outString += separator;
89 }
90 outString.erase(outString.size() - separator.size(), separator.size()); // remove needless separator at last
91 return outString;
92 }
93
PrintHexVector(const std::vector<uint8_t> & data,int line,const std::string & tag)94 void DBCommon::PrintHexVector(const std::vector<uint8_t> &data, int line, const std::string &tag)
95 {
96 const size_t maxDataLength = 1024;
97 const int byteHexNum = 2;
98 size_t dataLength = data.size();
99
100 if (data.size() > maxDataLength) {
101 dataLength = maxDataLength;
102 }
103
104 char *buff = new (std::nothrow) char[dataLength * byteHexNum + 1]; // dual and add one for the end;
105 if (buff == nullptr) {
106 return;
107 }
108
109 for (std::vector<uint8_t>::size_type i = 0; i < dataLength; ++i) {
110 buff[byteHexNum * i] = CAP_HEX_CHAR_MAP[data[i] >> 4]; // high 4 bits to one hex.
111 buff[byteHexNum * i + 1] = CAP_HEX_CHAR_MAP[data[i] & 0x0F]; // low 4 bits to one hex.
112 }
113 buff[dataLength * byteHexNum] = '\0';
114
115 if (line == 0) {
116 LOGD("[%s] size:%zu -- %s", tag.c_str(), data.size(), buff);
117 } else {
118 LOGD("[%s][%d] size:%zu -- %s", tag.c_str(), line, data.size(), buff);
119 }
120
121 delete []buff;
122 return;
123 }
124
TransferHashString(const std::string & devName)125 std::string DBCommon::TransferHashString(const std::string &devName)
126 {
127 if (devName.empty()) {
128 return "";
129 }
130 std::vector<uint8_t> devVect(devName.begin(), devName.end());
131 std::vector<uint8_t> hashVect;
132 int errCode = CalcValueHash(devVect, hashVect);
133 if (errCode != E_OK) {
134 return "";
135 }
136
137 return std::string(hashVect.begin(), hashVect.end());
138 }
139
TransferStringToHex(const std::string & origStr)140 std::string DBCommon::TransferStringToHex(const std::string &origStr)
141 {
142 if (origStr.empty()) {
143 return "";
144 }
145
146 std::string tmp;
147 for (auto item : origStr) {
148 unsigned char currentByte = static_cast<unsigned char>(item);
149 tmp.push_back(HEX_CHAR_MAP[currentByte >> 4]); // high 4 bits to one hex.
150 tmp.push_back(HEX_CHAR_MAP[currentByte & 0x0F]); // low 4 bits to one hex.
151 }
152 return tmp;
153 }
154
CalcValueHash(const std::vector<uint8_t> & value,std::vector<uint8_t> & hashValue)155 int DBCommon::CalcValueHash(const std::vector<uint8_t> &value, std::vector<uint8_t> &hashValue)
156 {
157 ValueHashCalc hashCalc;
158 int errCode = hashCalc.Initialize();
159 if (errCode != E_OK) {
160 return -E_INTERNAL_ERROR;
161 }
162
163 errCode = hashCalc.Update(value);
164 if (errCode != E_OK) {
165 return -E_INTERNAL_ERROR;
166 }
167
168 errCode = hashCalc.GetResult(hashValue);
169 if (errCode != E_OK) {
170 return -E_INTERNAL_ERROR;
171 }
172
173 return E_OK;
174 }
175
CreateStoreDirectory(const std::string & directory,const std::string & identifierName,const std::string & subDir,bool isCreate)176 int DBCommon::CreateStoreDirectory(const std::string &directory, const std::string &identifierName,
177 const std::string &subDir, bool isCreate)
178 {
179 std::string newDir = directory;
180 if (newDir.back() != '/') {
181 newDir += "/";
182 }
183
184 newDir += identifierName;
185 if (!isCreate) {
186 if (!OS::CheckPathExistence(newDir)) {
187 LOGE("Required path does not exist and won't create.");
188 return -E_INVALID_ARGS;
189 }
190 return E_OK;
191 }
192
193 if (directory.empty()) {
194 return -E_INVALID_ARGS;
195 }
196
197 int errCode = DBCommon::CreateDirectory(newDir);
198 if (errCode != E_OK) {
199 return errCode;
200 }
201
202 newDir += ("/" + subDir);
203 return DBCommon::CreateDirectory(newDir);
204 }
205
CopyFile(const std::string & srcFile,const std::string & dstFile)206 int DBCommon::CopyFile(const std::string &srcFile, const std::string &dstFile)
207 {
208 const int copyBlockSize = 4096;
209 std::vector<uint8_t> tmpBlock(copyBlockSize, 0);
210 int errCode;
211 FILE *fileIn = fopen(srcFile.c_str(), "rb");
212 if (fileIn == nullptr) {
213 LOGE("[Common:CpFile] open the source file error:%d", errno);
214 return -E_INVALID_FILE;
215 }
216 FILE *fileOut = fopen(dstFile.c_str(), "wb");
217 if (fileOut == nullptr) {
218 LOGE("[Common:CpFile] open the target file error:%d", errno);
219 errCode = -E_INVALID_FILE;
220 goto END;
221 }
222 for (;;) {
223 size_t readSize = fread(static_cast<void *>(tmpBlock.data()), 1, copyBlockSize, fileIn);
224 if (readSize < copyBlockSize) {
225 // not end and have error.
226 if (feof(fileIn) != 0 && ferror(fileIn) != 0) {
227 LOGE("Copy the file error:%d", errno);
228 errCode = -E_SYSTEM_API_FAIL;
229 break;
230 }
231 }
232
233 if (readSize != 0) {
234 size_t writeSize = fwrite(static_cast<void *>(tmpBlock.data()), 1, readSize, fileOut);
235 if (ferror(fileOut) != 0 || writeSize != readSize) {
236 LOGE("Write the data while copy:%d", errno);
237 errCode = -E_SYSTEM_API_FAIL;
238 break;
239 }
240 }
241
242 if (feof(fileIn) != 0) {
243 errCode = E_OK;
244 break;
245 }
246 }
247
248 END:
249 if (fileIn != nullptr) {
250 (void)fclose(fileIn);
251 }
252 if (fileOut != nullptr) {
253 (void)fclose(fileOut);
254 }
255 return errCode;
256 }
257
RemoveAllFilesOfDirectory(const std::string & dir,bool isNeedRemoveDir)258 int DBCommon::RemoveAllFilesOfDirectory(const std::string &dir, bool isNeedRemoveDir)
259 {
260 std::list<OS::FileAttr> fileList;
261 bool isExisted = OS::CheckPathExistence(dir);
262 if (!isExisted) {
263 return E_OK;
264 }
265 int errCode = OS::GetFileAttrFromPath(dir, fileList, true);
266 if (errCode != E_OK) {
267 return errCode;
268 }
269
270 RemoveFiles(fileList, OS::FileType::FILE);
271 RemoveDirectories(fileList, OS::FileType::PATH);
272 if (isNeedRemoveDir) {
273 // Pay attention to the order of deleting the directory
274 if (OS::CheckPathExistence(dir) && OS::RemoveDBDirectory(dir) != 0) {
275 LOGI("Remove the directory error:%d", errno);
276 errCode = -E_SYSTEM_API_FAIL;
277 }
278 }
279
280 return errCode;
281 }
282
GenerateIdentifierId(const std::string & storeId,const std::string & appId,const std::string & userId,int32_t instanceId)283 std::string DBCommon::GenerateIdentifierId(const std::string &storeId,
284 const std::string &appId, const std::string &userId, int32_t instanceId)
285 {
286 std::string id = userId + "-" + appId + "-" + storeId;
287 if (instanceId != 0) {
288 id += "-" + std::to_string(instanceId);
289 }
290 return id;
291 }
292
GenerateDualTupleIdentifierId(const std::string & storeId,const std::string & appId)293 std::string DBCommon::GenerateDualTupleIdentifierId(const std::string &storeId, const std::string &appId)
294 {
295 return appId + "-" + storeId;
296 }
297
SetDatabaseIds(KvDBProperties & properties,const std::string & appId,const std::string & userId,const std::string & storeId,int32_t instanceId)298 void DBCommon::SetDatabaseIds(KvDBProperties &properties, const std::string &appId, const std::string &userId,
299 const std::string &storeId, int32_t instanceId)
300 {
301 properties.SetIdentifier(userId, appId, storeId, instanceId);
302 std::string oriStoreDir;
303 // IDENTIFIER_DIR no need cal with instanceId
304 std::string identifier = GenerateIdentifierId(storeId, appId, userId);
305 if (properties.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false)) {
306 oriStoreDir = storeId;
307 } else {
308 oriStoreDir = identifier;
309 }
310 std::string hashIdentifier = TransferHashString(identifier);
311 std::string hashDir = TransferHashString(oriStoreDir);
312 std::string hexHashDir = TransferStringToHex(hashDir);
313 properties.SetStringProp(KvDBProperties::IDENTIFIER_DIR, hexHashDir);
314 }
315
StringMasking(const std::string & oriStr,size_t remain)316 std::string DBCommon::StringMasking(const std::string &oriStr, size_t remain)
317 {
318 if (oriStr.size() > remain) {
319 return oriStr.substr(0, remain);
320 }
321 return oriStr;
322 }
323
GetDistributedTableName(const std::string & device,const std::string & tableName)324 std::string DBCommon::GetDistributedTableName(const std::string &device, const std::string &tableName)
325 {
326 if (!RuntimeContext::GetInstance()->ExistTranslateDevIdCallback()) {
327 return GetDistributedTableNameWithHash(device, tableName);
328 }
329 return CalDistributedTableName(device, tableName);
330 }
331
GetDistributedTableName(const std::string & device,const std::string & tableName,const StoreInfo & info)332 std::string DBCommon::GetDistributedTableName(const std::string &device, const std::string &tableName,
333 const StoreInfo &info)
334 {
335 std::string newDeviceId;
336 if (RuntimeContext::GetInstance()->TranslateDeviceId(device, info, newDeviceId) != E_OK) {
337 return GetDistributedTableNameWithHash(device, tableName);
338 }
339 return CalDistributedTableName(newDeviceId, tableName);
340 }
341
GetDistributedTableNameWithHash(const std::string & device,const std::string & tableName)342 std::string DBCommon::GetDistributedTableNameWithHash(const std::string &device, const std::string &tableName)
343 {
344 std::string deviceHashHex = DBCommon::TransferStringToHex(DBCommon::TransferHashString(device));
345 return CalDistributedTableName(deviceHashHex, tableName);
346 }
347
CalDistributedTableName(const std::string & device,const std::string & tableName)348 std::string DBCommon::CalDistributedTableName(const std::string &device, const std::string &tableName)
349 {
350 return DBConstant::RELATIONAL_PREFIX + tableName + "_" + device;
351 }
352
GetDeviceFromName(const std::string & deviceTableName,std::string & deviceHash,std::string & tableName)353 void DBCommon::GetDeviceFromName(const std::string &deviceTableName, std::string &deviceHash, std::string &tableName)
354 {
355 std::size_t found = deviceTableName.rfind('_');
356 if (found != std::string::npos && found + 1 < deviceTableName.length() &&
357 found > DBConstant::RELATIONAL_PREFIX.length()) {
358 deviceHash = deviceTableName.substr(found + 1);
359 tableName = deviceTableName.substr(DBConstant::RELATIONAL_PREFIX.length(),
360 found - DBConstant::RELATIONAL_PREFIX.length());
361 }
362 }
363
TrimSpace(const std::string & input)364 std::string DBCommon::TrimSpace(const std::string &input)
365 {
366 std::string res;
367 res.reserve(input.length());
368 bool isPreSpace = true;
369 for (char c : input) {
370 if (std::isspace(c)) {
371 isPreSpace = true;
372 } else {
373 if (!res.empty() && isPreSpace) {
374 res += ' ';
375 }
376 res += c;
377 isPreSpace = false;
378 }
379 }
380 res.shrink_to_fit();
381 return res;
382 }
383
384 namespace {
CharIn(char c,const std::string & pattern)385 bool CharIn(char c, const std::string &pattern)
386 {
387 return std::any_of(pattern.begin(), pattern.end(), [c] (char p) {
388 return c == p;
389 });
390 }
391 }
392
HasConstraint(const std::string & sql,const std::string & keyWord,const std::string & prePattern,const std::string & nextPattern)393 bool DBCommon::HasConstraint(const std::string &sql, const std::string &keyWord, const std::string &prePattern,
394 const std::string &nextPattern)
395 {
396 size_t pos = 0;
397 while ((pos = sql.find(keyWord, pos)) != std::string::npos) {
398 if (pos >= 1 && CharIn(sql[pos - 1], prePattern) && ((pos + keyWord.length() == sql.length()) ||
399 ((pos + keyWord.length() < sql.length()) && CharIn(sql[pos + keyWord.length()], nextPattern)))) {
400 return true;
401 }
402 pos++;
403 }
404 return false;
405 }
406
IsSameCipher(CipherType srcType,CipherType inputType)407 bool DBCommon::IsSameCipher(CipherType srcType, CipherType inputType)
408 {
409 // At present, the default type is AES-256-GCM.
410 // So when src is default and input is AES-256-GCM,
411 // or when src is AES-256-GCM and input is default,
412 // we think they are the same type.
413 if (srcType == inputType ||
414 ((srcType == CipherType::DEFAULT || srcType == CipherType::AES_256_GCM) &&
415 (inputType == CipherType::DEFAULT || inputType == CipherType::AES_256_GCM))) {
416 return true;
417 }
418 return false;
419 }
420
ToLowerCase(const std::string & str)421 std::string DBCommon::ToLowerCase(const std::string &str)
422 {
423 std::string res(str.length(), ' ');
424 std::transform(str.begin(), str.end(), res.begin(), ::tolower);
425 return res;
426 }
427
CaseInsensitiveCompare(std::string first,std::string second)428 bool DBCommon::CaseInsensitiveCompare(std::string first, std::string second)
429 {
430 std::transform(first.begin(), first.end(), first.begin(), ::tolower);
431 std::transform(second.begin(), second.end(), second.begin(), ::tolower);
432 return first == second;
433 }
434
CheckIsAlnumAndUnderscore(const std::string & text)435 bool DBCommon::CheckIsAlnumAndUnderscore(const std::string &text)
436 {
437 auto iter = std::find_if_not(text.begin(), text.end(), [](char c) {
438 return (std::isalnum(c) || c == '_');
439 });
440 return iter == text.end();
441 }
442 } // namespace DistributedDB
443