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 }
32
RemoveKvDB(const std::string & dirAll,const std::string & dirStoreOnly,const std::string & dbName)33 int KvDBUtils::RemoveKvDB(const std::string &dirAll, const std::string &dirStoreOnly, const std::string &dbName)
34 {
35 int errCodeAll = KvDBUtils::RemoveKvDB(dirAll, dbName);
36 if (errCodeAll != -E_NOT_FOUND && errCodeAll != E_OK) {
37 return errCodeAll;
38 }
39 int errCodeOnlyStore = KvDBUtils::RemoveKvDB(dirStoreOnly, dbName);
40 if (errCodeOnlyStore != -E_NOT_FOUND && errCodeOnlyStore != E_OK) {
41 return errCodeOnlyStore;
42 }
43 if ((errCodeAll == -E_NOT_FOUND) && (errCodeOnlyStore == -E_NOT_FOUND)) {
44 return -E_NOT_FOUND;
45 }
46 return E_OK;
47 }
48
RemoveKvDB(const std::string & dir,const std::string & dbName)49 int KvDBUtils::RemoveKvDB(const std::string &dir, const std::string &dbName)
50 {
51 std::string dbFileName = dir;
52 GetStoreDirectory(dbFileName, dbName);
53 dbFileName += DBConstant::DB_EXTENSION;
54 int errCode = E_OK;
55 if (OS::CheckPathExistence(dbFileName)) {
56 errCode = DBCommon::RemoveAllFilesOfDirectory(dir, true);
57 if (errCode != E_OK) {
58 LOGE("Failed to delete the db file! errno[%d], errCode[%d]", errno, errCode);
59 return -E_REMOVE_FILE;
60 }
61 } else {
62 errCode = -E_NOT_FOUND;
63 LOGD("Db file not existed! errCode[%d]", errCode);
64 }
65 return errCode;
66 }
67
GetKvDbSize(const std::string & dirAll,const std::string & dirStoreOnly,const std::string & dbName,uint64_t & size)68 int KvDBUtils::GetKvDbSize(const std::string &dirAll, const std::string &dirStoreOnly,
69 const std::string &dbName, uint64_t &size)
70 {
71 int errCodeAll = SQLiteUtils::GetDbSize(dirAll, dbName, size);
72 if (errCodeAll != -E_NOT_FOUND && errCodeAll != E_OK) {
73 return errCodeAll;
74 }
75
76 int errCodeOnlyStore = SQLiteUtils::GetDbSize(dirStoreOnly, dbName, size);
77 if (errCodeOnlyStore != -E_NOT_FOUND && errCodeOnlyStore != E_OK) {
78 return errCodeOnlyStore;
79 }
80 if ((errCodeAll == -E_NOT_FOUND) && (errCodeOnlyStore == -E_NOT_FOUND)) {
81 return -E_NOT_FOUND;
82 }
83 return E_OK;
84 }
85 } // namespace DistributedDB