Remove unnecessary function
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_db.c
index 37f2771..54492f4 100644 (file)
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -21,9 +22,6 @@
 #include "pkgmgr_parser.h"
 #include "pkgmgr_parser_db.h"
 
-__thread db_handle manifest_db;
-__thread db_handle cert_db;
-
 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
 
 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
@@ -61,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);
@@ -68,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);
@@ -80,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);
@@ -109,40 +111,6 @@ 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 = NULL;
-       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;
-}
-
 int _check_create_cert_db(void)
 {
        return pkgmgr_parser_initialize_cert_db();
@@ -223,34 +191,12 @@ API char *getUserPkgParserDBPathUID(uid_t uid)
 
 API char *getUserPkgCertDBPath(void)
 {
-        return getUserPkgCertDBPathUID(_getuid());
-}
-
-API char *getUserPkgCertDBPathUID(uid_t uid)
-{
-       char pkgmgr_cert_db[PATH_MAX];
-       uid_t uid_caller = getuid();
-       gid_t gid = ROOT_UID;
        char *db_path;
+       char pkgmgr_cert_db[PATH_MAX];
 
-       db_path = _get_db_path(uid);
-       if (db_path == NULL) {
-               _LOGE("Failed to get db path %d", uid);
-               return NULL;
-       }
+       db_path = _get_db_path(GLOBAL_USER);
        snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
                        "%s/.pkgmgr_cert.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();
-       }
-
-       /* 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_cert_db);
@@ -281,143 +227,6 @@ 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_info",
-       "package_app_data_control",
-       "package_localized_info",
-       "package_privilege_info",
-       "package_appdefined_privilege_info",
-       "package_app_data_control_privilege",
-       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);