X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fpkgmgrinfo_db.c;h=e382bf5398b61b0872cb175d7c55bb1c0c750de8;hb=49943f233bb83f85148a88989035b82103499001;hp=7f8ecf8432a22502e47f701ddbd1a70db98e6c25;hpb=0086b6c3645de8fbfc64a9c943920d3a4ba5f3cb;p=platform%2Fcore%2Fappfw%2Fpkgmgr-info.git diff --git a/src/pkgmgrinfo_db.c b/src/pkgmgrinfo_db.c index 7f8ecf8..e382bf5 100644 --- a/src/pkgmgrinfo_db.c +++ b/src/pkgmgrinfo_db.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include #include #include @@ -19,82 +20,7 @@ #include "pkgmgrinfo_debug.h" #include "pkgmgrinfo_private.h" #include "pkgmgr_parser.h" -#include "pkgmgr_parser_internal.h" - -#define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \ - "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \ - " cert_info TEXT UNIQUE, " \ - " cert_id INTEGER PRIMARY KEY, " \ - " cert_ref_count INTEGER NOT NULL)" - -#define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \ - "CREATE TABLE IF NOT EXISTS package_cert_info( " \ - " package TEXT PRIMARY KEY, " \ - " package_count INTEGER, " \ - " author_root_cert INTEGER, " \ - " author_im_cert INTEGER, " \ - " author_signer_cert INTEGER, " \ - " dist_root_cert INTEGER, " \ - " dist_im_cert INTEGER, " \ - " dist_signer_cert INTEGER, " \ - " dist2_root_cert INTEGER, " \ - " dist2_im_cert INTEGER, " \ - " dist2_signer_cert INTEGER)" - -#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO \ - "CREATE TRIGGER IF NOT EXISTS update_cert_info " \ - "AFTER UPDATE ON package_cert_info " \ - "WHEN (NEW.package_count = 0) " \ - "BEGIN" \ - " DELETE FROM package_cert_info WHERE package=OLD.package;" \ - "END;" - -#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \ - "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \ - "AFTER UPDATE ON package_cert_info " \ - "WHEN (NEW.package_count = OLD.package_count + 1) " \ - "BEGIN" \ - " UPDATE package_cert_index_info SET" \ - " cert_ref_count = cert_ref_count - 1" \ - " WHERE cert_id = OLD.author_root_cert" \ - " OR cert_id = OLD.author_im_cert" \ - " OR cert_id = OLD.author_signer_cert" \ - " OR cert_id = OLD.dist_root_cert" \ - " OR cert_id = OLD.dist_im_cert" \ - " OR cert_id = OLD.dist_signer_cert" \ - " OR cert_id = OLD.dist2_root_cert" \ - " OR cert_id = OLD.dist2_im_cert" \ - " OR cert_id = OLD.dist2_signer_cert;" \ - "END;" - -#define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \ - "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \ - "AFTER DELETE ON package_cert_info " \ - "BEGIN" \ - " UPDATE package_cert_index_info SET" \ - " cert_ref_count = cert_ref_count - 1" \ - " WHERE cert_id = OLD.author_root_cert" \ - " OR cert_id = OLD.author_im_cert" \ - " OR cert_id = OLD.author_signer_cert" \ - " OR cert_id = OLD.dist_root_cert" \ - " OR cert_id = OLD.dist_im_cert" \ - " OR cert_id = OLD.dist_signer_cert" \ - " OR cert_id = OLD.dist2_root_cert" \ - " OR cert_id = OLD.dist2_im_cert" \ - " OR cert_id = OLD.dist2_signer_cert;" \ - "END;" - -#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \ - "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \ - "AFTER UPDATE ON package_cert_index_info " \ - "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \ - " WHERE cert_id = OLD.cert_id) = 0) "\ - "BEGIN" \ - " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \ - "END;" - -__thread db_handle manifest_db; -__thread db_handle cert_db; +#include "pkgmgr_parser_db.h" typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname); @@ -133,6 +59,7 @@ static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid) if (fd == -1) { _LOGE("FAIL : open %s : %s", dir, strerror_r(errno, buf, sizeof(buf))); + free(fullpath); return -1; } ret = fstat(fd, &sb); @@ -140,11 +67,13 @@ static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid) _LOGE("FAIL : fstat %s : %s", dir, strerror_r(errno, buf, sizeof(buf))); close(fd); + free(fullpath); return -1; } if (S_ISLNK(sb.st_mode)) { _LOGE("FAIL : %s is symlink!", dir); close(fd); + free(fullpath); return -1; } ret = fchown(fd, uid, gid); @@ -152,6 +81,7 @@ static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid) _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid, gid, strerror_r(errno, buf, sizeof(buf))); close(fd); + free(fullpath); return -1; } close(fd); @@ -181,75 +111,11 @@ static char *_get_db_path(uid_t uid) return strdup(path); } -static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid) -{ - int i; - char *err; - char query[MAX_QUERY_LEN]; - - if (uid != GLOBAL_USER && uid != ROOT_UID) { - snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db); - if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) { - _LOGD("Don't execute query = %s error message = %s\n", query, err); - sqlite3_free(err); - return SQLITE_ERROR; - } - } - - for (i = 0; tables[i]; i++) { - if (uid != GLOBAL_USER && uid != ROOT_UID) - snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \ - FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \ - SELECT *,1 AS for_all_users FROM Global.'%s')", - tables[i], tables[i], tables[i]); - else - snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \ - FROM (SELECT *,1 AS for_all_users FROM main.'%s')", - tables[i], tables[i]); - if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) { - _LOGD("Don't execute query = %s error message = %s\n", query, err); - sqlite3_free(err); - } - } - - return SQLITE_OK; -} - -static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data) +int _check_create_cert_db(void) { - char *error_message = NULL; - int ret = sqlite3_exec(db, query, callback, data, &error_message); - if (SQLITE_OK != ret) { - _LOGE("Don't execute query = %s error message = %s ret = %d\n", query, - error_message, ret); - sqlite3_free(error_message); - return -1; - } - sqlite3_free(error_message); - return 0; + return pkgmgr_parser_initialize_cert_db(); } -int _check_create_cert_db(sqlite3 *certdb) -{ - int ret = 0; - ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL); - if (ret < 0) - return ret; - ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL); - if (ret < 0) - return ret; - ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL); - if (ret < 0) - return ret; - ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL); - if (ret < 0) - return ret; - ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL); - if (ret < 0) - return ret; - ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL); - return ret; -} static gid_t _get_gid(const char *name) { char buf[BUFSIZE]; @@ -279,11 +145,10 @@ API const char *getIconPath(uid_t uid, bool readonly) if (readonly) path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/"); - else - path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/"); /* just allow certain users to create the icon directory if needed. */ - if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid) + if (path && (uid_caller == ROOT_UID || + uid_caller == APPFW_UID || uid_caller == uid)) _mkdir_for_user(path, uid, gid); return path; @@ -308,17 +173,17 @@ API char *getUserPkgParserDBPathUID(uid_t uid) } snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db), "%s/.pkgmgr_parser.db", db_path); - - if (uid != GLOBAL_USER && uid != ROOT_UID) { - tzplatform_set_user(uid); - gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP)); - tzplatform_reset_user(); + if (access(db_path, F_OK) != 0) { + if (uid != GLOBAL_USER && uid != ROOT_UID) { + tzplatform_set_user(uid); + gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP)); + tzplatform_reset_user(); + } + /* just allow certain users to create the dbspace directory if needed. */ + if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || + uid_caller == uid) + _mkdir_for_user(db_path, uid, gid); } - - // just allow certain users to create the dbspace directory if needed. - if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid) - _mkdir_for_user(db_path, uid, gid); - free(db_path); return strdup(pkgmgr_parser_db); @@ -350,7 +215,7 @@ API char *getUserPkgCertDBPathUID(uid_t uid) tzplatform_reset_user(); } - // just allow certain users to create the dbspace directory if needed. + /* just allow certain users to create the dbspace directory if needed. */ if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid) _mkdir_for_user(db_path, uid, gid); @@ -384,152 +249,12 @@ API const char *getUserManifestPath(uid_t uid, bool readonly) return path; } -int __close_manifest_db(void) -{ - if (manifest_db.ref) { - if (--manifest_db.ref == 0) - sqlite3_close(GET_DB(manifest_db)); - return 0; - } - return -1; -} - -static const char *parserdb_tables[] = { - "package_app_app_category", - "package_app_info", - "package_app_app_control", - "package_app_localized_info", - "package_app_app_metadata", - "package_app_share_allowed", - "package_app_app_permission", - "package_app_share_request", - "package_info", - "package_app_data_control", - "package_localized_info", - "package_app_icon_section_info", - "package_privilege_info", - "package_app_image_info", - NULL -}; - -int __open_manifest_db(uid_t uid, bool readonly) -{ - int ret; - char *user_pkg_parser; - int flags; - - if (manifest_db.ref) { - manifest_db.ref++; - return 0; - } - - user_pkg_parser = getUserPkgParserDBPathUID(uid); - if (user_pkg_parser == NULL) { - _LOGE("Failed to get pkg parser db path - %d", uid); - return -1; - } - - if (access(user_pkg_parser, F_OK) != 0) { - _LOGE("Manifest DB does not exists !!"); - free(user_pkg_parser); - return -1; - } - - flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE; - ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db), - flags, NULL); - if (ret != SQLITE_OK) { - _LOGE("connect db [%s] failed!\n", user_pkg_parser); - free(user_pkg_parser); - return -1; - } - - manifest_db.ref++; - if (readonly) { - ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB, - parserdb_tables, uid); - if (ret != SQLITE_OK) { - _LOGE("attach db [%s] failed!\n", user_pkg_parser); - free(user_pkg_parser); - return -1; - } - } - - free(user_pkg_parser); - - return 0; -} - -int __close_cert_db(void) -{ - if (cert_db.ref) { - if (--cert_db.ref == 0) - sqlite3_close_v2(GET_DB(cert_db)); - return 0; - } - _LOGE("Certificate DB is already closed !!\n"); - return -1; -} - -static const char *certdb_tables[] = { - "package_cert_index_info", - "package_cert_info", - NULL -}; - -int __open_cert_db(uid_t uid, bool readonly) -{ - int ret; - char *user_cert_parser; - int flags; - - if (cert_db.ref) { - cert_db.ref++; - return 0; - } - - user_cert_parser = getUserPkgCertDBPathUID(uid); - if (user_cert_parser == NULL) { - _LOGE("Failed to get pkg cert db path - %d", uid); - return -1; - } - - if (access(user_cert_parser, F_OK) != 0) { - _LOGE("Cert DB does not exists !!"); - free(user_cert_parser); - return -1; - } - - flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE; - ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db), - flags, NULL); - if (ret != SQLITE_OK) { - _LOGE("connect db [%s] failed!", user_cert_parser); - free(user_cert_parser); - return -1; - } - cert_db.ref++; - if (readonly) { - ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB, - certdb_tables, uid); - if (ret != SQLITE_OK) { - _LOGE("attach db [%s] failed!", user_cert_parser); - free(user_cert_parser); - return -1; - } - } - - free(user_cert_parser); - - return 0; -} - void _save_column_int(sqlite3_stmt *stmt, int idx, int *i) { *i = sqlite3_column_int(stmt, idx); } -void _save_column_str(sqlite3_stmt *stmt, int idx, char **str) +inline void _save_column_str(sqlite3_stmt *stmt, int idx, char **str) { const char *val; @@ -538,13 +263,14 @@ void _save_column_str(sqlite3_stmt *stmt, int idx, char **str) *str = strdup(val); } -API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid) +API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid) { retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n"); int ret = -1; sqlite3 *pkgmgr_parser_db = NULL; char *query = NULL; char *db_path; + const char *location_str; db_path = getUserPkgParserDBPathUID(uid); if (db_path == NULL) { @@ -567,10 +293,16 @@ API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_ tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n"); _LOGD("Transaction Begin\n"); + if (location == INSTALL_INTERNAL) + location_str = "installed_internal"; + else if (location == INSTALL_EXTERNAL) + location_str = "installed_external"; + else + location_str = "installed_extended"; /* pkgcakge_info table */ query = sqlite3_mprintf( - "update package_info set installed_storage=%Q where package=%Q", - location ? "installed_external" : "installed_internal", pkgid); + "update package_info set installed_storage=%Q, external_path=%Q where package=%Q", + location_str, external_pkg_path, pkgid); ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL); tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query); @@ -578,8 +310,8 @@ API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_ /* package_app_info table */ query = sqlite3_mprintf( - "update package_app_info set app_installed_storage=%Q where package=%Q", - location ? "installed_external" : "installed_internal", pkgid); + "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q", + location_str, external_pkg_path, pkgid); ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL); tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query); @@ -600,7 +332,7 @@ catch: return ret; } -API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location) +API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path) { - return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, _getuid()); + return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid()); }