Merge remote-tracking branch 'origin/master' into fix_log
[platform/core/appfw/pkgmgr-info.git] / src / manager / pkginfo_manager.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "manager/pkginfo_manager.h"
18
19 #include <sys/types.h>
20
21 #include <map>
22 #include <string>
23
24 #include "sqlite3.h"
25 #include "glib.h"
26
27 #include "pkgmgr_parser.h"
28 #include "pkgmgrinfo_private.h"
29
30 #include "logging.hh"
31 #include "common/database/abstract_db_handler.hh"
32 #include "common/database/pkg_set_db_handler.hh"
33 #include "common/parcel/appinfo_parcelable.hh"
34 #include "common/parcel/certinfo_parcelable.hh"
35 #include "common/parcel/depinfo_parcelable.hh"
36 #include "common/parcel/filter_parcelable.hh"
37 #include "common/parcel/pkginfo_parcelable.hh"
38 #include "common/parcel/query_parcelable.hh"
39 #include "common/parcel/result_parcelable.hh"
40
41 #include "client/pkginfo_client.hh"
42
43 #include <parcel.hh>
44
45 #ifdef LOG_TAG
46 #undef LOG_TAG
47 #endif
48 #define LOG_TAG "PKGMGR_INFO"
49
50 #ifdef EXPORT_API
51 #undef EXPORT_API
52 #endif
53 #define EXPORT_API __attribute__((visibility("default")))
54
55 extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
56             pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
57         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
58                         new pkgmgr_common::parcel::FilterParcelable(uid,
59                                         static_cast<pkgmgrinfo_filter_x*>(filter), flag));
60
61         pkgmgr_client::PkgInfoClient client(parcelable, uid,
62                         pkgmgr_common::ReqType::GET_PKG_INFO);
63         if (!client.SendRequest())
64       return PMINFO_R_ERROR;
65
66         std::shared_ptr<pkgmgr_common::parcel::PkgInfoParcelable> return_parcel(
67                         std::static_pointer_cast<pkgmgr_common::parcel::PkgInfoParcelable>(
68                                         client.GetResultParcel()));
69
70         if (return_parcel == nullptr) {
71                 LOG(ERROR) << "Fail to get return parcelable";
72                 return PMINFO_R_ERROR;
73         }
74
75         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
76                 LOG(ERROR) << "Request fail";
77                 return return_parcel->GetRequestResult();
78         }
79
80         tizen_base::Parcel parcel;
81         parcel.ReadParcelable(return_parcel.get());
82
83         auto result_list = return_parcel->GetPkgInfo();
84         // TODO: check noentry error has returned if size of result_list is 0
85         for (auto& pkginfo : result_list)
86                 g_hash_table_insert(packages, (gpointer)pkginfo->package,
87                                 (gpointer)pkginfo);
88
89         return PMINFO_R_OK;
90 }
91
92 extern "C" EXPORT_API int _pkginfo_get_depends_on(uid_t uid,
93                 const char *pkgid, GList **dependencies) {
94         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
95                         new pkgmgr_common::parcel::DepInfoParcelable(std::string(pkgid)));
96
97         pkgmgr_client::PkgInfoClient client(parcelable, uid,
98                         pkgmgr_common::ReqType::GET_PKG_DEP_INFO);
99         if (!client.SendRequest())
100       return PMINFO_R_ERROR;
101
102         std::shared_ptr<pkgmgr_common::parcel::DepInfoParcelable> return_parcel(
103                         std::static_pointer_cast<pkgmgr_common::parcel::DepInfoParcelable>(
104                                         client.GetResultParcel()));
105
106         if (return_parcel == nullptr) {
107                 LOG(ERROR) << "Fail to get return parcelable";
108                 return PMINFO_R_ERROR;
109         }
110
111         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
112                 LOG(ERROR) << "Request fail";
113                 return return_parcel->GetRequestResult();
114         }
115
116         tizen_base::Parcel parcel;
117         parcel.ReadParcelable(return_parcel.get());
118
119         auto dependency_list = return_parcel->GetDependencyInfo();
120         for (auto& dependency : dependency_list)
121                 *dependencies = g_list_prepend(*dependencies, dependency);
122         return PMINFO_R_OK;
123 }
124
125 // TODO: Need to add target db uid to identify which database to be searched
126 extern "C" EXPORT_API int _appinfo_get_applications(uid_t db_uid, uid_t uid,
127                 pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
128         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
129                         new pkgmgr_common::parcel::FilterParcelable(uid,
130                                         static_cast<pkgmgrinfo_filter_x*>(filter), flag));
131
132         pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::GET_APP_INFO);
133         if (!client.SendRequest())
134       return PMINFO_R_ERROR;
135
136         std::shared_ptr<pkgmgr_common::parcel::AppInfoParcelable> return_parcel(
137                         std::static_pointer_cast<pkgmgr_common::parcel::AppInfoParcelable>(
138                                         client.GetResultParcel()));
139
140         if (return_parcel == nullptr) {
141                 LOG(ERROR) << "Fail to get return parcelable";
142                 return PMINFO_R_ERROR;
143         }
144
145         int ret = return_parcel->GetRequestResult();
146         if (ret != PMINFO_R_OK) {
147                 if (ret == PMINFO_R_ENOENT) {
148                         LOG(DEBUG) << "No such application";
149                 } else {
150                         LOG(ERROR) << "Request fail";
151                 }
152                 return ret;
153         }
154
155         tizen_base::Parcel parcel;
156         parcel.ReadParcelable(return_parcel.get());
157         auto result_list = return_parcel->GetAppInfo();
158         for (auto& appinfo : result_list)
159                 g_hash_table_insert(packages, (gpointer)appinfo->appid,
160                                 (gpointer)appinfo);
161
162         return PMINFO_R_OK;
163 }
164
165 extern "C" EXPORT_API char *_appinfo_get_localed_label(
166                 const char *appid, const char *locale, uid_t uid) {
167         char *query = nullptr;
168     query = sqlite3_mprintf(
169                         "SELECT COALESCE((SELECT app_label FROM package_app_localized_info "
170                         "WHERE app_id=%Q AND app_locale=%Q),"
171                         "(SELECT app_label FROM package_app_localized_info WHERE "
172                         "app_id=%Q AND app_locale='No Locale'))", appid, locale, appid);
173         if (query == nullptr) {
174                 LOG(ERROR) << "Out of memory";
175                 return nullptr;
176         }
177
178         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
179                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
180           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
181           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
182
183         pkgmgr_client::PkgInfoClient client(parcelable, uid,
184                         pkgmgr_common::ReqType::QUERY);
185         if (!client.SendRequest()) {
186                 sqlite3_free(query);
187                 return nullptr;
188         }
189
190         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
191                                 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
192                                                 client.GetResultParcel()));
193
194         if (return_parcel == nullptr) {
195                 LOG(ERROR) << "Fail to get return parcelable";
196                 return nullptr;
197         }
198
199         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
200                 LOG(ERROR) << "Request fail";
201                 return nullptr;
202         }
203
204         tizen_base::Parcel parcel;
205         parcel.ReadParcelable(return_parcel.get());
206         sqlite3_free(query);
207         // result_list is vector of string vector
208         char *label = nullptr;
209         auto result_list = return_parcel->GetResult();
210         for (auto result : result_list) {
211                 // result is string vector
212                 // it only has one string or not.
213                 if (result.front().empty() || result.front().length() == 0)
214                         return nullptr;
215                 label = strdup(result.front().c_str());
216                 if (label == nullptr) {
217                         LOG(ERROR) << "Out of memory";
218                         return nullptr;
219                 }
220                 return label;
221         }
222
223         return label;
224 }
225
226 extern "C" EXPORT_API int _appinfo_get_datacontrol_info(
227                 const char *providerid, const char *type, uid_t uid,
228                 char **appid, char **access) {
229         char *query = nullptr;
230         query = sqlite3_mprintf("SELECT app_id, access FROM "
231                         "package_app_data_control WHERE "
232                         "providerid=%Q AND type=%Q", providerid, type);
233         if (query == nullptr) {
234                 LOG(ERROR) << "Out of memory";
235                 return PMINFO_R_ERROR;
236         }
237
238         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
239                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
240           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
241           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
242
243         pkgmgr_client::PkgInfoClient client(parcelable, uid,
244                         pkgmgr_common::ReqType::QUERY);
245         if (!client.SendRequest()) {
246                 sqlite3_free(query);
247                 return PMINFO_R_ERROR;
248         }
249
250         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
251                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
252                                         client.GetResultParcel()));
253
254         if (return_parcel == nullptr) {
255                 LOG(ERROR) << "Fail to get return parcelable";
256                 return PMINFO_R_ERROR;
257         }
258
259         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
260                 LOG(ERROR) << "Request fail";
261                 return return_parcel->GetRequestResult();
262         }
263
264         tizen_base::Parcel parcel;
265         parcel.ReadParcelable(return_parcel.get());
266         sqlite3_free(query);
267         // result_list is vector of string vector
268         auto result_list = return_parcel->GetResult();
269         if (result_list.size() == 0)
270                 return PMINFO_R_ENOENT;
271         for (auto result : result_list) {
272                 if (result.size() != 2)
273                         return PMINFO_R_ERROR;
274                 if (result.front().empty() || result.front().size() == 0 ||
275                                 result.back().empty() || result.back().size() == 0)
276                         return PMINFO_R_ERROR;
277                 *appid = strdup(result.front().c_str());
278                 *access = strdup(result.back().c_str());
279                 if (*appid == nullptr || *access == nullptr) {
280                         LOG(ERROR) << "Out of memory";
281                         return PMINFO_R_ERROR;
282                 }
283         }
284
285         return PMINFO_R_OK;
286 }
287
288 extern "C" EXPORT_API int _appinfo_get_datacontrol_appid(
289                 const char *providerid, uid_t uid, char **appid) {
290         char *query = nullptr;
291
292         query = sqlite3_mprintf("SELECT app_id FROM package_app_data_control "
293                 "WHERE providerid=%Q", providerid);
294         if (query == nullptr) {
295                 LOGE("Out of memory");
296                 return PMINFO_R_ERROR;
297         }
298         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
299                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
300           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
301           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
302
303         pkgmgr_client::PkgInfoClient client(parcelable, uid,
304                         pkgmgr_common::ReqType::QUERY);
305         if (!client.SendRequest()) {
306                 sqlite3_free(query);
307                 return PMINFO_R_ERROR;
308         }
309         // TODO: deliver rawdata to reqhandler directly if server is not working
310
311         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
312                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
313                                         client.GetResultParcel()));
314
315         if (return_parcel == nullptr) {
316                 LOG(ERROR) << "Fail to get return parcelable";
317                 return PMINFO_R_ERROR;
318         }
319
320         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
321                 LOG(ERROR) << "Request fail";
322                 return return_parcel->GetRequestResult();
323         }
324
325         tizen_base::Parcel parcel;
326         parcel.ReadParcelable(return_parcel.get());
327         sqlite3_free(query);
328
329         // result_list is vector of string vector
330         auto result_list = return_parcel->GetResult();
331         if (result_list.size() == 0)
332                 return PMINFO_R_ENOENT;
333         for (auto result : result_list) {
334                 if (result.size() != 1)
335                         return PMINFO_R_ERROR;
336                 if (result.front().empty() || result.front().size() == 0)
337                         return PMINFO_R_ERROR;
338                 *appid = strdup(result.front().c_str());
339                 if (*appid == nullptr) {
340                         LOG(ERROR) << "Out of memory";
341                         return PMINFO_R_ERROR;
342                 }
343         }
344
345         return PMINFO_R_OK;
346 }
347
348 extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info(
349                 const char *providerid, const char *type, uid_t uid,
350                 char **appid, char **trusted) {
351         char *query = nullptr;
352         query = sqlite3_mprintf(
353                         "SELECT app_id, trusted FROM package_app_data_control "
354                         "WHERE providerid=%Q AND type=%Q", providerid, type);
355         if (query == nullptr) {
356                 LOGE("Out of memory");
357                 return PMINFO_R_ERROR;
358         }
359
360         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
361                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
362           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
363           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
364
365         pkgmgr_client::PkgInfoClient client(parcelable, uid,
366                         pkgmgr_common::ReqType::QUERY);
367         if (!client.SendRequest()) {
368                 sqlite3_free(query);
369                 return PMINFO_R_ERROR;
370         }
371         // TODO: deliver rawdata to reqhandler directly if server is not working
372
373         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
374                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
375                                         client.GetResultParcel()));
376
377         if (return_parcel == nullptr) {
378                 LOG(ERROR) << "Fail to get return parcelable";
379                 return PMINFO_R_ERROR;
380         }
381
382         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
383                 LOG(ERROR) << "Request fail";
384                 return return_parcel->GetRequestResult();
385         }
386
387         tizen_base::Parcel parcel;
388         parcel.ReadParcelable(return_parcel.get());
389         sqlite3_free(query);
390         // result_list is vector of string vector
391         auto result_list = return_parcel->GetResult();
392         if (result_list.size() == 0)
393                 return PMINFO_R_ENOENT;
394         for (auto result : result_list) {
395                 if (result.size() != 2)
396                         return PMINFO_R_ERROR;
397                 if (result.front().empty() || result.front().size() == 0 ||
398                                 result.back().empty() || result.back().size() == 0)
399                         return PMINFO_R_ERROR;
400                 *appid = strdup(result.front().c_str());
401                 *trusted = strdup(result.back().c_str());
402                 if (*appid == nullptr || *trusted == nullptr) {
403                         LOG(ERROR) << "Out of memory";
404                         return PMINFO_R_ERROR;
405                 }
406         }
407
408         return PMINFO_R_OK;
409 }
410
411 extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges(
412                 const char *providerid, const char *type, uid_t uid,
413                 GList **privileges) {
414         char *query = nullptr;
415         query = sqlite3_mprintf(
416                         "SELECT privilege FROM package_app_data_control_privilege "
417                         "WHERE providerid=%Q AND type=%Q", providerid, type);
418         if (query == nullptr) {
419                 LOGE("Out of memory");
420                 return PMINFO_R_ERROR;
421         }
422
423         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
424                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
425           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
426           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
427
428         pkgmgr_client::PkgInfoClient client(parcelable, uid,
429                         pkgmgr_common::ReqType::QUERY);
430         if (!client.SendRequest()) {
431                 sqlite3_free(query);
432                 return PMINFO_R_ERROR;
433         }
434         // TODO: deliver rawdata to reqhandler directly if server is not working
435
436         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
437                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
438                                         client.GetResultParcel()));
439
440         if (return_parcel == nullptr) {
441                 LOG(ERROR) << "Fail to get return parcelable";
442                 return PMINFO_R_ERROR;
443         }
444
445         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
446                 LOG(ERROR) << "Request fail";
447                 return return_parcel->GetRequestResult();
448         }
449
450         tizen_base::Parcel parcel;
451         parcel.ReadParcelable(return_parcel.get());
452         sqlite3_free(query);
453         // result_list is vector of string vector
454         auto result_list = return_parcel->GetResult();
455         if (result_list.size() == 0)
456                 return PMINFO_R_ENOENT;
457
458         for (auto result : result_list) {
459                 if (result.size() != 1)
460                         return PMINFO_R_ERROR;
461                 if (result.front().empty() || result.front().size() == 0)
462                         return PMINFO_R_ERROR;
463                 char *privilege = strdup(result.front().c_str());
464                 if (privilege == nullptr) {
465                         LOG(ERROR) << "Out of memory";
466                         return PMINFO_R_ERROR;
467                 }
468                 *privileges = g_list_append(*privileges, privilege);
469         }
470
471         return PMINFO_R_OK;
472 }
473
474 extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges(
475                 const char *appid, const char *operation, uid_t uid, GList **privileges) {
476         char *query = nullptr;
477         query = sqlite3_mprintf(
478                 "SELECT app_control, privilege FROM package_app_app_control_privilege "
479                 "WHERE app_id=%Q", appid);
480         if (query == nullptr) {
481                 LOG(ERROR) << "Out of memory";
482                 return PMINFO_R_ERROR;
483         }
484
485         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
486                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
487           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
488           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
489
490         pkgmgr_client::PkgInfoClient client(parcelable, uid,
491                         pkgmgr_common::ReqType::QUERY);
492         if (!client.SendRequest()) {
493                 sqlite3_free(query);
494                 return PMINFO_R_ERROR;
495         }
496         // TODO: deliver rawdata to reqhandler directly if server is not working
497
498         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
499                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
500                                         client.GetResultParcel()));
501
502         if (return_parcel == nullptr) {
503                 LOG(ERROR) << "Fail to get return parcelable";
504                 return PMINFO_R_ERROR;
505         }
506
507         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
508                 LOG(ERROR) << "Request fail";
509                 return return_parcel->GetRequestResult();
510         }
511
512         tizen_base::Parcel parcel;
513         parcel.ReadParcelable(return_parcel.get());
514         sqlite3_free(query);
515         // result_list is vector of string vector
516         auto result_list = return_parcel->GetResult();
517         if (result_list.size() == 0)
518                 return PMINFO_R_ENOENT;
519
520         for (auto result : result_list) {
521                 if (result.size() != 2)
522                         return PMINFO_R_ERROR;
523                 if (result.front().empty() || result.front().size() == 0 ||
524                                 result.back().empty() || result.back().size() == 0)
525                         return PMINFO_R_ERROR;
526                 std::string app_control = result.front();
527                 std::stringstream ss(app_control);
528                 std::string token;
529                 while (std::getline(ss, token, '|')) {
530                         if (token.compare(std::string(operation))) {
531                                 char *privilege = strdup(result.back().c_str());
532                                 if (privilege == nullptr) {
533                                         LOG(ERROR) << "Out of memory";
534                                         return PMINFO_R_ERROR;
535                                 }
536                                 *privileges = g_list_append(*privileges, privilege);
537                         }
538                 }
539         }
540         return PMINFO_R_OK;
541 }
542
543 extern "C" EXPORT_API int _plugininfo_get_appids(
544                 const char *pkgid, const char *plugin_type,
545                 const char *plugin_name, GList **list) {
546         if (!pkgid || !plugin_type || !plugin_name || !list) {
547                 LOG(ERROR) << "Invalid parameter";
548                 return PMINFO_R_EINVAL;
549         }
550
551         char *query = nullptr;
552         query = sqlite3_mprintf(
553                         "SELECT appid FROM "
554                         "package_plugin_info WHERE pkgid=%Q AND "
555                         "plugin_type=%Q AND plugin_name=%Q",
556       pkgid, plugin_type, plugin_name);
557         if (query == nullptr) {
558                 LOG(ERROR) << "Out of memory";
559                 return PMINFO_R_ERROR;
560         }
561
562         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
563                         new pkgmgr_common::parcel::QueryParcelable(_getuid(), std::string(query),
564           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
565           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
566         sqlite3_free(query);
567
568         pkgmgr_client::PkgInfoClient client(parcelable, _getuid(),
569                         pkgmgr_common::ReqType::QUERY);
570         if (!client.SendRequest()) {
571                 return PMINFO_R_ERROR;
572         }
573
574         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
575                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
576                                         client.GetResultParcel()));
577
578         if (return_parcel == nullptr) {
579                 LOG(ERROR) << "Fail to get return parcelable";
580                 return PMINFO_R_ERROR;
581         }
582
583         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
584                 LOG(ERROR) << "Request fail";
585                 return return_parcel->GetRequestResult();
586         }
587
588   if (return_parcel->GetCol() != 1) {
589     LOG(ERROR) << "Invalid result";
590     return PMINFO_R_ERROR;
591   }
592         // result_list is vector of string vector
593         auto& result_list = return_parcel->GetResult();
594         if (result_list.size() == 0)
595                 return PMINFO_R_ENOENT;
596
597         for (auto result : result_list) {
598     if (result.size() != 1) {
599       LOG(ERROR) << "Invalid result";
600       g_list_free_full(*list, free);
601       return PMINFO_R_ERROR;
602     }
603     *list = g_list_append(*list, strdup(result[0].c_str()));
604   }
605
606   return PMINFO_R_OK;
607 }
608
609 static int __convert_update_type(const char *type, pkgmgrinfo_updateinfo_update_type *convert_type)
610 {
611         if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_NONE,
612                         strlen(PMINFO_UPDATEINFO_TYPE_NONE)) == 0)
613                 *convert_type = PMINFO_UPDATEINFO_NONE;
614         else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_FORCE,
615                         strlen(PMINFO_UPDATEINFO_TYPE_FORCE)) == 0)
616                 *convert_type = PMINFO_UPDATEINFO_FORCE;
617         else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_OPTIONAL,
618                         strlen(PMINFO_UPDATEINFO_TYPE_OPTIONAL)) == 0)
619                 *convert_type = PMINFO_UPDATEINFO_OPTIONAL;
620         else
621                 return -1;
622         return 0;
623 }
624
625 static void __free_update_info(gpointer data)
626 {
627         updateinfo_x *update_info = (updateinfo_x *)data;
628         if (update_info == nullptr)
629                 return;
630
631         if (update_info->pkgid)
632                 free((void *)update_info->pkgid);
633         if (update_info->version)
634                 free((void *)update_info->version);
635         free((void *)update_info);
636
637 }
638
639 extern "C" EXPORT_API int _get_pkg_updateinfo(const char *pkgid,
640                 GSList **update_info_list, uid_t uid)
641 {
642         char *query = nullptr;
643         int ret;
644
645         if (pkgid == nullptr)
646                 query = sqlite3_mprintf(
647                                                 "SELECT package, update_version, update_type "
648                                                 "FROM package_update_info");
649         else
650                 query = sqlite3_mprintf(
651                                                 "SELECT package, update_version, update_type "
652                                                 "FROM package_update_info WHERE package=%Q",
653                                                 pkgid);
654         if (query == nullptr) {
655                 LOG(ERROR) << "Out of memory";
656                 return PMINFO_R_ERROR;
657         }
658
659         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
660                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
661           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
662           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
663         sqlite3_free(query);
664
665         pkgmgr_client::PkgInfoClient client(parcelable, uid,
666                         pkgmgr_common::ReqType::QUERY);
667         if (!client.SendRequest()) {
668                 return PMINFO_R_ERROR;
669         }
670
671         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
672                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
673                                         client.GetResultParcel()));
674
675         if (return_parcel == nullptr) {
676                 LOG(ERROR) << "Fail to get return parcelable";
677                 return PMINFO_R_ERROR;
678         }
679
680         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
681                 LOG(ERROR) << "Request fail";
682                 return return_parcel->GetRequestResult();
683         }
684
685   if (return_parcel->GetCol() != 3) {
686     LOG(ERROR) << "Invalid result";
687     return PMINFO_R_ERROR;
688   }
689
690         auto result_list = return_parcel->GetResult();
691         if (result_list.size() == 0)
692                 return PMINFO_R_ENOENT;
693
694         for (auto result : result_list) {
695     if (result.size() != 3) {
696       LOG(ERROR) << "Invalid result";
697       g_slist_free_full(*update_info_list, __free_update_info);
698       return PMINFO_R_ERROR;
699     }
700     updateinfo_x *update_info = reinterpret_cast<updateinfo_x *>(calloc(1, sizeof(updateinfo_x)));
701                 if (update_info == nullptr) {
702                         LOG(ERROR) << "Out of memory";
703                         return PMINFO_R_ERROR;
704                 }
705     update_info->pkgid = strdup(result[0].c_str());
706     update_info->version = strdup(result[1].c_str());
707           pkgmgrinfo_updateinfo_update_type convert_type;
708                 ret = __convert_update_type(result[2].c_str(), &convert_type);
709                 if (ret != 0) {
710                         __free_update_info(update_info);
711       g_slist_free_full(*update_info_list, __free_update_info);
712                         return PMINFO_R_ERROR;
713                 }
714                 update_info->type = static_cast<int>(convert_type);
715                 *update_info_list = g_slist_prepend(*update_info_list,
716                                 update_info);
717   }
718
719         return PMINFO_R_OK;
720 }
721
722 extern "C" EXPORT_API int _pkginfo_set_usr_installed_storage(const char *pkgid,
723                 INSTALL_LOCATION location, const char *external_pkg_path,
724                 uid_t uid)
725 {
726         char *query = nullptr;
727         const char *location_str;
728         std::vector<std::string> queries;
729
730         if (location == INSTALL_INTERNAL)
731                 location_str = "installed_internal";
732         else if (location == INSTALL_EXTERNAL)
733                 location_str = "installed_external";
734         else
735                 location_str = "installed_extended";
736         /* pkgcakge_info table */
737         query = sqlite3_mprintf(
738                         "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
739                         location_str, external_pkg_path, pkgid);
740         queries.emplace_back(query);
741         sqlite3_free(query);
742
743         /* package_app_info table */
744         query = sqlite3_mprintf(
745                         "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
746                         location_str, external_pkg_path, pkgid);
747         queries.emplace_back(query);
748         sqlite3_free(query);
749
750         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
751                         new pkgmgr_common::parcel::QueryParcelable(uid, queries,
752                                         pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
753                                         pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
754
755         pkgmgr_client::PkgInfoClient client(parcelable, uid,
756                         pkgmgr_common::ReqType::QUERY);
757         if (!client.SendRequest()) {
758                 return PMINFO_R_ERROR;
759         }
760
761         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
762                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
763                                         client.GetResultParcel()));
764
765         if (return_parcel == nullptr) {
766                 LOG(ERROR) << "Fail to get return parcelable";
767                 return PMINFO_R_ERROR;
768         }
769
770         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
771                 LOG(ERROR) << "Request fail";
772                 return return_parcel->GetRequestResult();
773         }
774
775         auto result_list = return_parcel->GetResult();
776         if (result_list.size() != 1) {
777                 LOG(ERROR) << "Invalid result";
778                 return PMINFO_R_ERROR;
779         }
780
781         if (result_list[0].size() != 1) {
782                 LOG(ERROR) << "Invalid result";
783                 return PMINFO_R_ERROR;
784         }
785
786         LOG(ERROR) << "result : " << result_list[0][0];
787         if (result_list[0][0] != "SUCCESS")
788                 return PMINFO_R_ERROR;
789
790         return PMINFO_R_OK;
791 }
792
793 extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char *l_pkgid,
794                 const char *r_pkgid, pkgmgrinfo_cert_compare_result_type_e *result) {
795         char *query = sqlite3_mprintf("SELECT package, "
796                         "COALESCE(author_signer_cert, -1) FROM package_cert_info WHERE "
797                         "package IN (%Q, %Q)", l_pkgid, r_pkgid);
798         if (query == nullptr) {
799                 LOG(ERROR) << "Out of memory";
800                 return PMINFO_R_ERROR;
801         }
802         std::vector<std::string> queries;
803         queries.emplace_back(query);
804         sqlite3_free(query);
805
806         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
807                 new pkgmgr_common::parcel::QueryParcelable(0, queries,
808                                 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_CERTDB,
809                                 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
810         pkgmgr_client::PkgInfoClient client(parcelable, 0,
811                         pkgmgr_common::ReqType::QUERY);
812         if (!client.SendRequest()) {
813                 return PMINFO_R_ERROR;
814         }
815
816         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
817                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
818                                         client.GetResultParcel()));
819
820         if (return_parcel == nullptr) {
821                 LOG(ERROR) << "Fail to get return parcelable";
822                 return PMINFO_R_ERROR;
823         }
824
825         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
826                 LOG(ERROR) << "Request fail";
827                 return return_parcel->GetRequestResult();
828         }
829
830         auto certinfo_list = return_parcel->GetResult();
831         if (certinfo_list.size() != 2)
832                 return PMINFO_R_ERROR;
833
834         std::map<std::string, std::string> result_map;
835         for (auto& certinfo : certinfo_list)
836                 result_map.insert(make_pair(certinfo.front(), certinfo.back()));
837
838         if (result_map.find(std::string(l_pkgid))->second == "-1" &&
839                         result_map.find(std::string(r_pkgid))->second == "-1")
840                 *result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
841         else if (result_map.find(std::string(l_pkgid))->second == "-1")
842                 *result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
843         else if (result_map.find(std::string(r_pkgid))->second == "-1")
844                 *result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
845         else if (result_map.find(std::string(l_pkgid))->second ==
846                         result_map.find(std::string(r_pkgid))->second)
847                 *result = PMINFO_CERT_COMPARE_MATCH;
848         else
849                 *result = PMINFO_CERT_COMPARE_MISMATCH;
850
851         return PMINFO_R_OK;
852 }
853
854 extern "C" EXPORT_API int _certinfo_compare_app_certinfo(uid_t uid,
855                 const char *l_appid, const char *r_appid,
856                 pkgmgrinfo_cert_compare_result_type_e *result) {
857         char* query = sqlite3_mprintf("SELECT app_id, package FROM "
858                         "package_app_info WHERE app_id IN (%Q, %Q)", l_appid, r_appid);
859         if (query == nullptr) {
860                 LOG(ERROR) << "Out of memory";
861                 return PMINFO_R_ERROR;
862         }
863         std::vector<std::string> queries;
864         queries.emplace_back(query);
865         sqlite3_free(query);
866
867         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
868                 new pkgmgr_common::parcel::QueryParcelable(uid, queries,
869                                 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
870                                 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
871         pkgmgr_client::PkgInfoClient client(parcelable, uid,
872                         pkgmgr_common::ReqType::QUERY);
873         if (!client.SendRequest()) {
874                 return PMINFO_R_ERROR;
875         }
876
877         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
878                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
879                                         client.GetResultParcel()));
880
881         if (return_parcel == nullptr) {
882                 LOG(ERROR) << "Fail to get return parcelable";
883                 return PMINFO_R_ERROR;
884         }
885
886         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
887                 LOG(ERROR) << "Request fail";
888                 return return_parcel->GetRequestResult();
889         }
890
891         auto pkgid_list = return_parcel->GetResult();
892         std::map<std::string, std::string> result_map;
893         for (auto& pkgid : pkgid_list)
894                 result_map.insert(make_pair(pkgid.front(), pkgid.back()));
895
896         if (result_map.find(std::string(l_appid)) == result_map.end()) {
897                 LOG(ERROR) << "Cannot find pkgid of app " << l_appid
898                                 << " for uid " << uid;
899                 return PMINFO_R_ENOENT;
900         } else if (result_map.find(std::string(r_appid)) == result_map.end()) {
901                 LOG(ERROR) << "Cannot find pkgid of app " << r_appid
902                                 << " for uid " << uid;
903                 return PMINFO_R_ENOENT;
904         }
905
906         const char* l_pkgid = result_map.find(
907                         std::string(l_appid))->second.c_str();
908         const char* r_pkgid = result_map.find(
909                         std::string(r_appid))->second.c_str();
910
911         return _certinfo_compare_pkg_certinfo(l_pkgid, r_pkgid, result);
912 }
913
914 extern "C" EXPORT_API int _parser_execute_write_query(const char *query, uid_t uid)
915 {
916         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
917                         new pkgmgr_common::parcel::QueryParcelable(uid, query,
918                                         pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
919                                         pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
920
921         pkgmgr_client::PkgInfoClient client(parcelable, uid,
922                         pkgmgr_common::ReqType::QUERY);
923         if (!client.SendRequest()) {
924                 return -1;
925         }
926
927         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
928                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
929                                         client.GetResultParcel()));
930
931         if (return_parcel == nullptr) {
932                 LOG(ERROR) << "Fail to get return parcelable";
933                 return -1;
934         }
935
936         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
937                 LOG(ERROR) << "Request fail";
938                 return -1;
939         }
940
941         auto result_list = return_parcel->GetResult();
942         if (result_list.size() != 1) {
943                 LOG(ERROR) << "Invalid result";
944                 return -1;
945         }
946
947         if (result_list[0].size() != 1) {
948                 LOG(ERROR) << "Invalid result";
949                 return -1;
950         }
951
952         LOG(ERROR) << "result : " << result_list[0][0];
953         if (result_list[0][0] != "SUCCESS")
954                 return -1;
955
956         return 0;
957 }
958
959 extern "C" EXPORT_API int _parser_execute_write_queries(const char **queries, int len, uid_t uid)
960 {
961         std::vector<std::string> query_vt;
962         for (int i = 0; i < len; ++i)
963                 query_vt.emplace_back(queries[i]);
964
965         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
966                         new pkgmgr_common::parcel::QueryParcelable(uid, query_vt,
967                                         pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
968                                         pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
969
970         pkgmgr_client::PkgInfoClient client(parcelable, uid,
971                         pkgmgr_common::ReqType::QUERY);
972         if (!client.SendRequest()) {
973                 return -1;
974         }
975
976         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
977                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
978                                         client.GetResultParcel()));
979
980         if (return_parcel == nullptr) {
981                 LOG(ERROR) << "Fail to get return parcelable";
982                 return -1;
983         }
984
985         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
986                 LOG(ERROR) << "Request fail";
987                 return -1;
988         }
989
990         auto result_list = return_parcel->GetResult();
991         if (result_list.size() != 1) {
992                 LOG(ERROR) << "Invalid result";
993                 return -1;
994         }
995
996         if (result_list[0].size() != 1) {
997                 LOG(ERROR) << "Invalid result";
998                 return -1;
999         }
1000
1001         LOG(ERROR) << "result : " << result_list[0][0];
1002         if (result_list[0][0] != "SUCCESS")
1003                 return -1;
1004
1005         return 0;
1006 }
1007
1008 extern "C" EXPORT_API int _parser_insert_manifest_info(manifest_x *mfx, uid_t uid)
1009 {
1010         std::vector<package_x *> vt { mfx };
1011
1012         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1013                         new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Insert));
1014
1015         pkgmgr_client::PkgInfoClient client(parcelable, uid,
1016                         pkgmgr_common::ReqType::SET_PKG_INFO);
1017         if (!client.SendRequest()) {
1018                 return -1;
1019         }
1020
1021         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
1022                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
1023                                         client.GetResultParcel()));
1024
1025         if (return_parcel == nullptr) {
1026                 LOG(ERROR) << "Fail to get return parcelable";
1027                 return -1;
1028         }
1029
1030         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
1031                 LOG(ERROR) << "Request fail";
1032                 return -1;
1033         }
1034
1035         return 0;
1036 }
1037
1038 extern "C" EXPORT_API int _parser_update_manifest_info(manifest_x *mfx, uid_t uid)
1039 {
1040         std::vector<package_x *> vt { mfx };
1041
1042         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1043                         new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Update));
1044
1045         pkgmgr_client::PkgInfoClient client(parcelable, uid,
1046                         pkgmgr_common::ReqType::SET_PKG_INFO);
1047         if (!client.SendRequest()) {
1048                 return -1;
1049         }
1050
1051         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
1052                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
1053                                         client.GetResultParcel()));
1054
1055         if (return_parcel == nullptr) {
1056                 LOG(ERROR) << "Fail to get return parcelable";
1057                 return -1;
1058         }
1059
1060         if (return_parcel->GetRequestResult() != PM_PARSER_R_OK) {
1061                 LOG(ERROR) << "Request fail";
1062                 return -1;
1063         }
1064
1065         return 0;
1066 }
1067
1068 extern "C" EXPORT_API int _parser_delete_manifest_info(manifest_x *mfx, uid_t uid)
1069 {
1070         std::vector<package_x *> vt { mfx };
1071
1072         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1073                         new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Delete));
1074
1075         pkgmgr_client::PkgInfoClient client(parcelable, uid,
1076                         pkgmgr_common::ReqType::SET_PKG_INFO);
1077         if (!client.SendRequest()) {
1078                 return -1;
1079         }
1080
1081         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
1082                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
1083                                         client.GetResultParcel()));
1084
1085         if (return_parcel == nullptr) {
1086                 LOG(ERROR) << "Fail to get return parcelable";
1087                 return -1;
1088         }
1089
1090         if (return_parcel->GetRequestResult() != PM_PARSER_R_OK) {
1091                 LOG(ERROR) << "Request fail";
1092                 return -1;
1093         }
1094
1095         return 0;
1096 }
1097
1098 extern "C" EXPORT_API int _pkginfo_insert_certinfo(const char* pkgid,
1099                 pkgmgr_certinfo_x* cert, uid_t uid)
1100 {
1101         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1102                         new pkgmgr_common::parcel::CertInfoParcelable(uid, cert));
1103
1104         pkgmgr_client::PkgInfoClient client(parcelable, uid,
1105                         pkgmgr_common::ReqType::SET_CERT_INFO);
1106         if (!client.SendRequest())
1107         return PMINFO_R_ERROR;
1108
1109         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
1110                 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
1111                                 client.GetResultParcel()));
1112
1113         if (return_parcel == nullptr) {
1114                 LOG(ERROR) << "Fail to get return parcelable";
1115                 return PMINFO_R_ERROR;
1116         }
1117
1118         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
1119                 LOG(ERROR) << "Request fail";
1120                 return return_parcel->GetRequestResult();
1121         }
1122
1123         return PMINFO_R_OK;
1124 }
1125
1126 extern "C" EXPORT_API int _pkginfo_get_certinfo(const char *pkgid,
1127                 pkgmgr_certinfo_x** cert, uid_t uid)
1128 {
1129         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1130                         new pkgmgr_common::parcel::CertInfoParcelable(uid,
1131                                         std::string(pkgid)));
1132
1133         pkgmgr_client::PkgInfoClient client(parcelable, uid,
1134                         pkgmgr_common::ReqType::GET_CERT_INFO);
1135         if (!client.SendRequest())
1136         return PMINFO_R_ERROR;
1137
1138         std::shared_ptr<pkgmgr_common::parcel::CertInfoParcelable> return_parcel(
1139                 std::static_pointer_cast<pkgmgr_common::parcel::CertInfoParcelable>(
1140                                 client.GetResultParcel()));
1141
1142         if (return_parcel == nullptr) {
1143                 LOG(ERROR) << "Fail to get return parcelable";
1144                 return PMINFO_R_ERROR;
1145         }
1146
1147         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
1148                 LOG(ERROR) << "Request fail";
1149                 return return_parcel->GetRequestResult();
1150         }
1151
1152         tizen_base::Parcel parcel;
1153         parcel.ReadParcelable(return_parcel.get());
1154         auto certinfo = return_parcel->GetCertInfo();
1155         if (certinfo == nullptr)
1156                 return PMINFO_R_ERROR;
1157
1158         *cert = (pkgmgr_certinfo_x*)certinfo;
1159         return PMINFO_R_OK;
1160 }
1161
1162 extern "C" EXPORT_API int _pkginfo_delete_certinfo(const char *pkgid)
1163 {
1164         char* query = sqlite3_mprintf("UPDATE package_cert_info SET "
1165                         "package_count = package_count - 1 WHERE package=%Q", pkgid);
1166         if (query == nullptr) {
1167                 LOG(ERROR) << "Out of memory";
1168                 return PMINFO_R_ERROR;
1169         }
1170
1171         std::vector<std::string> queries;
1172         queries.emplace_back(query);
1173         sqlite3_free(query);
1174
1175         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1176                         new pkgmgr_common::parcel::QueryParcelable(0, queries,
1177                                         pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_CERTDB,
1178                                         pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
1179
1180         pkgmgr_client::PkgInfoClient client(parcelable, 0,
1181                         pkgmgr_common::ReqType::QUERY);
1182         if (!client.SendRequest()) {
1183                 return PMINFO_R_ERROR;
1184         }
1185
1186         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
1187                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
1188                                         client.GetResultParcel()));
1189
1190         if (return_parcel == nullptr) {
1191                 LOG(ERROR) << "Fail to get return parcelable";
1192                 return PMINFO_R_ERROR;
1193         }
1194
1195         if (return_parcel->GetRequestResult() != PMINFO_R_OK) {
1196                 LOG(ERROR) << "Request fail";
1197                 return return_parcel->GetRequestResult();
1198         }
1199
1200         auto result_list = return_parcel->GetResult();
1201         if (result_list.size() != 1) {
1202                 LOG(ERROR) << "Invalid result";
1203                 return PMINFO_R_ERROR;
1204         }
1205
1206         if (result_list[0].size() != 1) {
1207                 LOG(ERROR) << "Invalid result";
1208                 return PMINFO_R_ERROR;
1209         }
1210
1211         LOG(ERROR) << "result : " << result_list[0][0];
1212         if (result_list[0][0] != "SUCCESS")
1213                 return PMINFO_R_ERROR;
1214
1215         return PMINFO_R_OK;
1216 }