4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Garima Shrivastava<garima.s@samsung.com>
7 * Jyotsna Dhumale <jyotsna.a@samsung.com>
8 * Venkatesha Sarpangala <sarpangala.v@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
28 #include "app2sd_internals.h"
30 #define MAX_QUERY_LEN 4096
31 #define PASSWORD_LENGTH 64
33 ########### Internal APIs ##################
37 #define APP2SD_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".app2sd.db")
39 #define QUERY_CREATE_TABLE_APP2SD "CREATE TABLE IF NOT EXISTS app2sd_info " \
40 "(pkgid TEXT PRIMARY KEY NOT NULL, password TEXT NOT NULL, " \
41 "filename TEXT NOT NULL, uid INTEGER)"
43 int _app2sd_initialize_db()
45 char *error_message = NULL;
49 fp = fopen(APP2SD_DB_FILE, "r");
52 ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
53 DB_UTIL_REGISTER_HOOK_METHOD);
55 if (ret != SQLITE_OK) {
56 _E("connect menu_db [%s] failed",
63 ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
64 DB_UTIL_REGISTER_HOOK_METHOD);
66 if (ret != SQLITE_OK) {
67 _E("connect menu_db [%s] failed",
72 if (SQLITE_OK != sqlite3_exec(app2sd_db,
73 QUERY_CREATE_TABLE_APP2SD, NULL, NULL,
75 _E("don't execute query = (%s), " \
76 "error message = (%s)",
77 QUERY_CREATE_TABLE_APP2SD, error_message);
84 static int _app2sd_check_existing_info(const char *pkgid, uid_t uid)
87 const char *val = NULL;
88 sqlite3_stmt *stmt = NULL;
91 query = sqlite3_mprintf("select count(*) from app2sd_info " \
92 "where pkgid=%Q and uid=%d", pkgid, uid);
94 _E("failed to make a query");
98 ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
99 if (ret != SQLITE_OK) {
100 _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
105 if (sqlite3_step(stmt) != SQLITE_ROW) {
106 _E("failed to step");
107 sqlite3_finalize(stmt);
111 val = (const char *)sqlite3_column_text(stmt, 0);
113 sqlite3_finalize(stmt);
118 int _app2sd_set_info_in_db(const char *pkgid, const char *passwd,
119 const char *loopback_device, uid_t uid)
121 char *error_message = NULL;
125 ret = _app2sd_check_existing_info(pkgid, uid);
127 _E("failed to get existing info");
128 return APP2EXT_ERROR_SQLITE_REGISTRY;
132 query = sqlite3_mprintf("insert into app2sd_info " \
133 "(pkgid, password, filename, uid) values (%Q, %Q, %Q, %d)",
134 pkgid, passwd, loopback_device, uid);
136 query = sqlite3_mprintf("update app2sd_info " \
137 "set password=%Q, filename=%Q where pkgid=%Q and uid=%d",
138 passwd, loopback_device, pkgid, uid);
140 ret = sqlite3_exec(app2sd_db, query, NULL, NULL, &error_message);
141 if (ret != SQLITE_OK) {
142 _E("failed to execute query(%s), error message(%s)",
143 query, error_message);
145 return APP2EXT_ERROR_SQLITE_REGISTRY;
149 return APP2EXT_SUCCESS;
152 int _app2sd_remove_info_from_db(const char *pkgid, uid_t uid)
154 char *error_message = NULL;
158 query = sqlite3_mprintf("delete from app2sd_info " \
159 "where pkgid=%Q and uid=%d", pkgid, uid);
161 ret = sqlite3_exec(app2sd_db, query, NULL, NULL, &error_message);
162 if (ret != SQLITE_OK) {
163 _E("failed to execute query(%s), error message(%s)",
164 query, error_message);
166 return APP2EXT_ERROR_SQLITE_REGISTRY;
170 return APP2EXT_SUCCESS;
173 int _app2sd_get_info_from_db(const char *filename, char **pkgid, uid_t *uid)
176 sqlite3_stmt *stmt = NULL;
177 int ret = APP2EXT_SUCCESS;
179 _D("filename(%s)", filename);
180 query = sqlite3_mprintf("select * from app2sd_info " \
181 "where filename=%Q", filename);
183 _E("failed to make a query");
184 return APP2EXT_ERROR_SQLITE_REGISTRY;
187 ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
188 if (ret != SQLITE_OK) {
189 _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
193 return APP2EXT_ERROR_SQLITE_REGISTRY;
196 ret = sqlite3_step(stmt);
197 if (ret != SQLITE_ROW || ret == SQLITE_DONE) {
198 _W("no records found");
199 ret = APP2EXT_SUCCESS;
203 *pkgid = strdup((const char *)sqlite3_column_text(stmt, 0));
204 if (*pkgid == NULL) {
206 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
210 *uid = sqlite3_column_int(stmt, 3);
211 if (*uid != GLOBAL_USER && *uid < REGULAR_USER) {
213 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
217 if (SQLITE_OK != sqlite3_finalize(stmt)) {
218 _E("error : sqlite3_finalize");
219 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
224 return APP2EXT_SUCCESS;
233 sqlite3_finalize(stmt);
239 char *_app2sd_get_password_from_db(const char *pkgid, uid_t uid)
243 sqlite3_stmt *stmt = NULL;
246 query = sqlite3_mprintf("select * from app2sd_info " \
247 "where pkgid=%Q and uid=%d", pkgid, uid);
249 _E("failed to make a query");
253 ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
254 if (ret != SQLITE_OK) {
255 _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
260 ret = sqlite3_step(stmt);
261 if (ret != SQLITE_ROW || ret == SQLITE_DONE) {
262 _W("no records found");
265 passwd = malloc(PASSWORD_LENGTH + 1);
266 if (passwd == NULL) {
267 _E("memory allocation failed");
271 strncpy(passwd, (const char *)sqlite3_column_text(stmt, 1),
273 if (passwd == NULL) {
277 if (SQLITE_OK != sqlite3_finalize(stmt)) {
278 _E("error : sqlite3_finalize");
289 sqlite3_finalize(stmt);