Implement app signal related with app disable/enable
[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 #include <package-manager-debug.h>
26
27 #include "pkgmgr-server.h"
28
29 #ifndef DB_DIR
30 #define DB_DIR "/var/lib/package-manager"
31 #endif
32
33 static const char *_get_db_path(void)
34 {
35         return DB_DIR"/blacklist.db";
36 }
37
38 static sqlite3 *_open_db(void)
39 {
40         int ret;
41         const char *path;
42         sqlite3 *db;
43
44         path = _get_db_path();
45         if (path == NULL) {
46                 ERR("get db path error");
47                 return NULL;
48         }
49
50         ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
51         if (ret != SQLITE_OK) {
52                 ERR("open db error: %d", ret);
53                 return NULL;
54         }
55
56         return db;
57 }
58
59 static int __add_blacklist_info(sqlite3 *db, uid_t uid, const char *pkgid)
60 {
61         static const char query[] =
62                 "INSERT INTO blacklist (uid, pkgid) VALUES(?, ?)";
63         int ret;
64         sqlite3_stmt *stmt;
65
66         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
67         if (ret != SQLITE_OK) {
68                 ERR("prepare error: %s", sqlite3_errmsg(db));
69                 return -1;
70         }
71
72         sqlite3_bind_int(stmt, 1, uid);
73         sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
74
75         ret = sqlite3_step(stmt);
76         sqlite3_finalize(stmt);
77         if (ret != SQLITE_DONE) {
78                 ERR("step error: %s", sqlite3_errmsg(db));
79                 return -1;
80         }
81
82         return 0;
83 }
84
85 int __add_blacklist(uid_t uid, const char *pkgid)
86 {
87         int ret;
88         sqlite3 *db;
89
90         db = _open_db();
91         if (db == NULL)
92                 return PKGMGR_R_ERROR;
93
94         ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
95         if (ret != SQLITE_OK) {
96                 sqlite3_close_v2(db);
97                 ERR("transaction failed");
98                 return PKGMGR_R_ERROR;
99         }
100
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;
105         }
106
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;
112         }
113
114         sqlite3_close_v2(db);
115
116         return PKGMGR_R_OK;
117 }
118
119 static int __remove_blacklist_info(sqlite3 *db, uid_t uid, const char *pkgid)
120 {
121         static const char query[] =
122                 "DELETE FROM blacklist WHERE uid=? AND pkgid=?";
123         int ret;
124         sqlite3_stmt *stmt;
125
126         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
127         if (ret != SQLITE_OK) {
128                 ERR("prepare error: %s", sqlite3_errmsg(db));
129                 return -1;
130         }
131
132         sqlite3_bind_int(stmt, 1, uid);
133         sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
134
135         ret = sqlite3_step(stmt);
136         sqlite3_finalize(stmt);
137         if (ret != SQLITE_DONE) {
138                 ERR("step error: %s", sqlite3_errmsg(db));
139                 return -1;
140         }
141
142         return 0;
143 }
144
145 int __remove_blacklist(uid_t uid, const char *pkgid)
146 {
147         int ret;
148         sqlite3 *db;
149
150         db = _open_db();
151         if (db == NULL)
152                 return PKGMGR_R_ERROR;
153
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;
159         }
160
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;
165         }
166
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;
172         }
173
174         sqlite3_close_v2(db);
175
176         return PKGMGR_R_OK;
177 }
178
179 int __check_blacklist(uid_t uid, const char *pkgid, int *result)
180 {
181         static const char query[] =
182                 "SELECT * FROM blacklist WHERE uid=? AND pkgid=?";
183         int ret;
184         sqlite3 *db;
185         sqlite3_stmt *stmt;
186
187         db = _open_db();
188         if (db == NULL) {
189                 *result = 0;
190                 return PKGMGR_R_ERROR;
191         }
192
193         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
194         if (ret != SQLITE_OK) {
195                 ERR("prepare error: %s", sqlite3_errmsg(db));
196                 *result = 0;
197                 sqlite3_close_v2(db);
198                 return PKGMGR_R_ERROR;
199         }
200
201         sqlite3_bind_int(stmt, 1, uid);
202         sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
203
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));
209                 *result = 0;
210                 sqlite3_close_v2(db);
211                 return ret == SQLITE_DONE ? PKGMGR_R_OK : PKGMGR_R_ERROR;
212         }
213
214         *result = 1;
215         sqlite3_close_v2(db);
216
217         return PKGMGR_R_OK;
218 }