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 "database_oper.h"
17
18 #include "db_errno.h"
19 #include "db_constant.h"
20 #include "db_common.h"
21 #include "log_print.h"
22 #include "platform_specific.h"
23 #include "package_file.h"
24 #include "res_finalizer.h"
25 #include "runtime_context.h"
26
27 namespace DistributedDB {
SetLocalDevId(const std::string & deviceId)28 void DatabaseOper::SetLocalDevId(const std::string &deviceId)
29 {
30 deviceId_ = deviceId;
31 }
32
ExecuteRekey(const CipherPassword & passwd,const KvDBProperties & property)33 int DatabaseOper::ExecuteRekey(const CipherPassword &passwd, const KvDBProperties &property)
34 {
35 int errCode = E_OK;
36 if (!RekeyPreHandle(passwd, errCode)) {
37 LOGI("Finish rekey when RekeyPre Handle, errCode = [%d]", errCode);
38 return errCode;
39 }
40
41 std::string ctrlFileName;
42 std::string newFileName;
43 errCode = CreateStatusCtrlFile(property, ctrlFileName, newFileName);
44 if (errCode != E_OK) {
45 return errCode;
46 }
47
48 LOGI("Backup the current file while rekey.");
49 errCode = BackupDb(passwd);
50 if (errCode != E_OK) {
51 LOGE("ExecuteRekey backup db failed! errCode = [%d]", errCode);
52 (void)RekeyRecover(property);
53 return errCode;
54 }
55
56 errCode = RenameStatusCtrlFile(ctrlFileName, newFileName);
57 if (errCode != E_OK) {
58 (void)RekeyRecover(property);
59 LOGE("ExecuteRekey rename status ctrl failed! errCode = [%d]", errCode);
60 return errCode;
61 }
62
63 errCode = CloseStorages();
64 if (errCode != E_OK) {
65 return errCode;
66 }
67
68 errCode = RekeyPostHandle(passwd);
69 if (errCode == -E_EKEYREVOKED) {
70 errCode = -E_FORBID_CACHEDB;
71 LOGI("Can not reopen database after rekey for the access controlled. errCode = [%d]", errCode);
72 }
73 return errCode;
74 }
75
GetCtrlFilePrefix(const KvDBProperties & property,std::string & filePrefix) const76 int DatabaseOper::GetCtrlFilePrefix(const KvDBProperties &property, std::string &filePrefix) const
77 {
78 std::string baseDir;
79 int errCode = GetWorkDir(property, baseDir);
80 if (errCode != E_OK) {
81 return errCode;
82 }
83
84 int dbType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::LOCAL_TYPE_SQLITE);
85 std::string dbSubDir = KvDBProperties::GetStoreSubDirectory(dbType);
86 filePrefix = baseDir + "/" + dbSubDir;
87 return E_OK;
88 }
89
RekeyRecover(const KvDBProperties & property)90 int DatabaseOper::RekeyRecover(const KvDBProperties &property)
91 {
92 std::string workDir;
93 int errCode = GetWorkDir(property, workDir);
94 if (errCode != E_OK) {
95 return errCode;
96 }
97
98 int dbType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::LOCAL_TYPE_SQLITE);
99 std::string dbSubDir = KvDBProperties::GetStoreSubDirectory(dbType);
100
101 std::string preCtrlFileName = workDir + "/" + dbSubDir + DBConstant::REKEY_FILENAME_POSTFIX_PRE;
102 bool isPreCtrlFileExist = OS::CheckPathExistence(preCtrlFileName);
103
104 std::string endCtrlFileName = workDir + "/" + dbSubDir + DBConstant::REKEY_FILENAME_POSTFIX_OK;
105 bool isEndCtrlFileExist = OS::CheckPathExistence(endCtrlFileName);
106
107 std::string currentDir = workDir + "/" + dbSubDir;
108 bool isPrimeDbDirExist = OS::CheckPathExistence(currentDir);
109
110 std::string backupDir = workDir + "/" + dbSubDir + DBConstant::PATH_BACKUP_POSTFIX;
111 bool isBackupDbDirExist = OS::CheckPathExistence(backupDir);
112
113 // remove the backup directory and ctrl file if Rekey not finish
114 // name of ctrl file is pre
115 if (isPreCtrlFileExist) {
116 LOGI("Rekey recovery:Remove the backup files");
117 return RecoverPrehandle(dbType, backupDir, preCtrlFileName);
118 }
119 // no ctrl file means nothing need to do
120 if (!isEndCtrlFileExist) {
121 return E_OK;
122 }
123
124 // name of ctrl file is ok
125 if (isBackupDbDirExist) {
126 if (isPrimeDbDirExist) {
127 // scenario 1: both prime and bak dir exist
128 // rm prime dir -> rename backup dir to prime dir -> rm ctrl file
129 LOGI("Rekey recovery:Remove the current files");
130 if (DBCommon::RemoveAllFilesOfDirectory(currentDir, true) != E_OK) {
131 LOGE("Remove the prime dir failed: %d", errno);
132 return -E_REMOVE_FILE;
133 }
134 }
135
136 // scenario 2: only bak dir exist
137 // rename backup dir to prime dir -> rm ctrl file
138 if (rename(backupDir.c_str(), currentDir.c_str()) != E_OK) {
139 LOGE("Rename the bak dir to prime dir failed:%d.", errno);
140 return -E_SYSTEM_API_FAIL;
141 }
142 }
143 // scenario 3: only prime dir exist
144 // scenario 4: both prime and bak dir not exist
145 // remove ctrl file
146 if (RemoveFile(endCtrlFileName) != E_OK) {
147 LOGE("Remove the end ctrl file failed: %d", errno);
148 return -E_REMOVE_FILE;
149 }
150 return E_OK;
151 }
152
CheckSecurityOption(const std::string & filePath,const KvDBProperties & property)153 int DatabaseOper::CheckSecurityOption(const std::string &filePath, const KvDBProperties &property)
154 {
155 SecurityOption dbSecOpt;
156 dbSecOpt.securityFlag = property.GetSecFlag();
157 dbSecOpt.securityLabel = property.GetSecLabel();
158 // set backup file security label first, avoid file loss label
159 (void)RuntimeContext::GetInstance()->SetSecurityOption(filePath, dbSecOpt);
160 SecurityOption secOption;
161 int errCode = RuntimeContext::GetInstance()->GetSecurityOption(filePath, secOption);
162 if (errCode != E_OK && errCode != -E_NOT_SUPPORT) {
163 LOGE("Get import package security option fail! errCode = [%d]", errCode);
164 return errCode;
165 }
166
167 if (dbSecOpt == secOption || secOption.securityLabel == SecurityLabel::NOT_SET) {
168 return E_OK;
169 }
170 LOGE("Import package secOpt %d %d vs database %d %d",
171 secOption.securityFlag, secOption.securityLabel, dbSecOpt.securityFlag, dbSecOpt.securityLabel);
172 return -E_SECURITY_OPTION_CHECK_ERROR;
173 }
174
ExecuteImport(const std::string & filePath,const CipherPassword & passwd,const KvDBProperties & property,bool isNeedIntegrityCheck) const175 int DatabaseOper::ExecuteImport(const std::string &filePath, const CipherPassword &passwd,
176 const KvDBProperties &property, bool isNeedIntegrityCheck) const
177 {
178 ImportFileInfo importInfo;
179 InitImportFileInfo(importInfo, property);
180
181 int errCode = CheckSecurityOption(filePath, property);
182 if (errCode != E_OK) {
183 return errCode;
184 }
185
186 // 1. unpack and check the file.
187 LOGI("Unpack the imported file");
188 errCode = UnpackAndCheckImportedFile(filePath, importInfo, property);
189 if (errCode != E_OK) {
190 return errCode;
191 }
192
193 // Using RAII define tempState clean when object finalize execute
194 ResFinalizer tempStateClean([&errCode, &property, this]() {
195 int innerCode = this->ClearImportTempFile(property);
196 if (innerCode != E_OK) {
197 LOGE("Failed to clean the intermediate import files, errCode = [%d]", innerCode);
198 }
199 // Finish. reinitialize the database.
200 if (errCode != E_OK) {
201 innerCode = this->ImportPostHandle();
202 LOGE("Reinit the database after import, errCode = [%d]", innerCode);
203 }
204 });
205
206 // 2. backup the current database.
207 LOGI("Backup the current database while import.");
208 errCode = BackupCurrentDatabase(importInfo);
209 if (errCode != E_OK) {
210 LOGE("Failed to backup current databases, errCode = [%d]", errCode);
211 return errCode;
212 }
213
214 // 3. export the unpacked file to the current database.
215 LOGI("Import the unpacked database.");
216 errCode = ImportUnpackedDatabase(importInfo, passwd, isNeedIntegrityCheck);
217 if (errCode != E_OK) {
218 LOGE("Failed to import from the unpacked databases, errCode = [%d]", errCode);
219 }
220 DBCommon::RemoveAllFilesOfDirectory(importInfo.unpackedDir);
221 return errCode;
222 }
223
CreateBackupDirForExport(const KvDBProperties & property,std::string & currentDir,std::string & backupDir) const224 int DatabaseOper::CreateBackupDirForExport(const KvDBProperties &property, std::string ¤tDir,
225 std::string &backupDir) const
226 {
227 std::string baseDir;
228 int errCode = GetWorkDir(property, baseDir);
229 if (errCode != E_OK) {
230 LOGE("Get work dir failed:%d.", errCode);
231 return errCode;
232 }
233
234 int databaseType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::LOCAL_TYPE_SQLITE);
235 std::string subDir = KvDBProperties::GetStoreSubDirectory(databaseType);
236 currentDir = baseDir + "/" + subDir;
237
238 backupDir = baseDir + "/" + subDir + DBConstant::PATH_POSTFIX_EXPORT_BACKUP + "/";
239 errCode = DBCommon::CreateDirectory(backupDir);
240 if (errCode != E_OK) {
241 return errCode;
242 }
243 std::vector<std::string> dbDir {DBConstant::MAINDB_DIR, DBConstant::METADB_DIR, DBConstant::CACHEDB_DIR};
244 for (const auto &item : dbDir) {
245 if (DBCommon::CreateDirectory(backupDir + "/" + item) != E_OK) {
246 return -E_SYSTEM_API_FAIL;
247 }
248 }
249 return errCode;
250 }
251
ExecuteExport(const std::string & filePath,const CipherPassword & passwd,const KvDBProperties & property) const252 int DatabaseOper::ExecuteExport(const std::string &filePath, const CipherPassword &passwd,
253 const KvDBProperties &property) const
254 {
255 if (deviceId_.empty()) {
256 return -E_NOT_INIT;
257 }
258
259 std::string currentDir;
260 std::string backupDir;
261 int errCode = CreateBackupDirForExport(property, currentDir, backupDir);
262 if (errCode != E_OK) {
263 return errCode;
264 }
265
266 errCode = ExportAllDatabases(currentDir, passwd, backupDir);
267 if (errCode != E_OK) {
268 LOGE("Export databases fail!:%d.", errCode);
269 (void)ClearExportedTempFiles(property);
270 return errCode;
271 }
272
273 errCode = PackExportedDatabase(backupDir, filePath, property);
274 if (errCode != E_OK) {
275 OS::RemoveFile(filePath); // Pack file failed, need rollback delete Intermediate state package file
276 LOGE("[DatabaseOper][ExecuteExport] Pack files fail! errCode = [%d], errno = [%d].", errCode, errno);
277 (void)ClearExportedTempFiles(property);
278 return errCode;
279 }
280
281 SecurityOption secOption {property.GetSecLabel(), property.GetSecFlag()};
282 // RuntimeContext can make sure GetInstance not nullptr
283 errCode = RuntimeContext::GetInstance()->SetSecurityOption(filePath, secOption);
284 if (errCode != E_OK) {
285 if (errCode == -E_NOT_SUPPORT) {
286 (void)ClearExportedTempFiles(property);
287 return E_OK;
288 }
289 OS::RemoveFile(filePath);
290 LOGE("[DatabaseOper][ExecuteExport] Set security option fail! errCode = [%d].", errCode);
291 }
292
293 (void)ClearExportedTempFiles(property);
294 return errCode;
295 }
296
297 // private begin
CreateStatusCtrlFile(const KvDBProperties & property,std::string & orgCtrlFile,std::string & newCtrlFile)298 int DatabaseOper::CreateStatusCtrlFile(const KvDBProperties &property, std::string &orgCtrlFile,
299 std::string &newCtrlFile)
300 {
301 std::string filePrefix;
302 int errCode = GetCtrlFilePrefix(property, filePrefix);
303 if (errCode != E_OK) {
304 return errCode;
305 }
306
307 // create control file
308 newCtrlFile = filePrefix + DBConstant::REKEY_FILENAME_POSTFIX_OK;
309 orgCtrlFile = filePrefix + DBConstant::REKEY_FILENAME_POSTFIX_PRE;
310 return OS::CreateFileByFileName(orgCtrlFile);
311 }
312
RenameStatusCtrlFile(const std::string & orgCtrlFile,const std::string & newCtrlFile)313 int DatabaseOper::RenameStatusCtrlFile(const std::string &orgCtrlFile, const std::string &newCtrlFile)
314 {
315 int errCode = rename(orgCtrlFile.c_str(), newCtrlFile.c_str());
316 if (errCode != E_OK) {
317 LOGE("change ctrl file name to ok failed: %d.", errCode);
318 return -E_SYSTEM_API_FAIL;
319 }
320 return E_OK;
321 }
322
RecoverPrehandle(int dbType,const std::string & dir,const std::string & fileName)323 int DatabaseOper::RecoverPrehandle(int dbType, const std::string &dir, const std::string &fileName)
324 {
325 if (DBCommon::RemoveAllFilesOfDirectory(dir, true) != E_OK) { // LCOV_EXCL_BR_LINE
326 LOGE("Remove the backup dir failed:%d", errno);
327 return -E_REMOVE_FILE;
328 }
329 if (RemoveFile(fileName) != E_OK) { // LCOV_EXCL_BR_LINE
330 LOGE("Remove the pre ctrl file failed:%d", errno);
331 return -E_REMOVE_FILE;
332 }
333 return E_OK;
334 }
335
RemoveDbDir(const std::string & dir,int dbType,bool isNeedDelDir)336 int DatabaseOper::RemoveDbDir(const std::string &dir, int dbType, bool isNeedDelDir)
337 {
338 if (!OS::CheckPathExistence(dir)) { // LCOV_EXCL_BR_LINE
339 return E_OK;
340 }
341
342 if (dbType == DBConstant::DB_TYPE_LOCAL) { // LCOV_EXCL_BR_LINE
343 std::vector<std::string> dbNameList = {
344 DBConstant::LOCAL_DATABASE_NAME
345 };
346 return RemoveDbFiles(dir, dbNameList, isNeedDelDir);
347 }
348 if (dbType == DBConstant::DB_TYPE_SINGLE_VER) { // LCOV_EXCL_BR_LINE
349 std::vector<std::string> dbNameList = {
350 DBConstant::SINGLE_VER_DATA_STORE
351 };
352 return RemoveDbFiles(dir, dbNameList, isNeedDelDir);
353 }
354 if (dbType == DBConstant::DB_TYPE_MULTI_VER) { // LCOV_EXCL_BR_LINE
355 std::vector<std::string> dbNameList = {
356 DBConstant::MULTI_VER_DATA_STORE, DBConstant::MULTI_VER_COMMIT_STORE,
357 DBConstant::MULTI_VER_VALUE_STORE, DBConstant::MULTI_VER_META_STORE
358 };
359 return RemoveDbFiles(dir, dbNameList, isNeedDelDir);
360 }
361 return -E_NOT_SUPPORT;
362 }
363
RemoveFile(const std::string & fileName)364 int DatabaseOper::RemoveFile(const std::string &fileName)
365 {
366 if (!OS::CheckPathExistence(fileName)) {
367 return E_OK;
368 }
369
370 if (OS::RemoveFile(fileName) != E_OK) {
371 LOGE("Remove file failed:%d", errno);
372 return -E_REMOVE_FILE;
373 }
374 return E_OK;
375 }
376
GetWorkDir(const KvDBProperties & property,std::string & workDir)377 int DatabaseOper::GetWorkDir(const KvDBProperties &property, std::string &workDir)
378 {
379 std::string dataDir = property.GetStringProp(KvDBProperties::DATA_DIR, "");
380 std::string identifierDir = property.GetStringProp(KvDBProperties::IDENTIFIER_DIR, "");
381 if (dataDir.empty()) {
382 return -E_INVALID_ARGS;
383 }
384
385 workDir = dataDir + "/" + identifierDir;
386 return E_OK;
387 }
388
389 // Only for remove the backup directory while rekey.
RemoveDbFiles(const std::string & dir,const std::vector<std::string> & dbNameList,bool isNeedDelDir)390 int DatabaseOper::RemoveDbFiles(const std::string &dir, const std::vector<std::string> &dbNameList, bool isNeedDelDir)
391 {
392 for (const auto &iter : dbNameList) {
393 // remove
394 std::string dbFile = dir + "/" + iter + ".db";
395 if (RemoveFile(dbFile) != E_OK) { // LCOV_EXCL_BR_LINE
396 LOGE("Remove the db file failed:%d", errno);
397 return -E_REMOVE_FILE;
398 }
399
400 dbFile = dir + "/" + iter + ".db-wal";
401 if (RemoveFile(dbFile) != E_OK) { // LCOV_EXCL_BR_LINE
402 LOGE("Remove the wal file failed:%d", errno);
403 return -E_REMOVE_FILE;
404 }
405
406 dbFile = dir + "/" + iter + ".db-shm";
407 if (RemoveFile(dbFile) != E_OK) { // LCOV_EXCL_BR_LINE
408 LOGE("Remove the shm file failed:%d", errno);
409 return -E_REMOVE_FILE;
410 }
411 }
412 if (isNeedDelDir && OS::RemoveDBDirectory(dir) != E_OK) { // LCOV_EXCL_BR_LINE
413 LOGE("Remove directory:%d", errno);
414 return -E_REMOVE_FILE;
415 }
416 return E_OK;
417 }
418
InitImportFileInfo(ImportFileInfo & info,const KvDBProperties & property)419 void DatabaseOper::InitImportFileInfo(ImportFileInfo &info, const KvDBProperties &property)
420 {
421 std::string dataDir = property.GetStringProp(KvDBProperties::DATA_DIR, "");
422 std::string identifierDir = property.GetStringProp(KvDBProperties::IDENTIFIER_DIR, "");
423 int databaseType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE_SQLITE);
424 std::string subDir = KvDBProperties::GetStoreSubDirectory(databaseType);
425
426 std::string baseDir = dataDir + "/" + identifierDir + "/" + subDir;
427 info.backupDir = baseDir + DBConstant::PATH_POSTFIX_IMPORT_BACKUP + "/";
428 info.unpackedDir = baseDir + DBConstant::PATH_POSTFIX_UNPACKED + "/";
429 info.currentDir = baseDir + "/";
430 info.curValidFile = baseDir + DBConstant::PATH_POSTFIX_IMPORT_ORIGIN; // origin directory is valid.
431 info.backValidFile = baseDir + DBConstant::PATH_POSTFIX_IMPORT_DUP; // the back directory is valid.
432 }
433
UnpackAndCheckImportedFile(const std::string & srcFile,const ImportFileInfo & info,const KvDBProperties & property) const434 int DatabaseOper::UnpackAndCheckImportedFile(const std::string &srcFile, const ImportFileInfo &info,
435 const KvDBProperties &property) const
436 {
437 int errCode = DBCommon::CreateDirectory(info.unpackedDir);
438 if (errCode != E_OK) {
439 return errCode;
440 }
441
442 FileInfo fileInfo;
443 errCode = PackageFile::UnpackFile(srcFile, info.unpackedDir, fileInfo);
444 if (errCode != E_OK) {
445 DBCommon::RemoveAllFilesOfDirectory(info.unpackedDir);
446 LOGE("Failed to unpack the imported file:%d", errCode);
447 return errCode;
448 }
449 int dbType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::LOCAL_TYPE_SQLITE);
450 if (fileInfo.dbType != static_cast<uint32_t>(dbType)) {
451 DBCommon::RemoveAllFilesOfDirectory(info.unpackedDir);
452 LOGE("Check db type [%u] vs [%u] or devicesId fail!", fileInfo.dbType, static_cast<uint32_t>(dbType));
453 return -E_INVALID_FILE;
454 }
455 return E_OK;
456 }
457
RecoverImportedBackFiles(const std::string & dir,const std::string & fileName,int dbType) const458 int DatabaseOper::RecoverImportedBackFiles(const std::string &dir, const std::string &fileName, int dbType) const
459 {
460 std::string backupDir = dir + DBConstant::PATH_POSTFIX_IMPORT_BACKUP;
461 // if backup directory is not existed
462 if (!OS::CheckPathExistence(backupDir)) {
463 goto END;
464 }
465
466 if (DBCommon::RemoveAllFilesOfDirectory(dir, true) != E_OK) {
467 LOGE("Remove the current db dir failed");
468 return -E_REMOVE_FILE;
469 }
470
471 if (rename(backupDir.c_str(), dir.c_str()) != E_OK) {
472 LOGE("Rename the backfile error:%d", errno);
473 return -E_SYSTEM_API_FAIL;
474 }
475
476 END:
477 if (RemoveFile(fileName) != E_OK) {
478 LOGE("Remove the pre ctrl file failed:%d", errno);
479 return -E_REMOVE_FILE;
480 }
481 return E_OK;
482 }
483
RemoveImportedBackFiles(const std::string & backupDir,const std::string & ctrlFileName,int dbType) const484 int DatabaseOper::RemoveImportedBackFiles(const std::string &backupDir, const std::string &ctrlFileName, int dbType)
485 const
486 {
487 if (DBCommon::RemoveAllFilesOfDirectory(backupDir, true) != E_OK) {
488 LOGE("Remove the backup dir failed");
489 return -E_REMOVE_FILE;
490 }
491
492 if (RemoveFile(ctrlFileName) != E_OK) {
493 LOGE("Remove the pre ctrl file failed");
494 return -E_REMOVE_FILE;
495 }
496 return E_OK;
497 }
498
ClearImportTempFile(const KvDBProperties & property) const499 int DatabaseOper::ClearImportTempFile(const KvDBProperties &property) const
500 {
501 // get work directory
502 std::string workDir;
503 int errCode = GetWorkDir(property, workDir);
504 if (errCode != E_OK) {
505 return errCode;
506 }
507
508 int dbType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::LOCAL_TYPE_SQLITE);
509 std::string dbSubDir = KvDBProperties::GetStoreSubDirectory(dbType);
510
511 std::string oriKeepFile = workDir + "/" + dbSubDir + DBConstant::PATH_POSTFIX_IMPORT_ORIGIN;
512 bool isOriKeepFileExist = OS::CheckPathExistence(oriKeepFile);
513
514 std::string backKeepFile = workDir + "/" + dbSubDir + DBConstant::PATH_POSTFIX_IMPORT_DUP;
515 bool isBakKeepFileExist = OS::CheckPathExistence(backKeepFile);
516
517 std::string currentDir = workDir + "/" + dbSubDir;
518 std::string backupDir = workDir + "/" + dbSubDir + DBConstant::PATH_POSTFIX_IMPORT_BACKUP;
519 bool isBackupDbDirExist = OS::CheckPathExistence(backupDir);
520 std::string exportBackupDir = workDir + "/" + dbSubDir + DBConstant::PATH_POSTFIX_UNPACKED;
521 DBCommon::RemoveAllFilesOfDirectory(exportBackupDir);
522
523 if (isOriKeepFileExist && isBakKeepFileExist) {
524 LOGE("Origin and backup file shouldn't exist concurrently");
525 }
526
527 // Clear the backup dir and the ctrl file
528 if (isOriKeepFileExist) {
529 return RemoveImportedBackFiles(backupDir, oriKeepFile, dbType);
530 }
531
532 // remove the main directory and restore the backup files.
533 if (isBakKeepFileExist) {
534 return RecoverImportedBackFiles(currentDir, backKeepFile, dbType);
535 }
536
537 if (isBackupDbDirExist) {
538 // Import success, clean backupdir
539 if (DBCommon::RemoveAllFilesOfDirectory(backupDir, true) != E_OK) {
540 LOGE("Remove the backup dir failed");
541 return -E_REMOVE_FILE;
542 }
543 }
544
545 return E_OK;
546 }
547
ClearExportedTempFiles(const KvDBProperties & property) const548 int DatabaseOper::ClearExportedTempFiles(const KvDBProperties &property) const
549 {
550 std::string workDir;
551 int errCode = GetWorkDir(property, workDir);
552 if (errCode != E_OK) {
553 return errCode;
554 }
555
556 int dbType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::LOCAL_TYPE_SQLITE);
557 std::string dbSubDir = KvDBProperties::GetStoreSubDirectory(dbType);
558 std::string backupDir = workDir + "/" + dbSubDir + DBConstant::PATH_POSTFIX_EXPORT_BACKUP;
559 errCode = DBCommon::RemoveAllFilesOfDirectory(backupDir);
560 if (errCode != E_OK) {
561 LOGE("Remove the exported backup dir failed");
562 return -E_REMOVE_FILE;
563 }
564
565 return errCode;
566 }
567
PackExportedDatabase(const std::string & fileDir,const std::string & packedFile,const KvDBProperties & property) const568 int DatabaseOper::PackExportedDatabase(const std::string &fileDir, const std::string &packedFile,
569 const KvDBProperties &property) const
570 {
571 LOGD("Pack the exported database.");
572 int databaseType = property.GetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE_SQLITE);
573 FileInfo fileInfo = {static_cast<uint32_t>(databaseType), deviceId_};
574 int errCode = PackageFile::PackageFiles(fileDir, packedFile, fileInfo);
575 if (errCode != E_OK) {
576 LOGE("Pack the database error:%d", errCode);
577 }
578
579 return errCode;
580 }
581 } // namespace DistributedDB
582