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