Fix db open functions 48/46148/1 accepted/tizen/mobile/20150817.134134 accepted/tizen/tv/20150817.134529 accepted/tizen/wearable/20150817.134809 submit/tizen/20150817.084057
authorSangyoon Jang <s89.jang@samsung.com>
Mon, 17 Aug 2015 07:50:54 +0000 (16:50 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Mon, 17 Aug 2015 07:50:54 +0000 (16:50 +0900)
add a new parameter to open db on writable or readonly mode
return error when manifest db does not exist

Change-Id: I15bf60d2b8d5bc1e75e05568124e6fc535559698
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
include/pkgmgrinfo_private.h
src/pkgmgrinfo_appinfo.c
src/pkgmgrinfo_certinfo.c
src/pkgmgrinfo_db.c
src/pkgmgrinfo_pkginfo.c

index c228545..a7f8cf9 100644 (file)
@@ -24,6 +24,7 @@
 #ifndef __PKGMGRINFO_PRIVATE_H__
 #define __PKGMGRINFO_PRIVATE_H__
 
+#include <stdbool.h>
 #include <unistd.h>
 #include <sys/types.h>
 
@@ -218,9 +219,9 @@ pkgmgrinfo_pkginfo_filter_prop_range _pminfo_pkginfo_convert_to_prop_range(const
 
 int _check_create_cert_db(sqlite3 *certdb);
 int __close_manifest_db(void);
-int __open_manifest_db(uid_t uid);
+int __open_manifest_db(uid_t uid, bool readonly);
 int __close_cert_db(void);
-int __open_cert_db(uid_t uid, char* mode);
+int __open_cert_db(uid_t uid, bool readonly);
 void _save_column_str(sqlite3_stmt *stmt, int idx, const char **str);
 char *_get_system_locale(void);
 void __get_filter_condition(gpointer data, char **condition);
index 28b4e09..678c3b0 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <ctype.h>
 #include <unistd.h>
@@ -618,7 +619,7 @@ API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
                return PMINFO_R_EINVAL;
        }
 
-       if (__open_manifest_db(uid) < 0)
+       if (__open_manifest_db(uid, true) < 0)
                return PMINFO_R_ERROR;
 
        locale = _get_system_locale();
@@ -657,7 +658,7 @@ static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
        char *locale;
        int stop = 0;
 
-       if (__open_manifest_db(uid) < 0)
+       if (__open_manifest_db(uid, true) < 0)
                return PMINFO_R_ERROR;
 
        locale = _get_system_locale();
@@ -1312,7 +1313,7 @@ API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid, cons
        sqlite3_stmt *stmt = NULL;
 
        /*open db*/
-       ret = __open_manifest_db(uid);
+       ret = __open_manifest_db(uid, true);
        retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
 
        /*Start constructing query*/
@@ -1353,7 +1354,7 @@ API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid
        sqlite3_stmt *stmt = NULL;
 
        /*open db*/
-       ret = __open_manifest_db(uid);
+       ret = __open_manifest_db(uid, true);
        retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
 
        /*Start constructing query*/
@@ -1862,7 +1863,7 @@ API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle,
                return PMINFO_R_ERROR;
        }
 
-       ret = __open_manifest_db(uid);
+       ret = __open_manifest_db(uid, true);
        if (ret == -1) {
                _LOGE("Fail to open manifest DB\n");
                free(locale);
@@ -2087,7 +2088,7 @@ API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
                return PMINFO_R_EINVAL;
        }
 
-       if (__open_manifest_db(uid) < 0)
+       if (__open_manifest_db(uid, true) < 0)
                return PMINFO_R_ERROR;
 
        list = _appinfo_get_metadata_filtered_list(handle);
index cb095ad..6bd3ebb 100644 (file)
@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
+#include <stdbool.h>
 #include <unistd.h>
 #include <sys/types.h>
 
@@ -205,7 +205,7 @@ static int __delete_certinfo(const char *pkgid, uid_t uid)
                goto err;
        }
 
-       __open_cert_db(uid, "w");
+       __open_cert_db(uid, false);
        /*populate certinfo from DB*/
        snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
        ret = __exec_certinfo_query(query, (void *)certinfo);
@@ -320,7 +320,7 @@ API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(const char *lhs_package_id,
        retvm_if(rhs_package_id == NULL, PMINFO_R_EINVAL, "rhs package ID is NULL");
        retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
 
-       ret = __open_cert_db(uid, "r");
+       ret = __open_cert_db(uid, true);
        if (ret != 0) {
                ret = PMINFO_R_ERROR;
                goto err;
@@ -547,7 +547,7 @@ API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(const char *lhs_app_id, con
        info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
        retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
 
-       ret = __open_manifest_db(uid);
+       ret = __open_manifest_db(uid, true);
        if (ret != SQLITE_OK) {
                _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
                ret = PMINFO_R_ERROR;
@@ -660,7 +660,7 @@ API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_
        int i = 0;
 
        /*Open db.*/
-       ret = __open_cert_db(uid,"r");
+       ret = __open_cert_db(uid, true);
        if (ret != SQLITE_OK) {
                _LOGE("connect db [%s] failed!\n");
                ret = PMINFO_R_ERROR;
@@ -800,7 +800,7 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
        info->pkgid = strdup(pkgid);
 
        /*Open db.*/
-       ret =__open_cert_db(uid, "w");
+       ret =__open_cert_db(uid, false);
        if (ret != 0) {
                ret = PMINFO_R_ERROR;
                _LOGE("Failed to open cert db \n");
@@ -980,7 +980,7 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
        retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
        int ret = -1;
        /*Open db.*/
-       ret = __open_cert_db(uid, "w");
+       ret = __open_cert_db(uid, false);
        if (ret != 0) {
                _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPathUID(uid));
                ret = PMINFO_R_ERROR;
index 42ef905..87bcd2d 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <grp.h>
@@ -319,35 +320,35 @@ static const char *parserdb_tables[] = {
        NULL
 };
 
-int __open_manifest_db(uid_t uid)
+int __open_manifest_db(uid_t uid, bool readonly)
 {
-       int ret = -1;
+       int ret;
+       const char *user_pkg_parser;
+       int flags;
+
        if (manifest_db.ref) {
                manifest_db.ref ++;
                return 0;
        }
-       const char* user_pkg_parser = getUserPkgParserDBPathUID(uid);
-       if (access(user_pkg_parser, F_OK) != 0) {
-               _LOGE("Manifest DB does not exists !! try to create\n");
-
-               if (pkgmgr_parser_check_and_create_db(uid)) {
-                       _LOGE("create db failed");
-                       return -1;
-               }
 
-               if (pkgmgr_parser_initialize_db(uid)) {
-                       _LOGE("initialize db failed");
-                       return -1;
-               }
+       user_pkg_parser = getUserPkgParserDBPathUID(uid);
+       if (access(user_pkg_parser, F_OK) != 0) {
+               _LOGE("Manifest DB does not exists !!");
+               return -1;
        }
 
+       flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
        ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
-                       SQLITE_OPEN_READONLY, NULL);
-       retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n", user_pkg_parser);
-       manifest_db.ref ++;
-       ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB, parserdb_tables, uid);
-       retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n", user_pkg_parser);
-
+                       flags, NULL);
+       retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
+                       user_pkg_parser);
+       manifest_db.ref++;
+       if (readonly) {
+               ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
+                               parserdb_tables, uid);
+               retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
+                               user_pkg_parser);
+       }
        return 0;
 }
 
@@ -368,28 +369,36 @@ static const char *certdb_tables[] = {
        NULL
 };
 
-int __open_cert_db(uid_t uid, char* mode)
+int __open_cert_db(uid_t uid, bool readonly)
 {
-       int ret = -1;
+       int ret;
+       const char *user_cert_parser;
+       int flags;
+
        if (cert_db.ref) {
                cert_db.ref ++;
                return 0;
        }
 
-       const char* user_cert_parser = getUserPkgCertDBPathUID(uid);
-       if (access(user_cert_parser, F_OK) == 0) {
-               ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
-                                SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
-               retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n", user_cert_parser);
-               cert_db.ref ++;
-               if ((strcmp(mode, "w") != 0)) {
-                       ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB, certdb_tables, uid);
-                       retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n", user_cert_parser);
-               }
-               return 0;
+       user_cert_parser = getUserPkgCertDBPathUID(uid);
+       if (access(user_cert_parser, F_OK) != 0) {
+               _LOGE("Cert DB does not exists !!");
+               return -1;
        }
-       _LOGE("Cert DB does not exists !!\n");
-       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);
+       retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
+                       user_cert_parser);
+       cert_db.ref++;
+       if (readonly) {
+               ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
+                               certdb_tables, uid);
+               retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
+                               user_cert_parser);
+       }
+       return 0;
 }
 
 void _save_column_str(sqlite3_stmt *stmt, int idx, const char **str)
@@ -416,7 +425,7 @@ API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled
        retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
 
        /* Open db.*/
-       ret = __open_manifest_db(uid);
+       ret = __open_manifest_db(uid, false);
        if (ret != SQLITE_OK) {
                _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
                return PMINFO_R_ERROR;
@@ -470,7 +479,7 @@ API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *
 
        retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
 
-       ret = __open_manifest_db(uid);
+       ret = __open_manifest_db(uid, false);
        if (ret == -1) {
                _LOGE("Fail to open manifest DB\n");
                return PMINFO_R_ERROR;
index 566a685..f7086be 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdbool.h>
 #include <unistd.h>
 #include <ctype.h>
 #include <sys/smack.h>
@@ -245,7 +246,7 @@ static int _pkginfo_get_filtered_foreach_pkginfo(pkgmgrinfo_filter_x *filter,
        char *locale;
        int stop = 0;
 
-       if (__open_manifest_db(uid) < 0)
+       if (__open_manifest_db(uid, true) < 0)
                return PMINFO_R_ERROR;
 
        locale = _get_system_locale();
@@ -737,7 +738,7 @@ API int pkgmgrinfo_pkginfo_get_usr_pkginfo(const char *pkgid, uid_t uid,
                return PMINFO_R_EINVAL;
        }
 
-       if (__open_manifest_db(uid) < 0)
+       if (__open_manifest_db(uid, true) < 0)
                return PMINFO_R_ERROR;
 
 
@@ -1828,7 +1829,7 @@ API int pkgmgrinfo_pkginfo_usr_filter_count(pkgmgrinfo_pkginfo_filter_h handle,
                return PMINFO_R_ERROR;
        }
 
-       ret = __open_manifest_db(uid);
+       ret = __open_manifest_db(uid, true);
        if (ret == -1) {
                _LOGE("Fail to open manifest DB\n");
                free(locale);