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 SINGLE_VER_DATABASE_OPER_H
17 #define SINGLE_VER_DATABASE_OPER_H
18 
19 #include "database_oper.h"
20 #include "sqlite_single_ver_natural_store.h"
21 
22 namespace DistributedDB {
23 class SingleVerDatabaseOper : public DatabaseOper {
24 public:
25     SingleVerDatabaseOper(SQLiteSingleVerNaturalStore *naturalStore, SQLiteStorageEngine *storageEngine);
~SingleVerDatabaseOper()26     ~SingleVerDatabaseOper() override {};
27 
28     int Rekey(const CipherPassword &passwd) override;
29 
30     int Import(const std::string &filePath, const CipherPassword &passwd) override;
31 
32     int Export(const std::string &filePath, const CipherPassword &passwd) const override;
33 
34 protected:
35     bool RekeyPreHandle(const CipherPassword &passwd, int &errCode) override;
36 
37     int BackupDb(const CipherPassword &passwd) const override;
38 
39     int CloseStorages() override;
40 
41     int RekeyPostHandle(const CipherPassword &passwd) override;
42 
43     int ExportAllDatabases(const std::string &currentDir, const CipherPassword &passwd,
44         const std::string &dbDir) const override;
45 
46     int BackupCurrentDatabase(const ImportFileInfo &info) const override;
47 
48     int ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd) const override;
49 
50     int ImportPostHandle() const override;
51 
52 private:
53     int InitStorageEngine();
54 
55     void InitDataBaseOption(OpenDbProperties &option) const;
56 
57     int RunExportLogic(const CipherPassword &passwd, const std::string &filePrefix) const;
58 
59     int RunRekeyLogic(CipherType type, const CipherPassword &passwd);
60 
61     int ExportMainDB(const std::string &currentDir, const CipherPassword &passwd, const std::string &dbDir) const;
62 
63     int ExportMetaDB(const std::string &currentDir, const CipherPassword &passwd, const std::string &dbDir) const;
64 
65     int ClearCurrentDatabase(const ImportFileInfo &info) const;
66 
67     int ImportUnpackedMainDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd) const;
68 
69     int ImportUnpackedMetaDatabase(const ImportFileInfo &info) const;
70 
71     int SetSecOpt(const std::string &path, bool isDir = true) const;
72 
73     int BackupDatabase(const ImportFileInfo &info) const;
74 
75     SQLiteSingleVerNaturalStore *singleVerNaturalStore_;
76     SQLiteStorageEngine *storageEngine_;
77 };
78 } // namespace DistributedDB
79 #endif // SINGLE_VER_DATABASE_OPER_H