15 #include <tzplatform_config.h>
18 #include "pkgmgr-info.h"
19 #include "pkgmgrinfo_debug.h"
20 #include "pkgmgrinfo_private.h"
21 #include "pkgmgr_parser.h"
22 #include "pkgmgr_parser_db.h"
24 __thread db_handle manifest_db;
25 __thread db_handle cert_db;
27 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
29 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
38 fullpath = strdup(dir);
41 subpath = dirname(fullpath);
42 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
43 ret = _mkdir_for_user(fullpath, uid, gid);
50 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
51 if (ret && errno != EEXIST) {
54 } else if (ret && errno == EEXIST) {
59 if (getuid() == ROOT_UID) {
60 fd = open(dir, O_RDONLY);
62 _LOGE("FAIL : open %s : %s", dir,
63 strerror_r(errno, buf, sizeof(buf)));
68 _LOGE("FAIL : fstat %s : %s", dir,
69 strerror_r(errno, buf, sizeof(buf)));
73 if (S_ISLNK(sb.st_mode)) {
74 _LOGE("FAIL : %s is symlink!", dir);
78 ret = fchown(fd, uid, gid);
80 _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid,
81 gid, strerror_r(errno, buf, sizeof(buf)));
93 static char *_get_db_path(uid_t uid)
98 db_path = tzplatform_getenv(TZ_SYS_DB);
99 if (db_path == NULL) {
100 _LOGE("Failed to get TZ_SYS_DB path");
104 if (uid == GLOBAL_USER || uid == ROOT_UID)
105 return strdup(db_path);
107 snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
112 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
116 char query[MAX_QUERY_LEN];
118 if (uid != GLOBAL_USER && uid != ROOT_UID) {
119 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
120 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
121 _LOGD("Don't execute query = %s error message = %s\n", query, err);
127 for (i = 0; tables[i]; i++) {
128 if (uid != GLOBAL_USER && uid != ROOT_UID)
129 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
130 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
131 SELECT *,1 AS for_all_users FROM Global.'%s')",
132 tables[i], tables[i], tables[i]);
134 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
135 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
136 tables[i], tables[i]);
137 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
138 _LOGD("Don't execute query = %s error message = %s\n", query, err);
146 int _check_create_cert_db(void)
148 return pkgmgr_parser_initialize_cert_db();
151 static gid_t _get_gid(const char *name)
158 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
159 if (ret || ge == NULL) {
160 _LOGE("fail to get gid of %s", name);
167 API const char *getIconPath(uid_t uid, bool readonly)
169 const char *path = NULL;
170 uid_t uid_caller = getuid();
171 gid_t gid = ROOT_UID;
173 if (uid != GLOBAL_USER && uid != ROOT_UID) {
174 _LOGD("not supported target user");
179 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
181 /* just allow certain users to create the icon directory if needed. */
182 if (path && (uid_caller == ROOT_UID ||
183 uid_caller == APPFW_UID || uid_caller == uid))
184 _mkdir_for_user(path, uid, gid);
189 API char *getUserPkgParserDBPath(void)
191 return getUserPkgParserDBPathUID(_getuid());
194 API char *getUserPkgParserDBPathUID(uid_t uid)
196 char pkgmgr_parser_db[PATH_MAX];
197 uid_t uid_caller = getuid();
198 gid_t gid = ROOT_UID;
201 db_path = _get_db_path(uid);
202 if (db_path == NULL) {
203 _LOGE("Failed to get db path %d", uid);
206 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
207 "%s/.pkgmgr_parser.db", db_path);
208 if (access(db_path, F_OK) != 0) {
209 if (uid != GLOBAL_USER && uid != ROOT_UID) {
210 tzplatform_set_user(uid);
211 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
212 tzplatform_reset_user();
214 /* just allow certain users to create the dbspace directory if needed. */
215 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID ||
217 _mkdir_for_user(db_path, uid, gid);
221 return strdup(pkgmgr_parser_db);
224 API char *getUserPkgCertDBPath(void)
226 return getUserPkgCertDBPathUID(_getuid());
229 API char *getUserPkgCertDBPathUID(uid_t uid)
231 char pkgmgr_cert_db[PATH_MAX];
232 uid_t uid_caller = getuid();
233 gid_t gid = ROOT_UID;
236 db_path = _get_db_path(uid);
237 if (db_path == NULL) {
238 _LOGE("Failed to get db path %d", uid);
241 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
242 "%s/.pkgmgr_cert.db", db_path);
244 if (uid != GLOBAL_USER && uid != ROOT_UID) {
245 tzplatform_set_user(uid);
246 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
247 tzplatform_reset_user();
250 /* just allow certain users to create the dbspace directory if needed. */
251 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
252 _mkdir_for_user(db_path, uid, gid);
256 return strdup(pkgmgr_cert_db);
259 API const char *getUserManifestPath(uid_t uid, bool readonly)
261 const char *path = NULL;
262 uid_t uid_caller = getuid();
263 gid_t gid = ROOT_UID;
265 if (uid != GLOBAL_USER && uid != ROOT_UID) {
266 tzplatform_set_user(uid);
267 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
268 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
269 tzplatform_reset_user();
272 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
274 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
277 /* just allow certain users to create the icon directory if needed. */
278 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
279 _mkdir_for_user(path, uid, gid);
284 int __close_manifest_db(void)
286 if (manifest_db.ref) {
287 if (--manifest_db.ref == 0)
288 sqlite3_close(GET_DB(manifest_db));
294 static const char *parserdb_tables[] = {
295 "package_app_app_category",
297 "package_app_app_control",
298 "package_app_localized_info",
299 "package_app_app_metadata",
301 "package_app_data_control",
302 "package_localized_info",
303 "package_privilege_info",
304 "package_appdefined_privilege_info",
305 "package_app_data_control_privilege",
309 int __open_manifest_db(uid_t uid, bool readonly)
312 char *user_pkg_parser;
315 if (manifest_db.ref) {
320 user_pkg_parser = getUserPkgParserDBPathUID(uid);
321 if (user_pkg_parser == NULL) {
322 _LOGE("Failed to get pkg parser db path - %d", uid);
326 if (access(user_pkg_parser, F_OK) != 0) {
327 _LOGE("Manifest DB does not exists !!");
328 free(user_pkg_parser);
332 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
333 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
335 if (ret != SQLITE_OK) {
336 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
337 free(user_pkg_parser);
343 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
344 parserdb_tables, uid);
345 if (ret != SQLITE_OK) {
346 _LOGE("attach db [%s] failed!\n", user_pkg_parser);
347 free(user_pkg_parser);
352 free(user_pkg_parser);
357 int __close_cert_db(void)
360 if (--cert_db.ref == 0)
361 sqlite3_close_v2(GET_DB(cert_db));
364 _LOGE("Certificate DB is already closed !!\n");
368 static const char *certdb_tables[] = {
369 "package_cert_index_info",
374 int __open_cert_db(uid_t uid, bool readonly)
377 char *user_cert_parser;
385 user_cert_parser = getUserPkgCertDBPathUID(uid);
386 if (user_cert_parser == NULL) {
387 _LOGE("Failed to get pkg cert db path - %d", uid);
391 if (access(user_cert_parser, F_OK) != 0) {
392 _LOGE("Cert DB does not exists !!");
393 free(user_cert_parser);
397 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
398 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
400 if (ret != SQLITE_OK) {
401 _LOGE("connect db [%s] failed!", user_cert_parser);
402 free(user_cert_parser);
407 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
409 if (ret != SQLITE_OK) {
410 _LOGE("attach db [%s] failed!", user_cert_parser);
411 free(user_cert_parser);
416 free(user_cert_parser);
421 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
423 *i = sqlite3_column_int(stmt, idx);
426 inline void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
430 val = (const char *)sqlite3_column_text(stmt, idx);
435 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
437 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
439 sqlite3 *pkgmgr_parser_db = NULL;
442 const char *location_str;
444 db_path = getUserPkgParserDBPathUID(uid);
445 if (db_path == NULL) {
446 _LOGE("Failed to get pkg parser db path - %d", uid);
447 return PMINFO_R_ERROR;
450 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
451 SQLITE_OPEN_READWRITE, NULL);
452 if (ret != SQLITE_OK) {
453 _LOGE("connect db failed!");
455 return PMINFO_R_ERROR;
459 /*Begin transaction*/
460 /* Setting Manifest DB */
461 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
462 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
463 _LOGD("Transaction Begin\n");
465 if (location == INSTALL_INTERNAL)
466 location_str = "installed_internal";
467 else if (location == INSTALL_EXTERNAL)
468 location_str = "installed_external";
470 location_str = "installed_extended";
471 /* pkgcakge_info table */
472 query = sqlite3_mprintf(
473 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
474 location_str, external_pkg_path, pkgid);
476 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
477 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
480 /* package_app_info table */
481 query = sqlite3_mprintf(
482 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
483 location_str, external_pkg_path, pkgid);
485 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
486 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
488 /*Commit transaction*/
489 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
490 if (ret != SQLITE_OK) {
491 _LOGE("Failed to commit transaction. Rollback now\n");
492 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
493 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
495 _LOGD("Transaction Commit and End\n");
499 sqlite3_close(pkgmgr_parser_db);
504 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
506 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());