resolved coverity issue
[platform/core/pim/contacts-service.git] / server / ctsvc_ipc_server.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <pims-ipc-svc.h>
22 #include "contacts.h"
23
24 #include "ctsvc_handle.h"
25 #include "ctsvc_server_service.h"
26 #include "ctsvc_db_init.h"
27 #include "ctsvc_db_query.h"
28 #include "ctsvc_db_access_control.h"
29
30 #include "ctsvc_ipc_marshal.h"
31 #include "ctsvc_internal.h"
32 #include "ctsvc_ipc_server.h"
33 #include "ctsvc_db_utils.h"
34 #include "ctsvc_server_utils.h"
35
36 void ctsvc_ipc_server_connect(pims_ipc_h ipc, pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
37 {
38         CTS_FN_CALL;
39         int ret = CONTACTS_ERROR_NONE;
40         contacts_h contact = NULL;
41
42         if (indata) {
43                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
44                 if (ret != CONTACTS_ERROR_NONE) {
45                         /* LCOV_EXCL_START */
46                         ERR("ctsvc_ipc_unmarshal_handle() Fail");
47                         goto ERROR_RETURN;
48                         /* LCOV_EXCL_STOP */
49                 }
50         } else {
51                 /* LCOV_EXCL_START */
52                 ERR("There is no indata Fail");
53                 ret = CONTACTS_ERROR_SYSTEM;
54                 goto ERROR_RETURN;
55                 /* LCOV_EXCL_STOP */
56         }
57
58         ret = ctsvc_connect();
59
60         if (CONTACTS_ERROR_NONE == ret) {
61                 char *smack = NULL;
62                 if (0 != pims_ipc_svc_get_smack_label(ipc, &smack))
63                         ERR("pims_ipc_svc_get_smack_label() Fail()");
64                 ctsvc_set_client_access_info(ipc, smack);
65                 free(smack);
66         }
67
68 ERROR_RETURN:
69         if (outdata) {
70                 *outdata = pims_ipc_data_create(0);
71                 if (NULL == *outdata) {
72                         /* LCOV_EXCL_START */
73                         ERR("pims_ipc_data_create() Fail");
74                         goto DATA_FREE;
75                         /* LCOV_EXCL_STOP */
76                 }
77
78                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
79                         /* LCOV_EXCL_START */
80                         pims_ipc_data_destroy(*outdata);
81                         *outdata = NULL;
82                         ERR("ctsvc_ipc_unmarshal_int() Fail");
83                         goto DATA_FREE;
84                         /* LCOV_EXCL_STOP */
85                 }
86         } else {
87                 ERR("outdata is NULL");
88         }
89 DATA_FREE:
90         ctsvc_handle_destroy(contact);
91 }
92
93 void ctsvc_ipc_server_disconnect(pims_ipc_h ipc, pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
94 {
95         int ret = CONTACTS_ERROR_NONE;
96         contacts_h contact = NULL;
97
98 #ifdef TIZEN_TEST_GCOV
99         void __gcov_flush(void);
100         __gcov_flush();
101 #endif
102
103
104         if (indata) {
105                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
106                 if (CONTACTS_ERROR_NONE != ret) {
107                         /* LCOV_EXCL_START */
108                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
109                         ret = CONTACTS_ERROR_IPC;
110                         goto ERROR_RETURN;
111                         /* LCOV_EXCL_STOP */
112                 }
113         }
114
115         ret = ctsvc_disconnect();
116
117 ERROR_RETURN:
118         if (outdata) {
119                 *outdata = pims_ipc_data_create(0);
120                 if (NULL == *outdata) {
121                         /* LCOV_EXCL_START */
122                         ERR("pims_ipc_data_create() Fail");
123                         goto DATA_FREE;
124                         /* LCOV_EXCL_STOP */
125                 }
126                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
127                         /* LCOV_EXCL_START */
128                         pims_ipc_data_destroy(*outdata);
129                         *outdata = NULL;
130                         ERR("ctsvc_ipc_marshal_int() Fail");
131                         goto DATA_FREE;
132                         /* LCOV_EXCL_STOP */
133                 }
134         } else {
135                 /* LCOV_EXCL_START */
136                 ERR("outdata is NULL");
137                 /* LCOV_EXCL_STOP */
138         }
139 DATA_FREE:
140         ctsvc_handle_destroy(contact);
141         ctsvc_server_trim_memory();
142         ctsvc_server_start_timeout();
143 }
144
145 void ctsvc_ipc_server_check_permission(pims_ipc_h ipc, pims_ipc_data_h indata,
146                 pims_ipc_data_h *outdata, void *userdata)
147 {
148         int ret = CONTACTS_ERROR_NONE;
149         int permission;
150         bool result = false;
151
152         if (NULL == indata) {
153                 /* LCOV_EXCL_START */
154                 ret = CONTACTS_ERROR_INVALID_PARAMETER;
155                 ERR("check permission Fail");
156                 goto ERROR_RETURN;
157                 /* LCOV_EXCL_STOP */
158         }
159
160         ret = ctsvc_ipc_unmarshal_int(indata, &permission);
161         if (ret != CONTACTS_ERROR_NONE) {
162                 /* LCOV_EXCL_START */
163                 ERR("ctsvc_ipc_unmarshal_int() Fail");
164                 goto ERROR_RETURN;
165                 /* LCOV_EXCL_STOP */
166         }
167
168         result = ctsvc_have_permission(ipc, permission);
169
170 ERROR_RETURN:
171         *outdata = pims_ipc_data_create(0);
172         if (NULL == *outdata) {
173                 /* LCOV_EXCL_START */
174                 ERR("pims_ipc_data_create() Fail");
175                 ctsvc_server_start_timeout();
176                 return;
177                 /* LCOV_EXCL_STOP */
178         }
179
180         if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
181                 /* LCOV_EXCL_START */
182                 pims_ipc_data_destroy(*outdata);
183                 *outdata = NULL;
184                 ERR("ctsvc_ipc_marshal_int() Fail");
185                 ctsvc_server_start_timeout();
186                 return;
187                 /* LCOV_EXCL_STOP */
188         }
189
190         if (ret == CONTACTS_ERROR_NONE) {
191                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_bool(result, *outdata)) {
192                         /* LCOV_EXCL_START */
193                         pims_ipc_data_destroy(*outdata);
194                         *outdata = NULL;
195                         ERR("ctsvc_ipc_marshal_bool() Fail");
196                         ctsvc_server_start_timeout();
197                         return;
198                         /* LCOV_EXCL_STOP */
199                 }
200         }
201         ctsvc_server_start_timeout();
202 }
203
204 void ctsvc_ipc_server_db_insert_record(pims_ipc_h ipc, pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
205 {
206         int ret = CONTACTS_ERROR_NONE;
207         contacts_record_h record = NULL;
208         int id = 0;
209         contacts_h contact = NULL;
210
211         if (indata) {
212                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
213                 if (CONTACTS_ERROR_NONE != ret) {
214                         /* LCOV_EXCL_START */
215                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
216                         ret = CONTACTS_ERROR_IPC;
217                         goto ERROR_RETURN;
218                         /* LCOV_EXCL_STOP */
219                 }
220
221                 ret = ctsvc_ipc_unmarshal_record(indata, &record);
222                 if (ret != CONTACTS_ERROR_NONE) {
223                         /* LCOV_EXCL_START */
224                         ERR("ctsvc_ipc_unmarshal_record() Fail");
225                         record = NULL;
226                         goto ERROR_RETURN;
227                         /* LCOV_EXCL_STOP */
228                 }
229         } else {
230                 /* LCOV_EXCL_START */
231                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
232                 goto ERROR_RETURN;
233                 /* LCOV_EXCL_STOP */
234         }
235
236         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(((ctsvc_record_s*)record)->view_uri))) {
237                 /* LCOV_EXCL_START */
238                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
239                 goto ERROR_RETURN;
240                 /* LCOV_EXCL_STOP */
241         }
242
243         ret = ctsvc_db_insert_record(record, &id);
244
245         if (outdata) {
246                 *outdata = pims_ipc_data_create(0);
247                 if (NULL == *outdata) {
248                         /* LCOV_EXCL_START */
249                         ERR("pims_ipc_data_create() Fail");
250                         goto DATA_FREE;
251                         /* LCOV_EXCL_STOP */
252                 }
253                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
254                         /* LCOV_EXCL_START */
255                         pims_ipc_data_destroy(*outdata);
256                         *outdata = NULL;
257                         ERR("ctsvc_ipc_marshal_int() Fail");
258                         goto DATA_FREE;
259                         /* LCOV_EXCL_STOP */
260                 }
261                 if (ret == CONTACTS_ERROR_NONE) {
262                         int transaction_ver = ctsvc_get_transaction_ver();
263                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
264                                 /* LCOV_EXCL_START */
265                                 pims_ipc_data_destroy(*outdata);
266                                 *outdata = NULL;
267                                 ERR("ctsvc_ipc_marshal_int() Fail");
268                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
269                                 goto ERROR_RETURN;
270                                 /* LCOV_EXCL_STOP */
271                         }
272                         if (ctsvc_ipc_marshal_int(id, *outdata) != CONTACTS_ERROR_NONE) {
273                                 /* LCOV_EXCL_START */
274                                 pims_ipc_data_destroy(*outdata);
275                                 ERR("ctsvc_ipc_marshal_int() Fail");
276                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
277                                 goto ERROR_RETURN;
278                                 /* LCOV_EXCL_STOP */
279                         }
280                 }
281         } else {
282                 /* LCOV_EXCL_START */
283                 ERR("outdata is NULL");
284                 /* LCOV_EXCL_STOP */
285         }
286         goto DATA_FREE;
287
288 ERROR_RETURN:
289         if (outdata) {
290                 *outdata = pims_ipc_data_create(0);
291                 if (NULL == *outdata) {
292                         /* LCOV_EXCL_START */
293                         ERR("pims_ipc_data_create() Fail");
294                         goto DATA_FREE;
295                         /* LCOV_EXCL_STOP */
296                 }
297                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
298                         /* LCOV_EXCL_START */
299                         pims_ipc_data_destroy(*outdata);
300                         *outdata = NULL;
301                         ERR("ctsvc_ipc_marshal_int() Fail");
302                         goto DATA_FREE;
303                         /* LCOV_EXCL_STOP */
304                 }
305         } else {
306                 /* LCOV_EXCL_START */
307                 ERR("outdata is NULL");
308                 /* LCOV_EXCL_STOP */
309         }
310
311 DATA_FREE:
312         ctsvc_handle_destroy(contact);
313         contacts_record_destroy(record, true);
314         ctsvc_server_trim_memory();
315         ctsvc_server_start_timeout();
316         return;
317 }
318
319 void ctsvc_ipc_server_db_get_record(pims_ipc_h ipc, pims_ipc_data_h indata,
320                 pims_ipc_data_h *outdata, void *userdata)
321 {
322         int ret = CONTACTS_ERROR_NONE;
323         char *view_uri = NULL;
324         int id = 0;
325         contacts_record_h record = NULL;
326         contacts_h contact = NULL;
327
328         if (indata) {
329                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
330                 if (CONTACTS_ERROR_NONE != ret) {
331                         /* LCOV_EXCL_START */
332                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
333                         ret = CONTACTS_ERROR_IPC;
334                         goto ERROR_RETURN;
335                         /* LCOV_EXCL_STOP */
336                 }
337                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
338                 if (ret != CONTACTS_ERROR_NONE) {
339                         /* LCOV_EXCL_START */
340                         ERR("ctsvc_ipc_unmarshal_string Fail");
341                         goto ERROR_RETURN;
342                         /* LCOV_EXCL_STOP */
343                 }
344                 ret = ctsvc_ipc_unmarshal_int(indata, &id);
345                 if (ret != CONTACTS_ERROR_NONE) {
346                         /* LCOV_EXCL_START */
347                         ERR("ctsvc_ipc_unmarshal_int() Fail");
348                         goto ERROR_RETURN;
349                         /* LCOV_EXCL_STOP */
350                 }
351         } else {
352                 /* LCOV_EXCL_START */
353                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
354                 goto ERROR_RETURN;
355                 /* LCOV_EXCL_STOP */
356         }
357
358         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
359                 /* LCOV_EXCL_START */
360                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
361                 goto ERROR_RETURN;
362                 /* LCOV_EXCL_STOP */
363         }
364
365         ret = ctsvc_db_get_record(view_uri, id, &record);
366
367 ERROR_RETURN:
368         if (outdata) {
369                 *outdata = pims_ipc_data_create(0);
370                 if (NULL == *outdata) {
371                         /* LCOV_EXCL_START */
372                         ERR("pims_ipc_data_create() Fail");
373                         goto DATA_FREE;
374                         /* LCOV_EXCL_STOP */
375                 }
376                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
377                         /* LCOV_EXCL_START */
378                         pims_ipc_data_destroy(*outdata);
379                         *outdata = NULL;
380                         ERR("ctsvc_ipc_marshal_int() Fail");
381                         goto DATA_FREE;
382                         /* LCOV_EXCL_STOP */
383                 }
384
385                 if (CONTACTS_ERROR_NO_DATA == ret) {
386                         DBG("no data");
387                 } else if (CONTACTS_ERROR_NONE == ret) {
388                         if (ctsvc_ipc_marshal_record(record, *outdata) != CONTACTS_ERROR_NONE) {
389                                 /* LCOV_EXCL_START */
390                                 pims_ipc_data_destroy(*outdata);
391                                 *outdata = NULL;
392                                 ERR("ctsvc_ipc_marshal_record() Fail");
393                                 goto DATA_FREE;
394                                 /* LCOV_EXCL_STOP */
395                         }
396                 }
397         } else {
398                 /* LCOV_EXCL_START */
399                 ERR("outdata is NULL");
400                 /* LCOV_EXCL_STOP */
401         }
402 DATA_FREE:
403         ctsvc_handle_destroy(contact);
404         contacts_record_destroy(record, true);
405         free(view_uri);
406         ctsvc_server_start_timeout();
407         return;
408 }
409
410 void ctsvc_ipc_server_db_update_record(pims_ipc_h ipc, pims_ipc_data_h indata,
411                 pims_ipc_data_h *outdata, void *userdata)
412 {
413         int ret = CONTACTS_ERROR_NONE;
414         contacts_record_h record = NULL;
415         contacts_h contact = NULL;
416
417         if (indata) {
418                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
419                 if (CONTACTS_ERROR_NONE != ret) {
420                         /* LCOV_EXCL_START */
421                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
422                         ret = CONTACTS_ERROR_IPC;
423                         goto ERROR_RETURN;
424                         /* LCOV_EXCL_STOP */
425                 }
426                 ret = ctsvc_ipc_unmarshal_record(indata, &record);
427                 if (ret != CONTACTS_ERROR_NONE) {
428                         /* LCOV_EXCL_START */
429                         ERR("ctsvc_ipc_unmarshal_record() Fail");
430                         goto ERROR_RETURN;
431                         /* LCOV_EXCL_STOP */
432                 }
433         } else {
434                 /* LCOV_EXCL_START */
435                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
436                 goto ERROR_RETURN;
437                 /* LCOV_EXCL_STOP */
438         }
439
440         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(((ctsvc_record_s*)record)->view_uri))) {
441                 /* LCOV_EXCL_START */
442                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
443                 goto ERROR_RETURN;
444                 /* LCOV_EXCL_STOP */
445         }
446
447         ret = ctsvc_db_update_record(record);
448
449         if (outdata) {
450                 *outdata = pims_ipc_data_create(0);
451                 if (NULL == *outdata) {
452                         /* LCOV_EXCL_START */
453                         ERR("pims_ipc_data_create() Fail");
454                         goto DATA_FREE;
455                         /* LCOV_EXCL_STOP */
456                 }
457                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
458                         /* LCOV_EXCL_START */
459                         pims_ipc_data_destroy(*outdata);
460                         *outdata = NULL;
461                         ERR("ctsvc_ipc_marshal_int() Fail");
462                         goto DATA_FREE;
463                         /* LCOV_EXCL_STOP */
464                 }
465
466                 if (ret == CONTACTS_ERROR_NONE) {
467                         int transaction_ver = ctsvc_get_transaction_ver();
468                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
469                                 /* LCOV_EXCL_START */
470                                 pims_ipc_data_destroy(*outdata);
471                                 *outdata = NULL;
472                                 ERR("ctsvc_ipc_marshal_int() Fail");
473                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
474                                 goto ERROR_RETURN;
475                                 /* LCOV_EXCL_STOP */
476                         }
477                 }
478         }
479
480         goto DATA_FREE;
481
482 ERROR_RETURN:
483         if (outdata) {
484                 *outdata = pims_ipc_data_create(0);
485                 if (NULL == *outdata) {
486                         /* LCOV_EXCL_START */
487                         ERR("pims_ipc_data_create() Fail");
488                         goto DATA_FREE;
489                         /* LCOV_EXCL_STOP */
490                 }
491                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
492                         /* LCOV_EXCL_START */
493                         pims_ipc_data_destroy(*outdata);
494                         *outdata = NULL;
495                         ERR("ctsvc_ipc_marshal_int() Fail");
496                         goto DATA_FREE;
497                         /* LCOV_EXCL_STOP */
498                 }
499         } else {
500                 /* LCOV_EXCL_START */
501                 ERR("outdata is NULL");
502                 /* LCOV_EXCL_STOP */
503         }
504 DATA_FREE:
505         ctsvc_handle_destroy(contact);
506         contacts_record_destroy(record, true);
507         ctsvc_server_trim_memory();
508         ctsvc_server_start_timeout();
509         return;
510 }
511
512 void ctsvc_ipc_server_db_delete_record(pims_ipc_h ipc, pims_ipc_data_h indata,
513                 pims_ipc_data_h *outdata, void *userdata)
514 {
515         int ret = CONTACTS_ERROR_NONE;
516         char *view_uri = NULL;
517         int id = 0;
518         contacts_h contact = NULL;
519
520         if (indata) {
521                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
522                 if (CONTACTS_ERROR_NONE != ret) {
523                         /* LCOV_EXCL_START */
524                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
525                         ret = CONTACTS_ERROR_IPC;
526                         goto ERROR_RETURN;
527                         /* LCOV_EXCL_STOP */
528                 }
529                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
530                 if (ret != CONTACTS_ERROR_NONE) {
531                         /* LCOV_EXCL_START */
532                         ERR("ctsvc_ipc_unmarshal_record() Fail");
533                         goto ERROR_RETURN;
534                         /* LCOV_EXCL_STOP */
535                 }
536                 ret = ctsvc_ipc_unmarshal_int(indata, &id);
537                 if (ret != CONTACTS_ERROR_NONE) {
538                         /* LCOV_EXCL_START */
539                         ERR("ctsvc_ipc_unmarshal_int() Fail");
540                         goto ERROR_RETURN;
541                         /* LCOV_EXCL_STOP */
542                 }
543         } else {
544                 /* LCOV_EXCL_START */
545                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
546                 goto ERROR_RETURN;
547                 /* LCOV_EXCL_STOP */
548         }
549
550         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(view_uri))) {
551                 /* LCOV_EXCL_START */
552                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
553                 goto ERROR_RETURN;
554                 /* LCOV_EXCL_STOP */
555         }
556
557         ret = ctsvc_db_delete_record(view_uri, id);
558
559         if (outdata) {
560                 *outdata = pims_ipc_data_create(0);
561                 if (NULL == *outdata) {
562                         /* LCOV_EXCL_START */
563                         ERR("pims_ipc_data_create() Fail");
564                         goto DATA_FREE;
565                         /* LCOV_EXCL_STOP */
566                 }
567                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
568                         /* LCOV_EXCL_START */
569                         pims_ipc_data_destroy(*outdata);
570                         *outdata = NULL;
571                         ERR("ctsvc_ipc_marshal_int() Fail");
572                         goto DATA_FREE;
573                         /* LCOV_EXCL_STOP */
574                 }
575
576                 if (ret == CONTACTS_ERROR_NONE) {
577                         int transaction_ver = ctsvc_get_transaction_ver();
578                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
579                                 /* LCOV_EXCL_START */
580                                 pims_ipc_data_destroy(*outdata);
581                                 *outdata = NULL;
582                                 ERR("ctsvc_ipc_marshal_int() Fail");
583                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
584                                 goto ERROR_RETURN;
585                                 /* LCOV_EXCL_STOP */
586                         }
587                 }
588         }
589
590         goto DATA_FREE;
591
592 ERROR_RETURN:
593         if (outdata) {
594                 *outdata = pims_ipc_data_create(0);
595                 if (NULL == *outdata) {
596                         /* LCOV_EXCL_START */
597                         ERR("pims_ipc_data_create() Fail");
598                         goto DATA_FREE;
599                         /* LCOV_EXCL_STOP */
600                 }
601                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
602                         /* LCOV_EXCL_START */
603                         pims_ipc_data_destroy(*outdata);
604                         *outdata = NULL;
605                         ERR("ctsvc_ipc_marshal_int() Fail");
606                         goto DATA_FREE;
607                         /* LCOV_EXCL_STOP */
608                 }
609         } else {
610                 /* LCOV_EXCL_START */
611                 ERR("outdata is NULL");
612                 /* LCOV_EXCL_STOP */
613         }
614
615 DATA_FREE:
616         ctsvc_handle_destroy(contact);
617         free(view_uri);
618         ctsvc_server_start_timeout();
619         return;
620 }
621
622 void ctsvc_ipc_server_db_replace_record(pims_ipc_h ipc, pims_ipc_data_h indata,
623                 pims_ipc_data_h *outdata, void *userdata)
624 {
625         int ret = CONTACTS_ERROR_NONE;
626         contacts_record_h record = NULL;
627         int id = 0;
628         contacts_h contact = NULL;
629
630         if (indata) {
631                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
632                 if (CONTACTS_ERROR_NONE != ret) {
633                         /* LCOV_EXCL_START */
634                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
635                         ret = CONTACTS_ERROR_IPC;
636                         goto ERROR_RETURN;
637                         /* LCOV_EXCL_STOP */
638                 }
639
640                 ret = ctsvc_ipc_unmarshal_record(indata, &record);
641                 if (ret != CONTACTS_ERROR_NONE) {
642                         /* LCOV_EXCL_START */
643                         ERR("ctsvc_ipc_unmarshal_record() Fail");
644                         record = NULL;
645                         goto ERROR_RETURN;
646                         /* LCOV_EXCL_STOP */
647                 }
648                 ret = ctsvc_ipc_unmarshal_int(indata, &id);
649                 if (ret != CONTACTS_ERROR_NONE) {
650                         /* LCOV_EXCL_START */
651                         ERR("ctsvc_ipc_unmarshal_int() Fail");
652                         goto ERROR_RETURN;
653                         /* LCOV_EXCL_STOP */
654                 }
655         } else {
656                 /* LCOV_EXCL_START */
657                 ERR("ctsvc_ipc_server_db_replace_record Fail");
658                 goto ERROR_RETURN;
659                 /* LCOV_EXCL_STOP */
660         }
661
662         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(((ctsvc_record_s*)record)->view_uri))) {
663                 /* LCOV_EXCL_START */
664                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
665                 goto ERROR_RETURN;
666                 /* LCOV_EXCL_STOP */
667         }
668
669
670         ret = ctsvc_db_replace_record(record, id);
671
672         if (outdata) {
673                 *outdata = pims_ipc_data_create(0);
674                 if (NULL == *outdata) {
675                         /* LCOV_EXCL_START */
676                         ERR("pims_ipc_data_create() Fail");
677                         goto DATA_FREE;
678                         /* LCOV_EXCL_STOP */
679                 }
680
681                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
682                         /* LCOV_EXCL_START */
683                         pims_ipc_data_destroy(*outdata);
684                         *outdata = NULL;
685                         ERR("ctsvc_ipc_marshal_int() Fail");
686                         goto DATA_FREE;
687                         /* LCOV_EXCL_STOP */
688                 }
689
690                 if (ret == CONTACTS_ERROR_NONE) {
691                         int transaction_ver = ctsvc_get_transaction_ver();
692                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
693                                 /* LCOV_EXCL_START */
694                                 pims_ipc_data_destroy(*outdata);
695                                 *outdata = NULL;
696                                 ERR("ctsvc_ipc_marshal_int() Fail");
697                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
698                                 goto ERROR_RETURN;
699                                 /* LCOV_EXCL_STOP */
700                         }
701                 }
702         } else {
703                 /* LCOV_EXCL_START */
704                 ERR("outdata is NULL");
705                 /* LCOV_EXCL_STOP */
706         }
707         goto DATA_FREE;
708
709 ERROR_RETURN:
710         if (outdata) {
711                 *outdata = pims_ipc_data_create(0);
712                 if (NULL == *outdata) {
713                         /* LCOV_EXCL_START */
714                         ERR("pims_ipc_data_create() Fail");
715                         goto DATA_FREE;
716                         /* LCOV_EXCL_STOP */
717                 }
718                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
719                         /* LCOV_EXCL_START */
720                         pims_ipc_data_destroy(*outdata);
721                         *outdata = NULL;
722                         ERR("ctsvc_ipc_marshal_int() Fail");
723                         goto DATA_FREE;
724                         /* LCOV_EXCL_STOP */
725                 }
726         } else {
727                 /* LCOV_EXCL_START */
728                 ERR("outdata is NULL");
729                 /* LCOV_EXCL_STOP */
730         }
731
732 DATA_FREE:
733         ctsvc_handle_destroy(contact);
734         contacts_record_destroy(record, true);
735         ctsvc_server_start_timeout();
736         return;
737 }
738
739 void ctsvc_ipc_server_db_get_all_records(pims_ipc_h ipc, pims_ipc_data_h indata,
740                 pims_ipc_data_h *outdata, void *userdata)
741 {
742         int ret = CONTACTS_ERROR_NONE;
743         char *view_uri = NULL;
744         int offset = 0;
745         int limit = 0;
746         contacts_list_h list = NULL;
747         contacts_h contact = NULL;
748
749         if (indata) {
750                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
751                 if (CONTACTS_ERROR_NONE != ret) {
752                         /* LCOV_EXCL_START */
753                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
754                         ret = CONTACTS_ERROR_IPC;
755                         goto ERROR_RETURN;
756                         /* LCOV_EXCL_STOP */
757                 }
758
759                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
760                 if (ret != CONTACTS_ERROR_NONE) {
761                         /* LCOV_EXCL_START */
762                         ERR("ctsvc_ipc_unmarshal_record() Fail");
763                         goto ERROR_RETURN;
764                         /* LCOV_EXCL_STOP */
765                 }
766                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
767                 if (ret != CONTACTS_ERROR_NONE) {
768                         /* LCOV_EXCL_START */
769                         ERR("ctsvc_ipc_unmarshal_int() Fail");
770                         goto ERROR_RETURN;
771                         /* LCOV_EXCL_STOP */
772                 }
773                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
774                 if (ret != CONTACTS_ERROR_NONE) {
775                         /* LCOV_EXCL_START */
776                         ERR("ctsvc_ipc_unmarshal_int() Fail");
777                         goto ERROR_RETURN;
778                         /* LCOV_EXCL_STOP */
779                 }
780         } else {
781                 /* LCOV_EXCL_START */
782                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
783                 goto ERROR_RETURN;
784                 /* LCOV_EXCL_STOP */
785         }
786
787         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
788                 /* LCOV_EXCL_START */
789                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
790                 goto ERROR_RETURN;
791                 /* LCOV_EXCL_STOP */
792         }
793
794         ret = ctsvc_db_get_all_records(view_uri, offset, limit, &list);
795
796         if (outdata) {
797                 *outdata = pims_ipc_data_create(0);
798                 if (NULL == *outdata) {
799                         /* LCOV_EXCL_START */
800                         ERR("pims_ipc_data_create() Fail");
801                         goto DATA_FREE;
802                         /* LCOV_EXCL_STOP */
803                 }
804                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
805                         /* LCOV_EXCL_START */
806                         pims_ipc_data_destroy(*outdata);
807                         *outdata = NULL;
808                         ERR("ctsvc_ipc_marshal_int() Fail");
809                         goto DATA_FREE;
810                         /* LCOV_EXCL_STOP */
811                 }
812
813                 if (CONTACTS_ERROR_NO_DATA == ret) {
814                         DBG("no data");
815                 } else if (CONTACTS_ERROR_NONE == ret) {
816                         ret = ctsvc_ipc_marshal_list(list, *outdata);
817
818                         if (ret != CONTACTS_ERROR_NONE) {
819                                 /* LCOV_EXCL_START */
820                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
821                                 goto DATA_FREE;
822                                 /* LCOV_EXCL_STOP */
823                         }
824                 }
825
826         } else {
827                 /* LCOV_EXCL_START */
828                 ERR("outdata is NULL");
829                 /* LCOV_EXCL_STOP */
830         }
831
832         goto DATA_FREE;
833
834 ERROR_RETURN:
835         if (outdata) {
836                 *outdata = pims_ipc_data_create(0);
837                 if (NULL == *outdata) {
838                         /* LCOV_EXCL_START */
839                         ERR("pims_ipc_data_create() Fail");
840                         goto DATA_FREE;
841                         /* LCOV_EXCL_STOP */
842                 }
843                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
844                         /* LCOV_EXCL_START */
845                         pims_ipc_data_destroy(*outdata);
846                         *outdata = NULL;
847                         ERR("ctsvc_ipc_marshal_int() Fail");
848                         goto DATA_FREE;
849                         /* LCOV_EXCL_STOP */
850                 }
851         } else {
852                 /* LCOV_EXCL_START */
853                 ERR("outdata is NULL");
854                 /* LCOV_EXCL_STOP */
855         }
856 DATA_FREE:
857         ctsvc_handle_destroy(contact);
858         contacts_list_destroy(list, true);
859         free(view_uri);
860         ctsvc_server_start_timeout();
861         return;
862 }
863
864 void ctsvc_ipc_server_db_get_records_with_query(pims_ipc_h ipc, pims_ipc_data_h indata,
865                 pims_ipc_data_h *outdata, void *userdata)
866 {
867         int ret = CONTACTS_ERROR_NONE;
868         contacts_query_h query = NULL;
869         int offset = 0;
870         int limit = 0;
871         contacts_list_h list = NULL;
872         contacts_h contact = NULL;
873
874         if (indata) {
875                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
876                 if (CONTACTS_ERROR_NONE != ret) {
877                         /* LCOV_EXCL_START */
878                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
879                         goto ERROR_RETURN;
880                         /* LCOV_EXCL_STOP */
881                 }
882
883                 ret = ctsvc_ipc_unmarshal_query(indata, &query);
884                 if (ret != CONTACTS_ERROR_NONE) {
885                         /* LCOV_EXCL_START */
886                         ERR("ctsvc_ipc_unmarshal_record() Fail");
887                         goto ERROR_RETURN;
888                         /* LCOV_EXCL_STOP */
889                 }
890                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
891                 if (ret != CONTACTS_ERROR_NONE) {
892                         /* LCOV_EXCL_START */
893                         ERR("ctsvc_ipc_unmarshal_int() Fail");
894                         goto ERROR_RETURN;
895                         /* LCOV_EXCL_STOP */
896                 }
897                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
898                 if (ret != CONTACTS_ERROR_NONE) {
899                         /* LCOV_EXCL_START */
900                         ERR("ctsvc_ipc_unmarshal_int() Fail");
901                         goto ERROR_RETURN;
902                         /* LCOV_EXCL_STOP */
903                 }
904         } else {
905                 /* LCOV_EXCL_START */
906                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
907                 goto ERROR_RETURN;
908                 /* LCOV_EXCL_STOP */
909         }
910
911         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(((ctsvc_query_s*)query)->view_uri))) {
912                 /* LCOV_EXCL_START */
913                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
914                 goto ERROR_RETURN;
915                 /* LCOV_EXCL_STOP */
916         }
917
918         ret = ctsvc_db_get_records_with_query(query, offset, limit, &list);
919
920         if (outdata) {
921                 *outdata = pims_ipc_data_create(0);
922                 if (NULL == *outdata) {
923                         /* LCOV_EXCL_START */
924                         ERR("pims_ipc_data_create() Fail");
925                         goto DATA_FREE;
926                         /* LCOV_EXCL_STOP */
927                 }
928                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
929                         /* LCOV_EXCL_START */
930                         pims_ipc_data_destroy(*outdata);
931                         *outdata = NULL;
932                         ERR("ctsvc_ipc_marshal_int() Fail");
933                         goto DATA_FREE;
934                         /* LCOV_EXCL_STOP */
935                 }
936
937                 if (CONTACTS_ERROR_NO_DATA == ret) {
938                         DBG("no data");
939                 } else if (CONTACTS_ERROR_NONE == ret) {
940                         ret = ctsvc_ipc_marshal_list(list, *outdata);
941
942                         if (ret != CONTACTS_ERROR_NONE) {
943                                 /* LCOV_EXCL_START */
944                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
945                                 goto DATA_FREE;
946                                 /* LCOV_EXCL_STOP */
947                         }
948                 }
949         } else {
950                 /* LCOV_EXCL_START */
951                 ERR("outdata is NULL");
952                 /* LCOV_EXCL_STOP */
953         }
954         goto DATA_FREE;
955
956 ERROR_RETURN:
957         if (outdata) {
958                 *outdata = pims_ipc_data_create(0);
959                 if (NULL == *outdata) {
960                         /* LCOV_EXCL_START */
961                         ERR("pims_ipc_data_create() Fail");
962                         goto DATA_FREE;
963                         /* LCOV_EXCL_STOP */
964                 }
965                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
966                         /* LCOV_EXCL_START */
967                         pims_ipc_data_destroy(*outdata);
968                         *outdata = NULL;
969                         ERR("ctsvc_ipc_marshal_int() Fail");
970                         goto DATA_FREE;
971                         /* LCOV_EXCL_STOP */
972                 }
973         } else {
974                 /* LCOV_EXCL_START */
975                 ERR("outdata is NULL");
976                 /* LCOV_EXCL_STOP */
977         }
978 DATA_FREE:
979         ctsvc_handle_destroy(contact);
980         contacts_list_destroy(list, true);
981         contacts_query_destroy(query);
982         ctsvc_server_start_timeout();
983         return;
984 }
985
986
987 void ctsvc_ipc_server_db_get_count(pims_ipc_h ipc, pims_ipc_data_h indata,
988                 pims_ipc_data_h *outdata, void *userdata)
989 {
990         int ret = CONTACTS_ERROR_NONE;
991         char *view_uri = NULL;
992         int count = 0;
993         contacts_h contact = NULL;
994
995         if (indata) {
996                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
997                 if (CONTACTS_ERROR_NONE != ret) {
998                         /* LCOV_EXCL_START */
999                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1000                         goto ERROR_RETURN;
1001                         /* LCOV_EXCL_STOP */
1002                 }
1003
1004                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
1005                 if (ret != CONTACTS_ERROR_NONE) {
1006                         /* LCOV_EXCL_START */
1007                         ERR("ctsvc_ipc_unmarshal_record() Fail");
1008                         goto ERROR_RETURN;
1009                         /* LCOV_EXCL_STOP */
1010                 }
1011         } else {
1012                 /* LCOV_EXCL_START */
1013                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
1014                 goto ERROR_RETURN;
1015                 /* LCOV_EXCL_STOP */
1016         }
1017
1018         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
1019                 /* LCOV_EXCL_START */
1020                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1021                 goto ERROR_RETURN;
1022                 /* LCOV_EXCL_STOP */
1023         }
1024
1025         ret = ctsvc_db_get_count(view_uri, &count);
1026
1027         if (outdata) {
1028                 *outdata = pims_ipc_data_create(0);
1029                 if (NULL == *outdata) {
1030                         /* LCOV_EXCL_START */
1031                         ERR("pims_ipc_data_create() Fail");
1032                         goto DATA_FREE;
1033                         /* LCOV_EXCL_STOP */
1034                 }
1035                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1036                         /* LCOV_EXCL_START */
1037                         pims_ipc_data_destroy(*outdata);
1038                         *outdata = NULL;
1039                         ERR("ctsvc_ipc_marshal_int() Fail");
1040                         goto DATA_FREE;
1041                         /* LCOV_EXCL_STOP */
1042                 }
1043
1044                 if (CONTACTS_ERROR_NO_DATA == ret) {
1045                         DBG("no data");
1046                 } else if (CONTACTS_ERROR_NONE == ret) {
1047                         ret = ctsvc_ipc_marshal_int(count, *outdata);
1048
1049                         if (ret != CONTACTS_ERROR_NONE) {
1050                                 /* LCOV_EXCL_START */
1051                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
1052                                 goto DATA_FREE;
1053                                 /* LCOV_EXCL_STOP */
1054                         }
1055                 }
1056         } else {
1057                 /* LCOV_EXCL_START */
1058                 ERR("outdata is NULL");
1059                 /* LCOV_EXCL_STOP */
1060         }
1061         goto DATA_FREE;
1062
1063 ERROR_RETURN:
1064         if (outdata) {
1065                 *outdata = pims_ipc_data_create(0);
1066                 if (NULL == *outdata) {
1067                         /* LCOV_EXCL_START */
1068                         ERR("pims_ipc_data_create() Fail");
1069                         goto DATA_FREE;
1070                         /* LCOV_EXCL_STOP */
1071                 }
1072                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1073                         /* LCOV_EXCL_START */
1074                         pims_ipc_data_destroy(*outdata);
1075                         *outdata = NULL;
1076                         ERR("ctsvc_ipc_marshal_int() Fail");
1077                         goto DATA_FREE;
1078                         /* LCOV_EXCL_STOP */
1079                 }
1080         } else {
1081                 /* LCOV_EXCL_START */
1082                 ERR("outdata is NULL");
1083                 /* LCOV_EXCL_STOP */
1084         }
1085 DATA_FREE:
1086         ctsvc_handle_destroy(contact);
1087         free(view_uri);
1088         ctsvc_server_start_timeout();
1089         return;
1090 }
1091
1092 void ctsvc_ipc_server_db_get_count_with_query(pims_ipc_h ipc, pims_ipc_data_h indata,
1093                 pims_ipc_data_h *outdata, void *userdata)
1094 {
1095         int ret = CONTACTS_ERROR_NONE;
1096         contacts_query_h query = NULL;
1097         int count = 0;
1098         contacts_h contact = NULL;
1099
1100         if (indata) {
1101                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1102                 if (CONTACTS_ERROR_NONE != ret) {
1103                         /* LCOV_EXCL_START */
1104                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1105                         goto ERROR_RETURN;
1106                         /* LCOV_EXCL_STOP */
1107                 }
1108
1109                 ret = ctsvc_ipc_unmarshal_query(indata, &query);
1110                 if (ret != CONTACTS_ERROR_NONE) {
1111                         /* LCOV_EXCL_START */
1112                         ERR("ctsvc_ipc_unmarshal_record() Fail");
1113                         goto ERROR_RETURN;
1114                         /* LCOV_EXCL_STOP */
1115                 }
1116         } else {
1117                 /* LCOV_EXCL_START */
1118                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
1119                 goto ERROR_RETURN;
1120                 /* LCOV_EXCL_STOP */
1121         }
1122
1123         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(((ctsvc_query_s*)query)->view_uri))) {
1124                 /* LCOV_EXCL_START */
1125                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1126                 goto ERROR_RETURN;
1127                 /* LCOV_EXCL_STOP */
1128         }
1129
1130         ret = ctsvc_db_get_count_with_query(query, &count);
1131
1132         if (outdata) {
1133                 *outdata = pims_ipc_data_create(0);
1134                 if (NULL == *outdata) {
1135                         /* LCOV_EXCL_START */
1136                         ERR("pims_ipc_data_create() Fail");
1137                         goto DATA_FREE;
1138                         /* LCOV_EXCL_STOP */
1139                 }
1140                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1141                         /* LCOV_EXCL_START */
1142                         pims_ipc_data_destroy(*outdata);
1143                         *outdata = NULL;
1144                         ERR("ctsvc_ipc_marshal_int() Fail");
1145                         goto DATA_FREE;
1146                         /* LCOV_EXCL_STOP */
1147                 }
1148
1149                 if (CONTACTS_ERROR_NO_DATA == ret) {
1150                         DBG("no data");
1151                 } else if (CONTACTS_ERROR_NONE == ret) {
1152                         ret = ctsvc_ipc_marshal_int(count, *outdata);
1153
1154                         if (ret != CONTACTS_ERROR_NONE) {
1155                                 /* LCOV_EXCL_START */
1156                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
1157                                 goto DATA_FREE;
1158                                 /* LCOV_EXCL_STOP */
1159                         }
1160                 }
1161         } else {
1162                 /* LCOV_EXCL_START */
1163                 ERR("outdata is NULL");
1164                 /* LCOV_EXCL_STOP */
1165         }
1166         goto DATA_FREE;
1167
1168 ERROR_RETURN:
1169         if (outdata) {
1170                 *outdata = pims_ipc_data_create(0);
1171                 if (NULL == *outdata) {
1172                         /* LCOV_EXCL_START */
1173                         ERR("pims_ipc_data_create() Fail");
1174                         goto DATA_FREE;
1175                         /* LCOV_EXCL_STOP */
1176                 }
1177                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1178                         /* LCOV_EXCL_START */
1179                         pims_ipc_data_destroy(*outdata);
1180                         *outdata = NULL;
1181                         ERR("ctsvc_ipc_marshal_int() Fail");
1182                         goto DATA_FREE;
1183                         /* LCOV_EXCL_STOP */
1184                 }
1185         } else {
1186                 /* LCOV_EXCL_START */
1187                 ERR("outdata is NULL");
1188                 /* LCOV_EXCL_STOP */
1189         }
1190 DATA_FREE:
1191         ctsvc_handle_destroy(contact);
1192         contacts_query_destroy(query);
1193         ctsvc_server_start_timeout();
1194         return;
1195 }
1196
1197 void ctsvc_ipc_server_db_insert_records(pims_ipc_h ipc, pims_ipc_data_h indata,
1198                 pims_ipc_data_h *outdata, void *userdata)
1199 {
1200         int ret = CONTACTS_ERROR_NONE;
1201         contacts_list_h list = NULL;
1202         int id_count = 0;
1203         int *ids = NULL;
1204         int i = 0;
1205         contacts_h contact = NULL;
1206
1207         if (indata) {
1208                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1209                 if (CONTACTS_ERROR_NONE != ret) {
1210                         /* LCOV_EXCL_START */
1211                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1212                         goto ERROR_RETURN;
1213                         /* LCOV_EXCL_STOP */
1214                 }
1215
1216                 ret = ctsvc_ipc_unmarshal_list(indata, &list);
1217                 if (ret != CONTACTS_ERROR_NONE) {
1218                         /* LCOV_EXCL_START */
1219                         ERR("ctsvc_ipc_unmarshal_list Fail");
1220                         goto ERROR_RETURN;
1221                         /* LCOV_EXCL_STOP */
1222                 }
1223         } else {
1224                 /* LCOV_EXCL_START */
1225                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
1226                 goto ERROR_RETURN;
1227                 /* LCOV_EXCL_STOP */
1228         }
1229
1230         if (list) {
1231                 contacts_record_h record = NULL;
1232                 contacts_list_first(list);
1233                 do {
1234                         ret = contacts_list_get_current_record_p(list, &record);
1235                         if (CONTACTS_ERROR_NONE != ret) {
1236                                 /* LCOV_EXCL_START */
1237                                 ERR("contacts_list_get_current_record_p() Fail(%d)", ret);
1238                                 goto ERROR_RETURN;
1239                                 /* LCOV_EXCL_STOP */
1240                         }
1241
1242                         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(((ctsvc_record_s*)record)->view_uri))) {
1243                                 /* LCOV_EXCL_START */
1244                                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1245                                 goto ERROR_RETURN;
1246                                 /* LCOV_EXCL_STOP */
1247                         }
1248                 } while (CONTACTS_ERROR_NONE == contacts_list_next(list));
1249                 contacts_list_first(list);
1250         }
1251
1252         ctsvc_server_stop_timeout();
1253         ret = ctsvc_db_insert_records(list, &ids, &id_count);
1254
1255         if (outdata) {
1256                 *outdata = pims_ipc_data_create(0);
1257                 if (NULL == *outdata) {
1258                         /* LCOV_EXCL_START */
1259                         ERR("pims_ipc_data_create() Fail");
1260                         goto DATA_FREE;
1261                         /* LCOV_EXCL_STOP */
1262                 }
1263                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1264                         /* LCOV_EXCL_START */
1265                         pims_ipc_data_destroy(*outdata);
1266                         *outdata = NULL;
1267                         ERR("ctsvc_ipc_marshal_int() Fail");
1268                         goto DATA_FREE;
1269                         /* LCOV_EXCL_STOP */
1270                 }
1271
1272                 if (ret == CONTACTS_ERROR_NONE) {
1273                         int transaction_ver = ctsvc_get_transaction_ver();
1274                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
1275                                 /* LCOV_EXCL_START */
1276                                 pims_ipc_data_destroy(*outdata);
1277                                 *outdata = NULL;
1278                                 ERR("ctsvc_ipc_marshal_int() Fail");
1279                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1280                                 goto ERROR_RETURN;
1281                                 /* LCOV_EXCL_STOP */
1282                         }
1283                         /* marshal : id_count+property_id+[ids]*id_count */
1284                         /* id_count */
1285                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(id_count, *outdata)) {
1286                                 /* LCOV_EXCL_START */
1287                                 pims_ipc_data_destroy(*outdata);
1288                                 *outdata = NULL;
1289                                 ERR("ctsvc_ipc_marshal_int() Fail");
1290                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1291                                 goto ERROR_RETURN;
1292                                 /* LCOV_EXCL_STOP */
1293                         }
1294
1295                         for (i = 0; i < id_count; i++) {
1296                                 /* marshal ids */
1297                                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ids[i], *outdata)) {
1298                                         /* LCOV_EXCL_START */
1299                                         pims_ipc_data_destroy(*outdata);
1300                                         *outdata = NULL;
1301                                         ERR("ctsvc_ipc_marshal_int() Fail");
1302                                         ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1303                                         goto ERROR_RETURN;
1304                                         /* LCOV_EXCL_STOP */
1305                                 }
1306                         }
1307                 }
1308         } else {
1309                 /* LCOV_EXCL_START */
1310                 ERR("outdata is NULL");
1311                 /* LCOV_EXCL_STOP */
1312         }
1313         goto DATA_FREE;
1314
1315 ERROR_RETURN:
1316         if (outdata) {
1317                 *outdata = pims_ipc_data_create(0);
1318                 if (NULL == *outdata) {
1319                         /* LCOV_EXCL_START */
1320                         ERR("pims_ipc_data_create() Fail");
1321                         goto DATA_FREE;
1322                         /* LCOV_EXCL_STOP */
1323                 }
1324                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1325                         /* LCOV_EXCL_START */
1326                         pims_ipc_data_destroy(*outdata);
1327                         *outdata = NULL;
1328                         ERR("ctsvc_ipc_marshal_int() Fail");
1329                         goto DATA_FREE;
1330                         /* LCOV_EXCL_STOP */
1331                 }
1332         } else {
1333                 /* LCOV_EXCL_START */
1334                 ERR("outdata is NULL");
1335                 /* LCOV_EXCL_STOP */
1336         }
1337 DATA_FREE:
1338         ctsvc_handle_destroy(contact);
1339         contacts_list_destroy(list, true);
1340         free(ids);
1341         ctsvc_server_start_timeout();
1342         return;
1343 }
1344
1345 void ctsvc_ipc_server_db_update_records(pims_ipc_h ipc, pims_ipc_data_h indata,
1346                 pims_ipc_data_h *outdata, void *userdata)
1347 {
1348         int ret = CONTACTS_ERROR_NONE;
1349         contacts_list_h list = NULL;
1350         contacts_h contact = NULL;
1351
1352         if (indata) {
1353                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1354                 if (CONTACTS_ERROR_NONE != ret) {
1355                         /* LCOV_EXCL_START */
1356                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1357                         goto ERROR_RETURN;
1358                         /* LCOV_EXCL_STOP */
1359                 }
1360
1361                 ret = ctsvc_ipc_unmarshal_list(indata, &list);
1362                 if (ret != CONTACTS_ERROR_NONE) {
1363                         /* LCOV_EXCL_START */
1364                         ERR("ctsvc_ipc_unmarshal_list Fail");
1365                         goto ERROR_RETURN;
1366                         /* LCOV_EXCL_STOP */
1367                 }
1368         } else {
1369                 /* LCOV_EXCL_START */
1370                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
1371                 goto ERROR_RETURN;
1372                 /* LCOV_EXCL_STOP */
1373         }
1374
1375         if (list) {
1376                 contacts_record_h record = NULL;
1377                 contacts_list_first(list);
1378                 do {
1379                         ret = contacts_list_get_current_record_p(list, &record);
1380                         if (CONTACTS_ERROR_NONE != ret) {
1381                                 /* LCOV_EXCL_START */
1382                                 ERR("contacts_list_get_current_record_p() Fail(%d)", ret);
1383                                 goto ERROR_RETURN;
1384                                 /* LCOV_EXCL_STOP */
1385                         }
1386
1387                         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(((ctsvc_record_s*)record)->view_uri))) {
1388                                 /* LCOV_EXCL_START */
1389                                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1390                                 goto ERROR_RETURN;
1391                                 /* LCOV_EXCL_STOP */
1392                         }
1393                 } while (CONTACTS_ERROR_NONE == contacts_list_next(list));
1394                 contacts_list_first(list);
1395         }
1396
1397         ctsvc_server_stop_timeout();
1398         ret = ctsvc_db_update_records(list);
1399
1400         if (outdata) {
1401                 *outdata = pims_ipc_data_create(0);
1402                 if (NULL == *outdata) {
1403                         /* LCOV_EXCL_START */
1404                         ERR("pims_ipc_data_create() Fail");
1405                         goto DATA_FREE;
1406                         /* LCOV_EXCL_STOP */
1407                 }
1408                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1409                         /* LCOV_EXCL_START */
1410                         pims_ipc_data_destroy(*outdata);
1411                         *outdata = NULL;
1412                         ERR("ctsvc_ipc_marshal_int() Fail");
1413                         goto DATA_FREE;
1414                         /* LCOV_EXCL_STOP */
1415                 }
1416
1417                 if (ret == CONTACTS_ERROR_NONE) {
1418                         int transaction_ver = ctsvc_get_transaction_ver();
1419                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
1420                                 /* LCOV_EXCL_START */
1421                                 pims_ipc_data_destroy(*outdata);
1422                                 *outdata = NULL;
1423                                 ERR("ctsvc_ipc_marshal_int() Fail");
1424                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1425                                 goto ERROR_RETURN;
1426                                 /* LCOV_EXCL_STOP */
1427                         }
1428                 }
1429         } else {
1430                 /* LCOV_EXCL_START */
1431                 ERR("outdata is NULL");
1432                 /* LCOV_EXCL_STOP */
1433         }
1434         goto DATA_FREE;
1435
1436 ERROR_RETURN:
1437         if (outdata) {
1438                 *outdata = pims_ipc_data_create(0);
1439                 if (NULL == *outdata) {
1440                         /* LCOV_EXCL_START */
1441                         ERR("pims_ipc_data_create() Fail");
1442                         goto DATA_FREE;
1443                         /* LCOV_EXCL_STOP */
1444                 }
1445                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1446                         /* LCOV_EXCL_START */
1447                         pims_ipc_data_destroy(*outdata);
1448                         *outdata = NULL;
1449                         ERR("ctsvc_ipc_marshal_int() Fail");
1450                         goto DATA_FREE;
1451                         /* LCOV_EXCL_STOP */
1452                 }
1453         } else {
1454                 /* LCOV_EXCL_START */
1455                 ERR("outdata is NULL");
1456                 /* LCOV_EXCL_STOP */
1457         }
1458 DATA_FREE:
1459         ctsvc_handle_destroy(contact);
1460         contacts_list_destroy(list, true);
1461         ctsvc_server_start_timeout();
1462         return;
1463 }
1464
1465 void ctsvc_ipc_server_db_delete_records(pims_ipc_h ipc, pims_ipc_data_h indata,
1466                 pims_ipc_data_h *outdata, void *userdata)
1467 {
1468         int ret = CONTACTS_ERROR_NONE;
1469         int count = 0;
1470         int *ids = NULL;
1471         char *uri = NULL;
1472         int i = 0;
1473         contacts_h contact = NULL;
1474
1475         if (indata) {
1476                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1477                 if (CONTACTS_ERROR_NONE != ret) {
1478                         /* LCOV_EXCL_START */
1479                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1480                         goto ERROR_RETURN;
1481                         /* LCOV_EXCL_STOP */
1482                 }
1483
1484                 ret = ctsvc_ipc_unmarshal_string(indata, &uri);
1485                 if (ret != CONTACTS_ERROR_NONE) {
1486                         /* LCOV_EXCL_START */
1487                         ERR("ctsvc_ipc_unmarshal_string Fail");
1488                         goto ERROR_RETURN;
1489                         /* LCOV_EXCL_STOP */
1490                 }
1491                 ret = ctsvc_ipc_unmarshal_int(indata, &count);
1492                 if (ret != CONTACTS_ERROR_NONE) {
1493                         /* LCOV_EXCL_START */
1494                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1495                         goto ERROR_RETURN;
1496                         /* LCOV_EXCL_STOP */
1497                 }
1498                 if (count <= 0) {
1499                         /* LCOV_EXCL_START */
1500                         ret = CONTACTS_ERROR_INVALID_PARAMETER;
1501                         goto ERROR_RETURN;
1502                         /* LCOV_EXCL_STOP */
1503                 }
1504                 ids = (int*)malloc(sizeof(int)*count);
1505                 if (ids == NULL) {
1506                         ERR("malloc failed()");
1507                         return;
1508                 }
1509                 for (i = 0; i < count; i++) {
1510                         ret = ctsvc_ipc_unmarshal_int(indata, &ids[i]);
1511                         if (ret != CONTACTS_ERROR_NONE) {
1512                                 /* LCOV_EXCL_START */
1513                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
1514                                 goto ERROR_RETURN;
1515                                 /* LCOV_EXCL_STOP */
1516                         }
1517                 }
1518         } else {
1519                 /* LCOV_EXCL_START */
1520                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
1521                 goto ERROR_RETURN;
1522                 /* LCOV_EXCL_STOP */
1523         }
1524
1525         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(uri))) {
1526                 /* LCOV_EXCL_START */
1527                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1528                 goto ERROR_RETURN;
1529                 /* LCOV_EXCL_STOP */
1530         }
1531
1532         ret = ctsvc_db_delete_records(uri, ids, count);
1533
1534         if (outdata) {
1535                 *outdata = pims_ipc_data_create(0);
1536                 if (NULL == *outdata) {
1537                         /* LCOV_EXCL_START */
1538                         ERR("pims_ipc_data_create() Fail");
1539                         goto DATA_FREE;
1540                         /* LCOV_EXCL_STOP */
1541                 }
1542
1543                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1544                         /* LCOV_EXCL_START */
1545                         pims_ipc_data_destroy(*outdata);
1546                         *outdata = NULL;
1547                         ERR("ctsvc_ipc_marshal_int() Fail");
1548                         goto DATA_FREE;
1549                         /* LCOV_EXCL_STOP */
1550                 }
1551
1552                 if (ret == CONTACTS_ERROR_NONE) {
1553                         int transaction_ver = ctsvc_get_transaction_ver();
1554                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
1555                                 /* LCOV_EXCL_START */
1556                                 pims_ipc_data_destroy(*outdata);
1557                                 *outdata = NULL;
1558                                 ERR("ctsvc_ipc_marshal_int() Fail");
1559                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1560                                 goto ERROR_RETURN;
1561                                 /* LCOV_EXCL_STOP */
1562                         }
1563                 }
1564         } else {
1565                 /* LCOV_EXCL_START */
1566                 ERR("outdata is NULL");
1567                 /* LCOV_EXCL_STOP */
1568         }
1569
1570         goto DATA_FREE;
1571
1572 ERROR_RETURN:
1573         if (outdata) {
1574                 *outdata = pims_ipc_data_create(0);
1575                 if (NULL == *outdata) {
1576                         /* LCOV_EXCL_START */
1577                         ERR("pims_ipc_data_create() Fail");
1578                         goto DATA_FREE;
1579                         /* LCOV_EXCL_STOP */
1580                 }
1581                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1582                         /* LCOV_EXCL_START */
1583                         pims_ipc_data_destroy(*outdata);
1584                         *outdata = NULL;
1585                         ERR("ctsvc_ipc_marshal_int() Fail");
1586                         goto DATA_FREE;
1587                         /* LCOV_EXCL_STOP */
1588                 }
1589         } else {
1590                 /* LCOV_EXCL_START */
1591                 ERR("outdata is NULL");
1592                 /* LCOV_EXCL_STOP */
1593         }
1594 DATA_FREE:
1595         ctsvc_handle_destroy(contact);
1596         free(uri);
1597         free(ids);
1598         ctsvc_server_start_timeout();
1599         return;
1600 }
1601
1602 void ctsvc_ipc_server_db_replace_records(pims_ipc_h ipc, pims_ipc_data_h indata,
1603                 pims_ipc_data_h *outdata, void *userdata)
1604 {
1605         int ret = CONTACTS_ERROR_NONE;
1606         contacts_list_h list = NULL;
1607         int count = 0;
1608         int *ids = NULL;
1609         int i = 0;
1610         contacts_h contact = NULL;
1611
1612         if (indata) {
1613                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1614                 if (CONTACTS_ERROR_NONE != ret) {
1615                         /* LCOV_EXCL_START */
1616                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1617                         goto ERROR_RETURN;
1618                         /* LCOV_EXCL_STOP */
1619                 }
1620
1621                 ret = ctsvc_ipc_unmarshal_list(indata, &list);
1622                 if (ret != CONTACTS_ERROR_NONE) {
1623                         /* LCOV_EXCL_START */
1624                         ERR("ctsvc_ipc_unmarshal_list Fail");
1625                         goto ERROR_RETURN;
1626                         /* LCOV_EXCL_STOP */
1627                 }
1628
1629                 ret = ctsvc_ipc_unmarshal_int(indata, &count);
1630                 if (ret != CONTACTS_ERROR_NONE) {
1631                         /* LCOV_EXCL_START */
1632                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1633                         goto ERROR_RETURN;
1634                         /* LCOV_EXCL_STOP */
1635                 }
1636
1637                 if (count <= 0) {
1638                         /* LCOV_EXCL_START */
1639                         ret = CONTACTS_ERROR_INVALID_PARAMETER;
1640                         goto ERROR_RETURN;
1641                         /* LCOV_EXCL_STOP */
1642                 }
1643
1644                 ids = (int*)malloc(sizeof(int)*count);
1645                 if (ids == NULL) {
1646                         ERR("malloc() failed");
1647                         return;
1648                 }
1649                 for (i = 0; i < count; i++) {
1650                         ret = ctsvc_ipc_unmarshal_int(indata, &ids[i]);
1651                         if (ret != CONTACTS_ERROR_NONE) {
1652                                 /* LCOV_EXCL_START */
1653                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
1654                                 goto ERROR_RETURN;
1655                                 /* LCOV_EXCL_STOP */
1656                         }
1657                 }
1658         } else {
1659                 /* LCOV_EXCL_START */
1660                 ERR("ctsvc_ipc_server_db_repalce_records Fail");
1661                 goto ERROR_RETURN;
1662                 /* LCOV_EXCL_STOP */
1663         }
1664
1665         if (list) {
1666                 contacts_record_h record = NULL;
1667                 contacts_list_first(list);
1668                 do {
1669                         ret = contacts_list_get_current_record_p(list, &record);
1670                         if (CONTACTS_ERROR_NONE != ret) {
1671                                 /* LCOV_EXCL_START */
1672                                 ERR("contacts_list_get_current_record_p() Fail(%d)", ret);
1673                                 goto ERROR_RETURN;
1674                                 /* LCOV_EXCL_STOP */
1675                         }
1676
1677                         if (!ctsvc_have_permission(ipc, ctsvc_required_write_permission(((ctsvc_record_s*)record)->view_uri))) {
1678                                 /* LCOV_EXCL_START */
1679                                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1680                                 goto ERROR_RETURN;
1681                                 /* LCOV_EXCL_STOP */
1682                         }
1683                 } while (CONTACTS_ERROR_NONE == contacts_list_next(list));
1684                 contacts_list_first(list);
1685         }
1686
1687         ctsvc_server_stop_timeout();
1688         ret = ctsvc_db_replace_records(list, ids, count);
1689
1690         if (outdata) {
1691                 *outdata = pims_ipc_data_create(0);
1692                 if (NULL == *outdata) {
1693                         /* LCOV_EXCL_START */
1694                         ERR("pims_ipc_data_create() Fail");
1695                         goto DATA_FREE;
1696                         /* LCOV_EXCL_STOP */
1697                 }
1698                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1699                         /* LCOV_EXCL_START */
1700                         pims_ipc_data_destroy(*outdata);
1701                         *outdata = NULL;
1702                         ERR("ctsvc_ipc_marshal_int() Fail");
1703                         goto DATA_FREE;
1704                         /* LCOV_EXCL_STOP */
1705                 }
1706                 if (ret == CONTACTS_ERROR_NONE) {
1707                         int transaction_ver = ctsvc_get_transaction_ver();
1708                         if (ctsvc_ipc_marshal_int(transaction_ver, *outdata) != CONTACTS_ERROR_NONE) {
1709                                 /* LCOV_EXCL_START */
1710                                 pims_ipc_data_destroy(*outdata);
1711                                 *outdata = NULL;
1712                                 ERR("ctsvc_ipc_marshal_int() Fail");
1713                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1714                                 goto ERROR_RETURN;
1715                                 /* LCOV_EXCL_STOP */
1716                         }
1717                 }
1718         } else {
1719                 /* LCOV_EXCL_START */
1720                 ERR("outdata is NULL");
1721                 /* LCOV_EXCL_STOP */
1722         }
1723         goto DATA_FREE;
1724
1725 ERROR_RETURN:
1726         if (outdata) {
1727                 *outdata = pims_ipc_data_create(0);
1728                 if (NULL == *outdata) {
1729                         /* LCOV_EXCL_START */
1730                         ERR("pims_ipc_data_create() Fail");
1731                         goto DATA_FREE;
1732                         /* LCOV_EXCL_STOP */
1733                 }
1734                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1735                         /* LCOV_EXCL_START */
1736                         pims_ipc_data_destroy(*outdata);
1737                         *outdata = NULL;
1738                         ERR("ctsvc_ipc_marshal_int() Fail");
1739                         goto DATA_FREE;
1740                         /* LCOV_EXCL_STOP */
1741                 }
1742         } else {
1743                 /* LCOV_EXCL_START */
1744                 ERR("outdata is NULL");
1745                 /* LCOV_EXCL_STOP */
1746         }
1747 DATA_FREE:
1748         ctsvc_handle_destroy(contact);
1749         contacts_list_destroy(list, true);
1750         free(ids);
1751         ctsvc_server_start_timeout();
1752         return;
1753 }
1754
1755 void ctsvc_ipc_server_db_get_changes_by_version(pims_ipc_h ipc, pims_ipc_data_h indata,
1756                 pims_ipc_data_h *outdata, void *userdata)
1757 {
1758         int ret = CONTACTS_ERROR_NONE;
1759         char *view_uri = NULL;
1760         int address_book_id = 0;
1761         int contacts_db_version = 0;
1762         contacts_list_h record_list = NULL;
1763         int current_contacts_db_version = 0;
1764         contacts_h contact = NULL;
1765
1766         if (indata) {
1767                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1768                 if (CONTACTS_ERROR_NONE != ret) {
1769                         /* LCOV_EXCL_START */
1770                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1771                         goto ERROR_RETURN;
1772                         /* LCOV_EXCL_STOP */
1773                 }
1774
1775                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
1776                 if (ret != CONTACTS_ERROR_NONE) {
1777                         /* LCOV_EXCL_START */
1778                         ERR("ctsvc_ipc_unmarshal_string Fail");
1779                         goto ERROR_RETURN;
1780                         /* LCOV_EXCL_STOP */
1781                 }
1782                 ret = ctsvc_ipc_unmarshal_int(indata, &address_book_id);
1783                 if (ret != CONTACTS_ERROR_NONE) {
1784                         /* LCOV_EXCL_START */
1785                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1786                         goto ERROR_RETURN;
1787                         /* LCOV_EXCL_STOP */
1788                 }
1789                 ret = ctsvc_ipc_unmarshal_int(indata, &contacts_db_version);
1790                 if (ret != CONTACTS_ERROR_NONE) {
1791                         /* LCOV_EXCL_START */
1792                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1793                         goto ERROR_RETURN;
1794                         /* LCOV_EXCL_STOP */
1795                 }
1796         } else {
1797                 /* LCOV_EXCL_START */
1798                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
1799                 goto ERROR_RETURN;
1800                 /* LCOV_EXCL_STOP */
1801         }
1802
1803         if (!ctsvc_have_permission(ipc, CTSVC_PERMISSION_CONTACT_READ)) {
1804                 /* LCOV_EXCL_START */
1805                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1806                 goto ERROR_RETURN;
1807                 /* LCOV_EXCL_STOP */
1808         }
1809
1810         ret = ctsvc_db_get_changes_by_version(view_uri, address_book_id, contacts_db_version,
1811                         &record_list, &current_contacts_db_version);
1812
1813         if (outdata) {
1814                 *outdata = pims_ipc_data_create(0);
1815                 if (NULL == *outdata) {
1816                         /* LCOV_EXCL_START */
1817                         ERR("pims_ipc_data_create() Fail");
1818                         goto DATA_FREE;
1819                         /* LCOV_EXCL_STOP */
1820                 }
1821                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1822                         /* LCOV_EXCL_START */
1823                         pims_ipc_data_destroy(*outdata);
1824                         *outdata = NULL;
1825                         ERR("ctsvc_ipc_marshal_int() Fail");
1826                         goto DATA_FREE;
1827                         /* LCOV_EXCL_STOP */
1828                 }
1829
1830                 if (CONTACTS_ERROR_NO_DATA == ret) {
1831                         DBG("no data");
1832                 } else if (CONTACTS_ERROR_NONE == ret) {
1833                         ret = ctsvc_ipc_marshal_list(record_list, *outdata);
1834                         if (ret != CONTACTS_ERROR_NONE) {
1835                                 /* LCOV_EXCL_START */
1836                                 ERR("ctsvc_ipc_marshal_list Fail");
1837                                 pims_ipc_data_destroy(*outdata);
1838                                 *outdata = NULL;
1839                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1840                                 goto ERROR_RETURN;
1841                                 /* LCOV_EXCL_STOP */
1842                         }
1843                         ret = ctsvc_ipc_marshal_int(current_contacts_db_version, *outdata);
1844                         if (ret != CONTACTS_ERROR_NONE) {
1845                                 /* LCOV_EXCL_START */
1846                                 ERR("ctsvc_ipc_marshal_int() Fail");
1847                                 pims_ipc_data_destroy(*outdata);
1848                                 *outdata = NULL;
1849                                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
1850                                 goto ERROR_RETURN;
1851                                 /* LCOV_EXCL_STOP */
1852                         }
1853                 }
1854         } else {
1855                 /* LCOV_EXCL_START */
1856                 ERR("outdata is NULL");
1857                 /* LCOV_EXCL_STOP */
1858         }
1859         goto DATA_FREE;
1860
1861 ERROR_RETURN:
1862         if (outdata) {
1863                 *outdata = pims_ipc_data_create(0);
1864                 if (NULL == *outdata) {
1865                         /* LCOV_EXCL_START */
1866                         ERR("pims_ipc_data_create() Fail");
1867                         goto DATA_FREE;
1868                         /* LCOV_EXCL_STOP */
1869                 }
1870                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1871                         /* LCOV_EXCL_START */
1872                         pims_ipc_data_destroy(*outdata);
1873                         *outdata = NULL;
1874                         ERR("ctsvc_ipc_marshal_int() Fail");
1875                         goto DATA_FREE;
1876                         /* LCOV_EXCL_STOP */
1877                 }
1878         } else {
1879                 /* LCOV_EXCL_START */
1880                 ERR("outdata is NULL");
1881                 /* LCOV_EXCL_STOP */
1882         }
1883 DATA_FREE:
1884         ctsvc_handle_destroy(contact);
1885         contacts_list_destroy(record_list, true);
1886         free(view_uri);
1887         ctsvc_server_start_timeout();
1888         return;
1889 }
1890
1891 void ctsvc_ipc_server_db_get_current_version(pims_ipc_h ipc, pims_ipc_data_h indata,
1892                 pims_ipc_data_h *outdata, void *userdata)
1893 {
1894         int ret = CONTACTS_ERROR_NONE;
1895         int contacts_db_version = 0;
1896         contacts_h contact = NULL;
1897
1898         if (indata) {
1899                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1900                 if (CONTACTS_ERROR_NONE != ret) {
1901                         /* LCOV_EXCL_START */
1902                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1903                         goto ERROR_RETURN;
1904                         /* LCOV_EXCL_STOP */
1905                 }
1906         }
1907
1908         if (!ctsvc_have_permission(ipc, CTSVC_PERMISSION_CONTACT_READ) &&
1909                         !ctsvc_have_permission(ipc, CTSVC_PERMISSION_PHONELOG_READ)) {
1910                 /* LCOV_EXCL_START */
1911                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
1912                 goto ERROR_RETURN;
1913                 /* LCOV_EXCL_STOP */
1914         }
1915
1916         ret = ctsvc_db_get_current_version(&contacts_db_version);
1917
1918 ERROR_RETURN:
1919         if (outdata) {
1920                 *outdata = pims_ipc_data_create(0);
1921                 if (NULL == *outdata) {
1922                         /* LCOV_EXCL_START */
1923                         ERR("pims_ipc_data_create() Fail");
1924                         goto DATA_FREE;
1925                         /* LCOV_EXCL_STOP */
1926                 }
1927                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
1928                         /* LCOV_EXCL_START */
1929                         pims_ipc_data_destroy(*outdata);
1930                         *outdata = NULL;
1931                         ERR("ctsvc_ipc_marshal_int() Fail");
1932                         goto DATA_FREE;
1933                         /* LCOV_EXCL_STOP */
1934                 }
1935
1936                 if (CONTACTS_ERROR_NO_DATA == ret) {
1937                         DBG("no data");
1938                 } else if (CONTACTS_ERROR_NONE == ret) {
1939                         ret = ctsvc_ipc_marshal_int(contacts_db_version, *outdata);
1940                         if (ret != CONTACTS_ERROR_NONE) {
1941                                 /* LCOV_EXCL_START */
1942                                 ERR("ctsvc_ipc_marshal_int() Fail");
1943                                 goto DATA_FREE;
1944                                 /* LCOV_EXCL_STOP */
1945                         }
1946                 }
1947         } else {
1948                 /* LCOV_EXCL_START */
1949                 ERR("outdata is NULL");
1950                 /* LCOV_EXCL_STOP */
1951         }
1952
1953 DATA_FREE:
1954         ctsvc_handle_destroy(contact);
1955         ctsvc_server_start_timeout();
1956         return;
1957 }
1958
1959 void ctsvc_ipc_server_db_search_records_for_snippet(pims_ipc_h ipc,
1960                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
1961 {
1962         int ret = CONTACTS_ERROR_NONE;
1963         char *view_uri = NULL;
1964         char *keyword = NULL;
1965         int offset = 0;
1966         int limit = 0;
1967         contacts_list_h list = NULL;
1968         contacts_h contact = NULL;
1969         char *start_match = NULL;
1970         char *end_match = NULL;
1971         int token_number = 0;
1972
1973         if (indata) {
1974                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
1975                 if (CONTACTS_ERROR_NONE != ret) {
1976                         /* LCOV_EXCL_START */
1977                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
1978                         goto ERROR_RETURN;
1979                         /* LCOV_EXCL_STOP */
1980                 }
1981                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
1982                 if (ret != CONTACTS_ERROR_NONE) {
1983                         /* LCOV_EXCL_START */
1984                         ERR("ctsvc_ipc_unmarshal_record() Fail(%d)", ret);
1985                         goto ERROR_RETURN;
1986                         /* LCOV_EXCL_STOP */
1987                 }
1988                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
1989                 if (ret != CONTACTS_ERROR_NONE) {
1990                         /* LCOV_EXCL_START */
1991                         ERR("ctsvc_ipc_unmarshal_record() Fail(%d)", ret);
1992                         goto ERROR_RETURN;
1993                         /* LCOV_EXCL_STOP */
1994                 }
1995                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
1996                 if (ret != CONTACTS_ERROR_NONE) {
1997                         /* LCOV_EXCL_START */
1998                         ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
1999                         goto ERROR_RETURN;
2000                         /* LCOV_EXCL_STOP */
2001                 }
2002                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
2003                 if (ret != CONTACTS_ERROR_NONE) {
2004                         /* LCOV_EXCL_START */
2005                         ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
2006                         goto ERROR_RETURN;
2007                         /* LCOV_EXCL_STOP */
2008                 }
2009                 ret = ctsvc_ipc_unmarshal_string(indata, &start_match);
2010                 if (ret != CONTACTS_ERROR_NONE) {
2011                         /* LCOV_EXCL_START */
2012                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2013                         goto ERROR_RETURN;
2014                         /* LCOV_EXCL_STOP */
2015                 }
2016                 ret = ctsvc_ipc_unmarshal_string(indata, &end_match);
2017                 if (ret != CONTACTS_ERROR_NONE) {
2018                         /* LCOV_EXCL_START */
2019                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2020                         goto ERROR_RETURN;
2021                         /* LCOV_EXCL_STOP */
2022                 }
2023                 ret = ctsvc_ipc_unmarshal_int(indata, &token_number);
2024                 if (ret != CONTACTS_ERROR_NONE) {
2025                         /* LCOV_EXCL_START */
2026                         ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
2027                         goto ERROR_RETURN;
2028                         /* LCOV_EXCL_STOP */
2029                 }
2030         } else {
2031                 /* LCOV_EXCL_START */
2032                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
2033                 goto ERROR_RETURN;
2034                 /* LCOV_EXCL_STOP */
2035         }
2036
2037         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
2038                 /* LCOV_EXCL_START */
2039                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
2040                 goto ERROR_RETURN;
2041                 /* LCOV_EXCL_STOP */
2042         }
2043
2044         ret = ctsvc_db_search_records_for_snippet(view_uri, keyword, offset, limit,
2045                         start_match, end_match, token_number, &list);
2046
2047         if (outdata) {
2048                 *outdata = pims_ipc_data_create(0);
2049                 if (NULL == *outdata) {
2050                         /* LCOV_EXCL_START */
2051                         ERR("pims_ipc_data_create() Fail");
2052                         goto DATA_FREE;
2053                         /* LCOV_EXCL_STOP */
2054                 }
2055                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2056                         /* LCOV_EXCL_START */
2057                         pims_ipc_data_destroy(*outdata);
2058                         *outdata = NULL;
2059                         ERR("ctsvc_ipc_marshal_int() Fail");
2060                         goto DATA_FREE;
2061                         /* LCOV_EXCL_STOP */
2062                 }
2063
2064                 if (CONTACTS_ERROR_NO_DATA == ret) {
2065                         DBG("no data");
2066                 } else if (CONTACTS_ERROR_NONE == ret) {
2067                         ret = ctsvc_ipc_marshal_list(list, *outdata);
2068
2069                         if (ret != CONTACTS_ERROR_NONE) {
2070                                 /* LCOV_EXCL_START */
2071                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
2072                                 goto DATA_FREE;
2073                                 /* LCOV_EXCL_STOP */
2074                         }
2075                 }
2076         } else {
2077                 /* LCOV_EXCL_START */
2078                 ERR("outdata is NULL");
2079                 /* LCOV_EXCL_STOP */
2080         }
2081         goto DATA_FREE;
2082
2083 ERROR_RETURN:
2084         if (outdata) {
2085                 *outdata = pims_ipc_data_create(0);
2086                 if (NULL == *outdata) {
2087                         /* LCOV_EXCL_START */
2088                         ERR("pims_ipc_data_create() Fail");
2089                         goto DATA_FREE;
2090                         /* LCOV_EXCL_STOP */
2091                 }
2092                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2093                         /* LCOV_EXCL_START */
2094                         pims_ipc_data_destroy(*outdata);
2095                         *outdata = NULL;
2096                         ERR("ctsvc_ipc_marshal_int() Fail");
2097                         goto DATA_FREE;
2098                         /* LCOV_EXCL_STOP */
2099                 }
2100         } else {
2101                 /* LCOV_EXCL_START */
2102                 ERR("outdata is NULL");
2103                 /* LCOV_EXCL_STOP */
2104         }
2105 DATA_FREE:
2106         ctsvc_handle_destroy(contact);
2107         contacts_list_destroy(list, true);
2108         free(view_uri);
2109         free(keyword);
2110         free(start_match);
2111         free(end_match);
2112         ctsvc_server_start_timeout();
2113         return;
2114 }
2115
2116 void ctsvc_ipc_server_db_search_records_with_range_for_snippet(pims_ipc_h ipc,
2117                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
2118 {
2119         int ret = CONTACTS_ERROR_NONE;
2120         char *view_uri = NULL;
2121         char *keyword = NULL;
2122         int offset = 0;
2123         int limit = 0;
2124         int range = 0;
2125         contacts_list_h list = NULL;
2126         contacts_h contact = NULL;
2127         char *start_match = NULL;
2128         char *end_match = NULL;
2129         int token_number = 0;
2130
2131         if (indata) {
2132                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
2133                 if (CONTACTS_ERROR_NONE != ret) {
2134                         /* LCOV_EXCL_START */
2135                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
2136                         goto ERROR_RETURN;
2137                         /* LCOV_EXCL_STOP */
2138                 }
2139                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
2140                 if (ret != CONTACTS_ERROR_NONE) {
2141                         /* LCOV_EXCL_START */
2142                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2143                         goto ERROR_RETURN;
2144                         /* LCOV_EXCL_STOP */
2145                 }
2146                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
2147                 if (ret != CONTACTS_ERROR_NONE) {
2148                         /* LCOV_EXCL_START */
2149                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2150                         goto ERROR_RETURN;
2151                         /* LCOV_EXCL_STOP */
2152                 }
2153                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
2154                 if (ret != CONTACTS_ERROR_NONE) {
2155                         /* LCOV_EXCL_START */
2156                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2157                         goto ERROR_RETURN;
2158                         /* LCOV_EXCL_STOP */
2159                 }
2160                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
2161                 if (ret != CONTACTS_ERROR_NONE) {
2162                         /* LCOV_EXCL_START */
2163                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2164                         goto ERROR_RETURN;
2165                         /* LCOV_EXCL_STOP */
2166                 }
2167                 ret = ctsvc_ipc_unmarshal_int(indata, &range);
2168                 if (ret != CONTACTS_ERROR_NONE) {
2169                         /* LCOV_EXCL_START */
2170                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2171                         goto ERROR_RETURN;
2172                         /* LCOV_EXCL_STOP */
2173                 }
2174                 ret = ctsvc_ipc_unmarshal_string(indata, &start_match);
2175                 if (ret != CONTACTS_ERROR_NONE) {
2176                         /* LCOV_EXCL_START */
2177                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2178                         goto ERROR_RETURN;
2179                         /* LCOV_EXCL_STOP */
2180                 }
2181                 ret = ctsvc_ipc_unmarshal_string(indata, &end_match);
2182                 if (ret != CONTACTS_ERROR_NONE) {
2183                         /* LCOV_EXCL_START */
2184                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2185                         goto ERROR_RETURN;
2186                         /* LCOV_EXCL_STOP */
2187                 }
2188                 ret = ctsvc_ipc_unmarshal_int(indata, &token_number);
2189                 if (ret != CONTACTS_ERROR_NONE) {
2190                         /* LCOV_EXCL_START */
2191                         ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
2192                         goto ERROR_RETURN;
2193                         /* LCOV_EXCL_STOP */
2194                 }
2195         } else {
2196                 /* LCOV_EXCL_START */
2197                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
2198                 goto ERROR_RETURN;
2199                 /* LCOV_EXCL_START */
2200         }
2201
2202         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
2203                 /* LCOV_EXCL_START */
2204                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
2205                 goto ERROR_RETURN;
2206                 /* LCOV_EXCL_STOP */
2207         }
2208
2209         ret = ctsvc_db_search_records_with_range_for_snippet(view_uri, keyword, offset,
2210                         limit, range, start_match, end_match, token_number, &list);
2211
2212         if (outdata) {
2213                 *outdata = pims_ipc_data_create(0);
2214                 if (NULL == *outdata) {
2215                         /* LCOV_EXCL_START */
2216                         ERR("pims_ipc_data_create() Fail");
2217                         goto DATA_FREE;
2218                         /* LCOV_EXCL_STOP */
2219                 }
2220                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2221                         /* LCOV_EXCL_START */
2222                         pims_ipc_data_destroy(*outdata);
2223                         *outdata = NULL;
2224                         ERR("ctsvc_ipc_marshal_int() Fail");
2225                         goto DATA_FREE;
2226                         /* LCOV_EXCL_STOP */
2227                 }
2228
2229                 if (CONTACTS_ERROR_NO_DATA == ret) {
2230                         DBG("no data");
2231                 } else if (CONTACTS_ERROR_NONE == ret) {
2232                         ret = ctsvc_ipc_marshal_list(list, *outdata);
2233
2234                         if (ret != CONTACTS_ERROR_NONE) {
2235                                 /* LCOV_EXCL_START */
2236                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
2237                                 goto DATA_FREE;
2238                                 /* LCOV_EXCL_STOP */
2239                         }
2240                 }
2241         } else {
2242                 /* LCOV_EXCL_START */
2243                 ERR("outdata is NULL");
2244                 /* LCOV_EXCL_STOP */
2245         }
2246         goto DATA_FREE;
2247
2248 ERROR_RETURN:
2249         if (outdata) {
2250                 *outdata = pims_ipc_data_create(0);
2251                 if (NULL == *outdata) {
2252                         /* LCOV_EXCL_START */
2253                         ERR("pims_ipc_data_create() Fail");
2254                         goto DATA_FREE;
2255                         /* LCOV_EXCL_STOP */
2256                 }
2257                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2258                         /* LCOV_EXCL_START */
2259                         pims_ipc_data_destroy(*outdata);
2260                         *outdata = NULL;
2261                         ERR("ctsvc_ipc_marshal_int() Fail");
2262                         goto DATA_FREE;
2263                         /* LCOV_EXCL_STOP */
2264                 }
2265         } else {
2266                 /* LCOV_EXCL_START */
2267                 ERR("outdata is NULL");
2268                 /* LCOV_EXCL_STOP */
2269         }
2270
2271 DATA_FREE:
2272         ctsvc_handle_destroy(contact);
2273         contacts_list_destroy(list, true);
2274         free(view_uri);
2275         free(keyword);
2276         free(start_match);
2277         free(end_match);
2278         ctsvc_server_start_timeout();
2279         return;
2280 }
2281
2282 void ctsvc_ipc_server_db_search_records_with_query_for_snippet(pims_ipc_h ipc,
2283                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
2284 {
2285         int ret = CONTACTS_ERROR_NONE;
2286         contacts_query_h query = NULL;
2287         char *keyword = NULL;
2288         int offset = 0;
2289         int limit = 0;
2290         contacts_list_h list = NULL;
2291         contacts_h contact = NULL;
2292         char *start_match = NULL;
2293         char *end_match = NULL;
2294         int token_number = 0;
2295
2296         if (indata) {
2297                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
2298                 if (CONTACTS_ERROR_NONE != ret) {
2299                         /* LCOV_EXCL_START */
2300                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
2301                         goto ERROR_RETURN;
2302                         /* LCOV_EXCL_STOP */
2303                 }
2304                 ret = ctsvc_ipc_unmarshal_query(indata, &query);
2305                 if (ret != CONTACTS_ERROR_NONE) {
2306                         /* LCOV_EXCL_START */
2307                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2308                         goto ERROR_RETURN;
2309                         /* LCOV_EXCL_STOP */
2310                 }
2311                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
2312                 if (ret != CONTACTS_ERROR_NONE) {
2313                         /* LCOV_EXCL_START */
2314                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2315                         goto ERROR_RETURN;
2316                         /* LCOV_EXCL_STOP */
2317                 }
2318                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
2319                 if (ret != CONTACTS_ERROR_NONE) {
2320                         /* LCOV_EXCL_START */
2321                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2322                         goto ERROR_RETURN;
2323                         /* LCOV_EXCL_STOP */
2324                 }
2325                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
2326                 if (ret != CONTACTS_ERROR_NONE) {
2327                         /* LCOV_EXCL_START */
2328                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2329                         goto ERROR_RETURN;
2330                         /* LCOV_EXCL_STOP */
2331                 }
2332                 ret = ctsvc_ipc_unmarshal_string(indata, &start_match);
2333                 if (ret != CONTACTS_ERROR_NONE) {
2334                         /* LCOV_EXCL_START */
2335                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2336                         goto ERROR_RETURN;
2337                         /* LCOV_EXCL_STOP */
2338                 }
2339                 ret = ctsvc_ipc_unmarshal_string(indata, &end_match);
2340                 if (ret != CONTACTS_ERROR_NONE) {
2341                         /* LCOV_EXCL_START */
2342                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2343                         goto ERROR_RETURN;
2344                         /* LCOV_EXCL_STOP */
2345                 }
2346                 ret = ctsvc_ipc_unmarshal_int(indata, &token_number);
2347                 if (ret != CONTACTS_ERROR_NONE) {
2348                         /* LCOV_EXCL_START */
2349                         ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
2350                         goto ERROR_RETURN;
2351                         /* LCOV_EXCL_STOP */
2352                 }
2353         } else {
2354                 /* LCOV_EXCL_START */
2355                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
2356                 goto ERROR_RETURN;
2357                 /* LCOV_EXCL_STOP */
2358         }
2359
2360         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(((ctsvc_query_s*)query)->view_uri))) {
2361                 /* LCOV_EXCL_START */
2362                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
2363                 goto ERROR_RETURN;
2364                 /* LCOV_EXCL_STOP */
2365         }
2366
2367         ret = ctsvc_db_search_records_with_query_for_snippet(query, keyword, offset, limit,
2368                         start_match, end_match, token_number, &list);
2369
2370         if (outdata) {
2371                 *outdata = pims_ipc_data_create(0);
2372                 if (NULL == *outdata) {
2373                         /* LCOV_EXCL_START */
2374                         ERR("pims_ipc_data_create() Fail");
2375                         goto DATA_FREE;
2376                         /* LCOV_EXCL_STOP */
2377                 }
2378                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2379                         /* LCOV_EXCL_START */
2380                         pims_ipc_data_destroy(*outdata);
2381                         *outdata = NULL;
2382                         ERR("ctsvc_ipc_marshal_int() Fail");
2383                         goto DATA_FREE;
2384                         /* LCOV_EXCL_STOP */
2385                 }
2386
2387                 if (CONTACTS_ERROR_NO_DATA == ret) {
2388                         DBG("no data");
2389                 } else if (CONTACTS_ERROR_NONE == ret) {
2390                         ret = ctsvc_ipc_marshal_list(list, *outdata);
2391
2392                         if (ret != CONTACTS_ERROR_NONE) {
2393                                 /* LCOV_EXCL_START */
2394                                 ERR("ctsvc_ipc_marshal_list Fail");
2395                                 goto DATA_FREE;
2396                                 /* LCOV_EXCL_STOP */
2397                         }
2398                 }
2399         } else {
2400                 /* LCOV_EXCL_START */
2401                 ERR("outdata is NULL");
2402                 /* LCOV_EXCL_STOP */
2403         }
2404         goto DATA_FREE;
2405
2406 ERROR_RETURN:
2407         if (outdata) {
2408                 *outdata = pims_ipc_data_create(0);
2409                 if (NULL == *outdata) {
2410                         /* LCOV_EXCL_START */
2411                         ERR("pims_ipc_data_create() Fail");
2412                         goto DATA_FREE;
2413                         /* LCOV_EXCL_STOP */
2414                 }
2415                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2416                         /* LCOV_EXCL_START */
2417                         pims_ipc_data_destroy(*outdata);
2418                         *outdata = NULL;
2419                         ERR("ctsvc_ipc_marshal_int() Fail");
2420                         goto DATA_FREE;
2421                         /* LCOV_EXCL_STOP */
2422                 }
2423         } else {
2424                 /* LCOV_EXCL_START */
2425                 ERR("outdata is NULL");
2426                 /* LCOV_EXCL_STOP */
2427         }
2428 DATA_FREE:
2429         ctsvc_handle_destroy(contact);
2430         contacts_list_destroy(list, true);
2431         contacts_query_destroy(query);
2432         free(keyword);
2433         free(start_match);
2434         free(end_match);
2435         ctsvc_server_start_timeout();
2436         return;
2437 }
2438
2439 void ctsvc_ipc_server_db_search_records(pims_ipc_h ipc, pims_ipc_data_h indata,
2440                 pims_ipc_data_h *outdata, void *userdata)
2441 {
2442         int ret = CONTACTS_ERROR_NONE;
2443         char *view_uri = NULL;
2444         char *keyword = NULL;
2445         int offset = 0;
2446         int limit = 0;
2447         contacts_list_h list = NULL;
2448         contacts_h contact = NULL;
2449
2450         if (indata) {
2451                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
2452                 if (CONTACTS_ERROR_NONE != ret) {
2453                         /* LCOV_EXCL_START */
2454                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
2455                         goto ERROR_RETURN;
2456                         /* LCOV_EXCL_STOP */
2457                 }
2458                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
2459                 if (ret != CONTACTS_ERROR_NONE) {
2460                         /* LCOV_EXCL_START */
2461                         ERR("ctsvc_ipc_unmarshal_record() Fail(%d)", ret);
2462                         goto ERROR_RETURN;
2463                         /* LCOV_EXCL_STOP */
2464                 }
2465                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
2466                 if (ret != CONTACTS_ERROR_NONE) {
2467                         /* LCOV_EXCL_START */
2468                         ERR("ctsvc_ipc_unmarshal_record() Fail(%d)", ret);
2469                         goto ERROR_RETURN;
2470                         /* LCOV_EXCL_STOP */
2471                 }
2472                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
2473                 if (ret != CONTACTS_ERROR_NONE) {
2474                         /* LCOV_EXCL_START */
2475                         ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
2476                         goto ERROR_RETURN;
2477                         /* LCOV_EXCL_STOP */
2478                 }
2479                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
2480                 if (ret != CONTACTS_ERROR_NONE) {
2481                         /* LCOV_EXCL_START */
2482                         ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
2483                         goto ERROR_RETURN;
2484                         /* LCOV_EXCL_STOP */
2485                 }
2486         } else {
2487                 /* LCOV_EXCL_START */
2488                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
2489                 goto ERROR_RETURN;
2490                 /* LCOV_EXCL_STOP */
2491         }
2492
2493         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
2494                 /* LCOV_EXCL_START */
2495                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
2496                 goto ERROR_RETURN;
2497                 /* LCOV_EXCL_STOP */
2498         }
2499
2500         ret = ctsvc_db_search_records(view_uri, keyword, offset, limit, &list);
2501
2502         if (outdata) {
2503                 *outdata = pims_ipc_data_create(0);
2504                 if (NULL == *outdata) {
2505                         /* LCOV_EXCL_START */
2506                         ERR("pims_ipc_data_create() Fail");
2507                         goto DATA_FREE;
2508                         /* LCOV_EXCL_STOP */
2509                 }
2510                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2511                         /* LCOV_EXCL_START */
2512                         pims_ipc_data_destroy(*outdata);
2513                         *outdata = NULL;
2514                         ERR("ctsvc_ipc_marshal_int() Fail");
2515                         goto DATA_FREE;
2516                         /* LCOV_EXCL_STOP */
2517                 }
2518
2519                 if (CONTACTS_ERROR_NO_DATA == ret) {
2520                         DBG("no data");
2521                 } else if (CONTACTS_ERROR_NONE == ret) {
2522                         ret = ctsvc_ipc_marshal_list(list, *outdata);
2523
2524                         if (ret != CONTACTS_ERROR_NONE) {
2525                                 /* LCOV_EXCL_START */
2526                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
2527                                 goto DATA_FREE;
2528                                 /* LCOV_EXCL_STOP */
2529                         }
2530                 }
2531         } else {
2532                 /* LCOV_EXCL_START */
2533                 ERR("outdata is NULL");
2534                 /* LCOV_EXCL_STOP */
2535         }
2536         goto DATA_FREE;
2537
2538 ERROR_RETURN:
2539         if (outdata) {
2540                 *outdata = pims_ipc_data_create(0);
2541                 if (NULL == *outdata) {
2542                         /* LCOV_EXCL_START */
2543                         ERR("pims_ipc_data_create() Fail");
2544                         goto DATA_FREE;
2545                         /* LCOV_EXCL_STOP */
2546                 }
2547                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2548                         /* LCOV_EXCL_START */
2549                         pims_ipc_data_destroy(*outdata);
2550                         *outdata = NULL;
2551                         ERR("ctsvc_ipc_marshal_int() Fail");
2552                         goto DATA_FREE;
2553                         /* LCOV_EXCL_STOP */
2554                 }
2555         } else {
2556                 /* LCOV_EXCL_START */
2557                 ERR("outdata is NULL");
2558                 /* LCOV_EXCL_STOP */
2559         }
2560 DATA_FREE:
2561         ctsvc_handle_destroy(contact);
2562         contacts_list_destroy(list, true);
2563         free(view_uri);
2564         free(keyword);
2565         ctsvc_server_start_timeout();
2566         return;
2567 }
2568
2569 void ctsvc_ipc_server_db_search_records_with_range(pims_ipc_h ipc,
2570                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
2571 {
2572         int ret = CONTACTS_ERROR_NONE;
2573         char *view_uri = NULL;
2574         char *keyword = NULL;
2575         int offset = 0;
2576         int limit = 0;
2577         int range = 0;
2578         contacts_list_h list = NULL;
2579         contacts_h contact = NULL;
2580
2581         if (indata) {
2582                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
2583                 if (CONTACTS_ERROR_NONE != ret) {
2584                         /* LCOV_EXCL_START */
2585                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
2586                         goto ERROR_RETURN;
2587                         /* LCOV_EXCL_STOP */
2588                 }
2589                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
2590                 if (ret != CONTACTS_ERROR_NONE) {
2591                         /* LCOV_EXCL_START */
2592                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2593                         goto ERROR_RETURN;
2594                         /* LCOV_EXCL_STOP */
2595                 }
2596                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
2597                 if (ret != CONTACTS_ERROR_NONE) {
2598                         /* LCOV_EXCL_START */
2599                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2600                         goto ERROR_RETURN;
2601                         /* LCOV_EXCL_STOP */
2602                 }
2603                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
2604                 if (ret != CONTACTS_ERROR_NONE) {
2605                         /* LCOV_EXCL_START */
2606                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2607                         goto ERROR_RETURN;
2608                         /* LCOV_EXCL_STOP */
2609                 }
2610                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
2611                 if (ret != CONTACTS_ERROR_NONE) {
2612                         /* LCOV_EXCL_START */
2613                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2614                         goto ERROR_RETURN;
2615                         /* LCOV_EXCL_STOP */
2616                 }
2617                 ret = ctsvc_ipc_unmarshal_int(indata, &range);
2618                 if (ret != CONTACTS_ERROR_NONE) {
2619                         /* LCOV_EXCL_START */
2620                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2621                         goto ERROR_RETURN;
2622                         /* LCOV_EXCL_STOP */
2623                 }
2624         } else {
2625                 /* LCOV_EXCL_START */
2626                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
2627                 goto ERROR_RETURN;
2628                 /* LCOV_EXCL_STOP */
2629         }
2630
2631         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
2632                 /* LCOV_EXCL_START */
2633                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
2634                 goto ERROR_RETURN;
2635                 /* LCOV_EXCL_STOP */
2636         }
2637
2638         ret = ctsvc_db_search_records_with_range(view_uri, keyword, offset, limit, range,
2639                         &list);
2640
2641         if (outdata) {
2642                 *outdata = pims_ipc_data_create(0);
2643                 if (NULL == *outdata) {
2644                         /* LCOV_EXCL_START */
2645                         ERR("pims_ipc_data_create() Fail");
2646                         goto DATA_FREE;
2647                         /* LCOV_EXCL_STOP */
2648                 }
2649                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2650                         /* LCOV_EXCL_START */
2651                         pims_ipc_data_destroy(*outdata);
2652                         *outdata = NULL;
2653                         ERR("ctsvc_ipc_marshal_int() Fail");
2654                         goto DATA_FREE;
2655                         /* LCOV_EXCL_STOP */
2656                 }
2657
2658                 if (CONTACTS_ERROR_NO_DATA == ret) {
2659                         DBG("no data");
2660                 } else if (CONTACTS_ERROR_NONE == ret) {
2661                         ret = ctsvc_ipc_marshal_list(list, *outdata);
2662
2663                         if (ret != CONTACTS_ERROR_NONE) {
2664                                 /* LCOV_EXCL_START */
2665                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
2666                                 goto DATA_FREE;
2667                                 /* LCOV_EXCL_STOP */
2668                         }
2669                 }
2670         } else {
2671                 /* LCOV_EXCL_START */
2672                 ERR("outdata is NULL");
2673                 /* LCOV_EXCL_STOP */
2674         }
2675         goto DATA_FREE;
2676
2677 ERROR_RETURN:
2678         if (outdata) {
2679                 *outdata = pims_ipc_data_create(0);
2680                 if (NULL == *outdata) {
2681                         /* LCOV_EXCL_START */
2682                         ERR("pims_ipc_data_create() Fail");
2683                         goto DATA_FREE;
2684                         /* LCOV_EXCL_STOP */
2685                 }
2686                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2687                         /* LCOV_EXCL_START */
2688                         pims_ipc_data_destroy(*outdata);
2689                         *outdata = NULL;
2690                         ERR("ctsvc_ipc_marshal_int() Fail");
2691                         goto DATA_FREE;
2692                         /* LCOV_EXCL_STOP */
2693                 }
2694         } else {
2695                 /* LCOV_EXCL_START */
2696                 ERR("outdata is NULL");
2697                 /* LCOV_EXCL_STOP */
2698         }
2699
2700 DATA_FREE:
2701         ctsvc_handle_destroy(contact);
2702         contacts_list_destroy(list, true);
2703         free(view_uri);
2704         free(keyword);
2705         ctsvc_server_start_timeout();
2706         return;
2707 }
2708
2709 void ctsvc_ipc_server_db_search_records_with_query(pims_ipc_h ipc,
2710                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
2711 {
2712         int ret = CONTACTS_ERROR_NONE;
2713         contacts_query_h query = NULL;
2714         char *keyword = NULL;
2715         int offset = 0;
2716         int limit = 0;
2717         contacts_list_h list = NULL;
2718         contacts_h contact = NULL;
2719
2720         if (indata) {
2721                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
2722                 if (CONTACTS_ERROR_NONE != ret) {
2723                         /* LCOV_EXCL_START */
2724                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
2725                         goto ERROR_RETURN;
2726                         /* LCOV_EXCL_STOP */
2727                 }
2728                 ret = ctsvc_ipc_unmarshal_query(indata, &query);
2729                 if (ret != CONTACTS_ERROR_NONE) {
2730                         /* LCOV_EXCL_START */
2731                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2732                         goto ERROR_RETURN;
2733                         /* LCOV_EXCL_STOP */
2734                 }
2735                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
2736                 if (ret != CONTACTS_ERROR_NONE) {
2737                         /* LCOV_EXCL_START */
2738                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2739                         goto ERROR_RETURN;
2740                         /* LCOV_EXCL_STOP */
2741                 }
2742                 ret = ctsvc_ipc_unmarshal_int(indata, &offset);
2743                 if (ret != CONTACTS_ERROR_NONE) {
2744                         /* LCOV_EXCL_START */
2745                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2746                         goto ERROR_RETURN;
2747                         /* LCOV_EXCL_STOP */
2748                 }
2749                 ret = ctsvc_ipc_unmarshal_int(indata, &limit);
2750                 if (ret != CONTACTS_ERROR_NONE) {
2751                         /* LCOV_EXCL_START */
2752                         ERR("ctsvc_ipc_unmarshal_int() Fail");
2753                         goto ERROR_RETURN;
2754                         /* LCOV_EXCL_STOP */
2755                 }
2756         } else {
2757                 /* LCOV_EXCL_START */
2758                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
2759                 goto ERROR_RETURN;
2760                 /* LCOV_EXCL_STOP */
2761         }
2762
2763
2764         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(((ctsvc_query_s*)query)->view_uri))) {
2765                 /* LCOV_EXCL_START */
2766                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
2767                 goto ERROR_RETURN;
2768                 /* LCOV_EXCL_STOP */
2769         }
2770
2771
2772         ret = ctsvc_db_search_records_with_query(query, keyword, offset, limit, &list);
2773
2774         if (outdata) {
2775                 *outdata = pims_ipc_data_create(0);
2776                 if (NULL == *outdata) {
2777                         /* LCOV_EXCL_START */
2778                         ERR("pims_ipc_data_create() Fail");
2779                         goto DATA_FREE;
2780                         /* LCOV_EXCL_STOP */
2781                 }
2782                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2783                         /* LCOV_EXCL_START */
2784                         pims_ipc_data_destroy(*outdata);
2785                         *outdata = NULL;
2786                         ERR("ctsvc_ipc_marshal_int() Fail");
2787                         goto DATA_FREE;
2788                         /* LCOV_EXCL_STOP */
2789                 }
2790
2791                 if (CONTACTS_ERROR_NO_DATA == ret) {
2792                         DBG("no data");
2793                 } else if (CONTACTS_ERROR_NONE == ret) {
2794                         ret = ctsvc_ipc_marshal_list(list, *outdata);
2795
2796                         if (ret != CONTACTS_ERROR_NONE) {
2797                                 /* LCOV_EXCL_START */
2798                                 ERR("ctsvc_ipc_marshal_list Fail");
2799                                 goto DATA_FREE;
2800                                 /* LCOV_EXCL_STOP */
2801                         }
2802                 }
2803         } else {
2804                 /* LCOV_EXCL_START */
2805                 ERR("outdata is NULL");
2806                 /* LCOV_EXCL_STOP */
2807         }
2808         goto DATA_FREE;
2809
2810 ERROR_RETURN:
2811         if (outdata) {
2812                 *outdata = pims_ipc_data_create(0);
2813                 if (NULL == *outdata) {
2814                         /* LCOV_EXCL_START */
2815                         ERR("pims_ipc_data_create() Fail");
2816                         goto DATA_FREE;
2817                         /* LCOV_EXCL_STOP */
2818                 }
2819                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2820                         /* LCOV_EXCL_START */
2821                         pims_ipc_data_destroy(*outdata);
2822                         *outdata = NULL;
2823                         ERR("ctsvc_ipc_marshal_int() Fail");
2824                         goto DATA_FREE;
2825                         /* LCOV_EXCL_STOP */
2826                 }
2827         } else {
2828                 /* LCOV_EXCL_START */
2829                 ERR("outdata is NULL");
2830                 /* LCOV_EXCL_STOP */
2831         }
2832 DATA_FREE:
2833         ctsvc_handle_destroy(contact);
2834         contacts_list_destroy(list, true);
2835         contacts_query_destroy(query);
2836         free(keyword);
2837         ctsvc_server_start_timeout();
2838         return;
2839 }
2840
2841 void ctsvc_ipc_server_db_get_status(pims_ipc_h ipc, pims_ipc_data_h indata,
2842                 pims_ipc_data_h *outdata, void *userdata)
2843 {
2844         int ret = CONTACTS_ERROR_NONE;
2845         contacts_db_status_e status = CONTACTS_DB_STATUS_NORMAL;
2846         contacts_h contact = NULL;
2847
2848         if (indata) {
2849                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
2850                 if (CONTACTS_ERROR_NONE != ret) {
2851                         /* LCOV_EXCL_START */
2852                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
2853                         goto ERROR_RETURN;
2854                         /* LCOV_EXCL_STOP */
2855                 }
2856         }
2857
2858         ret = ctsvc_db_get_status(&status);
2859
2860 ERROR_RETURN:
2861         if (outdata) {
2862                 *outdata = pims_ipc_data_create(0);
2863                 if (NULL == *outdata) {
2864                         /* LCOV_EXCL_START */
2865                         ERR("pims_ipc_data_create() Fail");
2866                         goto DATA_FREE;
2867                         /* LCOV_EXCL_STOP */
2868                 }
2869
2870                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2871                         /* LCOV_EXCL_START */
2872                         pims_ipc_data_destroy(*outdata);
2873                         *outdata = NULL;
2874                         ERR("ctsvc_ipc_marshal_int() Fail");
2875                         goto DATA_FREE;
2876                         /* LCOV_EXCL_STOP */
2877                 }
2878
2879                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(status, *outdata)) {
2880                         /* LCOV_EXCL_START */
2881                         pims_ipc_data_destroy(*outdata);
2882                         *outdata = NULL;
2883                         ERR("ctsvc_ipc_marshal_int() Fail");
2884                         goto DATA_FREE;
2885                         /* LCOV_EXCL_STOP */
2886                 }
2887         } else {
2888                 /* LCOV_EXCL_START */
2889                 ERR("outdata is NULL");
2890                 /* LCOV_EXCL_STOP */
2891         }
2892 DATA_FREE:
2893         ctsvc_handle_destroy(contact);
2894         ctsvc_server_start_timeout();
2895         return;
2896 }
2897
2898 void ctsvc_ipc_server_db_get_count_for_search_records(pims_ipc_h ipc,
2899                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
2900 {
2901         int ret = CONTACTS_ERROR_NONE;
2902         char *view_uri = NULL;
2903         int count = 0;
2904         contacts_h contact = NULL;
2905         char *keyword = NULL;
2906
2907         if (indata) {
2908                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
2909                 if (CONTACTS_ERROR_NONE != ret) {
2910                         /* LCOV_EXCL_START */
2911                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
2912                         goto ERROR_RETURN;
2913                         /* LCOV_EXCL_STOP */
2914                 }
2915
2916                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
2917                 if (ret != CONTACTS_ERROR_NONE) {
2918                         /* LCOV_EXCL_START */
2919                         ERR("ctsvc_ipc_unmarshal_record() Fail");
2920                         goto ERROR_RETURN;
2921                         /* LCOV_EXCL_STOP */
2922                 }
2923
2924                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
2925                 if (ret != CONTACTS_ERROR_NONE) {
2926                         /* LCOV_EXCL_START */
2927                         ERR("ctsvc_ipc_unmarshal_string() Fail");
2928                         goto ERROR_RETURN;
2929                         /* LCOV_EXCL_STOP */
2930                 }
2931         } else {
2932                 /* LCOV_EXCL_START */
2933                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
2934                 goto ERROR_RETURN;
2935                 /* LCOV_EXCL_STOP */
2936         }
2937
2938         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
2939                 /* LCOV_EXCL_START */
2940                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
2941                 goto ERROR_RETURN;
2942                 /* LCOV_EXCL_STOP */
2943         }
2944
2945         ret = ctsvc_db_get_count_for_search_records(view_uri, keyword, &count);
2946
2947         if (outdata) {
2948                 *outdata = pims_ipc_data_create(0);
2949                 if (NULL == *outdata) {
2950                         /* LCOV_EXCL_START */
2951                         ERR("pims_ipc_data_create() Fail");
2952                         goto DATA_FREE;
2953                         /* LCOV_EXCL_STOP */
2954                 }
2955                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2956                         /* LCOV_EXCL_START */
2957                         pims_ipc_data_destroy(*outdata);
2958                         *outdata = NULL;
2959                         ERR("ctsvc_ipc_marshal_int() Fail");
2960                         goto DATA_FREE;
2961                         /* LCOV_EXCL_STOP */
2962                 }
2963
2964                 if (CONTACTS_ERROR_NO_DATA == ret) {
2965                         DBG("no data");
2966                 } else if (CONTACTS_ERROR_NONE == ret) {
2967                         ret = ctsvc_ipc_marshal_int(count, *outdata);
2968
2969                         if (ret != CONTACTS_ERROR_NONE) {
2970                                 /* LCOV_EXCL_START */
2971                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
2972                                 goto DATA_FREE;
2973                                 /* LCOV_EXCL_STOP */
2974                         }
2975                 }
2976         } else {
2977                 /* LCOV_EXCL_START */
2978                 ERR("outdata is NULL");
2979                 /* LCOV_EXCL_STOP */
2980         }
2981         goto DATA_FREE;
2982
2983 ERROR_RETURN:
2984         if (outdata) {
2985                 *outdata = pims_ipc_data_create(0);
2986                 if (NULL == *outdata) {
2987                         /* LCOV_EXCL_START */
2988                         ERR("pims_ipc_data_create() Fail");
2989                         goto DATA_FREE;
2990                         /* LCOV_EXCL_STOP */
2991                 }
2992                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
2993                         /* LCOV_EXCL_START */
2994                         pims_ipc_data_destroy(*outdata);
2995                         *outdata = NULL;
2996                         ERR("ctsvc_ipc_marshal_int() Fail");
2997                         goto DATA_FREE;
2998                         /* LCOV_EXCL_STOP */
2999                 }
3000         } else {
3001                 /* LCOV_EXCL_START */
3002                 ERR("outdata is NULL");
3003                 /* LCOV_EXCL_STOP */
3004         }
3005 DATA_FREE:
3006         ctsvc_handle_destroy(contact);
3007         free(view_uri);
3008         free(keyword);
3009         ctsvc_server_start_timeout();
3010         return;
3011 }
3012
3013 void ctsvc_ipc_server_db_get_count_for_search_records_with_range(pims_ipc_h ipc,
3014                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
3015 {
3016         int ret = CONTACTS_ERROR_NONE;
3017         char *view_uri = NULL;
3018         int count = 0;
3019         contacts_h contact = NULL;
3020         char *keyword = NULL;
3021         int range = 0;
3022
3023         if (indata) {
3024                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
3025                 if (CONTACTS_ERROR_NONE != ret) {
3026                         /* LCOV_EXCL_START */
3027                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
3028                         goto ERROR_RETURN;
3029                         /* LCOV_EXCL_STOP */
3030                 }
3031
3032                 ret = ctsvc_ipc_unmarshal_string(indata, &view_uri);
3033                 if (ret != CONTACTS_ERROR_NONE) {
3034                         /* LCOV_EXCL_START */
3035                         ERR("ctsvc_ipc_unmarshal_record() Fail");
3036                         goto ERROR_RETURN;
3037                         /* LCOV_EXCL_STOP */
3038                 }
3039
3040                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
3041                 if (ret != CONTACTS_ERROR_NONE) {
3042                         /* LCOV_EXCL_START */
3043                         ERR("ctsvc_ipc_unmarshal_string() Fail");
3044                         goto ERROR_RETURN;
3045                         /* LCOV_EXCL_STOP */
3046                 }
3047
3048                 ret = ctsvc_ipc_unmarshal_int(indata, &range);
3049                 if (ret != CONTACTS_ERROR_NONE) {
3050                         /* LCOV_EXCL_START */
3051                         ERR("ctsvc_ipc_unmarshal_string() Fail");
3052                         goto ERROR_RETURN;
3053                         /* LCOV_EXCL_STOP */
3054                 }
3055         } else {
3056                 /* LCOV_EXCL_START */
3057                 ERR("ctsvc_ipc_server_db_insert_record() Fail");
3058                 goto ERROR_RETURN;
3059                 /* LCOV_EXCL_STOP */
3060         }
3061
3062         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(view_uri))) {
3063                 /* LCOV_EXCL_START */
3064                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
3065                 goto ERROR_RETURN;
3066                 /* LCOV_EXCL_STOP */
3067         }
3068
3069         ret = ctsvc_db_get_count_for_search_records_with_range(view_uri, keyword, range, &count);
3070
3071         if (outdata) {
3072                 *outdata = pims_ipc_data_create(0);
3073                 if (NULL == *outdata) {
3074                         /* LCOV_EXCL_START */
3075                         ERR("pims_ipc_data_create() Fail");
3076                         goto DATA_FREE;
3077                         /* LCOV_EXCL_STOP */
3078                 }
3079                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
3080                         /* LCOV_EXCL_START */
3081                         pims_ipc_data_destroy(*outdata);
3082                         *outdata = NULL;
3083                         ERR("ctsvc_ipc_marshal_int() Fail");
3084                         goto DATA_FREE;
3085                         /* LCOV_EXCL_STOP */
3086                 }
3087
3088                 if (CONTACTS_ERROR_NO_DATA == ret) {
3089                         DBG("no data");
3090                 } else if (CONTACTS_ERROR_NONE == ret) {
3091                         ret = ctsvc_ipc_marshal_int(count, *outdata);
3092
3093                         if (ret != CONTACTS_ERROR_NONE) {
3094                                 /* LCOV_EXCL_START */
3095                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
3096                                 goto DATA_FREE;
3097                                 /* LCOV_EXCL_STOP */
3098                         }
3099                 }
3100         } else {
3101                 /* LCOV_EXCL_START */
3102                 ERR("outdata is NULL");
3103                 /* LCOV_EXCL_STOP */
3104         }
3105         goto DATA_FREE;
3106
3107 ERROR_RETURN:
3108         if (outdata) {
3109                 *outdata = pims_ipc_data_create(0);
3110                 if (NULL == *outdata) {
3111                         /* LCOV_EXCL_START */
3112                         ERR("pims_ipc_data_create() Fail");
3113                         goto DATA_FREE;
3114                         /* LCOV_EXCL_STOP */
3115                 }
3116                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
3117                         /* LCOV_EXCL_START */
3118                         pims_ipc_data_destroy(*outdata);
3119                         *outdata = NULL;
3120                         ERR("ctsvc_ipc_marshal_int() Fail");
3121                         goto DATA_FREE;
3122                         /* LCOV_EXCL_STOP */
3123                 }
3124         } else {
3125                 /* LCOV_EXCL_START */
3126                 ERR("outdata is NULL");
3127                 /* LCOV_EXCL_STOP */
3128         }
3129 DATA_FREE:
3130         ctsvc_handle_destroy(contact);
3131         free(view_uri);
3132         free(keyword);
3133         ctsvc_server_start_timeout();
3134         return;
3135 }
3136
3137 void ctsvc_ipc_server_db_get_count_for_search_records_with_query(pims_ipc_h ipc,
3138                 pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
3139 {
3140         int ret = CONTACTS_ERROR_NONE;
3141         contacts_query_h query = NULL;
3142         int count = 0;
3143         contacts_h contact = NULL;
3144         char *keyword = NULL;
3145
3146         if (indata) {
3147                 ret = ctsvc_ipc_unmarshal_handle(indata, &contact);
3148                 if (CONTACTS_ERROR_NONE != ret) {
3149                         /* LCOV_EXCL_START */
3150                         ERR("ctsvc_ipc_unmarshal_handle() Fail(%d)", ret);
3151                         goto ERROR_RETURN;
3152                         /* LCOV_EXCL_STOP */
3153                 }
3154
3155                 ret = ctsvc_ipc_unmarshal_query(indata, &query);
3156                 if (ret != CONTACTS_ERROR_NONE) {
3157                         /* LCOV_EXCL_START */
3158                         ERR("ctsvc_ipc_unmarshal_query() Fail");
3159                         goto ERROR_RETURN;
3160                         /* LCOV_EXCL_STOP */
3161                 }
3162
3163                 ret = ctsvc_ipc_unmarshal_string(indata, &keyword);
3164                 if (ret != CONTACTS_ERROR_NONE) {
3165                         /* LCOV_EXCL_START */
3166                         ERR("ctsvc_ipc_unmarshal_string() Fail");
3167                         goto ERROR_RETURN;
3168                         /* LCOV_EXCL_STOP */
3169                 }
3170         } else {
3171                 /* LCOV_EXCL_START */
3172                 ERR("ctsvc_ipc_server_db_get_count_for_search_records_with_query() Fail");
3173                 goto ERROR_RETURN;
3174                 /* LCOV_EXCL_STOP */
3175         }
3176
3177         if (!ctsvc_have_permission(ipc, ctsvc_required_read_permission(((ctsvc_query_s*)query)->view_uri))) {
3178                 /* LCOV_EXCL_START */
3179                 ret = CONTACTS_ERROR_PERMISSION_DENIED;
3180                 goto ERROR_RETURN;
3181                 /* LCOV_EXCL_STOP */
3182         }
3183
3184         ret = ctsvc_db_get_count_for_search_records_with_query(query, keyword, &count);
3185
3186         if (outdata) {
3187                 *outdata = pims_ipc_data_create(0);
3188                 if (NULL == *outdata) {
3189                         /* LCOV_EXCL_START */
3190                         ERR("pims_ipc_data_create() Fail");
3191                         goto DATA_FREE;
3192                         /* LCOV_EXCL_STOP */
3193                 }
3194                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
3195                         /* LCOV_EXCL_START */
3196                         pims_ipc_data_destroy(*outdata);
3197                         *outdata = NULL;
3198                         ERR("ctsvc_ipc_marshal_int() Fail");
3199                         goto DATA_FREE;
3200                         /* LCOV_EXCL_STOP */
3201                 }
3202
3203                 if (CONTACTS_ERROR_NO_DATA == ret) {
3204                         DBG("no data");
3205                 } else if (CONTACTS_ERROR_NONE == ret) {
3206                         ret = ctsvc_ipc_marshal_int(count, *outdata);
3207
3208                         if (ret != CONTACTS_ERROR_NONE) {
3209                                 /* LCOV_EXCL_START */
3210                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
3211                                 goto DATA_FREE;
3212                                 /* LCOV_EXCL_STOP */
3213                         }
3214                 }
3215         } else {
3216                 /* LCOV_EXCL_START */
3217                 ERR("outdata is NULL");
3218                 /* LCOV_EXCL_STOP */
3219         }
3220         goto DATA_FREE;
3221
3222 ERROR_RETURN:
3223         if (outdata) {
3224                 *outdata = pims_ipc_data_create(0);
3225                 if (NULL == *outdata) {
3226                         /* LCOV_EXCL_START */
3227                         ERR("pims_ipc_data_create() Fail");
3228                         goto DATA_FREE;
3229                         /* LCOV_EXCL_STOP */
3230                 }
3231                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(ret, *outdata)) {
3232                         /* LCOV_EXCL_START */
3233                         pims_ipc_data_destroy(*outdata);
3234                         *outdata = NULL;
3235                         ERR("ctsvc_ipc_marshal_int() Fail");
3236                         goto DATA_FREE;
3237                         /* LCOV_EXCL_STOP */
3238                 }
3239         } else {
3240                 /* LCOV_EXCL_START */
3241                 ERR("outdata is NULL");
3242                 /* LCOV_EXCL_STOP */
3243         }
3244 DATA_FREE:
3245         ctsvc_handle_destroy(contact);
3246         contacts_query_destroy(query);
3247         free(keyword);
3248         ctsvc_server_start_timeout();
3249         return;
3250 }
3251
3252