Implement pkgmgr_parser_insert_manifest_info_in_usr_db
[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 <string>
22
23 #include "sqlite3.h"
24 #include "glib.h"
25
26 #include "pkgmgrinfo_private.h"
27
28 #include "logging.hh"
29 #include "common/database/abstract_db_handler.hh"
30 #include "common/database/pkg_set_db_handler.hh"
31 #include "common/parcel/appinfo_parcelable.hh"
32 #include "common/parcel/filter_parcelable.hh"
33 #include "common/parcel/pkginfo_parcelable.hh"
34 #include "common/parcel/query_parcelable.hh"
35 #include "common/parcel/result_parcelable.hh"
36
37 #include "client/pkginfo_client.hh"
38
39 #include <parcel.hh>
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #endif
44 #define LOG_TAG "PKGMGR_INFO"
45
46 #ifdef EXPORT_API
47 #undef EXPORT_API
48 #endif
49 #define EXPORT_API __attribute__((visibility("default")))
50
51 extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
52             pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
53         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
54                         new pkgmgr_common::parcel::FilterParcelable(uid,
55                                         static_cast<pkgmgrinfo_filter_x*>(filter), flag));
56
57         pkgmgr_client::PkgInfoClient client(parcelable, uid,
58                         pkgmgr_common::ReqType::GET_PKG_INFO);
59         if (!client.SendRequest())
60       return PMINFO_R_ERROR;
61
62         std::shared_ptr<pkgmgr_common::parcel::PkgInfoParcelable> return_parcel(
63                         std::static_pointer_cast<pkgmgr_common::parcel::PkgInfoParcelable>(
64                                         client.GetResultParcel()));
65
66         tizen_base::Parcel parcel;
67         parcel.ReadParcelable(return_parcel.get());
68
69         auto result_list = return_parcel->GetPkgInfo();
70         // TODO: check noentry error has returned if size of result_list is 0
71         for (auto& pkginfo : result_list)
72                 g_hash_table_insert(packages, (gpointer)pkginfo->package,
73                                 (gpointer)pkginfo);
74
75         return PMINFO_R_OK;
76 }
77
78 // TODO: Need to add target db uid to identify which database to be searched
79 extern "C" EXPORT_API int _appinfo_get_applications(uid_t db_uid, uid_t uid,
80                 pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
81         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
82                         new pkgmgr_common::parcel::FilterParcelable(uid,
83                                         static_cast<pkgmgrinfo_filter_x*>(filter), flag));
84
85         pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::GET_APP_INFO);
86         if (!client.SendRequest())
87       return PMINFO_R_ERROR;
88
89         std::shared_ptr<pkgmgr_common::parcel::AppInfoParcelable> return_parcel(
90                         std::static_pointer_cast<pkgmgr_common::parcel::AppInfoParcelable>(
91                                         client.GetResultParcel()));
92
93         tizen_base::Parcel parcel;
94         parcel.ReadParcelable(return_parcel.get());
95         auto result_list = return_parcel->GetAppInfo();
96         for (auto& appinfo : result_list)
97                 g_hash_table_insert(packages, (gpointer)appinfo->appid,
98                                 (gpointer)appinfo);
99
100         return PMINFO_R_OK;
101 }
102
103 extern "C" EXPORT_API char *_appinfo_get_localed_label(
104                 const char *appid, const char *locale, uid_t uid) {
105         char *query = nullptr;
106     query = sqlite3_mprintf(
107                         "SELECT COALESCE((SELECT app_label FROM package_app_localized_info "
108                         "WHERE app_id=%Q AND app_locale=%Q),"
109                         "(SELECT app_label FROM package_app_localized_info WHERE "
110                         "app_id=%Q AND app_locale='No Locale'))", appid, locale, appid);
111         if (query == nullptr) {
112                 LOG(ERROR) << "Out of memory";
113                 return nullptr;
114         }
115
116         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
117                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
118           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
119           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
120
121         pkgmgr_client::PkgInfoClient client(parcelable, uid,
122                         pkgmgr_common::ReqType::QUERY);
123         if (!client.SendRequest()) {
124                 sqlite3_free(query);
125                 return nullptr;
126         }
127
128         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
129                                 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
130                                                 client.GetResultParcel()));
131         tizen_base::Parcel parcel;
132         parcel.ReadParcelable(return_parcel.get());
133         sqlite3_free(query);
134         // result_list is vector of string vector
135         char *label = nullptr;
136         auto result_list = return_parcel->GetResult();
137         for (auto result : result_list) {
138                 // result is string vector
139                 // it only has one string or not.
140                 if (result.front().empty() || result.front().length() == 0)
141                         return nullptr;
142                 label = strdup(result.front().c_str());
143                 if (label == nullptr) {
144                         LOG(ERROR) << "Out of memory";
145                         return nullptr;
146                 }
147                 return label;
148         }
149
150         return label;
151 }
152
153 extern "C" EXPORT_API int _appinfo_get_datacontrol_info(
154                 const char *providerid, const char *type, uid_t uid,
155                 char **appid, char **access) {
156         char *query = nullptr;
157         query = sqlite3_mprintf("SELECT app_id, access FROM "
158                         "package_app_data_control WHERE "
159                         "providerid=%Q AND type=%Q", providerid, type);
160         if (query == nullptr) {
161                 LOG(ERROR) << "Out of memory";
162                 return PMINFO_R_ERROR;
163         }
164
165         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
166                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
167           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
168           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
169
170         pkgmgr_client::PkgInfoClient client(parcelable, uid,
171                         pkgmgr_common::ReqType::QUERY);
172         if (!client.SendRequest()) {
173                 sqlite3_free(query);
174                 return PMINFO_R_ERROR;
175         }
176
177         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
178                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
179                                         client.GetResultParcel()));
180         tizen_base::Parcel parcel;
181         parcel.ReadParcelable(return_parcel.get());
182         sqlite3_free(query);
183         // result_list is vector of string vector
184         auto result_list = return_parcel->GetResult();
185         if (result_list.size() == 0)
186                 return PMINFO_R_ENOENT;
187         for (auto result : result_list) {
188                 if (result.size() != 2)
189                         return PMINFO_R_ERROR;
190                 if (result.front().empty() || result.front().size() == 0 ||
191                                 result.back().empty() || result.back().size() == 0)
192                         return PMINFO_R_ERROR;
193                 *appid = strdup(result.front().c_str());
194                 *access = strdup(result.back().c_str());
195                 if (*appid == nullptr || *access == nullptr) {
196                         LOG(ERROR) << "Out of memory";
197                         return PMINFO_R_ERROR;
198                 }
199         }
200
201         return PMINFO_R_OK;
202 }
203
204 extern "C" EXPORT_API int _appinfo_get_datacontrol_appid(
205                 const char *providerid, uid_t uid, char **appid) {
206         char *query = nullptr;
207
208         query = sqlite3_mprintf("SELECT app_id FROM package_app_data_control "
209                 "WHERE providerid=%Q", providerid);
210         if (query == NULL) {
211                 LOGE("Out of memory");
212                 return PMINFO_R_ERROR;
213         }
214         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
215                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
216           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
217           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
218
219         pkgmgr_client::PkgInfoClient client(parcelable, uid,
220                         pkgmgr_common::ReqType::QUERY);
221         if (!client.SendRequest()) {
222                 sqlite3_free(query);
223                 return PMINFO_R_ERROR;
224         }
225         // TODO: deliver rawdata to reqhandler directly if server is not working
226
227         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
228                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
229                                         client.GetResultParcel()));
230         tizen_base::Parcel parcel;
231         parcel.ReadParcelable(return_parcel.get());
232         sqlite3_free(query);
233
234         // result_list is vector of string vector
235         auto result_list = return_parcel->GetResult();
236         if (result_list.size() == 0)
237                 return PMINFO_R_ENOENT;
238         for (auto result : result_list) {
239                 if (result.size() != 1)
240                         return PMINFO_R_ERROR;
241                 if (result.front().empty() || result.front().size() == 0)
242                         return PMINFO_R_ERROR;
243                 *appid = strdup(result.front().c_str());
244                 if (*appid == nullptr) {
245                         LOG(ERROR) << "Out of memory";
246                         return PMINFO_R_ERROR;
247                 }
248         }
249
250         return PMINFO_R_OK;
251 }
252
253 extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info(
254                 const char *providerid, const char *type, uid_t uid,
255                 char **appid, char **trusted) {
256         char *query = nullptr;
257         query = sqlite3_mprintf(
258                         "SELECT app_id, trusted FROM package_app_data_control "
259                         "WHERE providerid=%Q AND type=%Q", providerid, type);
260         if (query == NULL) {
261                 LOGE("Out of memory");
262                 return PMINFO_R_ERROR;
263         }
264
265         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
266                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
267           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
268           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
269
270         pkgmgr_client::PkgInfoClient client(parcelable, uid,
271                         pkgmgr_common::ReqType::QUERY);
272         if (!client.SendRequest()) {
273                 sqlite3_free(query);
274                 return PMINFO_R_ERROR;
275         }
276         // TODO: deliver rawdata to reqhandler directly if server is not working
277
278         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
279                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
280                                         client.GetResultParcel()));
281         tizen_base::Parcel parcel;
282         parcel.ReadParcelable(return_parcel.get());
283         sqlite3_free(query);
284         // result_list is vector of string vector
285         auto result_list = return_parcel->GetResult();
286         if (result_list.size() == 0)
287                 return PMINFO_R_ENOENT;
288         for (auto result : result_list) {
289                 if (result.size() != 2)
290                         return PMINFO_R_ERROR;
291                 if (result.front().empty() || result.front().size() == 0 ||
292                                 result.back().empty() || result.back().size() == 0)
293                         return PMINFO_R_ERROR;
294                 *appid = strdup(result.front().c_str());
295                 *trusted = strdup(result.back().c_str());
296                 if (*appid == nullptr || *trusted == nullptr) {
297                         LOG(ERROR) << "Out of memory";
298                         return PMINFO_R_ERROR;
299                 }
300         }
301
302         return PMINFO_R_OK;
303 }
304
305 extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges(
306                 const char *providerid, const char *type, uid_t uid,
307                 GList **privileges) {
308         char *query = nullptr;
309         query = sqlite3_mprintf(
310                         "SELECT privilege FROM package_app_data_control_privilege "
311                         "WHERE providerid=%Q AND type=%Q", providerid, type);
312         if (query == NULL) {
313                 LOGE("Out of memory");
314                 return PMINFO_R_ERROR;
315         }
316
317         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
318                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
319           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
320           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
321
322         pkgmgr_client::PkgInfoClient client(parcelable, uid,
323                         pkgmgr_common::ReqType::QUERY);
324         if (!client.SendRequest()) {
325                 sqlite3_free(query);
326                 return PMINFO_R_ERROR;
327         }
328         // TODO: deliver rawdata to reqhandler directly if server is not working
329
330         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
331                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
332                                         client.GetResultParcel()));
333         tizen_base::Parcel parcel;
334         parcel.ReadParcelable(return_parcel.get());
335         sqlite3_free(query);
336         // result_list is vector of string vector
337         auto result_list = return_parcel->GetResult();
338         if (result_list.size() == 0)
339                 return PMINFO_R_ENOENT;
340
341         for (auto result : result_list) {
342                 if (result.size() != 1)
343                         return PMINFO_R_ERROR;
344                 if (result.front().empty() || result.front().size() == 0)
345                         return PMINFO_R_ERROR;
346                 char *privilege = strdup(result.front().c_str());
347                 if (privilege == nullptr) {
348                         LOG(ERROR) << "Out of memory";
349                         return PMINFO_R_ERROR;
350                 }
351                 *privileges = g_list_append(*privileges, privilege);
352         }
353
354         return PMINFO_R_OK;
355 }
356
357 extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges(
358                 const char *appid, const char *operation, uid_t uid, GList **privileges) {
359         char *query = nullptr;
360         query = sqlite3_mprintf(
361                 "SELECT app_control, privilege FROM package_app_app_control_privilege "
362                 "WHERE app_id=%Q", appid);
363         if (query == NULL) {
364                 LOG(ERROR) << "Out of memory";
365                 return PMINFO_R_ERROR;
366         }
367
368         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
369                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
370           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
371           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
372
373         pkgmgr_client::PkgInfoClient client(parcelable, uid,
374                         pkgmgr_common::ReqType::QUERY);
375         if (!client.SendRequest()) {
376                 sqlite3_free(query);
377                 return PMINFO_R_ERROR;
378         }
379         // TODO: deliver rawdata to reqhandler directly if server is not working
380
381         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
382                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
383                                         client.GetResultParcel()));
384         tizen_base::Parcel parcel;
385         parcel.ReadParcelable(return_parcel.get());
386         sqlite3_free(query);
387         // result_list is vector of string vector
388         auto result_list = return_parcel->GetResult();
389         if (result_list.size() == 0)
390                 return PMINFO_R_ENOENT;
391
392         for (auto result : result_list) {
393                 if (result.size() != 2)
394                         return PMINFO_R_ERROR;
395                 if (result.front().empty() || result.front().size() == 0 ||
396                                 result.back().empty() || result.back().size() == 0)
397                         return PMINFO_R_ERROR;
398                 std::string app_control = result.front();
399                 std::stringstream ss(app_control);
400                 std::string token;
401                 while (std::getline(ss, token, '|')) {
402                         if (token.compare(std::string(operation))) {
403                                 char *privilege = strdup(result.back().c_str());
404                                 if (privilege == nullptr) {
405                                         LOG(ERROR) << "Out of memory";
406                                         return PMINFO_R_ERROR;
407                                 }
408                                 *privileges = g_list_append(*privileges, privilege);
409                         }
410                 }
411         }
412         return PMINFO_R_OK;
413 }
414
415 extern "C" EXPORT_API int _plugininfo_get_appids(
416                 const char *pkgid, const char *plugin_type,
417                 const char *plugin_name, GList **list) {
418         if (!pkgid || !plugin_type || !plugin_name || !list) {
419                 LOG(ERROR) << "Invalid parameter";
420                 return PMINFO_R_EINVAL;
421         }
422
423         char *query = nullptr;
424         query = sqlite3_mprintf(
425                         "SELECT appid FROM "
426                         "package_plugin_info WHERE pkgid=%Q AND "
427                         "plugin_type=%Q AND plugin_name=%Q",
428       pkgid, plugin_type, plugin_name);
429         if (query == NULL) {
430                 LOG(ERROR) << "Out of memory";
431                 return PMINFO_R_ERROR;
432         }
433
434         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
435                         new pkgmgr_common::parcel::QueryParcelable(_getuid(), std::string(query),
436           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
437           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
438         sqlite3_free(query);
439
440         pkgmgr_client::PkgInfoClient client(parcelable, _getuid(),
441                         pkgmgr_common::ReqType::QUERY);
442         if (!client.SendRequest()) {
443                 return PMINFO_R_ERROR;
444         }
445
446         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
447                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
448                                         client.GetResultParcel()));
449   if (return_parcel->GetCol() != 1) {
450     LOG(ERROR) << "Invalid result";
451     return PMINFO_R_ERROR;
452   }
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       LOG(ERROR) << "Invalid result";
461       g_list_free_full(*list, free);
462       return PMINFO_R_ERROR;
463     }
464     *list = g_list_append(*list, strdup(result[0].c_str()));
465   }
466
467   return PMINFO_R_OK;
468 }
469
470 static int __convert_update_type(const char *type, pkgmgrinfo_updateinfo_update_type *convert_type)
471 {
472         if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_NONE,
473                         strlen(PMINFO_UPDATEINFO_TYPE_NONE)) == 0)
474                 *convert_type = PMINFO_UPDATEINFO_NONE;
475         else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_FORCE,
476                         strlen(PMINFO_UPDATEINFO_TYPE_FORCE)) == 0)
477                 *convert_type = PMINFO_UPDATEINFO_FORCE;
478         else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_OPTIONAL,
479                         strlen(PMINFO_UPDATEINFO_TYPE_OPTIONAL)) == 0)
480                 *convert_type = PMINFO_UPDATEINFO_OPTIONAL;
481         else
482                 return -1;
483         return 0;
484 }
485
486 static void __free_update_info(gpointer data)
487 {
488         updateinfo_x *update_info = (updateinfo_x *)data;
489         if (update_info == NULL)
490                 return;
491
492         if (update_info->pkgid)
493                 free((void *)update_info->pkgid);
494         if (update_info->version)
495                 free((void *)update_info->version);
496         free((void *)update_info);
497
498 }
499
500 extern "C" EXPORT_API int _get_pkg_updateinfo(const char *pkgid,
501                 GSList **update_info_list, uid_t uid)
502 {
503         char *query = nullptr;
504         int ret;
505
506         if (pkgid == NULL)
507                 query = sqlite3_mprintf(
508                                                 "SELECT package, update_version, update_type "
509                                                 "FROM package_update_info");
510         else
511                 query = sqlite3_mprintf(
512                                                 "SELECT package, update_version, update_type "
513                                                 "FROM package_update_info WHERE package=%Q",
514                                                 pkgid);
515         if (query == NULL) {
516                 LOG(ERROR) << "Out of memory";
517                 return PMINFO_R_ERROR;
518         }
519
520         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
521                         new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
522           pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
523           pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
524         sqlite3_free(query);
525
526         pkgmgr_client::PkgInfoClient client(parcelable, uid,
527                         pkgmgr_common::ReqType::QUERY);
528         if (!client.SendRequest()) {
529                 return PMINFO_R_ERROR;
530         }
531
532         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
533                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
534                                         client.GetResultParcel()));
535   if (return_parcel->GetCol() != 3) {
536     LOG(ERROR) << "Invalid result";
537     return PMINFO_R_ERROR;
538   }
539
540         auto result_list = return_parcel->GetResult();
541         if (result_list.size() == 0)
542                 return PMINFO_R_ENOENT;
543
544         for (auto result : result_list) {
545     if (result.size() != 3) {
546       LOG(ERROR) << "Invalid result";
547       g_slist_free_full(*update_info_list, __free_update_info);
548       return PMINFO_R_ERROR;
549     }
550     updateinfo_x *update_info = reinterpret_cast<updateinfo_x *>(calloc(1, sizeof(updateinfo_x)));
551                 if (update_info == NULL) {
552                         LOG(ERROR) << "Out of memory";
553                         return PMINFO_R_ERROR;
554                 }
555     update_info->pkgid = strdup(result[0].c_str());
556     update_info->version = strdup(result[1].c_str());
557           pkgmgrinfo_updateinfo_update_type convert_type;
558                 ret = __convert_update_type(result[2].c_str(), &convert_type);
559                 if (ret != 0) {
560                         __free_update_info(update_info);
561       g_slist_free_full(*update_info_list, __free_update_info);
562                         return PMINFO_R_ERROR;
563                 }
564                 update_info->type = static_cast<int>(convert_type);
565                 *update_info_list = g_slist_prepend(*update_info_list,
566                                 update_info);
567   }
568
569         return PMINFO_R_OK;
570 }
571
572 extern "C" EXPORT_API int _pkginfo_set_usr_installed_storage(const char *pkgid,
573                 INSTALL_LOCATION location, const char *external_pkg_path,
574                 uid_t uid)
575 {
576         char *query = NULL;
577         const char *location_str;
578         std::vector<std::string> queries;
579
580         if (location == INSTALL_INTERNAL)
581                 location_str = "installed_internal";
582         else if (location == INSTALL_EXTERNAL)
583                 location_str = "installed_external";
584         else
585                 location_str = "installed_extended";
586         /* pkgcakge_info table */
587         query = sqlite3_mprintf(
588                         "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
589                         location_str, external_pkg_path, pkgid);
590         queries.emplace_back(query);
591         sqlite3_free(query);
592
593         /* package_app_info table */
594         query = sqlite3_mprintf(
595                         "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
596                         location_str, external_pkg_path, pkgid);
597         queries.emplace_back(query);
598         sqlite3_free(query);
599
600         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
601                         new pkgmgr_common::parcel::QueryParcelable(uid, queries,
602                                         pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
603                                         pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
604
605         pkgmgr_client::PkgInfoClient client(parcelable, uid,
606                         pkgmgr_common::ReqType::QUERY);
607         if (!client.SendRequest()) {
608                 return PMINFO_R_ERROR;
609         }
610
611         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
612                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
613                                         client.GetResultParcel()));
614
615         auto result_list = return_parcel->GetResult();
616         if (result_list.size() != 1) {
617                 LOG(ERROR) << "Invalid result";
618                 return PMINFO_R_ERROR;
619         }
620
621         if (result_list[0].size() != 1) {
622                 LOG(ERROR) << "Invalid result";
623                 return PMINFO_R_ERROR;
624         }
625
626         LOG(ERROR) << "result : " << result_list[0][0];
627         if (result_list[0][0] != "SUCCESS")
628                 return PMINFO_R_ERROR;
629
630         return PMINFO_R_OK;
631 }
632
633 extern "C" EXPORT_API int _parser_execute_write_query(const char *query, uid_t uid)
634 {
635         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
636                         new pkgmgr_common::parcel::QueryParcelable(uid, query,
637                                         pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
638                                         pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
639
640         pkgmgr_client::PkgInfoClient client(parcelable, uid,
641                         pkgmgr_common::ReqType::QUERY);
642         if (!client.SendRequest()) {
643                 return -1;
644         }
645
646         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
647                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
648                                         client.GetResultParcel()));
649
650         auto result_list = return_parcel->GetResult();
651         if (result_list.size() != 1) {
652                 LOG(ERROR) << "Invalid result";
653                 return -1;
654         }
655
656         if (result_list[0].size() != 1) {
657                 LOG(ERROR) << "Invalid result";
658                 return -1;
659         }
660
661         LOG(ERROR) << "result : " << result_list[0][0];
662         if (result_list[0][0] != "SUCCESS")
663                 return -1;
664
665         return 0;
666 }
667
668 extern "C" EXPORT_API int _parser_execute_write_queries(const char **queries, int len, uid_t uid)
669 {
670         std::vector<std::string> query_vt;
671         for (int i = 0; i < len; ++i)
672                 query_vt.emplace_back(queries[i]);
673
674         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
675                         new pkgmgr_common::parcel::QueryParcelable(uid, query_vt,
676                                         pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
677                                         pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
678
679         pkgmgr_client::PkgInfoClient client(parcelable, uid,
680                         pkgmgr_common::ReqType::QUERY);
681         if (!client.SendRequest()) {
682                 return -1;
683         }
684
685         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
686                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
687                                         client.GetResultParcel()));
688
689         auto result_list = return_parcel->GetResult();
690         if (result_list.size() != 1) {
691                 LOG(ERROR) << "Invalid result";
692                 return -1;
693         }
694
695         if (result_list[0].size() != 1) {
696                 LOG(ERROR) << "Invalid result";
697                 return -1;
698         }
699
700         LOG(ERROR) << "result : " << result_list[0][0];
701         if (result_list[0][0] != "SUCCESS")
702                 return -1;
703
704         return 0;
705 }
706
707 extern "C" EXPORT_API int _parser_insert_manifest_info(manifest_x *mfx, uid_t uid)
708 {
709         std::vector<package_x *> vt { mfx };
710
711         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
712                         new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Insert));
713
714         pkgmgr_client::PkgInfoClient client(parcelable, uid,
715                         pkgmgr_common::ReqType::SET_PKG_INFO);
716         if (!client.SendRequest()) {
717                 return -1;
718         }
719
720         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
721                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
722                                         client.GetResultParcel()));
723
724         auto result_list = return_parcel->GetResult();
725         if (result_list.size() != 1) {
726                 LOG(ERROR) << "Invalid result";
727                 return -1;
728         }
729
730         if (result_list[0].size() != 1) {
731                 LOG(ERROR) << "Invalid result";
732                 return -1;
733         }
734
735         LOG(ERROR) << "result : " << result_list[0][0];
736         if (result_list[0][0] != "SUCCESS")
737                 return -1;
738
739         return 0;
740 }
741
742 extern "C" EXPORT_API int _parser_update_manifest_info(manifest_x *mfx, uid_t uid)
743 {
744         std::vector<package_x *> vt { mfx };
745
746         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
747                         new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Update));
748
749         pkgmgr_client::PkgInfoClient client(parcelable, uid,
750                         pkgmgr_common::ReqType::SET_PKG_INFO);
751         if (!client.SendRequest()) {
752                 return -1;
753         }
754
755         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
756                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
757                                         client.GetResultParcel()));
758
759         auto result_list = return_parcel->GetResult();
760         if (result_list.size() != 1) {
761                 LOG(ERROR) << "Invalid result";
762                 return -1;
763         }
764
765         if (result_list[0].size() != 1) {
766                 LOG(ERROR) << "Invalid result";
767                 return -1;
768         }
769
770         LOG(ERROR) << "result : " << result_list[0][0];
771         if (result_list[0][0] != "SUCCESS")
772                 return -1;
773
774         return 0;
775 }
776
777 extern "C" EXPORT_API int _parser_delete_manifest_info(manifest_x *mfx, uid_t uid)
778 {
779         std::vector<package_x *> vt { mfx };
780
781         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
782                         new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Delete));
783
784         pkgmgr_client::PkgInfoClient client(parcelable, uid,
785                         pkgmgr_common::ReqType::SET_PKG_INFO);
786         if (!client.SendRequest()) {
787                 return -1;
788         }
789
790         std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
791                         std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
792                                         client.GetResultParcel()));
793
794         auto result_list = return_parcel->GetResult();
795         if (result_list.size() != 1) {
796                 LOG(ERROR) << "Invalid result";
797                 return -1;
798         }
799
800         if (result_list[0].size() != 1) {
801                 LOG(ERROR) << "Invalid result";
802                 return -1;
803         }
804
805         LOG(ERROR) << "result : " << result_list[0][0];
806         if (result_list[0][0] != "SUCCESS")
807                 return -1;
808
809         return 0;
810 }