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);
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();
215 /* just allow certain users to create the dbspace directory if needed. */
216 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == 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",
300 "package_app_share_allowed",
301 "package_app_app_permission",
302 "package_app_share_request",
304 "package_app_data_control",
305 "package_localized_info",
306 "package_app_icon_section_info",
307 "package_privilege_info",
308 "package_app_image_info",
312 int __open_manifest_db(uid_t uid, bool readonly)
315 char *user_pkg_parser;
318 if (manifest_db.ref) {
323 user_pkg_parser = getUserPkgParserDBPathUID(uid);
324 if (user_pkg_parser == NULL) {
325 _LOGE("Failed to get pkg parser db path - %d", uid);
329 if (access(user_pkg_parser, F_OK) != 0) {
330 _LOGE("Manifest DB does not exists !!");
331 free(user_pkg_parser);
335 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
336 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
338 if (ret != SQLITE_OK) {
339 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
340 free(user_pkg_parser);
346 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
347 parserdb_tables, uid);
348 if (ret != SQLITE_OK) {
349 _LOGE("attach db [%s] failed!\n", user_pkg_parser);
350 free(user_pkg_parser);
355 free(user_pkg_parser);
360 int __close_cert_db(void)
363 if (--cert_db.ref == 0)
364 sqlite3_close_v2(GET_DB(cert_db));
367 _LOGE("Certificate DB is already closed !!\n");
371 static const char *certdb_tables[] = {
372 "package_cert_index_info",
377 int __open_cert_db(uid_t uid, bool readonly)
380 char *user_cert_parser;
388 user_cert_parser = getUserPkgCertDBPathUID(uid);
389 if (user_cert_parser == NULL) {
390 _LOGE("Failed to get pkg cert db path - %d", uid);
394 if (access(user_cert_parser, F_OK) != 0) {
395 _LOGE("Cert DB does not exists !!");
396 free(user_cert_parser);
400 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
401 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
403 if (ret != SQLITE_OK) {
404 _LOGE("connect db [%s] failed!", user_cert_parser);
405 free(user_cert_parser);
410 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
412 if (ret != SQLITE_OK) {
413 _LOGE("attach db [%s] failed!", user_cert_parser);
414 free(user_cert_parser);
419 free(user_cert_parser);
424 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
426 *i = sqlite3_column_int(stmt, idx);
429 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
433 val = (const char *)sqlite3_column_text(stmt, idx);
438 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
440 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
442 sqlite3 *pkgmgr_parser_db = NULL;
446 db_path = getUserPkgParserDBPathUID(uid);
447 if (db_path == NULL) {
448 _LOGE("Failed to get pkg parser db path - %d", uid);
449 return PMINFO_R_ERROR;
452 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
453 SQLITE_OPEN_READWRITE, NULL);
454 if (ret != SQLITE_OK) {
455 _LOGE("connect db failed!");
457 return PMINFO_R_ERROR;
461 /*Begin transaction*/
462 /* Setting Manifest DB */
463 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
464 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
465 _LOGD("Transaction Begin\n");
467 /* pkgcakge_info table */
468 query = sqlite3_mprintf(
469 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
470 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
472 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
473 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
476 /* package_app_info table */
477 query = sqlite3_mprintf(
478 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
479 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
481 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
482 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
484 /*Commit transaction*/
485 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
486 if (ret != SQLITE_OK) {
487 _LOGE("Failed to commit transaction. Rollback now\n");
488 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
489 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
491 _LOGD("Transaction Commit and End\n");
495 sqlite3_close(pkgmgr_parser_db);
500 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
502 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());