Tizen 2.4.0 rev3 SDK Public Release
[framework/account/libaccounts-svc.git] / src / account_offline.c
1 /*
2  *
3  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <db-util.h>
24 #include <pthread.h>
25 #include <vasum.h>
26 #include <vconf.h>
27
28 #include <account-private.h>
29 #include <account_free.h>
30 #include <dbg.h>
31 #include "account_private_client.h"
32 #include "account_internal.h"
33
34 #ifdef TIZEN_PROFILE_MOBILE
35 #include "mobile/account.h"
36 #else
37 #include "wearable/account.h"
38 #endif
39
40 #define ACCOUNT_DB_OPEN_READONLY 0
41 #define ACCOUNT_DB_OPEN_READWRITE 1
42
43 typedef sqlite3_stmt* account_stmt;
44
45 static sqlite3* g_hAccountDB = NULL;
46 static int              g_refCntDB = 0;
47 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
48
49 static char *_account_get_text(const char *text_data)
50 {
51         char *text_value = NULL;
52
53         if (text_data != NULL) {
54                 text_value = strdup(text_data);
55         }
56         return text_value;
57 }
58
59 static const char *_account_db_err_msg()
60 {
61         return sqlite3_errmsg(g_hAccountDB);
62 }
63
64 static int _account_db_err_code()
65 {
66         return sqlite3_errcode(g_hAccountDB);
67 }
68
69 static int _account_db_open(int mode)
70 {
71         int  rc = 0;
72         int ret = -1;
73         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
74
75         char *client_db_path = NULL;
76
77         _INFO( "start to get DB path");
78
79         ret = vsm_canonicalize_path("/opt/usr/dbspace/.account.db", &client_db_path);
80         if (ret <= 0){
81                 _ERR( "vsm_canonicalize_path fail ret = %d", ret);
82                 _ACCOUNT_FREE(client_db_path);
83                 return ACCOUNT_ERROR_PERMISSION_DENIED;
84         }
85
86         _INFO( "account_db_path canonicalized = %s", client_db_path);
87
88         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
89
90         if (!g_hAccountDB) {
91                 if(mode == ACCOUNT_DB_OPEN_READWRITE)
92                         rc = db_util_open(client_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
93                 else if(mode == ACCOUNT_DB_OPEN_READONLY)
94                         rc = db_util_open_with_options(client_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
95                 else {
96                         _ACCOUNT_FREE(client_db_path);
97                         return ACCOUNT_ERROR_DB_NOT_OPENED;
98                 }
99                 _ACCOUNT_FREE(client_db_path);
100
101                 if( _account_db_err_code() == SQLITE_PERM ){
102                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
103                         return ACCOUNT_ERROR_PERMISSION_DENIED;
104                 }
105
106                 ACCOUNT_RETURN_VAL((rc != SQLITE_PERM), {}, ACCOUNT_ERROR_PERMISSION_DENIED, ("Account permission denied rc : %d", rc));
107                 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
108                 g_refCntDB++;
109         } else {
110                 g_refCntDB++;
111                 _ACCOUNT_FREE(client_db_path);
112         }
113
114         ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("busy handler fail. rc : %d", rc));
115
116         return ACCOUNT_ERROR_NONE;
117 }
118
119 static int _account_db_close(void)
120 {
121         int rc = 0;
122         int ret = -1;
123
124         if (g_hAccountDB) {
125                 if (g_refCntDB > 0) {
126                         g_refCntDB--;
127                 }
128                 if (g_refCntDB == 0) {
129                         rc = db_util_close(g_hAccountDB);
130                         if(  rc == SQLITE_PERM ){
131                                 ACCOUNT_ERROR( "Access failed(SQLITE_PERM)");
132                                 return ACCOUNT_ERROR_PERMISSION_DENIED;
133                         } else if ( rc == SQLITE_BUSY ){
134                                 ACCOUNT_ERROR( "database busy");
135                                 return ACCOUNT_ERROR_DATABASE_BUSY;
136                         }
137                         ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
138                         g_hAccountDB = NULL;
139                 }
140                 ret = ACCOUNT_ERROR_NONE;
141         } else {
142                 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
143                 ret = ACCOUNT_ERROR_DB_FAILED;
144         }
145
146         return ret;
147 }
148
149 static int _account_execute_query(const char *query)
150 {
151         int rc = -1;
152         char* pszErrorMsg = NULL;
153
154         if(!query){
155                 ACCOUNT_ERROR("NULL query\n");
156                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
157         }
158
159         if(!g_hAccountDB){
160                 ACCOUNT_ERROR("DB is not opened\n");
161                 return ACCOUNT_ERROR_DB_NOT_OPENED;
162         }
163
164         rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
165         if (SQLITE_OK != rc) {
166                 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
167                 sqlite3_free(pszErrorMsg);
168         }
169
170         return rc;
171 }
172
173 static int _account_begin_transaction(void)
174 {
175         ACCOUNT_DEBUG("_account_begin_transaction start");
176         int ret = -1;
177
178         ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
179
180         if (ret == SQLITE_BUSY){
181                 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
182                 return ACCOUNT_ERROR_DATABASE_BUSY;
183         } else if(ret != SQLITE_OK) {
184                 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
185                 return ACCOUNT_ERROR_DB_FAILED;
186         }
187
188         ACCOUNT_DEBUG("_account_begin_transaction end");
189         return ACCOUNT_ERROR_NONE;
190 }
191
192 static int _account_end_transaction(bool is_success)
193 {
194         ACCOUNT_DEBUG("_account_end_transaction start");
195
196         int ret = -1;
197
198         if (is_success == true) {
199                 ret = _account_execute_query("COMMIT TRANSACTION");
200                 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
201         } else {
202                 ret = _account_execute_query("ROLLBACK TRANSACTION");
203                 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
204         }
205
206         if(ret == SQLITE_PERM){
207                 ACCOUNT_ERROR("Account permission denied :: %d", ret);
208                 return ACCOUNT_ERROR_PERMISSION_DENIED;
209         }
210
211         if (ret == SQLITE_BUSY){
212                 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
213                 return ACCOUNT_ERROR_DATABASE_BUSY;
214         }
215
216         if (ret != SQLITE_OK) {
217                 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
218                 return ACCOUNT_ERROR_DB_FAILED;
219         }
220
221         ACCOUNT_DEBUG("_account_end_transaction end");
222         return ACCOUNT_ERROR_NONE;
223 }
224
225 static int _account_get_record_count(char* query)
226 {
227         _INFO("_account_get_record_count");
228
229         int rc = -1;
230         int ncount = 0;
231         account_stmt pStmt = NULL;
232
233         if(!query){
234                 _ERR("NULL query\n");
235                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
236         }
237
238         if(!g_hAccountDB){
239                 _ERR("DB is not opened\n");
240                 return ACCOUNT_ERROR_DB_NOT_OPENED;
241         }
242
243         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
244
245         if (SQLITE_BUSY == rc){
246                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
247                 sqlite3_finalize(pStmt);
248                 return ACCOUNT_ERROR_DATABASE_BUSY;
249         } else if (SQLITE_OK != rc) {
250                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
251                 sqlite3_finalize(pStmt);
252                 return ACCOUNT_ERROR_DB_FAILED;
253         }
254
255         rc = sqlite3_step(pStmt);
256         if (SQLITE_BUSY == rc) {
257                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
258                 sqlite3_finalize(pStmt);
259                 return ACCOUNT_ERROR_DATABASE_BUSY;
260         } else if (SQLITE_ROW != rc) {
261                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
262                 sqlite3_finalize(pStmt);
263                 return ACCOUNT_ERROR_DB_FAILED;
264         }
265
266         ncount = sqlite3_column_int(pStmt, 0);
267
268         _INFO("account record count [%d]", ncount);
269         sqlite3_finalize(pStmt);
270
271         return ncount;
272 }
273
274 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
275 {
276         if(!pStmt){
277                 ACCOUNT_ERROR("statement is null");
278                 return -1;
279         }
280
281         if(pos < 0){
282                 ACCOUNT_ERROR("invalid pos");
283                 return -1;
284         }
285
286         return sqlite3_bind_int(pStmt, pos, num);
287 }
288
289 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
290 {
291         _INFO("_account_query_bind_text");
292
293         if(!pStmt)
294         {
295                 _ERR("statement is null");
296                 return -1;
297         }
298
299         if(str)
300         {
301                 _INFO("sqlite3_bind_text");
302                 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
303         }
304         else
305         {
306                 _INFO("sqlite3_bind_null");
307                 return sqlite3_bind_null(pStmt, pos);
308         }
309 }
310
311 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
312 {
313         _INFO("");
314
315         int count = 1;
316
317         /*Caution : Keep insert query orders.*/
318
319         /* 1. app id*/
320         _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
321
322         /* 2. service provider id*/
323         _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
324
325         /* 3. icon path*/
326         _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
327
328         /* 4. small icon path*/
329         _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
330
331         /* 5. multiple accont support*/
332         _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
333
334         _INFO("");
335
336         return count;
337 }
338
339 static gboolean _account_type_check_duplicated(account_type_s *data)
340 {
341         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
342         int count = 0;
343
344         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
345
346         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
347                         , ACCOUNT_TYPE_TABLE, data->app_id);
348
349         count = _account_get_record_count(query);
350         if (count > 0) {
351                 return TRUE;
352         }
353
354         return FALSE;
355 }
356
357 static int _account_query_finalize(account_stmt pStmt)
358 {
359         int rc = -1;
360
361         if (!pStmt) {
362                 ACCOUNT_ERROR( "pStmt is NULL");
363                 return ACCOUNT_ERROR_INVALID_PARAMETER;
364         }
365
366         rc = sqlite3_finalize(pStmt);
367         if (rc == SQLITE_BUSY){
368                 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
369                 return ACCOUNT_ERROR_DATABASE_BUSY;
370         } else if (rc != SQLITE_OK) {
371                 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
372                 return ACCOUNT_ERROR_DB_FAILED;
373         }
374
375         return ACCOUNT_ERROR_NONE;
376 }
377
378 static int _account_query_step(account_stmt pStmt)
379 {
380         if(!pStmt){
381                 ACCOUNT_ERROR( "pStmt is NULL");
382                 return -1;
383         }
384
385         return sqlite3_step(pStmt);
386 }
387
388 static account_stmt _account_prepare_query(char *query)
389 {
390         int                     rc = -1;
391         account_stmt    pStmt = NULL;
392
393         ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
394
395         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
396
397         ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
398
399         return pStmt;
400 }
401
402 static int _account_get_next_sequence(const char *pszName)
403 {
404         int                     rc = 0;
405         account_stmt    pStmt = NULL;
406         int                     max_seq = 0;
407         char                    szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
408
409         ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
410         ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery),  "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
411         rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
412         if (SQLITE_OK != rc) {
413                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
414                 sqlite3_finalize(pStmt);
415                 return ACCOUNT_ERROR_DB_FAILED;
416         }
417
418         rc = sqlite3_step(pStmt);
419         max_seq = sqlite3_column_int(pStmt, 0);
420         max_seq++;
421
422         /*Finalize Statement*/
423         rc = sqlite3_finalize(pStmt);
424         pStmt = NULL;
425
426         return max_seq;
427 }
428
429 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
430 {
431         int                     rc, count = 1;
432         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
433         account_stmt    hstmt = NULL;
434
435         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
436         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
437
438         if (g_slist_length( account_type->provider_feature_list)==0) {
439                 ACCOUNT_ERROR( "no capability\n");
440                 return ACCOUNT_ERROR_NONE;
441         }
442
443         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
444
445         rc = _account_get_record_count(query);
446
447         if( _account_db_err_code() == SQLITE_PERM ){
448                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
449                 return ACCOUNT_ERROR_PERMISSION_DENIED;
450         }
451
452         if (rc <= 0) {
453                 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
454                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
455         }
456
457         /* insert query*/
458
459         GSList *iter;
460
461         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
462                 int ret;
463                 count = 1;
464                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
465                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
466                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
467
468                 hstmt = _account_prepare_query(query);
469
470                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
471
472                 provider_feature_s* feature_data = NULL;
473                 feature_data = (provider_feature_s*)iter->data;
474
475                 ret = _account_query_bind_text(hstmt, count++, app_id);
476                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
477                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
478                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
479
480                 rc = _account_query_step(hstmt);
481
482                 if (rc != SQLITE_DONE) {
483                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
484                         break;
485                 }
486
487                 rc = _account_query_finalize(hstmt);
488                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
489                 hstmt = NULL;
490
491         }
492
493         return ACCOUNT_ERROR_NONE;
494 }
495
496 static int _account_type_insert_label(account_type_s *account_type)
497 {
498         int                     rc, count = 1;
499         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
500         account_stmt    hstmt = NULL;
501
502         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
503
504         if (g_slist_length( account_type->label_list)==0) {
505                 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
506                 return ACCOUNT_ERROR_NONE;
507         }
508
509         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
510
511         rc = _account_get_record_count(query);
512
513         if( _account_db_err_code() == SQLITE_PERM ){
514                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
515                 return ACCOUNT_ERROR_PERMISSION_DENIED;
516         }
517
518         if (rc <= 0) {
519                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
520         }
521
522         /* insert query*/
523         GSList *iter;
524
525         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
526                 int ret;
527                 count = 1;
528                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
529                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
530                                 "(?, ?, ?) ", LABEL_TABLE);
531
532                 hstmt = _account_prepare_query(query);
533
534                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
535
536                 label_s* label_data = NULL;
537                 label_data = (label_s*)iter->data;
538
539                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
540                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
541                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
542                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
543                 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
544                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
545
546                 rc = _account_query_step(hstmt);
547
548                 if (rc != SQLITE_DONE) {
549                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
550                         break;
551                 }
552
553                 rc = _account_query_finalize(hstmt);
554                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
555                 hstmt = NULL;
556
557         }
558
559         return ACCOUNT_ERROR_NONE;
560 }
561
562 static int _account_type_execute_insert_query(account_type_s *account_type)
563 {
564         _INFO("");
565
566         int                             rc = 0;
567         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
568         int                             error_code = ACCOUNT_ERROR_NONE;
569         account_stmt    hstmt = NULL;
570
571         /* check mandatory field */
572         // app id & service provider id
573         if (!account_type->app_id) {
574                 return ACCOUNT_ERROR_INVALID_PARAMETER;
575         }
576
577         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
578         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
579                         "(?, ?, ?, ?, ?)",      ACCOUNT_TYPE_TABLE);
580
581         _INFO("");
582         hstmt = _account_prepare_query(query);
583         _INFO("");
584
585         if( _account_db_err_code() == SQLITE_PERM ){
586                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
587                 return ACCOUNT_ERROR_PERMISSION_DENIED;
588         } else if( _account_db_err_code() == SQLITE_BUSY ){
589                 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
590                 return ACCOUNT_ERROR_DATABASE_BUSY;
591         }
592
593         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
594
595         _INFO("");
596         _account_type_convert_account_to_sql(account_type, hstmt, query);
597         _INFO("");
598
599         rc = _account_query_step(hstmt);
600         if (rc == SQLITE_BUSY) {
601                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
602                 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
603         } else if (rc != SQLITE_DONE) {
604                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
605                 error_code = ACCOUNT_ERROR_DB_FAILED;
606         }
607
608         _INFO("");
609         rc = _account_query_finalize(hstmt);
610         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
611         hstmt = NULL;
612
613         _INFO("");
614         return error_code;
615 }
616
617 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
618 {
619         _INFO("");
620
621         int             error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
622
623         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
624         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
625         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
626
627         account_type_s *data = (account_type_s*)account_type;
628
629         pthread_mutex_lock(&account_mutex);
630
631
632         /* transaction control required*/
633         ret_transaction = _account_begin_transaction();
634
635         _INFO("");
636
637         if( _account_db_err_code() == SQLITE_PERM ){
638                 pthread_mutex_unlock(&account_mutex);
639                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
640                 return ACCOUNT_ERROR_PERMISSION_DENIED;
641         }
642
643         _INFO("");
644         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
645                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
646                 pthread_mutex_unlock(&account_mutex);
647                 return ACCOUNT_ERROR_DATABASE_BUSY;
648         } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
649                 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
650                 pthread_mutex_unlock(&account_mutex);
651                 return ret_transaction;
652         }
653
654         _INFO("");
655         if (_account_type_check_duplicated(data)) {
656                 _INFO("");
657                 ret_transaction = _account_end_transaction(FALSE);
658                 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
659                 *account_type_id = -1;
660                 pthread_mutex_unlock(&account_mutex);
661                 return ACCOUNT_ERROR_DUPLICATED;
662         } else {
663                 _INFO("");
664                 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
665
666                 error_code = _account_type_execute_insert_query(data);
667
668                 if (error_code != ACCOUNT_ERROR_NONE){
669                         error_code = ACCOUNT_ERROR_DUPLICATED;
670                         ret_transaction = _account_end_transaction(FALSE);
671                         ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
672                         *account_type_id = -1;
673                         pthread_mutex_unlock(&account_mutex);
674                         return error_code;
675                 }
676         }
677
678         _INFO("");
679         error_code = _account_type_insert_provider_feature(data, data->app_id);
680         if(error_code != ACCOUNT_ERROR_NONE) {
681                 _INFO("");
682                 ret_transaction = _account_end_transaction(FALSE);
683                 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
684                 pthread_mutex_unlock(&account_mutex);
685                 return error_code;
686         }
687         _INFO("");
688         error_code = _account_type_insert_label(data);
689         if(error_code != ACCOUNT_ERROR_NONE) {
690                 _INFO("");
691                 ret_transaction = _account_end_transaction(FALSE);
692                 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
693                 pthread_mutex_unlock(&account_mutex);
694                 return error_code;
695         }
696
697         ret_transaction = _account_end_transaction(TRUE);
698         _INFO("");
699         pthread_mutex_unlock(&account_mutex);
700
701         _INFO("");
702         return ACCOUNT_ERROR_NONE;
703 }
704
705 int _account_type_delete_by_app_id(const char* app_id)
706 {
707         int                     error_code = ACCOUNT_ERROR_NONE;
708         account_stmt    hstmt = NULL;
709         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
710         int                     rc = 0, count = -1;
711         int                     ret_transaction = 0;
712         int                             binding_count = 1;
713         bool                    is_success = FALSE;
714
715         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
716         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
717
718         /* Check requested ID to delete */
719         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
720
721         count = _account_get_record_count(query);
722
723         if( _account_db_err_code() == SQLITE_PERM ){
724                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
725                 return ACCOUNT_ERROR_PERMISSION_DENIED;
726         }
727
728         if (count <= 0) {
729                 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
730                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
731         }
732
733         /* transaction control required*/
734         ret_transaction = _account_begin_transaction();
735
736         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
737                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
738                 pthread_mutex_unlock(&account_mutex);
739                 return ACCOUNT_ERROR_DATABASE_BUSY;
740         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
741                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
742                 pthread_mutex_unlock(&account_mutex);
743                 return ret_transaction;
744         }
745
746         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
747
748         hstmt = _account_prepare_query(query);
749
750         if( _account_db_err_code() == SQLITE_PERM ){
751                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
752                 pthread_mutex_unlock(&account_mutex);
753                 return ACCOUNT_ERROR_PERMISSION_DENIED;
754         }
755
756         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
757                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
758
759         _account_query_bind_text(hstmt, binding_count++, app_id);
760
761         rc = _account_query_step(hstmt);
762         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
763
764         rc = _account_query_finalize(hstmt);
765         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
766         hstmt = NULL;
767
768         binding_count = 1;
769         ACCOUNT_MEMSET(query, 0, sizeof(query));
770
771         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
772
773         hstmt = _account_prepare_query(query);
774         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
775                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
776
777         _account_query_bind_text(hstmt, binding_count++, app_id);
778
779         rc = _account_query_step(hstmt);
780         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
781
782         rc = _account_query_finalize(hstmt);
783         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
784         is_success = TRUE;
785
786         hstmt = NULL;
787
788         binding_count = 1;
789         ACCOUNT_MEMSET(query, 0, sizeof(query));
790
791         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
792
793         hstmt = _account_prepare_query(query);
794         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
795                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
796
797         _account_query_bind_text(hstmt, binding_count++, app_id);
798
799         rc = _account_query_step(hstmt);
800         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
801
802         rc = _account_query_finalize(hstmt);
803         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
804         is_success = TRUE;
805
806         hstmt = NULL;
807
808         CATCH:
809         if (hstmt != NULL) {
810                 rc = _account_query_finalize(hstmt);
811                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
812                 hstmt = NULL;
813         }
814
815         ret_transaction = _account_end_transaction(is_success);
816
817         if (ret_transaction != ACCOUNT_ERROR_NONE) {
818                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
819         }
820
821         pthread_mutex_unlock(&account_mutex);
822
823         return error_code;
824 }
825
826 ACCOUNT_INTERNAL_API int account_type_insert_to_db_offline(account_type_h account_type, int* account_type_id)
827 {
828         _INFO("account_type_insert_to_db starting");
829
830         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
831         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
832
833         int db_id = -1;
834
835         _INFO("account_manager_account_type_add start");
836
837         guint pid = getpid();
838         _INFO("client Id = [%u]", pid);
839
840         int return_code = _account_db_open(1);
841         if (return_code != ACCOUNT_ERROR_NONE)
842         {
843                 _ERR("_account_db_open() error, ret = %d", return_code);
844
845                 goto RETURN;
846         }
847
848         int uid = getuid();
849         if (uid != 0)
850         {
851                 _ERR("current daemon is not root user, uid=%d", uid);
852                 goto RETURN;
853         }
854
855         _INFO("before _account_type_insert_to_db");
856         return_code = _account_type_insert_to_db((account_type_s*)account_type, &db_id);
857         _INFO("after _account_type_insert_to_db");
858         if (return_code != ACCOUNT_ERROR_NONE)
859         {
860                 _ERR("_account_type_insert_to_db error");
861                 goto RETURN;
862         }
863
864         *account_type_id = db_id;
865
866         account_type_s* account_type_data = (account_type_s*)account_type;
867         account_type_data->id = db_id;
868
869 RETURN:
870         _INFO("account_manager_account_type_add end");
871
872         if( g_hAccountDB == NULL )
873                 return return_code;
874
875         return_code = _account_db_close();
876         if (return_code != ACCOUNT_ERROR_NONE)
877         {
878                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
879                 return_code = ACCOUNT_ERROR_DB_FAILED;
880         }
881
882         return return_code;
883 }
884
885 ACCOUNT_INTERNAL_API int account_type_delete_by_app_id_offline(const char* app_id)
886 {
887         _INFO("account_type_delete_by_app_id starting");
888
889         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
890
891         guint pid = getpid();
892
893         _INFO("client Id = [%u]", pid);
894
895         int return_code = _account_db_open(1);
896         if (return_code != ACCOUNT_ERROR_NONE)
897         {
898                 _ERR("_account_db_open() error, ret = %d", return_code);
899
900                 goto RETURN;
901         }
902
903         int uid = getuid();
904         if (uid != 0)
905         {
906                 _ERR("current daemon is not root user, uid=%d", uid);
907                 goto RETURN;
908         }
909
910         _INFO("before _account_type_delete_by_app_id");
911         return_code = _account_type_delete_by_app_id(app_id);
912         _INFO("after _account_type_delete_by_app_id=[%d]", return_code);
913
914         if (return_code != ACCOUNT_ERROR_NONE)
915         {
916                 _ERR("_account_type_delete_by_app_id error");
917                 goto RETURN;
918         }
919
920 RETURN:
921         _INFO("account_type_delete_by_app_id_offline end");
922
923         if( g_hAccountDB == NULL )
924                 return return_code;
925
926         return_code = _account_db_close();
927         if (return_code != ACCOUNT_ERROR_NONE)
928         {
929                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
930                 return_code = ACCOUNT_ERROR_DB_FAILED;
931         }
932
933         return return_code;
934 }
935
936 static void _account_db_data_to_text(const char *textbuf, char **output)
937 {
938         if (textbuf && strlen(textbuf)>0) {
939                 if (*output) {
940                         free(*output);
941                         *output = NULL;
942                 }
943                 *output = strdup(textbuf);
944         }
945 }
946 static int _account_query_table_column_int(account_stmt pStmt, int pos)
947 {
948         if(!pStmt){
949                 ACCOUNT_ERROR("statement is null");
950                 return -1;
951         }
952
953         if(pos < 0){
954                 ACCOUNT_ERROR("invalid pos");
955                 return -1;
956         }
957
958         return sqlite3_column_int(pStmt, pos);
959 }
960
961 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
962 {
963         if(!pStmt){
964                 ACCOUNT_ERROR("statement is null");
965                 return NULL;
966         }
967
968         if(pos < 0){
969                 ACCOUNT_ERROR("invalid pos");
970                 return NULL;
971         }
972
973         return (const char*)sqlite3_column_text(pStmt, pos);
974 }
975
976 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
977 {
978         const char *textbuf = NULL;
979
980         account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
981         ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
982
983         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
984         _account_db_data_to_text(textbuf, &(account_record->user_name));
985
986         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
987         _account_db_data_to_text(textbuf, &(account_record->email_address));
988
989         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
990         _account_db_data_to_text(textbuf, &(account_record->display_name));
991
992         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
993         _account_db_data_to_text(textbuf, &(account_record->icon_path));
994
995         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
996         _account_db_data_to_text(textbuf, &(account_record->source));
997
998         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
999         _account_db_data_to_text(textbuf, &(account_record->package_name));
1000
1001         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1002         _account_db_data_to_text(textbuf, &(account_record->access_token));
1003
1004         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1005         _account_db_data_to_text(textbuf, &(account_record->domain_name));
1006
1007         account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1008
1009         account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1010
1011         account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1012
1013         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1014         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1015
1016         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1017         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1018
1019         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1020         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1021
1022         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1023         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1024
1025         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1026         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1027
1028         account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1029         account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1030         account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1031         account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1032         account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1033 }
1034
1035 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1036 {
1037         const char *textbuf = NULL;
1038
1039         _INFO("start _account_convert_column_to_capability()");
1040         capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1041
1042         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1043         _account_db_data_to_text(textbuf, &(capability_record->type));
1044
1045         capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1046
1047         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1048         _account_db_data_to_text(textbuf, &(capability_record->package_name));
1049
1050         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1051         _account_db_data_to_text(textbuf, &(capability_record->user_name));
1052
1053         capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1054         _INFO("type = %s, value = %d", capability_record->type, capability_record->value);
1055         _INFO("end _account_convert_column_to_capability()");
1056 }
1057
1058 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1059 {
1060         _INFO("start _account_convert_column_to_custom()");
1061         const char *textbuf = NULL;
1062
1063         custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1064
1065         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1066         _account_db_data_to_text(textbuf, &(custom_record->app_id));
1067
1068         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1069         _account_db_data_to_text(textbuf, &(custom_record->key));
1070
1071         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1072         _account_db_data_to_text(textbuf, &(custom_record->value));
1073         _INFO("key = %s, value = %s", custom_record->key, custom_record->value);
1074         _INFO("end _account_convert_column_to_custom()");
1075 }
1076
1077 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
1078 {
1079         _INFO("start");
1080
1081         int count = 1;
1082
1083         /*Caution : Keep insert query orders.*/
1084
1085         /* 1. user name*/
1086         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1087         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1088
1089         /* 2. email address*/
1090         _account_query_bind_text(hstmt, count++, (char*)account->email_address);
1091         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address);
1092
1093         /* 3. display name*/
1094         _account_query_bind_text(hstmt, count++, (char*)account->display_name);
1095         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name);
1096
1097         /* 4. icon path*/
1098         _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
1099         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path);
1100
1101         /* 5. source*/
1102         _account_query_bind_text(hstmt, count++, (char*)account->source);
1103         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source);
1104
1105         /* 6. package name*/
1106         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1107         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name);
1108
1109         /* 7. access token*/
1110         _account_query_bind_text(hstmt, count++, (char*)account->access_token);
1111         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token);
1112
1113         /* 8. domain name*/
1114         _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
1115         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name);
1116
1117         /* 9. auth type*/
1118         _account_query_bind_int(hstmt, count++, account->auth_type);
1119         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type);
1120
1121         /* 10. secret */
1122         _account_query_bind_int(hstmt, count++, account->secret);
1123         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret);
1124
1125         /* 11. sync_support */
1126         _account_query_bind_int(hstmt, count++, account->sync_support);
1127         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support);
1128
1129         int i;
1130
1131         /* 12. user text*/
1132         for(i=0; i< USER_TXT_CNT; i++)
1133                 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
1134
1135         /* 13. user integer     */
1136         for(i=0; i< USER_INT_CNT; i++)
1137         {
1138                 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
1139         _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]);
1140         }
1141
1142         _INFO("end");
1143
1144         return count;
1145 }
1146
1147 static bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1148 {
1149         account_s *data = (account_s*)user_data;
1150
1151         account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1152
1153         if (cap_data == NULL)
1154                 return FALSE;
1155         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1156
1157         cap_data->type = _account_get_text(capability_type);
1158         cap_data->value = capability_value;
1159         _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
1160
1161         data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1162
1163         return TRUE;
1164 }
1165
1166 static bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1167 {
1168         account_s *data = (account_s*)user_data;
1169
1170         account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1171
1172         if (custom_data == NULL) {
1173                 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1174                 return FALSE;
1175         }
1176         ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1177
1178         custom_data->account_id = data->id;
1179         custom_data->app_id = _account_get_text(data->package_name);
1180         custom_data->key = _account_get_text(key);
1181         custom_data->value = _account_get_text(value);
1182         _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
1183
1184         data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1185
1186         return TRUE;
1187 }
1188
1189 static int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
1190 {
1191         int                     error_code = ACCOUNT_ERROR_NONE;
1192         account_stmt    hstmt = NULL;
1193         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1194         int                     rc = 0;
1195
1196         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1197         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1198         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1199
1200         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1201
1202         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1203         hstmt = _account_prepare_query(query);
1204
1205         if( _account_db_err_code() == SQLITE_PERM ){
1206                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1207                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1208         }
1209
1210         rc = _account_query_step(hstmt);
1211         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1212
1213         account_capability_s* capability_record = NULL;
1214
1215         while (rc == SQLITE_ROW) {
1216                 bool cb_ret = FALSE;
1217                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1218
1219                 if (capability_record == NULL) {
1220                         ACCOUNT_FATAL("malloc Failed");
1221                         break;
1222                 }
1223
1224                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1225
1226                 _account_convert_column_to_capability(hstmt, capability_record);
1227
1228                 cb_ret = callback(capability_record->type, capability_record->value, user_data);
1229
1230                 _account_free_capability_with_items(capability_record);
1231
1232                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1233
1234                 rc = _account_query_step(hstmt);
1235         }
1236
1237         rc = _account_query_finalize(hstmt);
1238         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1239         hstmt = NULL;
1240
1241         error_code = ACCOUNT_ERROR_NONE;
1242
1243 CATCH:
1244         if (hstmt != NULL) {
1245                 rc = _account_query_finalize(hstmt);
1246                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1247                 hstmt = NULL;
1248         }
1249
1250         pthread_mutex_unlock(&account_mutex);
1251         return error_code;
1252 }
1253
1254 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
1255 {
1256         int                     error_code = ACCOUNT_ERROR_NONE;
1257         account_stmt    hstmt = NULL;
1258         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1259         int                     rc = 0;
1260
1261         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1262         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1263         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1264
1265         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1266
1267         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
1268         hstmt = _account_prepare_query(query);
1269
1270         if( _account_db_err_code() == SQLITE_PERM ){
1271                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1272                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1273         }
1274
1275         rc = _account_query_step(hstmt);
1276
1277         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1278
1279         account_custom_s* custom_record = NULL;
1280
1281         while (rc == SQLITE_ROW) {
1282                 bool cb_ret = FALSE;
1283                 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
1284
1285                 if (custom_record == NULL) {
1286                         ACCOUNT_FATAL("malloc Failed");
1287                         break;
1288                 }
1289
1290                 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
1291
1292                 _account_convert_column_to_custom(hstmt, custom_record);
1293
1294                 cb_ret = callback(custom_record->key, custom_record->value, user_data);
1295
1296                 _account_free_custom_with_items(custom_record);
1297
1298                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1299
1300                 rc = _account_query_step(hstmt);
1301         }
1302
1303         rc = _account_query_finalize(hstmt);
1304         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1305         hstmt = NULL;
1306
1307         error_code = ACCOUNT_ERROR_NONE;
1308
1309 CATCH:
1310         if (hstmt != NULL) {
1311                 rc = _account_query_finalize(hstmt);
1312                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1313                 hstmt = NULL;
1314         }
1315
1316         pthread_mutex_unlock(&account_mutex);
1317         return error_code;
1318 }
1319
1320 static int _account_compare_old_record(account_s *new_account, int account_id)
1321 {
1322         int                             error_code = ACCOUNT_ERROR_NONE;
1323         account_stmt    hstmt = NULL;
1324         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1325         int                             rc = 0;
1326         account_s *old_account = NULL;
1327
1328         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1329         ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1330         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1331
1332         old_account = (account_s*)calloc(1, sizeof(account_s));
1333         if (old_account == NULL) {
1334                 _ERR("Out of Memory");
1335                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1336         }
1337
1338         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1339
1340         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1341         hstmt = _account_prepare_query(query);
1342
1343         rc = _account_query_step(hstmt);
1344         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1345
1346         while (rc == SQLITE_ROW) {
1347                 _account_convert_column_to_account(hstmt, old_account);
1348                 rc = _account_query_step(hstmt);
1349         }
1350
1351         rc = _account_query_finalize(hstmt);
1352         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1353         hstmt = NULL;
1354
1355         // get capability
1356         error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1357         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1358
1359         // get custom text
1360         error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1361         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1362
1363         // compare
1364
1365         new_account->id = old_account->id;
1366
1367         //user name
1368         if(!new_account->user_name) {
1369                 if(old_account->user_name)
1370                         new_account->user_name = _account_get_text(old_account->user_name);
1371         }
1372
1373         // display name
1374         if(!new_account->display_name) {
1375                 if(old_account->display_name)
1376                         new_account->display_name = _account_get_text(old_account->display_name);
1377         }
1378
1379         // email address
1380         if(!new_account->email_address) {
1381                 if(old_account->email_address)
1382                         new_account->email_address = _account_get_text(old_account->email_address);
1383         }
1384
1385         // domain name
1386         if(!new_account->domain_name) {
1387                 if(old_account->domain_name)
1388                         new_account->domain_name = _account_get_text(old_account->domain_name);
1389         }
1390
1391         // icon path
1392         if(!new_account->icon_path) {
1393                 if(old_account->icon_path)
1394                         new_account->icon_path = _account_get_text(old_account->icon_path);
1395         }
1396
1397         // source
1398         if(!new_account->source) {
1399                 if(old_account->source)
1400                         new_account->source = _account_get_text(old_account->source);
1401         }
1402
1403         _ACCOUNT_FREE(new_account->package_name);
1404         new_account->package_name = _account_get_text(old_account->package_name);
1405
1406         // access token
1407         if(!new_account->access_token) {
1408                 if(old_account->access_token)
1409                         new_account->access_token = _account_get_text(old_account->access_token);
1410         }
1411
1412         // user text
1413         int i;
1414         for(i=0;i<USER_TXT_CNT;i++) {
1415                 if(!new_account->user_data_txt[i]) {
1416                         if(old_account->user_data_txt[i])
1417                                 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1418                 }
1419         }
1420
1421         // auth type
1422         if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1423                 new_account->auth_type = old_account->auth_type;
1424         }
1425
1426         //secret
1427         if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1428                 new_account->secret = old_account->secret;
1429         }
1430
1431         // sync support
1432         if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1433                 new_account->sync_support = old_account->sync_support;
1434         }
1435
1436         // user int
1437         for(i=0;i<USER_INT_CNT;i++) {
1438                 if(new_account->user_data_int[i] == 0) {
1439                                 new_account->user_data_int[i] = old_account->user_data_int[i];
1440                 }
1441         }
1442
1443         // capability
1444
1445         // user custom table
1446
1447 CATCH:
1448                 if (old_account)
1449                         _account_free_account_with_items(old_account);
1450
1451                 if (hstmt != NULL) {
1452                         rc = _account_query_finalize(hstmt);
1453                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1454                         hstmt = NULL;
1455                 }
1456
1457         return ACCOUNT_ERROR_NONE;
1458 }
1459
1460 GList* _account_query_account_by_package_name(const char* package_name, int *error_code)
1461 {
1462         _INFO("_account_query_account_by_package_name start, package_name=[%s]", package_name);
1463
1464         *error_code = ACCOUNT_ERROR_NONE;
1465         account_stmt    hstmt = NULL;
1466         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1467         int                     rc = 0;
1468
1469         ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
1470         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
1471
1472         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1473
1474         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
1475
1476         hstmt = _account_prepare_query(query);
1477
1478         if( _account_db_err_code() == SQLITE_PERM ){
1479                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1480                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1481                 return NULL;
1482         }
1483
1484         int binding_count = 1;
1485         _account_query_bind_text(hstmt, binding_count++, package_name);
1486
1487         rc = _account_query_step(hstmt);
1488
1489         account_s* account_head = NULL;
1490
1491         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
1492
1493         int tmp = 0;
1494
1495         account_head = (account_s*) malloc(sizeof(account_s));
1496         if (account_head == NULL) {
1497                 ACCOUNT_FATAL("malloc Failed");
1498                 if (hstmt != NULL) {
1499                         rc = _account_query_finalize(hstmt);
1500                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1501                         hstmt = NULL;
1502                 }
1503                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
1504                 return NULL;
1505         }
1506         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1507
1508         while (rc == SQLITE_ROW) {
1509                 account_s* account_record = NULL;
1510
1511                 account_record = (account_s*) malloc(sizeof(account_s));
1512
1513                 if (account_record == NULL) {
1514                         ACCOUNT_FATAL("malloc Failed");
1515                         break;
1516                 }
1517                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1518
1519                 _account_convert_column_to_account(hstmt, account_record);
1520
1521                 _INFO("Adding account_list");
1522                 account_head->account_list = g_list_append(account_head->account_list, account_record);
1523
1524                 rc = _account_query_step(hstmt);
1525                 tmp++;
1526         }
1527
1528         rc = _account_query_finalize(hstmt);
1529         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1530         hstmt = NULL;
1531
1532         GList *iter;
1533
1534         tmp = g_list_length(account_head->account_list);
1535
1536         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
1537                 account_s* testaccount = (account_s*)iter->data;
1538
1539                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
1540                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
1541         }
1542
1543         *error_code = ACCOUNT_ERROR_NONE;
1544
1545 CATCH:
1546         if (hstmt != NULL)
1547         {
1548                 rc = _account_query_finalize(hstmt);
1549                 if (rc != ACCOUNT_ERROR_NONE) {
1550                         *error_code = rc;
1551                         _ERR("finalize error");
1552                 }
1553                 hstmt = NULL;
1554         }
1555
1556         pthread_mutex_unlock(&account_mutex);
1557
1558         if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
1559                 _account_glist_account_free(account_head->account_list);
1560                 _ACCOUNT_FREE(account_head);
1561                 account_head = NULL;
1562         }
1563
1564         if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
1565         {
1566                 _INFO("Returning account_list");
1567 //              _remove_sensitive_info_from_non_owning_account_list(getpid(), account_head->account_list);
1568                 GList* result = account_head->account_list;
1569                 _ACCOUNT_FREE(account_head);
1570                 return result;
1571         }
1572         return NULL;
1573 }
1574
1575 ACCOUNT_INTERNAL_API int account_query_account_by_package_name_offline(account_cb callback, const char *package_name, void *user_data)
1576 {
1577         _INFO("account_query_from_db_by_package_name_offline");
1578
1579         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
1580         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("callback is null!"));
1581
1582         int return_code = -1;
1583         GList* account_list = NULL;
1584         GList* iter;
1585
1586         return_code = _account_db_open(0);
1587         if (return_code != ACCOUNT_ERROR_NONE)
1588         {
1589                 _ERR("_account_db_open() error, ret = %d", return_code);
1590
1591                 goto RETURN;
1592         }
1593
1594         int uid = getuid();
1595         if (uid != 0)
1596         {
1597                 _ERR("current process user is not root, uid=%d", uid);
1598                 return_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1599                 goto RETURN;
1600         }
1601
1602         _INFO("before _account_query_from_db_by_package_name");
1603         account_list = _account_query_account_by_package_name(package_name, &return_code);
1604         _INFO("after _account_query_from_db_by_package_name=[%d]", return_code);
1605
1606         if (return_code != ACCOUNT_ERROR_NONE)
1607         {
1608                 _ERR("_account_query_from_db_by_package_name error");
1609                 goto RETURN;
1610         }
1611
1612         for (iter = account_list; iter != NULL; iter = g_list_next(iter))
1613         {
1614                 _INFO("iterating received account_list");
1615                 account_s *account = NULL;
1616                 account = (account_s*)iter->data;
1617
1618                 if (callback((account_h)account, user_data) == false)
1619                 {
1620                         _INFO("application callback requested to discontinue.");
1621                         break;
1622                 }
1623         }
1624
1625 RETURN:
1626         _account_glist_account_free(account_list);
1627         if( g_hAccountDB == NULL )
1628                 return return_code;
1629
1630         return_code = _account_db_close();
1631         if (return_code != ACCOUNT_ERROR_NONE)
1632         {
1633                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1634                 return_code = ACCOUNT_ERROR_DB_FAILED;
1635         }
1636
1637         _INFO("account_query_account_by_package_name_offline end");
1638         return return_code;
1639 }
1640
1641 static void _account_insert_delete_update_notification_send(char *noti_name, int pid)
1642 {
1643         vsm_context_h ctx;
1644         vsm_zone_h effective_zone, real_zone;
1645
1646         if (!noti_name) {
1647                 _ERR("Noti Name is NULL!!!!!!\n");
1648                 return;
1649         }
1650
1651         //Tizen zone [[
1652         ctx = vsm_create_context();
1653
1654         if(ctx == NULL) {
1655                 _ERR( "Failed to initialize domain control vsm context.");
1656                 return;
1657         }
1658
1659         effective_zone = vsm_lookup_zone_by_pid(ctx, pid);
1660         if(effective_zone == NULL) {
1661                 _ERR( "Failed vsm_lookup_zone_by_pid.");
1662                 return;
1663         }
1664
1665         _INFO( "before set vsm_join_zone()");
1666         real_zone = vsm_join_zone(effective_zone);
1667         _INFO( "after set vsm_join_zone()");
1668         //]]
1669
1670         _INFO("noti_type = %s", noti_name);
1671
1672         if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
1673                 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
1674         }
1675
1676         _INFO( "before recover vsm_join_zone()");
1677         vsm_join_zone(real_zone);
1678         _INFO( "after recover vsm_join_zone()");
1679
1680         _INFO( "before vsm_cleanup_context()");
1681         vsm_cleanup_context(ctx);
1682         _INFO( "after vsm_cleanup_context()");
1683 }
1684
1685 int _account_delete_from_db_by_package_name_offline(const char *package_name)
1686 {
1687         int                     error_code = ACCOUNT_ERROR_NONE;
1688         account_stmt    hstmt = NULL;
1689         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1690         int                     rc = 0;
1691         int                     ret_transaction = 0;
1692         bool                    is_success = FALSE;
1693         int                     binding_count = 1;
1694         GSList                  *account_id_list = NULL;
1695         int                             ret = -1;
1696
1697         // It only needs list of ids, does not need to query sensitive info. So sending 0
1698         GList* account_list_temp = _account_query_account_by_package_name(package_name, &ret);
1699         if (account_list_temp == NULL)
1700         {
1701                 _ERR("_account_query_account_by_package_name returned NULL");
1702                 return ACCOUNT_ERROR_DB_FAILED;
1703         }
1704
1705         if( _account_db_err_code() == SQLITE_PERM ){
1706                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1707                 _account_glist_account_free(account_list_temp);
1708                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1709         }
1710
1711         if(ret != ACCOUNT_ERROR_NONE){
1712                 _account_glist_account_free(account_list_temp);
1713                 return ret;
1714         }
1715
1716         account_list_temp = g_list_first(account_list_temp);
1717         _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
1718
1719         GList* iter = NULL;
1720         for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
1721         {
1722                 _INFO("iterating account_list_temp");
1723                 account_s *account = NULL;
1724                 _INFO("Before iter->data");
1725                 account = (account_s*)iter->data;
1726                 _INFO("After iter->data");
1727                 if (account != NULL)
1728                 {
1729                         char id[256] = {0, };
1730
1731                         ACCOUNT_MEMSET(id, 0, 256);
1732
1733                         ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
1734
1735                         _INFO("Adding account id [%s]", id);
1736                         account_id_list = g_slist_append(account_id_list, g_strdup(id));
1737                 }
1738         }
1739
1740         _account_glist_account_free(account_list_temp);
1741         /* transaction control required*/
1742         ret_transaction = _account_begin_transaction();
1743
1744         if( _account_db_err_code() == SQLITE_PERM ){
1745                 pthread_mutex_unlock(&account_mutex);
1746                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1747                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1748         }
1749
1750         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
1751                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
1752                 pthread_mutex_unlock(&account_mutex);
1753                 return ACCOUNT_ERROR_DATABASE_BUSY;
1754         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
1755                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1756                 pthread_mutex_unlock(&account_mutex);
1757                 return ret_transaction;
1758         }
1759
1760         /* delete custom table  */
1761         ACCOUNT_MEMSET(query, 0, sizeof(query));
1762         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
1763
1764         hstmt = _account_prepare_query(query);
1765
1766         if( _account_db_err_code() == SQLITE_PERM ){
1767                 _account_end_transaction(FALSE);
1768                 pthread_mutex_unlock(&account_mutex);
1769                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1770                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1771         }
1772
1773         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1774                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1775
1776         binding_count = 1;
1777         _account_query_bind_text(hstmt, binding_count++, package_name);
1778
1779         rc = _account_query_step(hstmt);
1780         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1781
1782         rc = _account_query_finalize(hstmt);
1783         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1784         hstmt = NULL;
1785
1786         /* delete capability table */
1787         ACCOUNT_MEMSET(query, 0, sizeof(query));
1788         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
1789
1790         hstmt = _account_prepare_query(query);
1791
1792         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1793                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1794
1795         binding_count = 1;
1796         _account_query_bind_text(hstmt, binding_count++, package_name);
1797
1798         rc = _account_query_step(hstmt);
1799         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1800
1801         rc = _account_query_finalize(hstmt);
1802         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1803         hstmt = NULL;
1804
1805         /* delete account table */
1806         ACCOUNT_MEMSET(query, 0, sizeof(query));
1807
1808         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
1809
1810         hstmt = _account_prepare_query(query);
1811         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1812                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1813
1814         binding_count = 1;
1815         _account_query_bind_text(hstmt, binding_count++, package_name);
1816
1817         rc = _account_query_step(hstmt);
1818         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc));
1819
1820         rc = _account_query_finalize(hstmt);
1821         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1822         is_success = TRUE;
1823
1824         hstmt = NULL;
1825
1826 CATCH:
1827         if (hstmt != NULL) {
1828                 rc = _account_query_finalize(hstmt);
1829                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1830                 hstmt = NULL;
1831         }
1832
1833         ret_transaction = _account_end_transaction(is_success);
1834
1835         if (ret_transaction != ACCOUNT_ERROR_NONE) {
1836                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
1837         } else {
1838                 if (is_success == true) {
1839                         GSList* gs_iter = NULL;
1840                         for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
1841                                 char* p_tmpid = NULL;
1842                                 p_tmpid = (char*)gs_iter->data;
1843                                 char buf[64]={0,};
1844                                 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
1845                                 ACCOUNT_SLOGD("%s", buf);
1846                                 _account_insert_delete_update_notification_send(buf, getpid());
1847                                 _ACCOUNT_FREE(p_tmpid);
1848                         }
1849                         g_slist_free(account_id_list);
1850                 }
1851         }
1852
1853         pthread_mutex_unlock(&account_mutex);
1854
1855         _INFO("_account_delete_from_db_by_package_name_offline end");
1856         return error_code;
1857 }
1858
1859 ACCOUNT_INTERNAL_API int account_delete_from_db_by_package_name_offline(const char *package_name)
1860 {
1861         _INFO("_account_delete_from_db_by_package_name_offline");
1862
1863         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
1864
1865         int return_code = _account_db_open(1);
1866         if (return_code != ACCOUNT_ERROR_NONE)
1867         {
1868                 _ERR("_account_db_open() error, ret = %d", return_code);
1869
1870                 goto RETURN;
1871         }
1872
1873         int uid = getuid();
1874         if (uid != 0)
1875         {
1876                 _ERR("current process user is not root, uid=%d", uid);
1877                 return_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1878                 goto RETURN;
1879         }
1880
1881         _INFO("before _account_delete_from_db_by_package_name_offline");
1882         return_code = _account_delete_from_db_by_package_name_offline(package_name);
1883         _INFO("after _account_delete_from_db_by_package_name_offline=[%d]", return_code);
1884
1885         if (return_code != ACCOUNT_ERROR_NONE)
1886         {
1887                 _ERR("_account_delete_from_db_by_package_name_offline error");
1888                 goto RETURN;
1889         }
1890
1891 RETURN:
1892         _INFO("account_delete_from_db_by_package_name_offline end");
1893
1894         if( g_hAccountDB == NULL )
1895                 return return_code;
1896
1897         return_code = _account_db_close();
1898         if (return_code != ACCOUNT_ERROR_NONE)
1899         {
1900                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1901                 return_code = ACCOUNT_ERROR_DB_FAILED;
1902         }
1903
1904         return return_code;
1905 }
1906
1907 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1908 {
1909         int                             error_code = ACCOUNT_ERROR_NONE;
1910         account_stmt    hstmt = NULL;
1911         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1912         int                             rc = 0;
1913         account_s *old_account = NULL;
1914
1915         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1916         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1917
1918         old_account = (account_s*)calloc(1, sizeof(account_s));
1919         if (old_account == NULL) {
1920                 _ERR("Out Of memory");
1921                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1922         }
1923
1924         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1925
1926         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1927         hstmt = _account_prepare_query(query);
1928
1929         rc = _account_query_step(hstmt);
1930         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1931
1932         while (rc == SQLITE_ROW) {
1933                 _account_convert_column_to_account(hstmt, old_account);
1934                 rc = _account_query_step(hstmt);
1935         }
1936
1937         rc = _account_query_finalize(hstmt);
1938         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1939         hstmt = NULL;
1940
1941         // get package name.
1942         *package_name = _account_get_text(old_account->package_name);
1943
1944
1945         CATCH:
1946                 if (old_account)
1947                         _account_free_account_with_items(old_account);
1948
1949                 if (hstmt != NULL) {
1950                         rc = _account_query_finalize(hstmt);
1951                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1952                         hstmt = NULL;
1953                 }
1954
1955         return error_code;
1956
1957 }
1958
1959 static int _account_update_capability(account_s *account, int account_id)
1960 {
1961         int                     rc, count = 1;
1962         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1963         account_stmt    hstmt = NULL;
1964
1965         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1966
1967         if (g_slist_length( account->capablity_list)==0) {
1968                 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
1969                 return ACCOUNT_ERROR_NONE;
1970         }
1971
1972         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1973
1974         rc = _account_get_record_count(query);
1975
1976         if (rc <= 0) {
1977                 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
1978                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1979         }
1980
1981         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1982
1983         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
1984         hstmt = _account_prepare_query(query);
1985         count = 1;
1986         _account_query_bind_int(hstmt, count++, (int)account_id);
1987         rc = _account_query_step(hstmt);
1988
1989         if (rc != SQLITE_DONE) {
1990                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1991                 return ACCOUNT_ERROR_DB_FAILED;
1992         }
1993         rc = _account_query_finalize(hstmt);
1994         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1995         hstmt = NULL;
1996
1997         GSList *iter;
1998
1999         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2000                 int ret;
2001                 count = 1;
2002                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2003                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
2004                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
2005
2006                 hstmt = _account_prepare_query(query);
2007
2008                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
2009
2010                 account_capability_s* cap_data = NULL;
2011                 cap_data = (account_capability_s*)iter->data;
2012
2013                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
2014                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2015                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
2016                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
2017                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
2018                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2019                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
2020                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2021                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
2022                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
2023
2024                 rc = _account_query_step(hstmt);
2025
2026                 if (rc != SQLITE_DONE) {
2027                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2028                         break;
2029                 }
2030
2031                 rc = _account_query_finalize(hstmt);
2032                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2033                 hstmt = NULL;
2034
2035         }
2036
2037         return ACCOUNT_ERROR_NONE;
2038 }
2039
2040 static int _account_update_custom(account_s *account, int account_id)
2041 {
2042         int                     rc, count = 1;
2043         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2044         account_stmt    hstmt = NULL;
2045
2046         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2047
2048         if (g_slist_length( account->custom_list)==0) {
2049                 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
2050                 return ACCOUNT_ERROR_NONE;
2051         }
2052
2053         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
2054
2055         rc = _account_get_record_count(query);
2056
2057         if( _account_db_err_code() == SQLITE_PERM ){
2058                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2059                 pthread_mutex_unlock(&account_mutex);
2060                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2061         } else if( _account_db_err_code() == SQLITE_BUSY ){
2062                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2063                 pthread_mutex_unlock(&account_mutex);
2064                 return ACCOUNT_ERROR_DATABASE_BUSY;
2065         }
2066
2067         if (rc <= 0) {
2068                 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2069                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2070         }
2071
2072         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2073
2074         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
2075         hstmt = _account_prepare_query(query);
2076         count = 1;
2077         _account_query_bind_int(hstmt, count++, (int)account_id);
2078         rc = _account_query_step(hstmt);
2079
2080         if (rc == SQLITE_BUSY) {
2081                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2082                 return ACCOUNT_ERROR_DATABASE_BUSY;
2083         } else if (rc != SQLITE_DONE) {
2084                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2085                 return ACCOUNT_ERROR_DB_FAILED;
2086         }
2087
2088         rc = _account_query_finalize(hstmt);
2089         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2090         hstmt = NULL;
2091
2092         GSList *iter;
2093
2094         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2095                 int ret;
2096                 count = 1;
2097                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2098                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
2099                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
2100
2101                 hstmt = _account_prepare_query(query);
2102
2103                 if( _account_db_err_code() == SQLITE_PERM ){
2104                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2105                         return ACCOUNT_ERROR_PERMISSION_DENIED;
2106                 }
2107
2108                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
2109
2110                 account_custom_s* custom_data = NULL;
2111                 custom_data = (account_custom_s*)iter->data;
2112
2113                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
2114                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
2115                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
2116                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2117                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
2118                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2119                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
2120                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2121
2122                 rc = _account_query_step(hstmt);
2123
2124                 if (rc != SQLITE_DONE) {
2125                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2126                         break;
2127                 }
2128
2129                 rc = _account_query_finalize(hstmt);
2130                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2131                 hstmt = NULL;
2132
2133         }
2134
2135         return ACCOUNT_ERROR_NONE;
2136 }
2137
2138 int _account_update_account(account_s *account, int account_id)
2139 {
2140         int                             rc = 0, binding_count =0;
2141         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2142         int                             error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2143         account_stmt    hstmt = NULL;
2144
2145         if (!account->package_name) {
2146                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2147                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2148         }
2149
2150         /* Check account_id*/
2151         char *package_name = NULL;
2152
2153         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2154
2155         if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2156                 ACCOUNT_ERROR("No package name with account_id\n");
2157                 _ACCOUNT_FREE(package_name);
2158                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2159         }
2160
2161         _ACCOUNT_FREE(package_name);
2162
2163         if (error_code != ACCOUNT_ERROR_NONE) {
2164                 ACCOUNT_ERROR("No permission to update\n");
2165                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2166         }
2167
2168         error_code = _account_compare_old_record(account, account_id);
2169         if (error_code != ACCOUNT_ERROR_NONE) {
2170                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2171                 return error_code;
2172         }
2173
2174         if (_account_db_err_code() == SQLITE_PERM ) {
2175                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2176                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2177         } else if( _account_db_err_code() == SQLITE_BUSY ) {
2178                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2179                 return ACCOUNT_ERROR_DATABASE_BUSY;
2180         }
2181
2182         if (!account->user_name && !account->display_name && !account->email_address) {
2183                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2184                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2185         }
2186
2187         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2188
2189         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2190
2191         count = _account_get_record_count(query);
2192         if (count <= 0) {
2193                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2194                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2195         }
2196
2197         /* transaction control required*/
2198         ret_transaction = _account_begin_transaction();
2199         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2200                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2201                 pthread_mutex_unlock(&account_mutex);
2202                 return ACCOUNT_ERROR_DATABASE_BUSY;
2203         }
2204
2205         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2206         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2207                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2208                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2209                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2210
2211         hstmt = _account_prepare_query(query);
2212
2213         if( _account_db_err_code() == SQLITE_PERM ){
2214                 ret_transaction = _account_end_transaction(FALSE);
2215                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2216                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2217         }
2218
2219         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2220
2221         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2222         _account_query_bind_int(hstmt, binding_count++, account_id);
2223
2224         rc = _account_query_step(hstmt);
2225         if (rc != SQLITE_DONE) {
2226                 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2227         }
2228
2229         rc = _account_query_finalize(hstmt);
2230         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2231         hstmt = NULL;
2232
2233         _INFO("update query=%s", query);
2234
2235         /*update capability*/
2236         error_code = _account_update_capability(account, account_id);
2237         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2238                 ret_transaction = _account_end_transaction(FALSE);
2239                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2240                 return error_code;
2241         }
2242
2243         /* update custom */
2244         error_code = _account_update_custom(account, account_id);
2245         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2246                 ret_transaction = _account_end_transaction(FALSE);
2247                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2248                 return error_code;
2249         }
2250
2251         ret_transaction = _account_end_transaction(TRUE);
2252
2253         _INFO("update end");
2254         return error_code;
2255 }
2256
2257 int _account_update_to_db_by_id_offline(account_s* account, int account_id)
2258 {
2259         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2260         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2261         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2262         int     error_code = ACCOUNT_ERROR_NONE;
2263         account_s* data = (account_s*)account;
2264
2265         pthread_mutex_lock(&account_mutex);
2266
2267         error_code = _account_update_account(data, account_id);
2268
2269         if(error_code != ACCOUNT_ERROR_NONE) {
2270                 pthread_mutex_unlock(&account_mutex);
2271                 return error_code;
2272         }
2273
2274         pthread_mutex_unlock(&account_mutex);
2275
2276         char buf[64]={0,};
2277         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2278         _account_insert_delete_update_notification_send(buf, getpid());
2279
2280         return ACCOUNT_ERROR_NONE;
2281 }
2282
2283 ACCOUNT_INTERNAL_API int account_update_to_db_by_id_offline(account_h account, int account_id)
2284 {
2285         _INFO("account_update_to_db_by_id_offline");
2286
2287         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account_h is null!"));
2288         ACCOUNT_RETURN_VAL((account_id >= 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account_id is invalid data!"));
2289
2290         int return_code = _account_db_open(1);
2291         if (return_code != ACCOUNT_ERROR_NONE)
2292         {
2293                 _ERR("_account_db_open() error, ret = %d", return_code);
2294
2295                 goto RETURN;
2296         }
2297
2298         int uid = getuid();
2299         if (uid != 0)
2300         {
2301                 _ERR("current process user is not root, uid=%d", uid);
2302                 return_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2303                 goto RETURN;
2304         }
2305
2306         _INFO("before _account_update_to_db_by_id_offline");
2307         return_code = _account_update_to_db_by_id_offline((account_s *)account, account_id);
2308         _INFO("after _account_update_to_db_by_id_offline=[%d]", return_code);
2309
2310         if (return_code != ACCOUNT_ERROR_NONE)
2311         {
2312                 _ERR("_account_update_to_db_by_id_offline error");
2313                 goto RETURN;
2314         }
2315
2316 RETURN:
2317         _INFO("account_update_to_db_by_id_offline end");
2318
2319         if( g_hAccountDB == NULL )
2320                 return return_code;
2321
2322         return_code = _account_db_close();
2323         if (return_code != ACCOUNT_ERROR_NONE)
2324         {
2325                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
2326                 return_code = ACCOUNT_ERROR_DB_FAILED;
2327         }
2328
2329         return return_code;
2330 }