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 app2sd \
40 (pkgid text primary key,\
44 int _app2sd_initialize_db()
46 char *error_message = NULL;
50 fp = fopen(APP2SD_DB_FILE, "r");
53 ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
54 DB_UTIL_REGISTER_HOOK_METHOD);
56 if (ret != SQLITE_OK) {
57 _E("connect menu_db [%s] failed",
64 ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
65 DB_UTIL_REGISTER_HOOK_METHOD);
67 if (ret != SQLITE_OK) {
68 _E("connect menu_db [%s] failed",
73 if (SQLITE_OK != sqlite3_exec(app2sd_db,
74 QUERY_CREATE_TABLE_APP2SD, NULL, NULL,
76 _E("don't execute query = (%s), " \
77 "error message = (%s)",
78 QUERY_CREATE_TABLE_APP2SD, error_message);
85 int _app2sd_set_password_in_db(const char *pkgid, const char *passwd)
87 char *error_message = NULL;
90 query = sqlite3_mprintf("insert into app2sd" \
91 "(pkgid, password) values (%Q, %Q)", pkgid, passwd);
93 if (SQLITE_OK != sqlite3_exec(app2sd_db, query, NULL, NULL,
95 _E("don't execute query = (%s), error message = (%s)",
96 query, error_message);
98 return APP2EXT_ERROR_SQLITE_REGISTRY;
102 return APP2EXT_SUCCESS;
105 int _app2sd_remove_password_from_db(const char *pkgid)
107 char *error_message = NULL;
110 query = sqlite3_mprintf("delete from app2sd" \
111 " where pkgid=%Q", pkgid);
113 if (SQLITE_OK != sqlite3_exec(app2sd_db, query, NULL,
114 NULL, &error_message)) {
115 _E("don't execute query = (%s), "
116 "error message = (%s)", query, error_message);
118 return APP2EXT_ERROR_SQLITE_REGISTRY;
123 return APP2EXT_SUCCESS;
126 char *_app2sd_get_password_from_db(const char *pkgid)
130 const char *tail = NULL;
131 sqlite3_stmt *stmt = NULL;
134 query = sqlite3_mprintf("select * from app2sd" \
135 " where pkgid=%Q", pkgid);
137 if (SQLITE_OK != sqlite3_prepare(app2sd_db, query,
138 strlen(query), &stmt, &tail)) {
139 _E("sqlite3_prepare error");
144 rc = sqlite3_step(stmt);
145 if (rc != SQLITE_ROW || rc == SQLITE_DONE) {
146 _E("no records found");
149 passwd = malloc(PASSWORD_LENGTH + 1);
150 if (passwd == NULL) {
151 _E("memory allocation failed");
155 strncpy(passwd, (const char*)sqlite3_column_text(stmt, 1),
157 if (passwd == NULL) {
161 if (SQLITE_OK != sqlite3_finalize(stmt)) {
162 _E("error : sqlite3_finalize");
170 rc = sqlite3_finalize(stmt);
171 if (rc != SQLITE_OK) {
172 _E("sqlite3_finalize failed(%d)", rc);