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 #ifndef DATABASE_OPER_H 17 #define DATABASE_OPER_H 18 19 #include <string> 20 21 #include "kvdb_properties.h" 22 #include "generic_kvdb.h" 23 24 namespace DistributedDB { 25 class DatabaseOper { 26 public: ~DatabaseOper()27 virtual ~DatabaseOper() {}; 28 29 virtual int Rekey(const CipherPassword &passwd) = 0; 30 31 virtual int Import(const std::string &filePath, const CipherPassword &passwd, 32 bool isNeedIntegrityCheck = false) = 0; 33 34 virtual int Export(const std::string &filePath, const CipherPassword &passwd) const = 0; 35 36 void SetLocalDevId(const std::string &deviceId); 37 38 int RekeyRecover(const KvDBProperties &property); 39 40 int ClearImportTempFile(const KvDBProperties &property) const; 41 42 int ClearExportedTempFiles(const KvDBProperties &property) const; 43 44 protected: 45 int ExecuteRekey(const CipherPassword &passwd, const KvDBProperties &property); 46 47 virtual bool RekeyPreHandle(const CipherPassword &passwd, int &errCode) = 0; 48 49 virtual int BackupDb(const CipherPassword &passwd) const = 0; 50 51 virtual int CloseStorages() = 0; 52 53 virtual int RekeyPostHandle(const CipherPassword &passwd) = 0; 54 55 int GetCtrlFilePrefix(const KvDBProperties &property, std::string &filePrefix) const; 56 57 virtual int ExportAllDatabases(const std::string ¤tDir, const CipherPassword &passwd, 58 const std::string &dbDir) const = 0; 59 60 static int RemoveFile(const std::string &fileName); 61 62 // import begin 63 int ExecuteImport(const std::string &filePath, const CipherPassword &passwd, const KvDBProperties &property, 64 bool isNeedIntegrityCheck = false) const; 65 66 virtual int BackupCurrentDatabase(const ImportFileInfo &info) const = 0; 67 68 virtual int ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd, 69 bool isNeedIntegrityCheck = false) const = 0; 70 71 virtual int ImportPostHandle() const = 0; 72 73 // export begin 74 int ExecuteExport(const std::string &filePath, const CipherPassword &passwd, const KvDBProperties &property) const; 75 private: 76 int CreateStatusCtrlFile(const KvDBProperties &property, std::string &orgCtrlFile, std::string &newCtrlFile); 77 78 static int RenameStatusCtrlFile(const std::string &orgCtrlFile, const std::string &newCtrlFile); 79 80 int RecoverPrehandle(int dbType, const std::string &dir, const std::string &fileName); 81 82 int RemoveDbDir(const std::string &dir, int dbType, bool isNeedDelDir = true); 83 84 static int GetWorkDir(const KvDBProperties &property, std::string &workDir); 85 86 int RemoveDbFiles(const std::string &dir, const std::vector<std::string> &dbNameList, bool isNeedDelDir = true); 87 88 static void InitImportFileInfo(ImportFileInfo &info, const KvDBProperties &property); 89 90 int UnpackAndCheckImportedFile(const std::string &srcFile, const ImportFileInfo &info, 91 const KvDBProperties &property) const; 92 93 int RecoverImportedBackFiles(const std::string &dir, const std::string &fileName, int dbType) const; 94 95 int RemoveImportedBackFiles(const std::string &backupDir, const std::string &ctrlFileName, int dbType) const; 96 97 int PackExportedDatabase(const std::string &fileDir, const std::string &packedFile, 98 const KvDBProperties &property) const; 99 100 int CreateBackupDirForExport(const KvDBProperties &property, std::string ¤tDir, std::string &backupDir) const; 101 102 static int CheckSecurityOption(const std::string &filePath, const KvDBProperties &property); 103 104 std::string deviceId_; 105 }; 106 } // namespace DistributedDB 107 108 #endif // DATABASE_OPER_H