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 #include "kvdb_utils.h"
16
17 #include "platform_specific.h"
18 #include "log_print.h"
19 #include "db_constant.h"
20 #include "db_errno.h"
21 #include "db_common.h"
22 #include "sqlite_utils.h"
23
24 namespace DistributedDB {
GetStoreDirectory(std::string & directory,const std::string & identifierName)25 void KvDBUtils::GetStoreDirectory(std::string &directory, const std::string &identifierName)
26 {
27 if (!directory.empty() && directory.back() != '/') {
28 directory += "/";
29 }
30 directory += identifierName;
31 return;
32 }
33
RemoveKvDB(const std::string & dirAll,const std::string & dirStoreOnly,const std::string & dbName)34 int KvDBUtils::RemoveKvDB(const std::string &dirAll, const std::string &dirStoreOnly, const std::string &dbName)
35 {
36 int errCodeAll = KvDBUtils::RemoveKvDB(dirAll, dbName);
37 if (errCodeAll != -E_NOT_FOUND && errCodeAll != E_OK) {
38 return errCodeAll;
39 }
40 int errCodeOnlyStore = KvDBUtils::RemoveKvDB(dirStoreOnly, dbName);
41 if (errCodeOnlyStore != -E_NOT_FOUND && errCodeOnlyStore != E_OK) {
42 return errCodeOnlyStore;
43 }
44 if ((errCodeAll == -E_NOT_FOUND) && (errCodeOnlyStore == -E_NOT_FOUND)) {
45 return -E_NOT_FOUND;
46 }
47 return E_OK;
48 }
49
RemoveKvDB(const std::string & dir,const std::string & dbName)50 int KvDBUtils::RemoveKvDB(const std::string &dir, const std::string &dbName)
51 {
52 std::string dbFileName = dir;
53 GetStoreDirectory(dbFileName, dbName);
54 dbFileName += DBConstant::SQLITE_DB_EXTENSION;
55 int errCode = E_OK;
56 if (OS::CheckPathExistence(dbFileName)) {
57 errCode = DBCommon::RemoveAllFilesOfDirectory(dir, true);
58 if (errCode != E_OK) {
59 LOGE("Failed to delete the db file! errno[%d], errCode[%d]", errno, errCode);
60 return -E_REMOVE_FILE;
61 }
62 } else {
63 errCode = -E_NOT_FOUND;
64 LOGD("Db file not existed! errCode[%d]", errCode);
65 }
66 return errCode;
67 }
68
GetKvDbSize(const std::string & dirAll,const std::string & dirStoreOnly,const std::string & dbName,uint64_t & size)69 int KvDBUtils::GetKvDbSize(const std::string &dirAll, const std::string &dirStoreOnly,
70 const std::string &dbName, uint64_t &size)
71 {
72 int errCodeAll = SQLiteUtils::GetDbSize(dirAll, dbName, size);
73 if (errCodeAll != -E_NOT_FOUND && errCodeAll != E_OK) {
74 return errCodeAll;
75 }
76
77 int errCodeOnlyStore = SQLiteUtils::GetDbSize(dirStoreOnly, dbName, size);
78 if (errCodeOnlyStore != -E_NOT_FOUND && errCodeOnlyStore != E_OK) {
79 return errCodeOnlyStore;
80 }
81 if ((errCodeAll == -E_NOT_FOUND) && (errCodeOnlyStore == -E_NOT_FOUND)) {
82 return -E_NOT_FOUND;
83 }
84 return E_OK;
85 }
86 } // namespace DistributedDB