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.c_str());
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 fileIn = nullptr;
252 }
253 if (fileOut != nullptr) {
254 (void)fclose(fileOut);
255 fileOut = nullptr;
256 }
257 return errCode;
258 }
259
RemoveAllFilesOfDirectory(const std::string & dir,bool isNeedRemoveDir)260 int DBCommon::RemoveAllFilesOfDirectory(const std::string &dir, bool isNeedRemoveDir)
261 {
262 std::list<OS::FileAttr> fileList;
263 bool isExisted = OS::CheckPathExistence(dir);
264 if (!isExisted) {
265 return E_OK;
266 }
267 int errCode = OS::GetFileAttrFromPath(dir, fileList, true);
268 if (errCode != E_OK) {
269 return errCode;
270 }
271
272 RemoveFiles(fileList, OS::FileType::FILE);
273 RemoveDirectories(fileList, OS::FileType::PATH);
274 if (isNeedRemoveDir) {
275 // Pay attention to the order of deleting the directory
276 if (OS::CheckPathExistence(dir) && OS::RemoveDBDirectory(dir.c_str()) != 0) {
277 LOGI("Remove the directory error:%d", errno);
278 errCode = -E_SYSTEM_API_FAIL;
279 }
280 }
281
282 return errCode;
283 }
284
GenerateIdentifierId(const std::string & storeId,const std::string & appId,const std::string & userId,int32_t instanceId)285 std::string DBCommon::GenerateIdentifierId(const std::string &storeId,
286 const std::string &appId, const std::string &userId, int32_t instanceId)
287 {
288 std::string id = userId + "-" + appId + "-" + storeId;
289 if (instanceId != 0) {
290 id += "-" + std::to_string(instanceId);
291 }
292 return id;
293 }
294
GenerateDualTupleIdentifierId(const std::string & storeId,const std::string & appId)295 std::string DBCommon::GenerateDualTupleIdentifierId(const std::string &storeId, const std::string &appId)
296 {
297 return appId + "-" + storeId;
298 }
299
SetDatabaseIds(KvDBProperties & properties,const std::string & appId,const std::string & userId,const std::string & storeId,int32_t instanceId)300 void DBCommon::SetDatabaseIds(KvDBProperties &properties, const std::string &appId, const std::string &userId,
301 const std::string &storeId, int32_t instanceId)
302 {
303 properties.SetIdentifier(userId, appId, storeId, instanceId);
304 std::string oriStoreDir;
305 // IDENTIFIER_DIR no need cal with instanceId
306 std::string identifier = GenerateIdentifierId(storeId, appId, userId);
307 if (properties.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false)) {
308 oriStoreDir = storeId;
309 } else {
310 oriStoreDir = identifier;
311 }
312 std::string hashIdentifier = TransferHashString(identifier);
313 std::string hashDir = TransferHashString(oriStoreDir);
314 std::string hexHashDir = TransferStringToHex(hashDir);
315 properties.SetStringProp(KvDBProperties::IDENTIFIER_DIR, hexHashDir);
316 }
317
StringMasking(const std::string & oriStr,size_t remain)318 std::string DBCommon::StringMasking(const std::string &oriStr, size_t remain)
319 {
320 if (oriStr.size() > remain) {
321 return oriStr.substr(0, remain);
322 }
323 return oriStr;
324 }
325
GetDistributedTableName(const std::string & device,const std::string & tableName)326 std::string DBCommon::GetDistributedTableName(const std::string &device, const std::string &tableName)
327 {
328 if (!RuntimeContext::GetInstance()->ExistTranslateDevIdCallback()) {
329 return GetDistributedTableNameWithHash(device, tableName);
330 }
331 return CalDistributedTableName(device, tableName);
332 }
333
GetDistributedTableName(const std::string & device,const std::string & tableName,const StoreInfo & info)334 std::string DBCommon::GetDistributedTableName(const std::string &device, const std::string &tableName,
335 const StoreInfo &info)
336 {
337 std::string newDeviceId;
338 if (RuntimeContext::GetInstance()->TranslateDeviceId(device, info, newDeviceId) != E_OK) {
339 return GetDistributedTableNameWithHash(device, tableName);
340 }
341 return CalDistributedTableName(newDeviceId, tableName);
342 }
343
GetDistributedTableNameWithHash(const std::string & device,const std::string & tableName)344 std::string DBCommon::GetDistributedTableNameWithHash(const std::string &device, const std::string &tableName)
345 {
346 std::string deviceHashHex = DBCommon::TransferStringToHex(DBCommon::TransferHashString(device));
347 return CalDistributedTableName(deviceHashHex, tableName);
348 }
349
CalDistributedTableName(const std::string & device,const std::string & tableName)350 std::string DBCommon::CalDistributedTableName(const std::string &device, const std::string &tableName)
351 {
352 return DBConstant::RELATIONAL_PREFIX + tableName + "_" + device;
353 }
354
GetDeviceFromName(const std::string & deviceTableName,std::string & deviceHash,std::string & tableName)355 void DBCommon::GetDeviceFromName(const std::string &deviceTableName, std::string &deviceHash, std::string &tableName)
356 {
357 std::size_t found = deviceTableName.rfind('_');
358 if (found != std::string::npos && found + 1 < deviceTableName.length() &&
359 found > DBConstant::RELATIONAL_PREFIX.length()) {
360 deviceHash = deviceTableName.substr(found + 1);
361 tableName = deviceTableName.substr(DBConstant::RELATIONAL_PREFIX.length(),
362 found - DBConstant::RELATIONAL_PREFIX.length());
363 }
364 }
365
TrimSpace(const std::string & input)366 std::string DBCommon::TrimSpace(const std::string &input)
367 {
368 std::string res;
369 res.reserve(input.length());
370 bool isPreSpace = true;
371 for (char c : input) {
372 if (std::isspace(c)) {
373 isPreSpace = true;
374 } else {
375 if (!res.empty() && isPreSpace) {
376 res += ' ';
377 }
378 res += c;
379 isPreSpace = false;
380 }
381 }
382 res.shrink_to_fit();
383 return res;
384 }
385
386 namespace {
CharIn(char c,const std::string & pattern)387 bool CharIn(char c, const std::string &pattern)
388 {
389 return std::any_of(pattern.begin(), pattern.end(), [c] (char p) {
390 return c == p;
391 });
392 }
393 }
394
HasConstraint(const std::string & sql,const std::string & keyWord,const std::string & prePattern,const std::string & nextPattern)395 bool DBCommon::HasConstraint(const std::string &sql, const std::string &keyWord, const std::string &prePattern,
396 const std::string &nextPattern)
397 {
398 size_t pos = 0;
399 while ((pos = sql.find(keyWord, pos)) != std::string::npos) {
400 if (pos - 1 >= 0 && CharIn(sql[pos - 1], prePattern) && ((pos + keyWord.length() == sql.length()) ||
401 ((pos + keyWord.length() < sql.length()) && CharIn(sql[pos + keyWord.length()], nextPattern)))) {
402 return true;
403 }
404 pos++;
405 }
406 return false;
407 }
408
IsSameCipher(CipherType srcType,CipherType inputType)409 bool DBCommon::IsSameCipher(CipherType srcType, CipherType inputType)
410 {
411 // At present, the default type is AES-256-GCM.
412 // So when src is default and input is AES-256-GCM,
413 // or when src is AES-256-GCM and input is default,
414 // we think they are the same type.
415 if (srcType == inputType ||
416 ((srcType == CipherType::DEFAULT || srcType == CipherType::AES_256_GCM) &&
417 (inputType == CipherType::DEFAULT || inputType == CipherType::AES_256_GCM))) {
418 return true;
419 }
420 return false;
421 }
422
CheckIsAlnumAndUnderscore(const std::string & text)423 bool DBCommon::CheckIsAlnumAndUnderscore(const std::string &text)
424 {
425 auto iter = std::find_if_not(text.begin(), text.end(), [](char c) {
426 return (std::isalnum(c) || c == '_');
427 });
428 return iter == text.end();
429 }
430 } // namespace DistributedDB
431