change functions for thread-safe
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_db.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <ctype.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <grp.h>
9 #include <dirent.h>
10 #include <libgen.h>
11
12 #include <sqlite3.h>
13
14 #include <tzplatform_config.h>
15 #include <db-util.h>
16
17 #include "pkgmgr-info.h"
18 #include "pkgmgrinfo_debug.h"
19 #include "pkgmgrinfo_private.h"
20 #include "pkgmgr_parser.h"
21 #include "pkgmgr_parser_internal.h"
22
23 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
24         "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
25         " cert_info TEXT UNIQUE, " \
26         " cert_id INTEGER PRIMARY KEY, " \
27         " cert_ref_count INTEGER NOT NULL)"
28
29 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
30         "CREATE TABLE IF NOT EXISTS package_cert_info( " \
31         " package TEXT PRIMARY KEY, " \
32         " author_root_cert INTEGER, " \
33         " author_im_cert INTEGER, " \
34         " author_signer_cert INTEGER, " \
35         " dist_root_cert INTEGER, " \
36         " dist_im_cert INTEGER, " \
37         " dist_signer_cert INTEGER, " \
38         " dist2_root_cert INTEGER, " \
39         " dist2_im_cert INTEGER, " \
40         " dist2_signer_cert INTEGER)"
41
42 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
43         "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
44         "AFTER DELETE ON package_cert_info " \
45         "BEGIN" \
46         " UPDATE package_cert_index_info SET" \
47         "  cert_ref_count = cert_ref_count - 1" \
48         " WHERE cert_id = OLD.author_root_cert" \
49         "  OR cert_id = OLD.author_im_cert" \
50         "  OR cert_id = OLD.author_signer_cert" \
51         "  OR cert_id = OLD.dist_root_cert" \
52         "  OR cert_id = OLD.dist_im_cert" \
53         "  OR cert_id = OLD.dist_signer_cert" \
54         "  OR cert_id = OLD.dist2_root_cert" \
55         "  OR cert_id = OLD.dist2_im_cert" \
56         "  OR cert_id = OLD.dist2_signer_cert;" \
57         "END;"
58
59 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
60         "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
61         "AFTER UPDATE ON package_cert_index_info " \
62         "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
63         "       WHERE cert_id = OLD.cert_id) = 0) "\
64         "BEGIN" \
65         " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
66         "END;"
67
68 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \
69         "CREATE TRIGGER IF NOT EXISTS update_%s_info " \
70         "AFTER UPDATE ON package_cert_info " \
71         "WHEN (OLD.%s IS NOT NULL) " \
72         "BEGIN" \
73         " UPDATE package_cert_index_info SET" \
74         "  cert_ref_count = cert_ref_count - 1" \
75         " WHERE cert_id = OLD.%s;" \
76         "END;"
77
78 __thread db_handle manifest_db;
79 __thread db_handle cert_db;
80
81 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
82
83 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
84 {
85         int ret;
86         char *fullpath;
87         char *subpath;
88         char buf[1024];
89
90         fullpath = strdup(dir);
91         if (fullpath == NULL)
92                 return -1;
93         subpath = dirname(fullpath);
94         if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
95                 ret = _mkdir_for_user(fullpath, uid, gid);
96                 if (ret == -1) {
97                         free(fullpath);
98                         return ret;
99                 }
100         }
101
102         ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
103         if (ret && errno != EEXIST) {
104                 free(fullpath);
105                 return ret;
106         } else if (ret && errno == EEXIST) {
107                 free(fullpath);
108                 return 0;
109         }
110
111         if (getuid() == ROOT_UID) {
112                 ret = chown(dir, uid, gid);
113                 if (ret == -1)
114                         _LOGE("FAIL : chown %s %d.%d, because %s", dir, uid,
115                                         gid, strerror_r(errno, buf, sizeof(buf)));
116         }
117
118         free(fullpath);
119
120         return 0;
121 }
122
123 static const char *_get_db_path(uid_t uid) {
124         const char *db_path = NULL;
125         if (uid != GLOBAL_USER && uid != ROOT_UID) {
126                 tzplatform_set_user(uid);
127                 db_path = tzplatform_getenv(TZ_USER_DB);
128                 tzplatform_reset_user();
129         } else {
130                 db_path = tzplatform_getenv(TZ_SYS_DB);
131         }
132         return db_path;
133 }
134
135 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
136 {
137         int i;
138         char *err;
139         char query[MAX_QUERY_LEN];
140
141         if (uid != GLOBAL_USER && uid != ROOT_UID) {
142                 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
143                 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
144                         _LOGD("Don't execute query = %s error message = %s\n", query, err);
145                         sqlite3_free(err);
146                         return SQLITE_ERROR;
147                 }
148         }
149
150         for (i = 0; tables[i]; i++) {
151                 if (uid != GLOBAL_USER && uid != ROOT_UID)
152                         snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
153                                         FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
154                                         SELECT *,1 AS for_all_users FROM Global.'%s')",
155                                         tables[i], tables[i], tables[i]);
156                 else
157                         snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
158                                         FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
159                                         tables[i], tables[i]);
160                 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
161                         _LOGD("Don't execute query = %s error message = %s\n", query, err);
162                         sqlite3_free(err);
163                 }
164         }
165
166         return SQLITE_OK;
167 }
168
169 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
170 {
171         char *error_message = NULL;
172         int ret = sqlite3_exec(db, query, callback, data, &error_message);
173         if (SQLITE_OK != ret) {
174                 _LOGE("Don't execute query = %s error message = %s   ret = %d\n", query,
175                        error_message, ret);
176                 sqlite3_free(error_message);
177                 return -1;
178         }
179         sqlite3_free(error_message);
180         return 0;
181 }
182
183 int _check_create_cert_db(sqlite3 *certdb)
184 {
185         int i;
186         char buf[BUFSIZE];
187         static const char *columns[] = {
188                 "author_root_cert", "author_im_cert", "author_signer_cert",
189                 "dist_root_cert", "dist_im_cert", "dist_signer_cert",
190                 "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert",
191                 NULL};
192         int ret = 0;
193         ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
194         if (ret < 0)
195                 return ret;
196         ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
197         if (ret < 0)
198                 return ret;
199         ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
200         if (ret < 0)
201                 return ret;
202         ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
203
204         for (i = 0; columns[i] != NULL; i++) {
205                 snprintf(buf, sizeof(buf),
206                                 QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT,
207                                 columns[i], columns[i], columns[i]);
208                 ret = __exec_db_query(certdb, buf, NULL, NULL);
209                 if (ret < 0)
210                         return ret;
211         }
212         return ret;
213 }
214 static gid_t _get_gid(const char *name)
215 {
216         char buf[BUFSIZE];
217         struct group entry;
218         struct group *ge;
219         int ret;
220
221         ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
222         if (ret || ge == NULL) {
223                 _LOGE("fail to get gid of %s", name);
224                 return -1;
225         }
226
227         return entry.gr_gid;
228 }
229
230 API const char *getIconPath(uid_t uid, bool readonly)
231 {
232         const char *path = NULL;
233         uid_t uid_caller = getuid();
234         gid_t gid = ROOT_UID;
235
236         if (uid != GLOBAL_USER && uid != ROOT_UID) {
237                 tzplatform_set_user(uid);
238                 path = tzplatform_mkpath(TZ_USER_ICONS, "/");
239                 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
240                 tzplatform_reset_user();
241         } else {
242                 if (readonly)
243                         path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
244                 else
245                         path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
246         }
247
248         // just allow certain users to create the icon directory if needed.
249         if (uid_caller == ROOT_UID || uid_caller == uid)
250                 _mkdir_for_user(path, uid, gid);
251
252         return path;
253 }
254
255 API const char *getUserPkgParserDBPath(void)
256 {
257         return getUserPkgParserDBPathUID(GLOBAL_USER);
258 }
259
260 API const char *getUserPkgParserDBPathUID(uid_t uid)
261 {
262         const char *pkgmgr_parser_db = NULL;
263         uid_t uid_caller = getuid();
264         gid_t gid = ROOT_UID;
265
266         if (uid != GLOBAL_USER && uid != ROOT_UID) {
267                 tzplatform_set_user(uid);
268                 pkgmgr_parser_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_parser.db");
269                 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
270                 tzplatform_reset_user();
271         } else {
272                 pkgmgr_parser_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
273         }
274
275         // just allow certain users to create the dbspace directory if needed.
276         if (uid_caller == ROOT_UID || uid_caller == uid) {
277                 const char *db_path = _get_db_path(uid);
278                 _mkdir_for_user(db_path, uid, gid);
279         }
280
281         return pkgmgr_parser_db;
282 }
283
284 API const char *getUserPkgCertDBPath(void)
285 {
286          return getUserPkgCertDBPathUID(GLOBAL_USER);
287 }
288
289 API const char *getUserPkgCertDBPathUID(uid_t uid)
290 {
291         const char *pkgmgr_cert_db = NULL;
292         uid_t uid_caller = getuid();
293         gid_t gid = ROOT_UID;
294
295         if (uid != GLOBAL_USER && uid != ROOT_UID) {
296                 tzplatform_set_user(uid);
297                 pkgmgr_cert_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_cert.db");
298                 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
299                 tzplatform_reset_user();
300         } else {
301                 pkgmgr_cert_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
302         }
303
304         // just allow certain users to create the dbspace directory if needed.
305         if (uid_caller == ROOT_UID || uid_caller == uid) {
306                 const char *db_path = _get_db_path(uid);
307                 _mkdir_for_user(db_path, uid, gid);
308         }
309
310         return pkgmgr_cert_db;
311 }
312
313 API const char *getUserManifestPath(uid_t uid, bool readonly)
314 {
315         const char *path = NULL;
316         uid_t uid_caller = getuid();
317         gid_t gid = ROOT_UID;
318
319         if (uid != GLOBAL_USER && uid != ROOT_UID) {
320                 tzplatform_set_user(uid);
321                 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
322                 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
323                 tzplatform_reset_user();
324         } else {
325                 if (readonly)
326                         path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
327                 else
328                         path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
329         }
330
331         // just allow certain users to create the icon directory if needed.
332         if (uid_caller == ROOT_UID || uid_caller == uid)
333                 _mkdir_for_user(path, uid, gid);
334
335         return path;
336 }
337
338 int __close_manifest_db(void)
339 {
340         if (manifest_db.ref) {
341                 if (--manifest_db.ref == 0)
342                         sqlite3_close(GET_DB(manifest_db));
343                 return 0;
344         }
345         return -1;
346 }
347
348 static const char *parserdb_tables[] = {
349         "package_app_app_category",
350         "package_app_info",
351         "package_app_app_control",
352         "package_app_localized_info",
353         "package_app_app_metadata",
354         "package_app_share_allowed",
355         "package_app_app_permission",
356         "package_app_share_request",
357         "package_info",
358         "package_app_data_control",
359         "package_localized_info",
360         "package_app_icon_section_info",
361         "package_privilege_info",
362         "package_app_image_info",
363         NULL
364 };
365
366 int __open_manifest_db(uid_t uid, bool readonly)
367 {
368         int ret;
369         const char *user_pkg_parser;
370         int flags;
371
372         if (manifest_db.ref) {
373                 manifest_db.ref ++;
374                 return 0;
375         }
376
377         user_pkg_parser = getUserPkgParserDBPathUID(uid);
378         if (access(user_pkg_parser, F_OK) != 0) {
379                 _LOGE("Manifest DB does not exists !!");
380                 return -1;
381         }
382
383         flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
384         ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
385                         flags, NULL);
386         retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
387                         user_pkg_parser);
388         manifest_db.ref++;
389         if (readonly) {
390                 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
391                                 parserdb_tables, uid);
392                 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
393                                 user_pkg_parser);
394         }
395         return 0;
396 }
397
398 int __close_cert_db(void)
399 {
400         if (cert_db.ref) {
401                 if (--cert_db.ref == 0)
402                         sqlite3_close_v2(GET_DB(cert_db));
403                         return 0;
404         }
405         _LOGE("Certificate DB is already closed !!\n");
406         return -1;
407 }
408
409 static const char *certdb_tables[] = {
410         "package_cert_index_info",
411         "package_cert_info",
412         NULL
413 };
414
415 int __open_cert_db(uid_t uid, bool readonly)
416 {
417         int ret;
418         const char *user_cert_parser;
419         int flags;
420
421         if (cert_db.ref) {
422                 cert_db.ref ++;
423                 return 0;
424         }
425
426         user_cert_parser = getUserPkgCertDBPathUID(uid);
427         if (access(user_cert_parser, F_OK) != 0) {
428                 _LOGE("Cert DB does not exists !!");
429                 return -1;
430         }
431
432         flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
433         ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
434                         flags, NULL);
435         retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
436                         user_cert_parser);
437         cert_db.ref++;
438         if (readonly) {
439                 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
440                                 certdb_tables, uid);
441                 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
442                                 user_cert_parser);
443         }
444         return 0;
445 }
446
447 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
448 {
449         *i = sqlite3_column_int(stmt, idx);
450 }
451
452 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
453 {
454         const char *val;
455
456         val = (const char *)sqlite3_column_text(stmt, idx);
457         if (val)
458                 *str = strdup(val);
459 }
460
461 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
462 {
463         /* Should be implemented later */
464         return 0;
465 }
466
467 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
468 {
469         int ret;
470         char query[MAX_QUERY_LEN] = {'\0'};
471         char *error_message;
472
473         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
474
475         /* Open db.*/
476         ret = __open_manifest_db(uid, false);
477         if (ret != SQLITE_OK) {
478                 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
479                 return PMINFO_R_ERROR;
480         }
481
482         /*Begin transaction*/
483         ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
484         if (ret != SQLITE_OK) {
485                 _LOGE("Failed to begin transaction\n");
486                 __close_manifest_db();
487                 return PMINFO_R_ERROR;
488         }
489         _LOGD("Transaction Begin\n");
490
491         memset(query, '\0', MAX_QUERY_LEN);
492         snprintf(query, MAX_QUERY_LEN,
493                 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
494
495         if (SQLITE_OK !=
496             sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
497                 _LOGE("Don't execute query = %s error message = %s\n", query,
498                        error_message);
499                 sqlite3_free(error_message);
500                 return PMINFO_R_ERROR;
501         }
502         sqlite3_free(error_message);
503
504         /*Commit transaction*/
505         ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
506         if (ret != SQLITE_OK) {
507                 _LOGE("Failed to commit transaction. Rollback now\n");
508                 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
509                 __close_manifest_db();
510                 return PMINFO_R_ERROR;
511         }
512         _LOGD("Transaction Commit and End\n");
513         __close_manifest_db();
514         return PMINFO_R_OK;
515 }
516
517 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
518 {
519         return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, GLOBAL_USER);
520 }
521
522 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
523 {
524         int ret;
525         char query[MAX_QUERY_LEN] = {'\0'};
526         char *error_message;
527
528         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
529
530         ret = __open_manifest_db(uid, false);
531         if (ret == -1) {
532                 _LOGE("Fail to open manifest DB\n");
533                 return PMINFO_R_ERROR;
534         }
535
536         /*Begin transaction*/
537         ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
538         if (ret != SQLITE_OK) {
539                 _LOGE("Failed to begin transaction\n");
540                 __close_manifest_db();
541                 return PMINFO_R_ERROR;
542         }
543         _LOGD("Transaction Begin\n");
544
545         memset(query, '\0', MAX_QUERY_LEN);
546         snprintf(query, MAX_QUERY_LEN,
547                 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
548
549         if (SQLITE_OK !=
550             sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
551                 _LOGE("Don't execute query = %s error message = %s\n", query,
552                        error_message);
553                 sqlite3_free(error_message);
554                 return PMINFO_R_ERROR;
555         }
556
557         /*Commit transaction*/
558         ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
559         if (ret != SQLITE_OK) {
560                 _LOGE("Failed to commit transaction. Rollback now\n");
561                 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
562                 __close_manifest_db();
563                 return PMINFO_R_ERROR;
564         }
565         _LOGD("Transaction Commit and End\n");
566         __close_manifest_db();
567         return PMINFO_R_OK;
568 }
569
570 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
571 {
572         return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, GLOBAL_USER);
573 }
574
575 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
576 {
577         const char *val;
578         int ret;
579         char query[MAX_QUERY_LEN] = {'\0'};
580         char *errmsg;
581         sqlite3 *pkgmgr_parser_db;
582
583         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
584
585         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
586         val = info->app_info->guestmode_visibility;
587         if (val) {
588                 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
589                                 SQLITE_OPEN_READWRITE, NULL);
590                 if (ret != SQLITE_OK) {
591                         _LOGE("DB Open Failed\n");
592                         return PMINFO_R_ERROR;
593                 }
594
595                 /*TODO: Write to DB here*/
596                 if (status == true)
597                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
598                 else
599                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
600
601                 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
602                 sqlite3_close(pkgmgr_parser_db);
603                 if (ret != SQLITE_OK) {
604                         _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
605                         free(errmsg);
606                         return PMINFO_R_ERROR;
607                 }
608         }
609         return PMINFO_R_OK;
610 }
611
612 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
613 {
614         return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, GLOBAL_USER, status);
615 }
616
617 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
618 {
619         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
620         int ret = -1;
621         sqlite3 *pkgmgr_parser_db = NULL;
622         char *query = NULL;
623
624         ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
625                         SQLITE_OPEN_READWRITE, NULL);
626         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db failed!");
627
628         /*Begin transaction*/
629         // Setting Manifest DB
630         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
631         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
632         _LOGD("Transaction Begin\n");
633
634         // pkgcakge_info table
635         query = sqlite3_mprintf("update package_info set installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
636
637         ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
638         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
639         sqlite3_free(query);
640
641         // package_app_info table
642         query = sqlite3_mprintf("update package_app_info set app_installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
643
644         ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
645         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
646
647         /*Commit transaction*/
648         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
649         if (ret != SQLITE_OK) {
650                 _LOGE("Failed to commit transaction. Rollback now\n");
651                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
652                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
653         }
654         _LOGD("Transaction Commit and End\n");
655
656         ret = PMINFO_R_OK;
657 catch:
658         sqlite3_close(pkgmgr_parser_db);
659         sqlite3_free(query);
660         return ret;
661 }
662
663 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
664 {
665         return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, GLOBAL_USER);
666 }