2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <sys/types.h>
24 #include <package-manager.h>
25 #include <package-manager-debug.h>
27 #include "pkgmgr-server.h"
30 #define DB_DIR "/var/lib/package-manager"
33 static const char *_get_db_path(void)
35 return DB_DIR"/blacklist.db";
38 static sqlite3 *_open_db(void)
44 path = _get_db_path();
46 ERR("get db path error");
50 ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
51 if (ret != SQLITE_OK) {
52 ERR("open db error: %d", ret);
59 static int __add_blacklist_info(sqlite3 *db, uid_t uid, const char *pkgid)
61 static const char query[] =
62 "INSERT INTO blacklist (uid, pkgid) VALUES(?, ?)";
66 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
67 if (ret != SQLITE_OK) {
68 ERR("prepare error: %s", sqlite3_errmsg(db));
72 sqlite3_bind_int(stmt, 1, uid);
73 sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
75 ret = sqlite3_step(stmt);
76 sqlite3_finalize(stmt);
77 if (ret != SQLITE_DONE) {
78 ERR("step error: %s", sqlite3_errmsg(db));
85 int __add_blacklist(uid_t uid, const char *pkgid)
92 return PKGMGR_R_ERROR;
94 ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
95 if (ret != SQLITE_OK) {
97 ERR("transaction failed");
98 return PKGMGR_R_ERROR;
101 if (__add_blacklist_info(db, uid, pkgid)) {
102 sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
103 sqlite3_close_v2(db);
104 return PKGMGR_R_ERROR;
107 ret = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
108 if (ret != SQLITE_OK) {
109 ERR("commit error: %s", sqlite3_errmsg(db));
110 sqlite3_close_v2(db);
111 return PKGMGR_R_ERROR;
114 sqlite3_close_v2(db);
119 static int __remove_blacklist_info(sqlite3 *db, uid_t uid, const char *pkgid)
121 static const char query[] =
122 "DELETE FROM blacklist WHERE uid=? AND pkgid=?";
126 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
127 if (ret != SQLITE_OK) {
128 ERR("prepare error: %s", sqlite3_errmsg(db));
132 sqlite3_bind_int(stmt, 1, uid);
133 sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
135 ret = sqlite3_step(stmt);
136 sqlite3_finalize(stmt);
137 if (ret != SQLITE_DONE) {
138 ERR("step error: %s", sqlite3_errmsg(db));
145 int __remove_blacklist(uid_t uid, const char *pkgid)
152 return PKGMGR_R_ERROR;
154 ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
155 if (ret != SQLITE_OK) {
156 sqlite3_close_v2(db);
157 ERR("transaction failed");
158 return PKGMGR_R_ERROR;
161 if (__remove_blacklist_info(db, uid, pkgid)) {
162 sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
163 sqlite3_close_v2(db);
164 return PKGMGR_R_ERROR;
167 ret = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
168 if (ret != SQLITE_OK) {
169 ERR("commit error: %s", sqlite3_errmsg(db));
170 sqlite3_close_v2(db);
171 return PKGMGR_R_ERROR;
174 sqlite3_close_v2(db);
179 int __check_blacklist(uid_t uid, const char *pkgid, int *result)
181 static const char query[] =
182 "SELECT * FROM blacklist WHERE uid=? AND pkgid=?";
190 return PKGMGR_R_ERROR;
193 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
194 if (ret != SQLITE_OK) {
195 ERR("prepare error: %s", sqlite3_errmsg(db));
197 sqlite3_close_v2(db);
198 return PKGMGR_R_ERROR;
201 sqlite3_bind_int(stmt, 1, uid);
202 sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
204 ret = sqlite3_step(stmt);
205 sqlite3_finalize(stmt);
206 if (ret != SQLITE_ROW) {
207 if (ret != SQLITE_DONE)
208 ERR("step error: %s", sqlite3_errmsg(db));
210 sqlite3_close_v2(db);
211 return ret == SQLITE_DONE ? PKGMGR_R_OK : PKGMGR_R_ERROR;
215 sqlite3_close_v2(db);