fix missing encoded_id related change & code clearing.
[platform/core/appfw/app2sd.git] / plugin / app2sd / src / app2sd_internals_registry.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Garima Shrivastava<garima.s@samsung.com>
7  *      Jyotsna Dhumale <jyotsna.a@samsung.com>
8  *      Venkatesha Sarpangala <sarpangala.v@samsung.com>
9  *
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  *
22  */
23
24 #include <dirent.h>
25 #include <time.h>
26 #include <db-util.h>
27
28 #include "app2sd_internals.h"
29
30 #define MAX_QUERY_LEN 4096
31 #define PASSWORD_LENGTH 64
32 /*
33 ########### Internal APIs ##################
34  */
35
36 /*sqlite  db code*/
37 #define APP2SD_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".app2sd.db")
38 sqlite3 *app2sd_db;
39 #define QUERY_CREATE_TABLE_APP2SD "create table app2sd \
40         (pkgid text primary key,\
41          password text\
42         )"
43
44 int _app2sd_initialize_db()
45 {
46         char *error_message = NULL;
47         int ret;
48         FILE * fp = NULL;
49
50         fp = fopen(APP2SD_DB_FILE, "r");
51         if (fp != NULL) {
52                 fclose(fp);
53                 ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
54                         DB_UTIL_REGISTER_HOOK_METHOD);
55
56                 if (ret != SQLITE_OK) {
57                         _E("connect menu_db [%s] failed",
58                                 APP2SD_DB_FILE);
59                         return -1;
60                 }
61                 return 0;
62         }
63
64         ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
65                  DB_UTIL_REGISTER_HOOK_METHOD);
66
67         if (ret != SQLITE_OK) {
68                 _E("connect menu_db [%s] failed",
69                         APP2SD_DB_FILE);
70                 return -1;
71         }
72
73         if (SQLITE_OK != sqlite3_exec(app2sd_db,
74                 QUERY_CREATE_TABLE_APP2SD, NULL, NULL,
75                 &error_message)) {
76                 _E("don't execute query = (%s), " \
77                         "error message = (%s)",
78                         QUERY_CREATE_TABLE_APP2SD, error_message);
79                 return -1;
80         }
81
82         return 0;
83 }
84
85 int _app2sd_set_password_in_db(const char *pkgid, const char *passwd)
86 {
87         char *error_message = NULL;
88         char *query = NULL;
89
90         query = sqlite3_mprintf("insert into app2sd" \
91                 "(pkgid, password) values (%Q, %Q)", pkgid, passwd);
92
93         if (SQLITE_OK != sqlite3_exec(app2sd_db, query, NULL, NULL,
94                 &error_message)) {
95                 _E("don't execute query = (%s), error message = (%s)",
96                         query, error_message);
97                 sqlite3_free(query);
98                 return APP2EXT_ERROR_SQLITE_REGISTRY;
99         }
100         sqlite3_free(query);
101
102         return APP2EXT_SUCCESS;
103 }
104
105 int _app2sd_remove_password_from_db(const char *pkgid)
106 {
107         char *error_message = NULL;
108         char *query = NULL;
109
110         query = sqlite3_mprintf("delete from app2sd" \
111                 " where pkgid=%Q", pkgid);
112
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);
117                 sqlite3_free(query);
118                 return APP2EXT_ERROR_SQLITE_REGISTRY;
119         }
120
121         sqlite3_free(query);
122
123         return APP2EXT_SUCCESS;
124 }
125
126 char *_app2sd_get_password_from_db(const char *pkgid)
127 {
128         char *query = NULL;
129         char *passwd = NULL;
130         const char *tail = NULL;
131         sqlite3_stmt *stmt = NULL;
132         int rc = 0;
133
134         query = sqlite3_mprintf("select * from app2sd" \
135                 " where pkgid=%Q", pkgid);
136
137         if (SQLITE_OK != sqlite3_prepare(app2sd_db, query,
138                 strlen(query), &stmt, &tail)) {
139                 _E("sqlite3_prepare error");
140                 sqlite3_free(query);
141                 return NULL;
142         }
143
144         rc = sqlite3_step(stmt);
145         if (rc != SQLITE_ROW || rc == SQLITE_DONE) {
146                 _E("no records found");
147                 goto FINISH_OFF;
148         }
149         passwd = malloc(PASSWORD_LENGTH + 1);
150         if (passwd == NULL) {
151                 _E("memory allocation failed");
152                 goto FINISH_OFF;
153         }
154
155         strncpy(passwd, (const char*)sqlite3_column_text(stmt, 1),
156                 PASSWORD_LENGTH);
157         if (passwd == NULL) {
158                 _E("data is NULL");
159                 goto FINISH_OFF;
160         }
161         if (SQLITE_OK != sqlite3_finalize(stmt)) {
162                 _E("error : sqlite3_finalize");
163                 goto FINISH_OFF;
164         }
165         sqlite3_free(query);
166
167         return passwd;
168
169 FINISH_OFF:
170         rc = sqlite3_finalize(stmt);
171         if (rc != SQLITE_OK) {
172                 _E("sqlite3_finalize failed(%d)", rc);
173         }
174         sqlite3_free(query);
175
176         if (passwd)
177                 free(passwd);
178
179         return NULL;
180 }