Add to check db integrity and restore logic
[platform/core/uifw/voice-control.git] / common / vc_cmd_db.c
1 /*
2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <app_manager.h>
18 #include <db-util.h>
19 #include <dlog.h>
20 #include <sqlite3.h>
21 #include <tzplatform_config.h>
22
23 #include "vc_cmd_db.h"
24 #include "vc_main.h"
25 #include "vc_command.h"
26 #include "voice_control_command_expand.h"
27
28
29 const char* vc_db_tag()
30 {
31         return TAG_VCDB;
32 }
33
34 /*!
35  * \note
36  * DB Table schema
37  *
38  * vc_info
39  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
40  * | id  | pid  | type | format | domain | command | parameter | appid | invocation_name | fixed |
41  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
42  *
43  * CREATE TABLE vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, appid TEXT);
44  *
45  * vc_result
46  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
47  * | id  | result | event | msg | exclusive | pid | type | format | domain | command | parameter | appid | invocation_name | fixed |
48  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
49  *
50  * CREATE TABLE vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,
51  *                                      pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT,\
52  *
53  */
54
55 #define FREE_MEM(X) {if (NULL != X) {free(X); X = NULL; } }
56
57
58 //#define DB_PATH tzplatform_mkpath(TZ_USER_DB, ".vc_info.db")
59 static sqlite3* g_db_handle = NULL;
60 static sqlite3* g_db_backup_handle = NULL;
61 char* g_path = NULL;
62 char* g_backup_path = NULL;
63 int g_fpid = -1;
64 int g_ref_cnt = 0;
65
66 static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
67 {
68         sqlite3_stmt* pStmt = NULL;
69
70         int ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
71         if (ret != SQLITE_OK) {
72                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
73                 return VC_DB_ERROR_OPERATION_FAILED;
74         }
75
76         if (sqlite3_step(pStmt) != SQLITE_DONE) {
77                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
78                 sqlite3_finalize(pStmt);
79                 return VC_DB_ERROR_OPERATION_FAILED;
80         }
81
82         sqlite3_finalize(pStmt);
83         return VC_DB_ERROR_NONE;
84 }
85
86 static int __vc_db_begin_transaction(sqlite3* db_handle)
87 {
88         int ret = __vc_db_transaction(db_handle, "BEGIN TRANSACTION");
89         return ret;
90 }
91
92 static int __vc_db_rollback_transaction(sqlite3* db_handle)
93 {
94         int ret = __vc_db_transaction(db_handle, "ROLLBACK TRANSACTION");
95         return ret;
96 }
97
98 static int __vc_db_commit_transaction(sqlite3* db_handle)
99 {
100         int ret = __vc_db_transaction(db_handle, "COMMIT TRANSACTION");
101         return ret;
102 }
103
104 static int __vc_db_exec_query(sqlite3* db_handle, const char* sql)
105 {
106         char* err_msg = NULL;
107
108         int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
109         if (ret != SQLITE_OK) {
110                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
111                 sqlite3_free(err_msg);
112                 return VC_DB_ERROR_OPERATION_FAILED;
113         }
114         return VC_DB_ERROR_NONE;
115 }
116
117 static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, const char* appid)
118 {
119         sqlite3_stmt* stmt = NULL;
120         char* sql = NULL;
121
122         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
123                 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
124         else if (NULL != appid)
125                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
126         else
127                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
128
129         if (NULL == sql) {
130                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
131                 return VC_DB_ERROR_OUT_OF_MEMORY;
132         }
133
134         int ret = 0;
135         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
136         if (ret != SQLITE_OK) {
137                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
138                 free(sql);
139                 sql = NULL;
140                 return VC_DB_ERROR_OPERATION_FAILED;
141         }
142         ret = sqlite3_bind_int(stmt, 1, (int)type);
143         if (ret != SQLITE_OK) {
144                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
145                 sqlite3_finalize(stmt);
146                 free(sql);
147                 sql = NULL;
148                 return VC_DB_ERROR_OPERATION_FAILED;
149         }
150
151         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
152                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
153         } else if (NULL != appid) {
154                 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
155                 if (ret != SQLITE_OK) {
156                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
157                         sqlite3_clear_bindings(stmt);
158                         sqlite3_finalize(stmt);
159                         free(sql);
160                         sql = NULL;
161                         return VC_DB_ERROR_OPERATION_FAILED;
162                 }
163                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
164         } else  {
165                 ret = sqlite3_bind_int(stmt, 2, pid);
166                 if (ret != SQLITE_OK) {
167                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
168                         sqlite3_clear_bindings(stmt);
169                         sqlite3_finalize(stmt);
170                         free(sql);
171                         sql = NULL;
172                         return VC_DB_ERROR_OPERATION_FAILED;
173                 }
174                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
175         }
176
177         ret = sqlite3_step(stmt);
178         if (ret != SQLITE_DONE) {
179                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
180                 sqlite3_clear_bindings(stmt);
181                 sqlite3_finalize(stmt);
182                 free(sql);
183                 sql = NULL;
184                 return VC_DB_ERROR_OPERATION_FAILED;
185         }
186
187         sqlite3_reset(stmt);
188         sqlite3_clear_bindings(stmt);
189         sqlite3_finalize(stmt);
190
191         free(sql);
192         sql = NULL;
193         return VC_DB_ERROR_NONE;
194 }
195
196 static int __vc_db_insert_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
197 {
198         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
199
200         sqlite3_stmt* stmt = NULL;
201         const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
202         int ret = 0;
203         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
204         if (ret != SQLITE_OK) {
205                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
206                 return VC_DB_ERROR_OPERATION_FAILED;
207         }
208         ret = sqlite3_bind_int(stmt, 2, pid);
209         if (ret != SQLITE_OK) {
210                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
211                 sqlite3_finalize(stmt);
212                 return VC_DB_ERROR_OPERATION_FAILED;
213         }
214         ret = sqlite3_bind_int(stmt, 3, cmd->type);
215         if (ret != SQLITE_OK) {
216                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
217                 sqlite3_clear_bindings(stmt);
218                 sqlite3_finalize(stmt);
219                 return VC_DB_ERROR_OPERATION_FAILED;
220         }
221         ret = sqlite3_bind_int(stmt, 4, cmd->format);
222         if (ret != SQLITE_OK) {
223                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
224                 sqlite3_clear_bindings(stmt);
225                 sqlite3_finalize(stmt);
226                 return VC_DB_ERROR_OPERATION_FAILED;
227         }
228         ret = sqlite3_bind_int(stmt, 5, cmd->domain);
229         if (ret != SQLITE_OK) {
230                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
231                 sqlite3_clear_bindings(stmt);
232                 sqlite3_finalize(stmt);
233                 return VC_DB_ERROR_OPERATION_FAILED;
234         }
235         ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
236         if (ret != SQLITE_OK) {
237                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
238                 sqlite3_clear_bindings(stmt);
239                 sqlite3_finalize(stmt);
240                 return VC_DB_ERROR_OPERATION_FAILED;
241         }
242
243         char* appid = NULL;
244         if (NULL == cmd->appid) {
245                 // Get appid by pid using app control
246                 ret = app_manager_get_app_id(pid, &appid);
247                 if (APP_MANAGER_ERROR_NONE != ret) {
248                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
249                 }
250                 if (NULL != appid) {
251                         cmd->appid = strdup(appid);
252                         FREE_MEM(appid);
253                 }
254         }
255         ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
256         if (ret != SQLITE_OK) {
257                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
258                 sqlite3_clear_bindings(stmt);
259                 sqlite3_finalize(stmt);
260                 return VC_DB_ERROR_OPERATION_FAILED;
261         }
262         if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
263                 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
264                 if (ret != SQLITE_OK) {
265                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
266                         sqlite3_clear_bindings(stmt);
267                         sqlite3_finalize(stmt);
268                         return VC_DB_ERROR_OPERATION_FAILED;
269                 }
270         }
271         ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
272         if (ret != SQLITE_OK) {
273                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
274                 sqlite3_clear_bindings(stmt);
275                 sqlite3_finalize(stmt);
276                 return VC_DB_ERROR_OPERATION_FAILED;
277         }
278         ret = sqlite3_step(stmt);
279         if (ret != SQLITE_DONE) {
280                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
281                 sqlite3_clear_bindings(stmt);
282                 sqlite3_finalize(stmt);
283                 return VC_DB_ERROR_OPERATION_FAILED;
284         }
285
286         SLOG(LOG_INFO, vc_db_tag(), "[SQL] INSERT INTO vc_info (id, pid(%d), type(%d), format(%d), domain(%d), command(%s)", pid, cmd->type, cmd->format, cmd->domain, cmd->command);
287         SLOG(LOG_INFO, vc_db_tag(), "[SQL] @@ appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
288
289         sqlite3_reset(stmt);
290         sqlite3_clear_bindings(stmt);
291         sqlite3_finalize(stmt);
292         return VC_DB_ERROR_NONE;
293 }
294
295 static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList** cmd_list)
296 {
297         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
298
299         int ret = 0;
300         sqlite3_stmt* stmt = NULL;
301         char* sql = NULL;
302         char* appid = NULL;
303
304         if (VC_COMMAND_TYPE_BACKGROUND == type) {
305                 /* For background command */
306                 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
307         } else {
308                 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
309         }
310
311         if (NULL == sql) {
312                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
313                 return VC_DB_ERROR_OUT_OF_MEMORY;
314         }
315         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
316         if (ret != SQLITE_OK) {
317                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
318                 free(sql);
319                 sql = NULL;
320                 return VC_DB_ERROR_OPERATION_FAILED;
321         }
322
323         ret = sqlite3_bind_int(stmt, 1, (int)type);
324         if (ret != SQLITE_OK) {
325                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
326                 sqlite3_finalize(stmt);
327                 free(sql);
328                 sql = NULL;
329                 return VC_DB_ERROR_OPERATION_FAILED;
330         }
331         if (VC_COMMAND_TYPE_BACKGROUND != type) {
332                 ret = sqlite3_bind_int(stmt, 2, pid);
333                 if (ret != SQLITE_OK) {
334                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
335                         sqlite3_clear_bindings(stmt);
336                         sqlite3_finalize(stmt);
337                         free(sql);
338                         sql = NULL;
339                         return VC_DB_ERROR_OPERATION_FAILED;
340                 }
341         }
342
343         if (VC_COMMAND_TYPE_BACKGROUND == type)
344                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
345         else
346                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
347
348         ret = sqlite3_step(stmt);
349         if (ret == SQLITE_DONE) {
350                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
351                 sqlite3_clear_bindings(stmt);
352                 sqlite3_finalize(stmt);
353                 free(sql);
354                 sql = NULL;
355                 return VC_DB_ERROR_NONE;
356         }
357
358         if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
359                 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
360                         SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
361                 }
362         }
363
364         while (SQLITE_ROW == ret) {
365                 int temp = 0;
366                 int ret = -1;
367                 char* temp_text = NULL;
368                 vc_cmd_h temp_cmd;
369                 ret = vc_cmd_create(&temp_cmd);
370
371                 if (0 != ret) {
372                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
373                         if (NULL != appid) {
374                                 free(appid);
375                                 appid = NULL;
376                         }
377                         sqlite3_reset(stmt);
378                         sqlite3_clear_bindings(stmt);
379                         sqlite3_finalize(stmt);
380                         free(sql);
381                         sql = NULL;
382                         return VC_DB_ERROR_OUT_OF_MEMORY;
383                 }
384
385                 temp_text = (char*)sqlite3_column_text(stmt, 7);
386                 if (NULL != temp_text)
387                         vc_cmd_set_appid(temp_cmd, temp_text);
388
389                 ret = vc_cmd_get_appid(temp_cmd, &temp_text);
390                 if (NULL != appid && 0 == ret) {
391                         if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
392                                 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
393
394                                 free(temp_text);
395                                 temp_text = NULL;
396
397                                 vc_cmd_destroy(temp_cmd);
398                                 temp_cmd = NULL;
399
400                                 ret = sqlite3_step(stmt);
401                                 if (SQLITE_DONE == ret)
402                                         break;
403
404                                 continue;
405                         }
406                 }
407
408                 if (NULL != temp_text) {
409                         free(temp_text);
410                         temp_text = NULL;
411                 }
412
413                 temp = sqlite3_column_int(stmt, 0);
414                 vc_cmd_set_id(temp_cmd, temp);
415
416                 temp = sqlite3_column_int(stmt, 1);
417                 vc_cmd_set_pid(temp_cmd, temp);
418
419                 temp = sqlite3_column_int(stmt, 2);
420                 vc_cmd_set_type(temp_cmd, temp);
421
422                 temp = sqlite3_column_int(stmt, 3);
423                 vc_cmd_set_format(temp_cmd, temp);
424
425                 temp  = sqlite3_column_int(stmt, 4);
426                 vc_cmd_set_domain(temp_cmd, temp);
427
428                 temp_text = (char*)sqlite3_column_text(stmt, 5);
429                 if (NULL != temp_text)
430                         vc_cmd_set_command(temp_cmd, temp_text);
431
432                 temp_text = (char*)sqlite3_column_text(stmt, 6);
433                 if (NULL != temp_text)
434                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
435
436                 temp_text = (char*)sqlite3_column_text(stmt, 8);
437                 if (NULL != temp_text)
438                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
439
440                 temp_text = (char*)sqlite3_column_text(stmt, 9);
441                 if (NULL != temp_text)
442                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
443
444                 ret = vc_cmd_get_type(temp_cmd, &temp);
445                 if (0 == ret && type == temp) {
446                         *cmd_list = g_slist_append(*cmd_list, temp_cmd);
447                 } else {
448                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
449
450                         vc_cmd_destroy(temp_cmd);
451                         temp_cmd = NULL;
452                 }
453                 ret = sqlite3_step(stmt);
454                 if (SQLITE_DONE == ret)
455                         break;
456         }
457
458         if (NULL != appid) {
459                 free(appid);
460                 appid = NULL;
461         }
462
463         sqlite3_reset(stmt);
464         sqlite3_clear_bindings(stmt);
465         sqlite3_finalize(stmt);
466
467         free(sql);
468         sql = NULL;
469         return VC_DB_ERROR_NONE;
470 }
471
472 static int __vc_db_get_pid(const char* appid, int* pid)
473 {
474         bool running = false;
475         int ret = app_manager_is_running(appid, &running);
476         if (APP_MANAGER_ERROR_NONE != ret) {
477                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
478                 return VC_DB_ERROR_OPERATION_FAILED;
479         }
480         if (true == running) {
481                 app_context_h app_context = NULL;
482                 ret = app_manager_get_app_context(appid, &app_context);
483                 if (APP_MANAGER_ERROR_NONE != ret) {
484                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
485                         return VC_DB_ERROR_OPERATION_FAILED;
486                 }
487
488                 ret = app_context_get_pid(app_context, pid);
489                 if (APP_MANAGER_ERROR_NONE != ret) {
490                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
491                         return VC_DB_ERROR_OPERATION_FAILED;
492                 }
493
494                 ret = app_context_destroy(app_context);
495                 if (APP_MANAGER_ERROR_NONE != ret) {
496                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", ret, appid);
497                         return VC_DB_ERROR_OPERATION_FAILED;
498                 }
499         } else {
500                 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
501         }
502         return VC_DB_ERROR_NONE;
503 }
504
505 static int __vc_db_insert_result(sqlite3* db_handle, const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
506 {
507         SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
508
509         sqlite3_stmt* stmt = NULL;
510         const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
511         int ret = 0;
512         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
513         if (ret != SQLITE_OK) {
514                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
515                 return VC_DB_ERROR_OPERATION_FAILED;
516         }
517         ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
518         if (ret != SQLITE_OK) {
519                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
520                 sqlite3_finalize(stmt);
521                 return VC_DB_ERROR_OPERATION_FAILED;
522         }
523         ret = sqlite3_bind_int(stmt, 3, event);
524         if (ret != SQLITE_OK) {
525                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
526                 sqlite3_clear_bindings(stmt);
527                 sqlite3_finalize(stmt);
528                 return VC_DB_ERROR_OPERATION_FAILED;
529         }
530         ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
531         if (ret != SQLITE_OK) {
532                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
533                 sqlite3_clear_bindings(stmt);
534                 sqlite3_finalize(stmt);
535                 return VC_DB_ERROR_OPERATION_FAILED;
536         }
537         ret = sqlite3_bind_int(stmt, 5, exclusive);
538         if (ret != SQLITE_OK) {
539                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
540                 sqlite3_clear_bindings(stmt);
541                 sqlite3_finalize(stmt);
542                 return VC_DB_ERROR_OPERATION_FAILED;
543         }
544
545         if (NULL == cmd) {
546                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
547
548                 ret = sqlite3_step(stmt);
549                 if (ret != SQLITE_DONE) {
550                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
551                         sqlite3_clear_bindings(stmt);
552                         sqlite3_finalize(stmt);
553                         return VC_DB_ERROR_OPERATION_FAILED;
554                 }
555                 sqlite3_reset(stmt);
556                 sqlite3_clear_bindings(stmt);
557                 sqlite3_finalize(stmt);
558                 return VC_DB_ERROR_NONE;
559         }
560
561         if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
562                 int pid = -1;
563                 ret = __vc_db_get_pid(cmd->appid, &pid);
564                 if (0 != ret) {
565                         SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
566                 } else {
567                         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
568                         if (-1 != pid)
569                                 cmd->pid = pid;
570                 }
571         }
572         ret = sqlite3_bind_int(stmt, 6, cmd->pid);
573         if (ret != SQLITE_OK) {
574                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
575                 sqlite3_clear_bindings(stmt);
576                 sqlite3_finalize(stmt);
577                 return VC_DB_ERROR_OPERATION_FAILED;
578         }
579         ret = sqlite3_bind_int(stmt, 7, cmd->type);
580         if (ret != SQLITE_OK) {
581                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
582                 sqlite3_clear_bindings(stmt);
583                 sqlite3_finalize(stmt);
584                 return VC_DB_ERROR_OPERATION_FAILED;
585         }
586         ret = sqlite3_bind_int(stmt, 8, cmd->format);
587         if (ret != SQLITE_OK) {
588                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
589                 sqlite3_clear_bindings(stmt);
590                 sqlite3_finalize(stmt);
591                 return VC_DB_ERROR_OPERATION_FAILED;
592         }
593         ret = sqlite3_bind_int(stmt, 9, cmd->domain);
594         if (ret != SQLITE_OK) {
595                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
596                 sqlite3_clear_bindings(stmt);
597                 sqlite3_finalize(stmt);
598                 return VC_DB_ERROR_OPERATION_FAILED;
599         }
600         ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
601         if (ret != SQLITE_OK) {
602                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
603                 sqlite3_clear_bindings(stmt);
604                 sqlite3_finalize(stmt);
605                 return VC_DB_ERROR_OPERATION_FAILED;
606         }
607         ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
608         if (ret != SQLITE_OK) {
609                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
610                 sqlite3_clear_bindings(stmt);
611                 sqlite3_finalize(stmt);
612                 return VC_DB_ERROR_OPERATION_FAILED;
613         }
614         ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
615         if (ret != SQLITE_OK) {
616                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
617                 sqlite3_clear_bindings(stmt);
618                 sqlite3_finalize(stmt);
619                 return VC_DB_ERROR_OPERATION_FAILED;
620         }
621         ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
622         if (ret != SQLITE_OK) {
623                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
624                 sqlite3_clear_bindings(stmt);
625                 sqlite3_finalize(stmt);
626                 return VC_DB_ERROR_OPERATION_FAILED;
627         }
628         ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
629         if (ret != SQLITE_OK) {
630                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
631                 sqlite3_clear_bindings(stmt);
632                 sqlite3_finalize(stmt);
633                 return VC_DB_ERROR_OPERATION_FAILED;
634         }
635         ret = sqlite3_step(stmt);
636         if (ret != SQLITE_DONE) {
637                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
638                 sqlite3_clear_bindings(stmt);
639                 sqlite3_finalize(stmt);
640                 return VC_DB_ERROR_OPERATION_FAILED;
641         }
642
643         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d), pid(%d), type(%d), format(%d), domain(%d), command(%s), parameter(%s), appid(%s), invocation(%s), fixed(%s)",
644                                 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
645
646         sqlite3_reset(stmt);
647         sqlite3_clear_bindings(stmt);
648         sqlite3_finalize(stmt);
649
650         return VC_DB_ERROR_NONE;
651 }
652
653 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
654 {
655         if (NULL == org_cmd || NULL == new_cmd) {
656                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
657                 return VC_DB_ERROR_INVALID_PARAMETER;
658         }
659
660         if (NULL == invocation_name) {
661                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
662                 return VC_DB_ERROR_INVALID_PARAMETER;
663         }
664
665         if (strlen(org_cmd) <= strlen(invocation_name)) {
666                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
667                 return VC_DB_ERROR_INVALID_PARAMETER;
668         }
669
670         if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
671                 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
672         }
673         SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
674         return VC_DB_ERROR_NONE;
675 }
676
677 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
678 {
679         if (NULL == command || NULL == temp_unfixed) {
680                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
681                 return VC_DB_ERROR_INVALID_PARAMETER;
682         }
683
684         if (NULL == fixed) {
685                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
686                 return VC_DB_ERROR_INVALID_PARAMETER;
687         }
688
689         if (strlen(command) <= strlen(fixed)) {
690                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
691                 return VC_DB_ERROR_INVALID_PARAMETER;
692         }
693
694         char* temp = (char*)calloc(256, sizeof(char));
695         if (0 == strncasecmp(command, fixed, strlen(fixed))) {
696                 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
697                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
698         } else  if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
699                 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
700                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
701         }
702
703         SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
704         if (NULL != temp) {
705                 *temp_unfixed = strdup(temp);
706                 free(temp);
707                 temp = NULL;
708         }
709         return VC_DB_ERROR_NONE;
710 }
711
712 static int __vc_db_get_result(sqlite3* db_handle, char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
713 {
714         int ret = 0;
715         sqlite3_stmt* stmt = NULL;
716         char* sql = NULL;
717         if (-1 == pid)
718                 sql = strdup("SELECT * FROM vc_result;");
719         else if (NULL != appid)
720                 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
721         else
722                 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
723
724         if (NULL == sql) {
725                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
726                 return VC_DB_ERROR_OUT_OF_MEMORY;
727         }
728
729         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
730         if (ret != SQLITE_OK) {
731                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
732                 free(sql);
733                 sql = NULL;
734                 return VC_DB_ERROR_OPERATION_FAILED;
735         }
736         if (NULL != appid) {
737                 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
738                 if (ret != SQLITE_OK) {
739                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
740                         sqlite3_finalize(stmt);
741                         free(sql);
742                         sql = NULL;
743                         return VC_DB_ERROR_OPERATION_FAILED;
744                 }
745         } else if (-1 != pid) {
746                 ret = sqlite3_bind_int(stmt, 1, pid);
747                 if (ret != SQLITE_OK) {
748                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
749                         sqlite3_finalize(stmt);
750                         free(sql);
751                         sql = NULL;
752                         return VC_DB_ERROR_OPERATION_FAILED;
753                 }
754         }
755         ret = sqlite3_step(stmt);
756         if (ret == SQLITE_DONE) {
757                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
758                 sqlite3_clear_bindings(stmt);
759                 sqlite3_finalize(stmt);
760                 free(sql);
761                 sql = NULL;
762                 return VC_DB_ERROR_NONE;
763         }
764
765         if (-1 == pid)
766                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
767         else if (NULL != appid)
768                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
769
770         else
771                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
772
773         vc_cmd_h temp_cmd = NULL;
774         while (SQLITE_ROW == ret) {
775                 int temp = 0;
776                 char* temp_text = NULL;
777                 const char* invocation_name = NULL;
778
779                 temp_text = (char*)sqlite3_column_text(stmt, 1);
780                 if (NULL != temp_text)
781                         *result_text = strdup(temp_text);
782
783                 temp = sqlite3_column_int(stmt, 2);
784                 *event = temp;
785
786                 if (NULL != msg) {
787                         temp_text = (char*)sqlite3_column_text(stmt, 3);
788                         if (NULL != temp_text)
789                                 *msg = strdup(temp_text);
790                 }
791
792                 if (0 != vc_cmd_create(&temp_cmd)) {
793                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
794                         if (NULL != *result_text) {
795                                 free(*result_text);
796                                 *result_text = NULL;
797                         }
798
799                         if (NULL != msg && NULL != *msg) {
800                                 free(*msg);
801                                 *msg = NULL;
802                         }
803
804                         sqlite3_reset(stmt);
805                         sqlite3_clear_bindings(stmt);
806                         sqlite3_finalize(stmt);
807                         free(sql);
808                         sql = NULL;
809                         return -1;
810                 }
811
812                 temp = sqlite3_column_int(stmt, 0);
813                 vc_cmd_set_id(temp_cmd, temp);
814
815                 temp = sqlite3_column_int(stmt, 5);
816                 vc_cmd_set_pid(temp_cmd, temp);
817
818                 temp = sqlite3_column_int(stmt, 6);
819                 vc_cmd_set_type(temp_cmd, temp);
820
821                 temp = sqlite3_column_int(stmt, 7);
822                 vc_cmd_set_format(temp_cmd, temp);
823
824                 temp = sqlite3_column_int(stmt, 8);
825                 vc_cmd_set_domain(temp_cmd, temp);
826
827                 // invocation name
828                 invocation_name = (char*)sqlite3_column_text(stmt, 12);
829                 // command name
830                 temp_text = (char*)sqlite3_column_text(stmt, 9);
831                 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
832
833                 if (NULL != temp_text) {
834                         char* temp_command = NULL;
835                         char* temp_unfixed = NULL;
836                         char* temp_fixed = NULL;
837
838                         if (NULL != invocation_name)
839                                 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
840
841                         if (NULL == temp_command) {
842                                 temp_command = strdup(temp_text);
843                         } else {
844                                 // remove invocation name from result_text
845                                 free(*result_text);
846                                 *result_text = strdup(temp_command);
847                         }
848
849                         // fixed
850                         temp_fixed = (char*)sqlite3_column_text(stmt, 13);
851                         if (NULL != temp_fixed)
852                                 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
853
854                         // unfixed
855                         temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
856
857                         if (NULL != temp_unfixed) {
858                                 char merge_result[256] = {0, };
859                                 vc_cmd_set_command(temp_cmd, temp_command);
860                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
861                                 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
862                                 if (NULL != *result_text)
863                                         free(*result_text);
864                                 *result_text = strdup(merge_result);
865                         } else if (NULL != temp_fixed) {
866                                 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
867                                 vc_cmd_set_command(temp_cmd, temp_fixed);
868                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
869                                 if (NULL != temp_unfixed) {
870                                         free(temp_unfixed);
871                                         temp_unfixed = NULL;
872                                 }
873                         } else {
874                                 vc_cmd_set_command(temp_cmd, temp_command);
875                         }
876                         free(temp_command);
877                         temp_command = NULL;
878                 }
879
880                 temp_text = (char*)sqlite3_column_text(stmt, 11);
881                 if (NULL != temp_text)
882                         vc_cmd_set_appid(temp_cmd, temp_text);
883
884                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
885                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
886                         vc_cmd_destroy(temp_cmd);
887                         vc_cmd_list_destroy(vc_cmd_list, true);
888                         if (NULL != *result_text) {
889                                 free(*result_text);
890                                 *result_text = NULL;
891                         }
892
893                         if (NULL != msg && NULL != *msg) {
894                                 free(*msg);
895                                 *msg = NULL;
896                         }
897
898                         sqlite3_reset(stmt);
899                         sqlite3_clear_bindings(stmt);
900                         sqlite3_finalize(stmt);
901                         free(sql);
902                         sql = NULL;
903                         return VC_DB_ERROR_OPERATION_FAILED;
904                 }
905
906                 ret = sqlite3_step(stmt);
907                 if (SQLITE_DONE == ret)
908                         break;
909         }
910
911         sqlite3_reset(stmt);
912         sqlite3_clear_bindings(stmt);
913         sqlite3_finalize(stmt);
914
915         free(sql);
916         sql = NULL;
917         return VC_DB_ERROR_NONE;
918 }
919
920 void __vc_db_demandable_client_free(void* data)
921 {
922         vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
923
924         if (NULL != d_app) {
925                 if (NULL != d_app->appid) {
926                         free(d_app->appid);
927                         d_app->appid = NULL;
928                 }
929
930                 free(d_app);
931         }
932 }
933
934 static int __vc_db_get_appid(sqlite3* db_handle, const char* result, GSList** app_list)
935 {
936         GSList* temp_app_list = NULL;
937
938         int ret = 0;
939         sqlite3_stmt* stmt = NULL;
940         const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
941
942         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
943         if (ret != SQLITE_OK) {
944                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
945                 return VC_DB_ERROR_OPERATION_FAILED;
946         }
947         ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
948         if (ret != SQLITE_OK) {
949                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
950                 sqlite3_finalize(stmt);
951                 return VC_DB_ERROR_OPERATION_FAILED;
952         }
953         ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
954         if (ret != SQLITE_OK) {
955                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
956                 sqlite3_clear_bindings(stmt);
957                 sqlite3_finalize(stmt);
958                 return VC_DB_ERROR_OPERATION_FAILED;
959         }
960         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
961         ret = sqlite3_step(stmt);
962         if (ret == SQLITE_DONE) {
963                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
964                 sqlite3_clear_bindings(stmt);
965                 sqlite3_finalize(stmt);
966                 return VC_DB_ERROR_NONE;
967         }
968
969         while (SQLITE_ROW == ret) {
970                 char* temp_text = NULL;
971                 vc_deactivated_app_s* temp_app = NULL;
972                 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
973                 if (NULL == temp_app) {
974                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
975
976                         if (NULL != temp_app_list) {
977                                 g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
978                                 temp_app_list = NULL;
979                         }
980
981                         sqlite3_reset(stmt);
982                         sqlite3_clear_bindings(stmt);
983                         sqlite3_finalize(stmt);
984                         return VC_DB_ERROR_OUT_OF_MEMORY;
985                 }
986
987                 temp_text = (char*)sqlite3_column_text(stmt, 11);
988                 if (NULL != temp_text)
989                         temp_app->appid = strdup(temp_text);
990
991                 temp_app_list = g_slist_append(temp_app_list, temp_app);
992
993                 ret = sqlite3_step(stmt);
994                 if (SQLITE_DONE == ret)
995                         break;
996         }
997
998         *app_list = temp_app_list;
999
1000         sqlite3_reset(stmt);
1001         sqlite3_clear_bindings(stmt);
1002         sqlite3_finalize(stmt);
1003         return VC_DB_ERROR_NONE;
1004 }
1005
1006 int __vc_db_get_result_pid_list(sqlite3* db_handle, const char* result, GSList** pid_list)
1007 {
1008         GSList* temp_pid_list = NULL;
1009
1010         int ret = 0;
1011         sqlite3_stmt* stmt = NULL;
1012         const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
1013
1014
1015         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1016         if (ret != SQLITE_OK) {
1017                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1018                 return VC_DB_ERROR_OPERATION_FAILED;
1019         }
1020         ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
1021         if (ret != SQLITE_OK) {
1022                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1023                 sqlite3_finalize(stmt);
1024                 return VC_DB_ERROR_OPERATION_FAILED;
1025         }
1026         ret = sqlite3_step(stmt);
1027         if (ret == SQLITE_DONE) {
1028                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1029                 sqlite3_clear_bindings(stmt);
1030                 sqlite3_finalize(stmt);
1031                 return VC_DB_ERROR_NONE;
1032         }
1033
1034         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
1035
1036         while (SQLITE_ROW == ret) {
1037                 int temp = 0;
1038                 vc_cmd_s* temp_cmd = NULL;
1039                 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
1040                 if (NULL == temp_cmd) {
1041                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
1042
1043                         if (NULL != temp_pid_list) {
1044                                 g_slist_free_full(temp_pid_list, free);
1045                                 temp_pid_list = NULL;
1046                         }
1047
1048                         sqlite3_reset(stmt);
1049                         sqlite3_clear_bindings(stmt);
1050                         sqlite3_finalize(stmt);
1051                         return VC_DB_ERROR_OUT_OF_MEMORY;
1052                 }
1053
1054                 temp = sqlite3_column_int(stmt, 0);
1055                 temp_cmd->pid = temp;
1056
1057                 temp = sqlite3_column_int(stmt, 1);
1058                 temp_cmd->type = temp;
1059
1060                 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
1061
1062                 ret = sqlite3_step(stmt);
1063                 if (SQLITE_DONE == ret)
1064                         break;
1065         }
1066
1067         *pid_list = temp_pid_list;
1068
1069         sqlite3_reset(stmt);
1070         sqlite3_clear_bindings(stmt);
1071         sqlite3_finalize(stmt);
1072         return VC_DB_ERROR_NONE;
1073 }
1074
1075 static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd_list_h vc_cmd_list)
1076 {
1077         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
1078
1079         int ret = 0;
1080         sqlite3_stmt* stmt = NULL;
1081         const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
1082
1083         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1084         if (ret != SQLITE_OK) {
1085                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1086                 return VC_DB_ERROR_OPERATION_FAILED;
1087         }
1088         ret = sqlite3_bind_int(stmt, 1, pid);
1089         if (ret != SQLITE_OK) {
1090                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1091                 sqlite3_finalize(stmt);
1092                 return VC_DB_ERROR_OPERATION_FAILED;
1093         }
1094         ret = sqlite3_bind_int(stmt, 2, type);
1095         if (ret != SQLITE_OK) {
1096                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1097                 sqlite3_clear_bindings(stmt);
1098                 sqlite3_finalize(stmt);
1099                 return VC_DB_ERROR_OPERATION_FAILED;
1100         }
1101         ret = sqlite3_step(stmt);
1102         if (ret == SQLITE_DONE) {
1103                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1104                 sqlite3_clear_bindings(stmt);
1105                 sqlite3_finalize(stmt);
1106                 return VC_DB_ERROR_NONE;
1107         }
1108
1109         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
1110
1111         vc_cmd_h temp_cmd = NULL;
1112
1113         while (SQLITE_ROW == ret) {
1114                 int temp = 0;
1115                 char* temp_text = NULL;
1116
1117                 if (0 != vc_cmd_create(&temp_cmd)) {
1118                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
1119                         sqlite3_reset(stmt);
1120                         sqlite3_clear_bindings(stmt);
1121                         sqlite3_finalize(stmt);
1122                         return -1;
1123                 }
1124                 temp = sqlite3_column_int(stmt, 0);
1125                 vc_cmd_set_id(temp_cmd, temp);
1126
1127                 temp = sqlite3_column_int(stmt, 1);
1128                 vc_cmd_set_pid(temp_cmd, temp);
1129
1130                 temp = sqlite3_column_int(stmt, 2);
1131                 vc_cmd_set_type(temp_cmd, temp);
1132
1133                 temp = sqlite3_column_int(stmt, 3);
1134                 vc_cmd_set_format(temp_cmd, temp);
1135
1136                 temp = sqlite3_column_int(stmt, 4);
1137                 vc_cmd_set_domain(temp_cmd, temp);
1138
1139                 temp_text = (char*)sqlite3_column_text(stmt, 5);
1140                 if (NULL != temp_text)
1141                         vc_cmd_set_command(temp_cmd, temp_text);
1142
1143                 temp_text = (char*)sqlite3_column_text(stmt, 6);
1144                 if (NULL != temp_text)
1145                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
1146
1147                 temp_text = (char*)sqlite3_column_text(stmt, 7);
1148                 if (NULL != temp_text)
1149                         vc_cmd_set_appid(temp_cmd, temp_text);
1150
1151                 temp_text = (char*)sqlite3_column_text(stmt, 8);
1152                 if (NULL != temp_text)
1153                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
1154
1155                 temp_text = (char*)sqlite3_column_text(stmt, 9);
1156                 if (NULL != temp_text)
1157                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
1158
1159                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1160                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1161                         vc_cmd_destroy(temp_cmd);
1162                         vc_cmd_list_destroy(vc_cmd_list, true);
1163                         sqlite3_reset(stmt);
1164                         sqlite3_clear_bindings(stmt);
1165                         sqlite3_finalize(stmt);
1166                         return VC_DB_ERROR_OPERATION_FAILED;
1167                 }
1168
1169                 ret = sqlite3_step(stmt);
1170                 if (SQLITE_DONE == ret)
1171                         break;
1172         }
1173
1174         sqlite3_reset(stmt);
1175         sqlite3_clear_bindings(stmt);
1176         sqlite3_finalize(stmt);
1177         return VC_DB_ERROR_NONE;
1178 }
1179
1180 static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
1181 {
1182         if (NULL == src_cmd) {
1183                 SLOG(LOG_WARN, vc_db_tag(), "[Client Data] Input command is NULL");
1184                 return NULL;
1185         }
1186
1187         vc_cmd_s* temp_cmd = NULL;
1188         temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
1189         if (NULL == temp_cmd) {
1190                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1191                 return NULL;
1192         }
1193
1194         temp_cmd->id = src_cmd->id;
1195         temp_cmd->pid = src_cmd->pid;
1196         temp_cmd->index = src_cmd->index;
1197         temp_cmd->type = src_cmd->type;
1198         temp_cmd->format = src_cmd->format;
1199         temp_cmd->domain = src_cmd->domain;
1200
1201         if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
1202         else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
1203         else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1204         else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1205         else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
1206         else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
1207
1208         if (NULL != src_cmd->command) {
1209                 temp_cmd->command = strdup(src_cmd->command);
1210         }
1211
1212         if (NULL != src_cmd->parameter) {
1213                 temp_cmd->parameter = strdup(src_cmd->parameter);
1214         }
1215
1216         if (NULL != src_cmd->appid) {
1217                 temp_cmd->appid = strdup(src_cmd->appid);
1218         }
1219
1220         if (NULL != src_cmd->invocation_name) {
1221                 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
1222         }
1223
1224         if (NULL != src_cmd->fixed) {
1225                 temp_cmd->fixed = strdup(src_cmd->fixed);
1226         }
1227
1228         temp_cmd->key = src_cmd->key;
1229         temp_cmd->modifier = src_cmd->modifier;
1230
1231         return temp_cmd;
1232 }
1233
1234 int __vc_db_create_table(sqlite3* db_handle)
1235 {
1236         const char* vc_info_sql = "CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
1237                                         command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1238         const char* vc_result_sql = "CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
1239                                         pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1240
1241         int ret = __vc_db_exec_query(db_handle, vc_info_sql);
1242         if (ret != VC_DB_ERROR_NONE) {
1243                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1244                 return VC_DB_ERROR_OPERATION_FAILED;
1245         }
1246         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
1247
1248         ret = __vc_db_exec_query(db_handle, vc_result_sql);
1249         if (ret != VC_DB_ERROR_NONE) {
1250                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1251                 return ret;
1252         }
1253         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
1254
1255         return VC_DB_ERROR_NONE;
1256 }
1257
1258 int __vc_db_open_db(char** path, sqlite3** db_handle)
1259 {
1260         struct stat stat;
1261         int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1262         if (ret != SQLITE_OK) {
1263                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
1264                 if (*db_handle) {
1265                         db_util_close(*db_handle);
1266                         *db_handle = NULL;
1267                 }
1268
1269                 free(*path);
1270                 *path = NULL;
1271                 return VC_DB_ERROR_OPERATION_FAILED;
1272         }
1273
1274         if (lstat(*path, &stat) < 0) {
1275                 char buf_err[256];
1276                 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1277                 if (*db_handle)
1278                         db_util_close(*db_handle);
1279                 *db_handle = NULL;
1280
1281                 free(*path);
1282                 *path = NULL;
1283                 return VC_DB_ERROR_OPERATION_FAILED;
1284         }
1285
1286         if (!S_ISREG(stat.st_mode)) {
1287                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1288                 if (*db_handle)
1289                         db_util_close(*db_handle);
1290                 *db_handle = NULL;
1291
1292                 free(*path);
1293                 *path = NULL;
1294                 return VC_DB_ERROR_OPERATION_FAILED;
1295         }
1296
1297         if (!stat.st_size) {
1298                 __vc_db_begin_transaction(*db_handle);
1299
1300                 int ret = __vc_db_create_table(*db_handle);
1301                 if (ret != VC_DB_ERROR_NONE) {
1302                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1303                         __vc_db_rollback_transaction(*db_handle);
1304                         return VC_DB_ERROR_OPERATION_FAILED;
1305                 }
1306
1307                 __vc_db_commit_transaction(*db_handle);
1308         }
1309
1310         if (*db_handle) {
1311                 char* err_msg = NULL;
1312                 static const const char* sql = "PRAGMA journal_mode = WAL";
1313                 int ret = sqlite3_exec(*db_handle, sql, NULL, NULL, &err_msg);
1314                 if (ret != SQLITE_OK) {
1315                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1316                 }
1317         }
1318         return VC_DB_ERROR_NONE;
1319 }
1320
1321 static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
1322 {
1323         SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
1324         return 0;
1325 }
1326
1327 int vc_db_initialize(void)
1328 {
1329         SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
1330
1331         if (0 < g_ref_cnt) {
1332                 g_ref_cnt++;
1333                 return VC_DB_ERROR_NONE;
1334         }
1335
1336         /* For voice control DB */
1337         g_path = (char*)calloc(256, sizeof(char));
1338         if (NULL == g_path) {
1339                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1340                 return VC_DB_ERROR_OUT_OF_MEMORY;
1341         }
1342         /* This should be changed to general DB space - TZ_USER_DB */
1343         snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1344
1345         /* For Backup DB */
1346         g_backup_path = (char*)calloc(256, sizeof(char));
1347         if (NULL == g_backup_path) {
1348                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1349                 return VC_DB_ERROR_OUT_OF_MEMORY;
1350         }
1351         snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1352
1353         if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
1354                 int cnt = 0;
1355                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1356                 if (0 != remove(g_path)) {
1357                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1358                 }
1359                 if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
1360                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1361                 }
1362                 while (cnt <= 5) {
1363                         if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1364                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1365                         } else {
1366                                 break;
1367                         }
1368                         cnt++;
1369                 }
1370                 if (0 != vc_db_restore_command()) {
1371                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1372                 }
1373         }
1374
1375         if (SQLITE_CORRUPT == sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL)) {
1376                 int cnt = 0;
1377                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1378                 if (0 != remove(g_path)) {
1379                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1380                 }
1381                 if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
1382                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1383                 }
1384                 while (cnt <= 5) {
1385                         if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1386                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1387                         } else {
1388                                 break;
1389                         }
1390                         cnt++;
1391                 }
1392                 if (0 != vc_db_restore_command()) {
1393                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1394                 }
1395         }
1396
1397         if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1398                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1399                 if (0 != remove(g_backup_path)) {
1400                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1401                 }
1402                 if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1403                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1404                 }
1405                 if (0 != vc_db_backup_command()) {
1406                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to backup command");
1407                 }
1408         }
1409
1410         g_ref_cnt++;
1411         return VC_DB_ERROR_NONE;
1412 }
1413
1414 int vc_db_finalize(void)
1415 {
1416         if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1417         if (0 != --g_ref_cnt)
1418                 return VC_DB_ERROR_NONE;
1419
1420         if (NULL != g_path) {
1421                 free(g_path);
1422                 g_path = NULL;
1423         }
1424
1425         if (NULL != g_backup_path) {
1426                 free(g_backup_path);
1427                 g_backup_path = NULL;
1428         }
1429
1430         if (!g_db_handle)
1431                 return 0;
1432         db_util_close(g_db_handle);
1433         g_db_handle = NULL;
1434
1435         if (!g_db_backup_handle)
1436                 return 0;
1437         db_util_close(g_db_backup_handle);
1438         g_db_backup_handle = NULL;
1439
1440         return VC_DB_ERROR_NONE;
1441 }
1442
1443 int vc_db_create_table()
1444 {
1445         __vc_db_begin_transaction(g_db_handle);
1446
1447         int ret = __vc_db_create_table(g_db_handle);
1448         if (ret != VC_DB_ERROR_NONE) {
1449                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1450                 __vc_db_rollback_transaction(g_db_handle);
1451                 return VC_DB_ERROR_OPERATION_FAILED;
1452         }
1453
1454         __vc_db_commit_transaction(g_db_handle);
1455         return VC_DB_ERROR_NONE;
1456 }
1457
1458 int __vc_db_delete_table(sqlite3* db_handle, const char* table)
1459 {
1460         char* sql = NULL;
1461         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1462                 sql = strdup("DELETE FROM vc_result;");
1463         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1464                 sql = strdup("DELETE FROM vc_info;");
1465         } else {
1466                 return VC_DB_ERROR_INVALID_PARAMETER;
1467         }
1468
1469         if (NULL == sql) {
1470                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1471                 return VC_DB_ERROR_OUT_OF_MEMORY;
1472         }
1473
1474         int ret = __vc_db_exec_query(db_handle, sql);
1475         if (ret != VC_DB_ERROR_NONE) {
1476                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1477                 free(sql);
1478                 sql = NULL;
1479                 return ret;
1480         }
1481
1482         free(sql);
1483         sql = NULL;
1484
1485         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1486                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1487         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1488                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1489         }
1490
1491         if (NULL == sql) {
1492                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1493                 return VC_DB_ERROR_OUT_OF_MEMORY;
1494         }
1495
1496         ret = __vc_db_exec_query(db_handle, sql);
1497         if (ret != VC_DB_ERROR_NONE) {
1498                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1499                 free(sql);
1500                 sql = NULL;
1501                 return ret;
1502         }
1503
1504         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1505
1506         free(sql);
1507         sql = NULL;
1508         return VC_DB_ERROR_NONE;
1509 }
1510
1511 int vc_db_delete_table(const char* table)
1512 {
1513         __vc_db_begin_transaction(g_db_handle);
1514
1515         int ret = __vc_db_delete_table(g_db_handle, table);
1516         if (0 != ret) {
1517                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1518                 __vc_db_rollback_transaction(g_db_handle);
1519                 return ret;
1520         }
1521
1522         __vc_db_commit_transaction(g_db_handle);
1523
1524         return VC_DB_ERROR_NONE;
1525 }
1526
1527 int vc_db_begin_transaction(void)
1528 {
1529         int ret = __vc_db_begin_transaction(g_db_handle);
1530         return ret;
1531 }
1532
1533 int vc_db_rollback_transaction(void)
1534 {
1535         int ret = __vc_db_rollback_transaction(g_db_handle);
1536         return ret;
1537 }
1538
1539 int vc_db_commit_transaction(void)
1540 {
1541         int ret = __vc_db_commit_transaction(g_db_handle);
1542         return ret;
1543 }
1544
1545 static void __vc_db_remove_space(char** string)
1546 {
1547         if (NULL == string || NULL == *string)
1548                 return;
1549
1550         char* temp = *string;
1551
1552         //remove previous space
1553         if (' ' == temp[0])
1554                 memmove(temp, temp + 1, strlen(temp));
1555
1556         // remove next space
1557         if (' ' == temp[strlen(temp) - 1])
1558                 temp[strlen(temp) - 1] = '\0';
1559 }
1560
1561 static bool __vc_db_is_valid_vfixed_string(char* string)
1562 {
1563         char* temp = strchr(string, '}');
1564         if (NULL == temp)
1565                 return false;
1566
1567         temp = strchr(string, '{');
1568         if (NULL == temp)
1569                 return false;
1570
1571         temp = strchr(string, '|');
1572         if (NULL == temp)
1573                 return false;
1574         return true;
1575 }
1576
1577 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1578 {
1579         if (NULL == cmd || NULL == cmd->command) {
1580                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1581                 return VC_DB_ERROR_INVALID_PARAMETER;
1582         }
1583
1584         GSList* temp_list = NULL;
1585         char* temp = NULL;
1586         char* src_cmd = strdup(cmd->command);
1587         char* dst_cmd = NULL;
1588         char merge_cmd[256] = {0, };
1589
1590         if (NULL == src_cmd) {
1591                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1592                 return VC_DB_ERROR_OUT_OF_MEMORY;
1593         }
1594
1595         if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1596                 // check string validation
1597                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1598                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1599                         free(src_cmd);
1600                         src_cmd = NULL;
1601                         return VC_DB_ERROR_INVALID_PARAMETER;
1602                 }
1603
1604                 // remove close brace, '}'
1605                 char* temp_close = strchr(src_cmd, '}');
1606                 if (NULL != temp_close) {
1607                         temp_close[0] = '\0';
1608                 }
1609
1610                 // extract fixed command and remove space in front of  '{'
1611                 char *tok_ptr = NULL;
1612                 temp = strtok_r(src_cmd, "{", &tok_ptr);
1613                 __vc_db_remove_space(&temp);
1614                 *fixed_cmd = strdup(temp);
1615
1616                 // merge command with fixed and vfixed
1617                 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1618                         __vc_db_remove_space(&temp);
1619
1620                         snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1621                         dst_cmd = strdup(merge_cmd);
1622                         temp_list = g_slist_append(temp_list, dst_cmd);
1623                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1624                 }
1625         } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1626                 // check string validation
1627                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1628                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1629                         free(src_cmd);
1630                         src_cmd = NULL;
1631                         return VC_DB_ERROR_INVALID_PARAMETER;
1632                 }
1633
1634                 // extract fixed command
1635                 char* temp_fixed = strchr(src_cmd, '}') + 1;
1636                 __vc_db_remove_space(&temp_fixed);
1637                 *fixed_cmd = strdup(temp_fixed);
1638
1639                 // remove close brace, '}'
1640                 char *tok_ptr = NULL;
1641                 temp = strtok_r(src_cmd, "}", &tok_ptr);
1642
1643                 // remove open brace, '{'
1644                 temp = strchr(src_cmd, '{') + 1;
1645
1646                 tok_ptr = NULL;
1647                 temp = strtok_r(temp, "|", &tok_ptr);
1648                 __vc_db_remove_space(&temp);
1649
1650                 // merge command with fixed and vfixed
1651                 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1652                 dst_cmd = strdup(merge_cmd);
1653                 temp_list = g_slist_append(temp_list, dst_cmd);
1654                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1655
1656                 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1657                         __vc_db_remove_space(&temp);
1658
1659                         // merge command with fixed and vfixed
1660                         snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1661                         dst_cmd = strdup(merge_cmd);
1662                         temp_list = g_slist_append(temp_list, dst_cmd);
1663                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1664                 }
1665         } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1666                 dst_cmd = strdup(src_cmd);
1667                 temp_list = g_slist_append(temp_list, dst_cmd);
1668                 *fixed_cmd = strdup(src_cmd);
1669
1670         } else {
1671                 dst_cmd = strdup(src_cmd);
1672                 temp_list = g_slist_append(temp_list, dst_cmd);
1673         }
1674
1675         *cmd_list = temp_list;
1676
1677         free(src_cmd);
1678         src_cmd = NULL;
1679         return VC_DB_ERROR_NONE;
1680 }
1681
1682 static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
1683 {
1684         GSList* cmd_list = NULL;
1685         char* fixed_cmd = NULL;
1686         vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
1687
1688         int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
1689         if (0 != ret) {
1690                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1691
1692                 if (NULL != tmp_cmd) {
1693                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1694                 }
1695                 return ret;
1696         }
1697
1698         if (0 != g_slist_length(cmd_list)) {
1699                 GSList *iter = NULL;
1700                 char* temp_command = NULL;
1701                 iter = g_slist_nth(cmd_list, 0);
1702
1703                 while (NULL != iter) {
1704                         temp_command = iter->data;
1705
1706                         if (NULL != temp_command) {
1707                                 if (NULL != tmp_cmd->command) {
1708                                         free(tmp_cmd->command);
1709                                         tmp_cmd->command = NULL;
1710                                 }
1711                                 tmp_cmd->command = strdup(temp_command);
1712                                 if (NULL != fixed_cmd)
1713                                         tmp_cmd->fixed = strdup(fixed_cmd);
1714                                 else
1715                                         tmp_cmd->fixed = NULL;
1716
1717                                 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
1718                                 if (ret != VC_DB_ERROR_NONE) {
1719                                         if (NULL != fixed_cmd) {
1720                                                 free(fixed_cmd);
1721                                                 fixed_cmd = NULL;
1722                                         }
1723                                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1724                                         break;
1725                                 }
1726
1727                                 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name && false == skip_invocation) {
1728                                         char temp[256] = {0, };
1729                                         snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
1730                                         if (NULL != tmp_cmd->command)
1731                                                 free(tmp_cmd->command);
1732
1733                                         tmp_cmd->command = strdup(temp);
1734
1735                                         ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
1736                                         if (ret != VC_DB_ERROR_NONE) {
1737                                                 if (NULL != fixed_cmd) {
1738                                                         free(fixed_cmd);
1739                                                         fixed_cmd = NULL;
1740                                                 }
1741                                                 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1742                                                 break;
1743                                         }
1744                                 }
1745                                 cmd_list = g_slist_remove(cmd_list, temp_command);
1746                                 free(temp_command);
1747                                 temp_command = NULL;
1748                         }
1749                         iter = g_slist_nth(cmd_list, 0);
1750                 }
1751
1752                 if (VC_DB_ERROR_NONE != ret) {
1753                         while (NULL != iter) {
1754                                 temp_command = iter->data;
1755
1756                                 if (NULL != temp_command) {
1757                                         cmd_list = g_slist_remove(cmd_list, temp_command);
1758                                         free(temp_command);
1759                                         temp_command = NULL;
1760                                 }
1761
1762                                 iter = g_slist_nth(cmd_list, 0);
1763                         }
1764
1765                         g_slist_free(cmd_list);
1766                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
1767
1768                         return ret;
1769                 }
1770                 cmd_list = NULL;
1771         }
1772
1773         if (NULL != fixed_cmd) {
1774                 free(fixed_cmd);
1775                 fixed_cmd = NULL;
1776         }
1777         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1778         return VC_DB_ERROR_NONE;
1779 }
1780
1781 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
1782 {
1783         int ret = __vc_db_insert_command(g_db_handle, pid, type, cmd, skip_invocation);
1784         if (0 != ret) {
1785                 SLOG(LOG_DEBUG, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
1786         }
1787         return ret;
1788 }
1789
1790
1791 static int __vc_db_insert_commands_list(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
1792 {
1793         GSList *iter = NULL;
1794         vc_cmd_s *temp_cmd;
1795
1796         int i;
1797         int count = g_slist_length(cmd_list);
1798         iter = g_slist_nth(cmd_list, 0);
1799
1800         SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
1801
1802         for (i = 0; i < count; i++) {
1803                 if (NULL == iter)
1804                         break;
1805
1806                 temp_cmd = iter->data;
1807
1808                 if (NULL == temp_cmd) {
1809                         SLOG(LOG_ERROR, vc_db_tag(), "comamnd is NULL");
1810                         break;
1811                 }
1812
1813                 if (type == temp_cmd->type) {
1814                         if (NULL != invocation_name)
1815                                 temp_cmd->invocation_name = strdup(invocation_name);
1816
1817                         int ret = __vc_db_insert_command(db_handle, pid, type, temp_cmd, skip_invocation);
1818                         if (ret != VC_DB_ERROR_NONE) {
1819                                 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
1820                                 return ret;
1821                         }
1822                 } else {
1823                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
1824                 }
1825                 iter = g_slist_next(iter);
1826         }
1827
1828         return VC_DB_ERROR_NONE;
1829 }
1830
1831 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
1832 {
1833         __vc_db_begin_transaction(g_db_handle);
1834
1835         int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
1836         if (ret != VC_DB_ERROR_NONE) {
1837                 __vc_db_rollback_transaction(g_db_handle);
1838                 return ret;
1839         }
1840
1841         __vc_db_commit_transaction(g_db_handle);
1842
1843         return VC_DB_ERROR_NONE;
1844 }
1845
1846 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
1847 {
1848         __vc_db_begin_transaction(g_db_handle);
1849
1850         int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
1851         if (ret != VC_DB_ERROR_NONE) {
1852                 __vc_db_rollback_transaction(g_db_handle);
1853                 return ret;
1854         }
1855
1856         __vc_db_commit_transaction(g_db_handle);
1857         return VC_DB_ERROR_NONE;
1858 }
1859
1860 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
1861 {
1862         if (NULL == result_text) {
1863                 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
1864                 return VC_DB_ERROR_INVALID_PARAMETER;
1865         }
1866
1867         int ret = vc_db_delete_table(VC_RESULT_TABLE);
1868         if (0 != ret)
1869                 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1870
1871         if (NULL == vc_cmd_list) {
1872                 __vc_db_begin_transaction(g_db_handle);
1873                 int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
1874                 if (ret != VC_DB_ERROR_NONE) {
1875                         __vc_db_rollback_transaction(g_db_handle);
1876                         return ret;
1877                 }
1878                 __vc_db_commit_transaction(g_db_handle);
1879                 return VC_DB_ERROR_NONE;
1880         }
1881
1882         /* Make client list node */
1883         vc_cmd_h vc_command = NULL;
1884         vc_cmd_list_first(vc_cmd_list);
1885
1886         while (VC_ERROR_ITERATION_END != ret) {
1887                 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
1888                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
1889                         break;
1890                 }
1891
1892                 if (NULL == vc_command) {
1893                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
1894                         break;
1895                 }
1896
1897                 vc_cmd_s* temp_cmd = NULL;
1898                 temp_cmd = (vc_cmd_s*)vc_command;
1899
1900                 __vc_db_begin_transaction(g_db_handle);
1901                 ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
1902                 if (ret != VC_DB_ERROR_NONE) {
1903                         __vc_db_rollback_transaction(g_db_handle);
1904                         return ret;
1905                 }
1906                 __vc_db_commit_transaction(g_db_handle);
1907
1908                 ret = vc_cmd_list_next(vc_cmd_list);
1909         }
1910
1911         return VC_DB_ERROR_NONE;
1912 }
1913
1914 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
1915 {
1916         __vc_db_begin_transaction(g_db_handle);
1917
1918         int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
1919         if (ret != VC_DB_ERROR_NONE) {
1920                 __vc_db_rollback_transaction(g_db_handle);
1921                 return VC_DB_ERROR_OPERATION_FAILED;
1922         }
1923
1924         if (NULL == msg) {
1925                 int count = 0;
1926                 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
1927                 if (ret != VC_DB_ERROR_NONE) {
1928                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
1929                 }
1930                 if (0 == count) {
1931                         char* appid = NULL;
1932                         // Get appid by pid using app control
1933                         ret = app_manager_get_app_id(pid, &appid);
1934                         if (APP_MANAGER_ERROR_NONE != ret) {
1935                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
1936                         }
1937                         ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
1938                         if (ret != VC_DB_ERROR_NONE) {
1939                                 __vc_db_rollback_transaction(g_db_handle);
1940                                 return ret;
1941                         }
1942                         if (NULL != appid) {
1943                                 free(appid);
1944                                 appid = NULL;
1945                         }
1946                 }
1947         }
1948         __vc_db_commit_transaction(g_db_handle);
1949         return VC_DB_ERROR_NONE;
1950 }
1951
1952 int vc_db_get_appid_list(const char* result, GSList** app_list)
1953 {
1954         __vc_db_begin_transaction(g_db_handle);
1955
1956         int ret = __vc_db_get_appid(g_db_handle, result, app_list);
1957         if (ret != VC_DB_ERROR_NONE) {
1958                 __vc_db_rollback_transaction(g_db_handle);
1959                 return ret;
1960         }
1961
1962         __vc_db_commit_transaction(g_db_handle);
1963         return VC_DB_ERROR_NONE;
1964 }
1965
1966 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1967 {
1968         __vc_db_begin_transaction(g_db_handle);
1969
1970         int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
1971         if (ret != VC_DB_ERROR_NONE) {
1972                 __vc_db_rollback_transaction(g_db_handle);
1973                 return ret;
1974         }
1975
1976         __vc_db_commit_transaction(g_db_handle);
1977         return VC_DB_ERROR_NONE;
1978 }
1979
1980 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1981 {
1982         __vc_db_begin_transaction(g_db_handle);
1983
1984         int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
1985         if (ret != VC_DB_ERROR_NONE) {
1986                 __vc_db_rollback_transaction(g_db_handle);
1987                 return ret;
1988         }
1989
1990         __vc_db_commit_transaction(g_db_handle);
1991         return VC_DB_ERROR_NONE;
1992 }
1993
1994 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
1995 {
1996         __vc_db_begin_transaction(g_db_handle);
1997
1998         int ret = 0;
1999         ret = __vc_db_delete_commands(g_db_handle, pid, type, appid);
2000         if (ret != VC_DB_ERROR_NONE) {
2001                 __vc_db_rollback_transaction(g_db_handle);
2002                 return ret;
2003         }
2004
2005         __vc_db_commit_transaction(g_db_handle);
2006         return VC_DB_ERROR_NONE;
2007 }
2008
2009 int vc_db_backup_command()
2010 {
2011         GSList* list = NULL;
2012
2013         __vc_db_begin_transaction(g_db_backup_handle);
2014
2015         int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
2016         if (0 != ret) {
2017                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2018                 __vc_db_rollback_transaction(g_db_backup_handle);
2019                 return ret;
2020         }
2021         __vc_db_commit_transaction(g_db_backup_handle);
2022
2023         ret = vc_db_get_commands(-1, VC_COMMAND_TYPE_BACKGROUND, &list);
2024         if (ret != VC_DB_ERROR_NONE) {
2025                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands");
2026                 return ret;
2027         }
2028
2029         __vc_db_begin_transaction(g_db_backup_handle);
2030
2031         ret = __vc_db_insert_commands_list(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2032         if (ret != VC_DB_ERROR_NONE) {
2033                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
2034                 __vc_db_rollback_transaction(g_db_backup_handle);
2035                 return ret;
2036         }
2037
2038         __vc_db_commit_transaction(g_db_backup_handle);
2039
2040         SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
2041         return VC_DB_ERROR_NONE;
2042 }
2043
2044 int vc_db_restore_command()
2045 {
2046         GSList* list = NULL;
2047
2048         __vc_db_begin_transaction(g_db_backup_handle);
2049
2050         int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);
2051         if (ret != VC_DB_ERROR_NONE) {
2052                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands from backup db");
2053                 __vc_db_rollback_transaction(g_db_backup_handle);
2054                 return ret;
2055         }
2056         __vc_db_commit_transaction(g_db_backup_handle);
2057
2058         ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2059         if (ret != VC_DB_ERROR_NONE) {
2060                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list");
2061                 return ret;
2062         }
2063
2064         SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
2065         return VC_DB_ERROR_NONE;
2066 }