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.
24 #include <app2sd_internals.h>
25 #include <app2sd_interface.h>
27 #include <sys/types.h>
31 #include <sys/types.h>
38 /* For multi-user support */
39 #include <tzplatform_config.h>
41 #define MAX_QUERY_LEN 4096
42 #define PASSWORD_LENGTH 64
44 ########### Internal APIs ##################
48 #define APP2SD_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".app2sd.db")
50 #define QUERY_CREATE_TABLE_APP2SD "create table app2sd \
51 (pkgid text primary key,\
56 *@_app2sd_initialize_db
57 *This function is to initialize sqlite db.
58 * return: On success, it will return zero else if fail then return val<0.
60 int _app2sd_initialize_db()
62 char *error_message = NULL;
65 fp = fopen(APP2SD_DB_FILE, "r");
69 db_util_open(APP2SD_DB_FILE, &app2sd_db,
70 DB_UTIL_REGISTER_HOOK_METHOD);
72 if (ret != SQLITE_OK) {
73 app2ext_print("====>>>> connect menu_db [%s] failed!\n",
81 db_util_open(APP2SD_DB_FILE, &app2sd_db,
82 DB_UTIL_REGISTER_HOOK_METHOD);
84 if (ret != SQLITE_OK) {
85 app2ext_print("====>>>> connect menu_db [%s] failed!\n",
91 sqlite3_exec(app2sd_db, QUERY_CREATE_TABLE_APP2SD,
92 NULL, NULL, &error_message)) {
93 app2ext_print("Don't execute query = %s, "
94 "error message = %s\n",
95 QUERY_CREATE_TABLE_APP2SD, error_message);
99 app2ext_print("\n db_initialize_done ");
104 *@_app2sd_set_password_in_db
105 *This function is to store password into db.
106 * param[in]: pkgid: package id
107 * param[in]: password: password string
108 * return: On success, it will return 0.
109 * Else appropriate error will be returned.
111 int _app2sd_set_password_in_db(const char *pkgid,
114 char *error_message = NULL;
116 char *query = sqlite3_mprintf("insert into app2sd(pkgid,password) values (%Q, %Q)", pkgid, passwd);
118 if (SQLITE_OK != sqlite3_exec(app2sd_db, query, NULL, NULL,
120 app2ext_print("Don't execute query = %s, error message = %s\n",
121 query, error_message);
124 return APP2EXT_ERROR_SQLITE_REGISTRY;
127 app2ext_print("\n sqlite insertion done ");
128 return APP2EXT_SUCCESS;
132 *@_app2sd_remove_password_from_db
133 *This function is to remove passwod from db.
134 * param[in]: pkgid: package id
135 * return: On success, it will return 0.
136 * Else appropriate error will be returned.
138 int _app2sd_remove_password_from_db(const char *pkgid)
140 char *error_message = NULL;
142 char *query = sqlite3_mprintf("delete from app2sd where pkgid LIKE %Q", pkgid);
143 app2ext_print("\n deletion querys is %s ", query);
145 if (SQLITE_OK != sqlite3_exec(app2sd_db, query, NULL,
146 NULL, &error_message)) {
147 app2ext_print("Don't execute query = %s, error message = %s\n",
148 query, error_message);
150 return APP2EXT_ERROR_SQLITE_REGISTRY;
154 app2ext_print("\n app2sd password deletion done ");
155 return APP2EXT_SUCCESS;
160 *@_app2sd_get_password_from_db
161 *This function is to retrive password from DB
162 * param[in]: pkgid: package id
163 * return: On success, it will return the password, else NULL.
165 char *_app2sd_get_password_from_db(const char *pkgid)
167 char query[MAX_QUERY_LEN] = { 0 };
168 sqlite3_stmt *stmt = NULL;
169 const char *tail = NULL;
173 sqlite3_snprintf(MAX_QUERY_LEN, query,
174 "select * from app2sd where pkgid LIKE '%s'", pkgid);
175 app2ext_print("\n access querys is %s ", query);
177 if (SQLITE_OK != sqlite3_prepare(app2sd_db, query,
178 strlen(query), &stmt, &tail)) {
179 app2ext_print("sqlite3_prepare error\n");
183 rc = sqlite3_step(stmt);
184 if (rc != SQLITE_ROW || rc == SQLITE_DONE) {
185 app2ext_print("No records found");
188 passwd = malloc(PASSWORD_LENGTH + 1);
189 if (passwd == NULL) {
190 app2ext_print("memory allocation failed\n");
194 app2ext_print("\n entry available in sqlite");
195 strncpy(passwd, (const char*)sqlite3_column_text(stmt, 1),
197 if (passwd == NULL) {
198 app2ext_print("\n password is NULL ");
201 app2ext_print("\n passwd is %s ", passwd);
202 if (SQLITE_OK != sqlite3_finalize(stmt)) {
203 app2ext_print("error : sqlite3_finalize\n");
209 rc = sqlite3_finalize(stmt);
210 if (rc != SQLITE_OK) {
211 app2ext_print(" sqlite3_finalize failed - %d", rc);