Change user db path 22/81322/6
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 25 Jul 2016 12:05:32 +0000 (21:05 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Sun, 7 Aug 2016 22:45:37 +0000 (07:45 +0900)
The user db path is changed to "/opt/dbspace/user/<uid>/".

Change-Id: I771705c9125512138ed0191ad60d173157ad517e
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
include/pkgmgr-info.h
packaging/pkgmgr-info.spec
parser/pkgmgr_parser_db.c
src/pkgmgrinfo_appinfo.c
src/pkgmgrinfo_certinfo.c
src/pkgmgrinfo_db.c
src/pkgmgrinfo_pkginfo.c

index 652828c..bc323e1 100644 (file)
@@ -170,10 +170,10 @@ extern "C" {
 
 /* For multiuser support */
 const char *getIconPath(uid_t uid, bool readonly);
-const char *getUserPkgParserDBPath(void);
-const char *getUserPkgParserDBPathUID(uid_t uid);
-const char *getUserPkgCertDBPath(void);
-const char *getUserPkgCertDBPathUID(uid_t uid);
+char *getUserPkgParserDBPath(void);
+char *getUserPkgParserDBPathUID(uid_t uid);
+char *getUserPkgCertDBPath(void);
+char *getUserPkgCertDBPathUID(uid_t uid);
 const char *getUserManifestPath(uid_t uid, bool readonly);
 
 /**
index 482c60d..a654a7d 100644 (file)
@@ -1,6 +1,6 @@
 Name:       pkgmgr-info
 Summary:    Packager Manager infomation api for package
-Version:    0.1.0
+Version:    0.1.1
 Release:    1
 Group:      Application Framework/Package Management
 License:    Apache-2.0
index 434c778..0551393 100644 (file)
@@ -2414,6 +2414,7 @@ static int __enable_app_splash_screen(const char *appid)
 API int pkgmgr_parser_initialize_db(uid_t uid)
 {
        int ret = -1;
+       char *db_path;
 
        /*Manifest DB*/
        ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
@@ -2539,11 +2540,25 @@ API int pkgmgr_parser_initialize_db(uid_t uid)
                return ret;
        }
 
-       if (0 != __parserdb_change_perm(getUserPkgCertDBPathUID(GLOBAL_USER), GLOBAL_USER))
-               _LOGD("Failed to change cert db permission\n");
+       db_path = getUserPkgCertDBPathUID(GLOBAL_USER);
+       if (db_path == NULL) {
+               _LOGD("Failed to get user cert db path - GLOBAL_USER");
+       } else {
+               ret = __parserdb_change_perm(db_path, GLOBAL_USER);
+               if (ret != 0)
+                        _LOGD("Failed to change cert db permission");
+               free(db_path);
+       }
 
-       if (0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid))
-               _LOGD("Failed to change parser db permission\n");
+       db_path = getUserPkgParserDBPathUID(uid);
+       if (db_path == NULL) {
+               _LOGD("Failed to get user pkg parser db path - %d", uid);
+       } else {
+               ret = __parserdb_change_perm(db_path, uid);
+               if (ret != 0)
+                       _LOGD("Failed to change parser db permission\n");
+               free(db_path);
+       }
 
        return 0;
 }
@@ -2629,21 +2644,38 @@ static int __parserdb_change_perm(const char *db_file, uid_t uid)
 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
 {
        int ret;
+       char *db_path;
 
        if (getuid() != OWNER_ROOT) {
                _LOGE("Only root user is allowed");
                return -1;
        }
 
-       if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
+       db_path = getUserPkgParserDBPathUID(uid);
+       if (db_path == NULL) {
+               _LOGE("Failed to get pkg parser db path - %d", uid);
+               return -1;
+       }
+
+       if (access(db_path, F_OK) != -1) {
                _LOGE("Manifest db for user %d is already exists", uid);
+               free(db_path);
+               return -1;
+       }
+       free(db_path);
+
+       db_path = getUserPkgCertDBPathUID(uid);
+       if (db_path == NULL) {
+               _LOGE("Failed to get pkg cert db path - %d", uid);
                return -1;
        }
 
-       if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
+       if (access(db_path, F_OK) != -1) {
                _LOGE("Cert db for user %d is already exists", uid);
+               free(db_path);
                return -1;
        }
+       free(db_path);
 
        ret = pkgmgr_parser_check_and_create_db(uid);
        if (ret < 0)
@@ -2661,19 +2693,39 @@ API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
 API int pkgmgr_parser_check_and_create_db(uid_t uid)
 {
        int ret = -1;
+       char *db_path;
+
+       db_path = getUserPkgParserDBPathUID(uid);
+       if (db_path == NULL) {
+               _LOGD("Failed to get pkg parser db path - %d", uid);
+               return -1;
+       }
+
        /*Manifest DB*/
-       ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
+       ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, db_path);
        if (ret) {
                _LOGD("Manifest DB creation Failed\n");
+               free(db_path);
+               return -1;
+       }
+       free(db_path);
+
+       db_path = getUserPkgCertDBPathUID(GLOBAL_USER);
+       if (db_path == NULL) {
+               _LOGE("Failed to get pkg cert db path - GLOBAL_USER");
                return -1;
        }
 
        /*Cert DB*/
-       ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(GLOBAL_USER));
+       ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, db_path);
        if (ret) {
                _LOGD("Cert DB creation Failed\n");
+               free(db_path);
                return -1;
        }
+
+       free(db_path);
+
        return 0;
 }
 
index 3607bf4..75f1077 100644 (file)
@@ -556,7 +556,7 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
                "FROM package_app_info as ai";
        int ret = PMINFO_R_ERROR;
        int idx;
-       const char *dbpath;
+       char *dbpath;
        char *bg_category_str = NULL;
        char *constraint = NULL;
        char query[MAX_QUERY_LEN] = { '\0' };
@@ -572,8 +572,10 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
        ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to open db: %d", ret);
+               free(dbpath);
                return PMINFO_R_ERROR;
        }
+       free(dbpath);
 
        ret = _get_filtered_query(filter, locale, &constraint, &bind_params);
        if (ret != PMINFO_R_OK) {
@@ -1618,18 +1620,20 @@ static char *_get_localed_label(const char *appid, const char *locale, uid_t uid
        sqlite3_stmt *stmt = NULL;
        sqlite3 *db = NULL;
        char *val;
-       const char *manifest_db;
+       char *parser_db;
 
-       manifest_db = getUserPkgParserDBPathUID(uid);
-       if (manifest_db == NULL) {
-               _LOGE("Failed to get manifest db path");
+       parser_db = getUserPkgParserDBPathUID(uid);
+       if (parser_db == NULL) {
+               _LOGE("Failed to get parser db path");
                goto err;
        }
 
-       if (sqlite3_open_v2(manifest_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
+       if (sqlite3_open_v2(parser_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
                _LOGE("DB open fail\n");
+               free(parser_db);
                goto err;
        }
+       free(parser_db);
 
        query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
        if (query == NULL) {
index 0bcf967..4fceee2 100644 (file)
@@ -116,7 +116,7 @@ API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(const char *lhs_package_id,
 {
        int ret;
        sqlite3 *db;
-       const char *dbpath;
+       char *dbpath;
 
        if (lhs_package_id == NULL || rhs_package_id == NULL ||
                        compare_result == NULL) {
@@ -132,8 +132,10 @@ API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(const char *lhs_package_id,
        ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to open db: %d", ret);
+               free(dbpath);
                return PMINFO_R_ERROR;
        }
+       free(dbpath);
 
        if (_pkginfo_compare_certinfo(db, lhs_package_id, rhs_package_id,
                                compare_result)) {
@@ -159,7 +161,7 @@ static int _pkginfo_get_pkgid_from_appid(uid_t uid, const char *appid,
                "SELECT package FROM package_app_info WHERE app_id=?";
        int ret;
        sqlite3 *db;
-       const char *dbpath;
+       char *dbpath;
        sqlite3_stmt *stmt;
 
        dbpath = getUserPkgParserDBPathUID(uid);
@@ -169,8 +171,10 @@ static int _pkginfo_get_pkgid_from_appid(uid_t uid, const char *appid,
        ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to open db: %d", ret);
+               free(dbpath);
                return PMINFO_R_ERROR;
        }
+       free(dbpath);
 
        ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -356,7 +360,7 @@ static int _pkginfo_get_certinfo(const char *pkgid, pkgmgr_certinfo_x *info)
 {
        int ret;
        sqlite3 *db;
-       const char *dbpath;
+       char *dbpath;
 
        /* open unified global cert db */
        dbpath = getUserPkgCertDBPathUID(GLOBAL_USER);
@@ -366,8 +370,10 @@ static int _pkginfo_get_certinfo(const char *pkgid, pkgmgr_certinfo_x *info)
        ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to open db: %d", ret);
+               free(dbpath);
                return PMINFO_R_ERROR;
        }
+       free(dbpath);
 
        ret = _pkginfo_get_certid(db, pkgid, info->cert_id);
        if (ret != PMINFO_R_OK) {
@@ -587,7 +593,7 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
 {
        int ret;
        sqlite3 *db;
-       const char *dbpath;
+       char *dbpath;
        pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
 
        if (pkgid == NULL || handle == NULL) {
@@ -603,8 +609,10 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
        ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READWRITE, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to open db: %d", ret);
+               free(dbpath);
                return PMINFO_R_ERROR;
        }
+       free(dbpath);
 
        ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
@@ -699,7 +707,7 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
 {
        int ret;
        sqlite3 *db;
-       const char *dbpath;
+       char *dbpath;
 
        if (pkgid == NULL) {
                _LOGE("invalid parameter");
@@ -714,8 +722,10 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
        ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READWRITE, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to open db: %d", ret);
+               free(dbpath);
                return PMINFO_R_ERROR;
        }
+       free(dbpath);
 
        ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
index 1fb4032..b92bfa2 100644 (file)
@@ -162,17 +162,23 @@ static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
        return 0;
 }
 
-static const char *_get_db_path(uid_t uid)
+static char *_get_db_path(uid_t uid)
 {
-       const char *db_path = NULL;
-       if (uid != GLOBAL_USER && uid != ROOT_UID) {
-               tzplatform_set_user(uid);
-               db_path = tzplatform_getenv(TZ_USER_DB);
-               tzplatform_reset_user();
-       } else {
-               db_path = tzplatform_getenv(TZ_SYS_DB);
+       const char *db_path;
+       char path[PATH_MAX];
+
+       db_path = tzplatform_getenv(TZ_SYS_DB);
+       if (db_path == NULL) {
+               _LOGE("Failed to get TZ_SYS_DB path");
+               return NULL;
        }
-       return db_path;
+
+       if (uid == GLOBAL_USER || uid == ROOT_UID)
+               return strdup(db_path);
+
+       snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
+
+       return strdup(path);
 }
 
 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
@@ -285,62 +291,74 @@ API const char *getIconPath(uid_t uid, bool readonly)
        return path;
 }
 
-API const char *getUserPkgParserDBPath(void)
+API char *getUserPkgParserDBPath(void)
 {
        return getUserPkgParserDBPathUID(_getuid());
 }
 
-API const char *getUserPkgParserDBPathUID(uid_t uid)
+API char *getUserPkgParserDBPathUID(uid_t uid)
 {
-       const char *pkgmgr_parser_db = NULL;
+       char pkgmgr_parser_db[PATH_MAX];
        uid_t uid_caller = getuid();
        gid_t gid = ROOT_UID;
+       char *db_path;
+
+       db_path = _get_db_path(uid);
+       if (db_path == NULL) {
+               _LOGE("Failed to get db path %d", uid);
+               return NULL;
+       }
+       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);
-               pkgmgr_parser_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_parser.db");
                gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
                tzplatform_reset_user();
-       } else {
-               pkgmgr_parser_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
        }
 
-       /* just allow certain users to create the dbspace directory if needed. */
-       if (uid_caller == ROOT_UID || uid_caller == uid) {
-               const char *db_path = _get_db_path(uid);
+       // just allow certain users to create the dbspace directory if needed.
+       if (uid_caller == ROOT_UID || uid_caller == uid)
                _mkdir_for_user(db_path, uid, gid);
-       }
 
-       return pkgmgr_parser_db;
+       free(db_path);
+
+       return strdup(pkgmgr_parser_db);
 }
 
-API const char *getUserPkgCertDBPath(void)
+API char *getUserPkgCertDBPath(void)
 {
         return getUserPkgCertDBPathUID(_getuid());
 }
 
-API const char *getUserPkgCertDBPathUID(uid_t uid)
+API char *getUserPkgCertDBPathUID(uid_t uid)
 {
-       const char *pkgmgr_cert_db = NULL;
+       char pkgmgr_cert_db[PATH_MAX];
        uid_t uid_caller = getuid();
        gid_t gid = ROOT_UID;
+       char *db_path;
+
+       db_path = _get_db_path(uid);
+       if (db_path == NULL) {
+               _LOGE("Failed to get db path %d", uid);
+               return NULL;
+       }
+       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);
-               pkgmgr_cert_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_cert.db");
                gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
                tzplatform_reset_user();
-       } else {
-               pkgmgr_cert_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
        }
 
-       /* just allow certain users to create the dbspace directory if needed. */
-       if (uid_caller == ROOT_UID || uid_caller == uid) {
-               const char *db_path = _get_db_path(uid);
+       // just allow certain users to create the dbspace directory if needed.
+       if (uid_caller == ROOT_UID || uid_caller == uid)
                _mkdir_for_user(db_path, uid, gid);
-       }
 
-       return pkgmgr_cert_db;
+       free(db_path);
+
+       return strdup(pkgmgr_cert_db);
 }
 
 API const char *getUserManifestPath(uid_t uid, bool readonly)
@@ -399,7 +417,7 @@ static const char *parserdb_tables[] = {
 int __open_manifest_db(uid_t uid, bool readonly)
 {
        int ret;
-       const char *user_pkg_parser;
+       char *user_pkg_parser;
        int flags;
 
        if (manifest_db.ref) {
@@ -408,23 +426,39 @@ int __open_manifest_db(uid_t uid, bool readonly)
        }
 
        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);
-       retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
-                       user_pkg_parser);
+       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);
-               retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
-                               user_pkg_parser);
+               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;
 }
 
@@ -448,7 +482,7 @@ static const char *certdb_tables[] = {
 int __open_cert_db(uid_t uid, bool readonly)
 {
        int ret;
-       const char *user_cert_parser;
+       char *user_cert_parser;
        int flags;
 
        if (cert_db.ref) {
@@ -457,23 +491,38 @@ int __open_cert_db(uid_t uid, bool readonly)
        }
 
        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);
-       retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
-                       user_cert_parser);
+       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);
-               retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
-                               user_cert_parser);
+               if (ret != SQLITE_OK) {
+                       _LOGE("attach db [%s] failed!", user_cert_parser);
+                       free(user_cert_parser);
+                       return -1;
+               }
        }
+
+       free(user_cert_parser);
+
        return 0;
 }
 
@@ -508,7 +557,7 @@ API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled
        /* Open db.*/
        ret = __open_manifest_db(uid, false);
        if (ret != SQLITE_OK) {
-               _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
+               _LOGE("connect db [%d] failed!\n", uid);
                return PMINFO_R_ERROR;
        }
 
@@ -613,18 +662,27 @@ API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h han
        char query[MAX_QUERY_LEN] = {'\0'};
        char *errmsg;
        sqlite3 *pkgmgr_parser_db;
+       char *db_path;
 
        retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
 
        pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
        val = info->app_info->guestmode_visibility;
        if (val) {
-               ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
+               db_path = getUserPkgParserDBPathUID(uid);
+               if (db_path == NULL) {
+                       _LOGE("Failed to get pkg parser db path - %d", uid);
+                       return PMINFO_R_ERROR;
+               }
+
+               ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
                                SQLITE_OPEN_READWRITE, NULL);
                if (ret != SQLITE_OK) {
                        _LOGE("DB Open Failed\n");
+                       free(db_path);
                        return PMINFO_R_ERROR;
                }
+               free(db_path);
 
                /*TODO: Write to DB here*/
                if (status == true)
@@ -654,10 +712,22 @@ API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_
        int ret = -1;
        sqlite3 *pkgmgr_parser_db = NULL;
        char *query = NULL;
+       char *db_path;
 
-       ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
+       db_path = getUserPkgParserDBPathUID(uid);
+       if (db_path == NULL) {
+               _LOGE("Failed to get pkg parser db path - %d", uid);
+               return PMINFO_R_ERROR;
+       }
+
+       ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
                        SQLITE_OPEN_READWRITE, NULL);
-       retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db failed!");
+       if (ret != SQLITE_OK) {
+               _LOGE("connect db failed!");
+               free(db_path);
+               return PMINFO_R_ERROR;
+       }
+       free(db_path);
 
        /*Begin transaction*/
        /* Setting Manifest DB */
index c47d1cc..bec41f6 100644 (file)
@@ -470,7 +470,7 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                "FROM package_info as pi ";
        int ret = PMINFO_R_ERROR;
        int idx = 0;
-       const char *dbpath;
+       char *dbpath;
        char *constraints = NULL;
        char query[MAX_QUERY_LEN] = { '\0' };
        package_x *info = NULL;
@@ -486,8 +486,10 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
        ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
        if (ret != SQLITE_OK) {
                _LOGD("failed to open db: %d", ret);
+               free(dbpath);
                return PMINFO_R_ERROR;
        }
+       free(dbpath);
 
        if (filter != NULL) {
                tmp_filter = filter;