Fix build fail issue
[platform/core/appfw/pkgmgr-server.git] / src / db.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21
22 #include <sqlite3.h>
23
24 #include <package-manager.h>
25
26 #include "pkgmgr-server.h"
27
28 #ifndef DB_DIR
29 #define DB_DIR "/var/lib/package-manager"
30 #endif
31
32 static const char *_get_db_path(void)
33 {
34         return DB_DIR"/blacklist.db";
35 }
36
37 static sqlite3 *_open_db(void)
38 {
39         int ret;
40         const char *path;
41         sqlite3 *db;
42
43         path = _get_db_path();
44         if (path == NULL) {
45                 ERR("get db path error");
46                 return NULL;
47         }
48
49         ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
50         if (ret != SQLITE_OK) {
51                 ERR("open db error: %d", ret);
52                 return NULL;
53         }
54
55         return db;
56 }
57
58 static int __add_blacklist_info(sqlite3 *db, uid_t uid, const char *pkgid)
59 {
60         static const char query[] =
61                 "INSERT INTO blacklist (uid, pkgid) VALUES(?, ?)";
62         int ret;
63         sqlite3_stmt *stmt;
64
65         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
66         if (ret != SQLITE_OK) {
67                 ERR("prepare error: %s", sqlite3_errmsg(db));
68                 return -1;
69         }
70
71         sqlite3_bind_int(stmt, 1, uid);
72         sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
73
74         ret = sqlite3_step(stmt);
75         sqlite3_finalize(stmt);
76         if (ret != SQLITE_DONE) {
77                 ERR("step error: %s", sqlite3_errmsg(db));
78                 return -1;
79         }
80
81         return 0;
82 }
83
84 int __add_blacklist(uid_t uid, const char *pkgid)
85 {
86         int ret;
87         sqlite3 *db;
88
89         db = _open_db();
90         if (db == NULL)
91                 return PKGMGR_R_ERROR;
92
93         ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
94         if (ret != SQLITE_OK) {
95                 sqlite3_close_v2(db);
96                 ERR("transaction failed");
97                 return PKGMGR_R_ERROR;
98         }
99
100         if (__add_blacklist_info(db, uid, pkgid)) {
101                 sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
102                 sqlite3_close_v2(db);
103                 return PKGMGR_R_ERROR;
104         }
105
106         ret = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
107         if (ret != SQLITE_OK) {
108                 ERR("commit error: %s", sqlite3_errmsg(db));
109                 sqlite3_close_v2(db);
110                 return PKGMGR_R_ERROR;
111         }
112
113         sqlite3_close_v2(db);
114
115         return PKGMGR_R_OK;
116 }
117
118 static int __remove_blacklist_info(sqlite3 *db, uid_t uid, const char *pkgid)
119 {
120         static const char query[] =
121                 "DELETE FROM blacklist WHERE uid=? AND pkgid=?";
122         int ret;
123         sqlite3_stmt *stmt;
124
125         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
126         if (ret != SQLITE_OK) {
127                 ERR("prepare error: %s", sqlite3_errmsg(db));
128                 return -1;
129         }
130
131         sqlite3_bind_int(stmt, 1, uid);
132         sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
133
134         ret = sqlite3_step(stmt);
135         sqlite3_finalize(stmt);
136         if (ret != SQLITE_DONE) {
137                 ERR("step error: %s", sqlite3_errmsg(db));
138                 return -1;
139         }
140
141         return 0;
142 }
143
144 int __remove_blacklist(uid_t uid, const char *pkgid)
145 {
146         int ret;
147         sqlite3 *db;
148
149         db = _open_db();
150         if (db == NULL)
151                 return PKGMGR_R_ERROR;
152
153         ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
154         if (ret != SQLITE_OK) {
155                 sqlite3_close_v2(db);
156                 ERR("transaction failed");
157                 return PKGMGR_R_ERROR;
158         }
159
160         if (__remove_blacklist_info(db, uid, pkgid)) {
161                 sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
162                 sqlite3_close_v2(db);
163                 return PKGMGR_R_ERROR;
164         }
165
166         ret = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
167         if (ret != SQLITE_OK) {
168                 ERR("commit error: %s", sqlite3_errmsg(db));
169                 sqlite3_close_v2(db);
170                 return PKGMGR_R_ERROR;
171         }
172
173         sqlite3_close_v2(db);
174
175         return PKGMGR_R_OK;
176 }
177
178 int __check_blacklist(uid_t uid, const char *pkgid, int *result)
179 {
180         static const char query[] =
181                 "SELECT * FROM blacklist WHERE uid=? AND pkgid=?";
182         int ret;
183         sqlite3 *db;
184         sqlite3_stmt *stmt;
185
186         db = _open_db();
187         if (db == NULL) {
188                 *result = 0;
189                 return PKGMGR_R_ERROR;
190         }
191
192         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
193         if (ret != SQLITE_OK) {
194                 ERR("prepare error: %s", sqlite3_errmsg(db));
195                 *result = 0;
196                 sqlite3_close_v2(db);
197                 return PKGMGR_R_ERROR;
198         }
199
200         sqlite3_bind_int(stmt, 1, uid);
201         sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
202
203         ret = sqlite3_step(stmt);
204         sqlite3_finalize(stmt);
205         if (ret != SQLITE_ROW) {
206                 if (ret != SQLITE_DONE)
207                         ERR("step error: %s", sqlite3_errmsg(db));
208                 *result = 0;
209                 sqlite3_close_v2(db);
210                 return ret == SQLITE_DONE ? PKGMGR_R_OK : PKGMGR_R_ERROR;
211         }
212
213         *result = 1;
214         sqlite3_close_v2(db);
215
216         return PKGMGR_R_OK;
217 }