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