1 /*
2 * Copyright (c) 2021-2022 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 #include "sqlite_helper.h"
17
18 #include "accesstoken_log.h"
19 #include "sqlite3ext.h"
20 #include <sys/types.h>
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SqliteHelper"};
27 }
28
SqliteHelper(const std::string & dbName,const std::string & dbPath,int32_t version)29 SqliteHelper::SqliteHelper(const std::string& dbName, const std::string& dbPath, int32_t version)
30 : dbName_(dbName), dbPath_(dbPath), currentVersion_(version), db_(nullptr)
31 {}
32
~SqliteHelper()33 SqliteHelper::~SqliteHelper()
34 {}
35
Open()36 void SqliteHelper::Open() __attribute__((no_sanitize("cfi")))
37 {
38 if (db_ != nullptr) {
39 ACCESSTOKEN_LOG_WARN(LABEL, "db s already open");
40 return;
41 }
42 if (dbName_.empty() || dbPath_.empty() || currentVersion_ < 0) {
43 ACCESSTOKEN_LOG_ERROR(LABEL, "param invalid, dbName: %{public}s, "
44 "dbPath: %{public}s, currentVersion: %{public}d",
45 dbName_.c_str(), dbPath_.c_str(), currentVersion_);
46 return;
47 }
48 // set soft heap limit as 10KB
49 const int32_t heapLimit = 10 * 1024;
50 sqlite3_soft_heap_limit64(heapLimit);
51 std::string fileName = dbPath_ + dbName_;
52 int32_t res = sqlite3_open(fileName.c_str(), &db_);
53 if (res != SQLITE_OK) {
54 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to open db: %{public}s", sqlite3_errmsg(db_));
55 return;
56 }
57
58 int32_t version = GetVersion();
59 if (version == currentVersion_) {
60 return;
61 }
62
63 BeginTransaction();
64 if (version == DataBaseVersion::VERISION_0) {
65 OnCreate();
66 } else {
67 if (version < currentVersion_) {
68 OnUpdate(version);
69 }
70 }
71 SetVersion();
72 CommitTransaction();
73 }
74
Close()75 void SqliteHelper::Close()
76 {
77 if (db_ == nullptr) {
78 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
79 return;
80 }
81 int32_t ret = sqlite3_close(db_);
82 if (ret != SQLITE_OK) {
83 ACCESSTOKEN_LOG_WARN(LABEL, "sqlite3_close error, ret=%{public}d", ret);
84 return;
85 }
86 db_ = nullptr;
87 }
88
BeginTransaction() const89 int32_t SqliteHelper::BeginTransaction() const
90 {
91 if (db_ == nullptr) {
92 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
93 return GENERAL_ERROR;
94 }
95 char* errorMessage = nullptr;
96 int32_t result = 0;
97 int32_t ret = sqlite3_exec(db_, "BEGIN;", nullptr, nullptr, &errorMessage);
98 if (ret != SQLITE_OK) {
99 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
100 result = GENERAL_ERROR;
101 }
102 sqlite3_free(errorMessage);
103 return result;
104 }
105
CommitTransaction() const106 int32_t SqliteHelper::CommitTransaction() const
107 {
108 if (db_ == nullptr) {
109 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
110 return GENERAL_ERROR;
111 }
112 char* errorMessage = nullptr;
113 int32_t result = 0;
114 int32_t ret = sqlite3_exec(db_, "COMMIT;", nullptr, nullptr, &errorMessage);
115 if (ret != SQLITE_OK) {
116 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
117 result = GENERAL_ERROR;
118 }
119 sqlite3_free(errorMessage);
120 return result;
121 }
122
RollbackTransaction() const123 int32_t SqliteHelper::RollbackTransaction() const
124 {
125 if (db_ == nullptr) {
126 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
127 return GENERAL_ERROR;
128 }
129 int32_t result = 0;
130 char* errorMessage = nullptr;
131 int32_t ret = sqlite3_exec(db_, "ROLLBACK;", nullptr, nullptr, &errorMessage);
132 if (ret != SQLITE_OK) {
133 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
134 result = GENERAL_ERROR;
135 }
136 sqlite3_free(errorMessage);
137 return result;
138 }
139
Prepare(const std::string & sql) const140 Statement SqliteHelper::Prepare(const std::string& sql) const
141 {
142 return Statement(db_, sql);
143 }
144
ExecuteSql(const std::string & sql) const145 int32_t SqliteHelper::ExecuteSql(const std::string& sql) const
146 {
147 if (db_ == nullptr) {
148 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
149 return GENERAL_ERROR;
150 }
151 char* errorMessage = nullptr;
152 int32_t result = 0;
153 int32_t res = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errorMessage);
154 if (res != SQLITE_OK) {
155 ACCESSTOKEN_LOG_ERROR(LABEL, "failed, errorMsg: %{public}s", errorMessage);
156 result = GENERAL_ERROR;
157 }
158 sqlite3_free(errorMessage);
159 return result;
160 }
161
GetVersion() const162 int32_t SqliteHelper::GetVersion() const __attribute__((no_sanitize("cfi")))
163 {
164 if (db_ == nullptr) {
165 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
166 return GENERAL_ERROR;
167 }
168 auto statement = Prepare(PRAGMA_VERSION_COMMAND);
169 int32_t version = 0;
170 while (statement.Step() == Statement::State::ROW) {
171 version = statement.GetColumnInt(0);
172 }
173 ACCESSTOKEN_LOG_INFO(LABEL, "version: %{public}d", version);
174 return version;
175 }
176
SetVersion() const177 void SqliteHelper::SetVersion() const
178 {
179 if (db_ == nullptr) {
180 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
181 return;
182 }
183 auto statement = Prepare(PRAGMA_VERSION_COMMAND + " = " + std::to_string(currentVersion_));
184 statement.Step();
185 }
186
SpitError() const187 std::string SqliteHelper::SpitError() const
188 {
189 if (db_ == nullptr) {
190 ACCESSTOKEN_LOG_WARN(LABEL, "do open data base first!");
191 return "";
192 }
193 return sqlite3_errmsg(db_);
194 }
195 } // namespace AccessToken
196 } // namespace Security
197 } // namespace OHOS
198