2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "manager/pkginfo_manager.h"
19 #include <sys/types.h>
27 #include "pkgmgr_parser.h"
28 #include "pkgmgrinfo_private.h"
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/command_parcelable.hh"
36 #include "common/parcel/depinfo_parcelable.hh"
37 #include "common/parcel/filter_parcelable.hh"
38 #include "common/parcel/pkginfo_parcelable.hh"
39 #include "common/parcel/query_parcelable.hh"
40 #include "common/parcel/result_parcelable.hh"
42 #include "client/pkginfo_client.hh"
49 #define LOG_TAG "PKGMGR_INFO"
54 #define EXPORT_API __attribute__((visibility("default")))
56 extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
57 pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
58 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
59 new pkgmgr_common::parcel::FilterParcelable(uid,
60 static_cast<pkgmgrinfo_filter_x*>(filter), flag));
62 pkgmgr_client::PkgInfoClient client(parcelable, uid,
63 pkgmgr_common::ReqType::GET_PKG_INFO);
64 if (!client.SendRequest())
65 return PMINFO_R_ERROR;
67 auto ptr = client.GetResultParcel();
69 LOG(ERROR) << "Fail to get return parcelable";
70 return PMINFO_R_ERROR;
73 if (ptr->GetRequestResult() != PMINFO_R_OK) {
74 LOG(ERROR) << "Request fail";
75 return ptr->GetRequestResult();
78 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::PkgInfo) {
79 LOG(ERROR) << "Invalid parcelable type";
80 return PMINFO_R_ERROR;
83 std::shared_ptr<pkgmgr_common::parcel::PkgInfoParcelable> return_parcel(
84 std::static_pointer_cast<pkgmgr_common::parcel::PkgInfoParcelable>(
87 auto result_list = return_parcel->GetPkgInfo();
88 // TODO: check noentry error has returned if size of result_list is 0
89 for (auto& pkginfo : result_list)
90 g_hash_table_insert(packages, (gpointer)pkginfo->package,
96 extern "C" EXPORT_API int _pkginfo_get_depends_on(uid_t uid,
97 const char *pkgid, GList **dependencies) {
98 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
99 new pkgmgr_common::parcel::DepInfoParcelable(std::string(pkgid)));
101 pkgmgr_client::PkgInfoClient client(parcelable, uid,
102 pkgmgr_common::ReqType::GET_PKG_DEP_INFO);
103 if (!client.SendRequest())
104 return PMINFO_R_ERROR;
106 auto ptr = client.GetResultParcel();
107 if (ptr == nullptr) {
108 LOG(ERROR) << "Fail to get return parcelable";
109 return PMINFO_R_ERROR;
112 if (ptr->GetRequestResult() != PMINFO_R_OK) {
113 LOG(ERROR) << "Request fail";
114 return ptr->GetRequestResult();
117 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::DepInfo) {
118 LOG(ERROR) << "Invalid parcelable type";
119 return PMINFO_R_ERROR;
122 std::shared_ptr<pkgmgr_common::parcel::DepInfoParcelable> return_parcel(
123 std::static_pointer_cast<pkgmgr_common::parcel::DepInfoParcelable>(
126 auto dependency_list = return_parcel->GetDependencyInfo();
127 for (auto& dependency : dependency_list)
128 *dependencies = g_list_prepend(*dependencies, dependency);
132 // TODO: Need to add target db uid to identify which database to be searched
133 extern "C" EXPORT_API int _appinfo_get_applications(uid_t db_uid, uid_t uid,
134 pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
135 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
136 new pkgmgr_common::parcel::FilterParcelable(uid,
137 static_cast<pkgmgrinfo_filter_x*>(filter), flag));
139 pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::GET_APP_INFO);
140 if (!client.SendRequest())
141 return PMINFO_R_ERROR;
143 auto ptr = client.GetResultParcel();
144 if (ptr == nullptr) {
145 LOG(ERROR) << "Fail to get return parcelable";
146 return PMINFO_R_ERROR;
149 int ret = ptr->GetRequestResult();
150 if (ret != PMINFO_R_OK) {
151 if (ret == PMINFO_R_ENOENT) {
152 LOG(DEBUG) << "No such application";
154 LOG(ERROR) << "Request fail";
159 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::AppInfo) {
160 LOG(ERROR) << "Invalid parcelable type";
161 return PMINFO_R_ERROR;
164 std::shared_ptr<pkgmgr_common::parcel::AppInfoParcelable> return_parcel(
165 std::static_pointer_cast<pkgmgr_common::parcel::AppInfoParcelable>(
168 auto result_list = return_parcel->GetAppInfo();
169 for (auto& appinfo : result_list)
170 g_hash_table_insert(packages, (gpointer)appinfo->appid,
176 extern "C" EXPORT_API char *_appinfo_get_localed_label(
177 const char *appid, const char *locale, uid_t uid) {
178 char *query = nullptr;
179 query = sqlite3_mprintf(
180 "SELECT COALESCE((SELECT app_label FROM package_app_localized_info "
181 "WHERE app_id=%Q AND app_locale=%Q),"
182 "(SELECT app_label FROM package_app_localized_info WHERE "
183 "app_id=%Q AND app_locale='No Locale'))", appid, locale, appid);
184 if (query == nullptr) {
185 LOG(ERROR) << "Out of memory";
189 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
190 new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
191 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
192 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
195 pkgmgr_client::PkgInfoClient client(parcelable, uid,
196 pkgmgr_common::ReqType::QUERY);
197 if (!client.SendRequest()) {
201 auto ptr = client.GetResultParcel();
202 if (ptr == nullptr) {
203 LOG(ERROR) << "Fail to get return parcelable";
207 if (ptr->GetRequestResult() != PMINFO_R_OK) {
208 LOG(ERROR) << "Request fail";
212 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
213 LOG(ERROR) << "Invalid parcelable type";
217 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
218 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
221 // result_list is vector of string vector
222 char *label = nullptr;
223 auto result_list = return_parcel->GetResult();
224 for (auto result : result_list) {
225 // result is string vector
226 // it only has one string or not.
227 if (result.front().empty() || result.front().length() == 0)
229 label = strdup(result.front().c_str());
230 if (label == nullptr) {
231 LOG(ERROR) << "Out of memory";
240 extern "C" EXPORT_API int _appinfo_get_datacontrol_info(
241 const char *providerid, const char *type, uid_t uid,
242 char **appid, char **access) {
243 char *query = nullptr;
244 query = sqlite3_mprintf("SELECT app_id, access FROM "
245 "package_app_data_control WHERE "
246 "providerid=%Q AND type=%Q", providerid, type);
247 if (query == nullptr) {
248 LOG(ERROR) << "Out of memory";
249 return PMINFO_R_ERROR;
252 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
253 new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
254 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
255 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
258 pkgmgr_client::PkgInfoClient client(parcelable, uid,
259 pkgmgr_common::ReqType::QUERY);
260 if (!client.SendRequest()) {
261 return PMINFO_R_ERROR;
264 auto ptr = client.GetResultParcel();
265 if (ptr == nullptr) {
266 LOG(ERROR) << "Fail to get return parcelable";
267 return PMINFO_R_ERROR;
270 if (ptr->GetRequestResult() != PMINFO_R_OK) {
271 LOG(ERROR) << "Request fail";
272 return ptr->GetRequestResult();
275 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
276 LOG(ERROR) << "Invalid parcelable type";
277 return PMINFO_R_ERROR;
280 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
281 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
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 *access = strdup(result.back().c_str());
296 if (*appid == nullptr || *access == nullptr) {
297 LOG(ERROR) << "Out of memory";
298 return PMINFO_R_ERROR;
305 extern "C" EXPORT_API int _appinfo_get_datacontrol_appid(
306 const char *providerid, uid_t uid, char **appid) {
307 char *query = nullptr;
309 query = sqlite3_mprintf("SELECT app_id FROM package_app_data_control "
310 "WHERE providerid=%Q", providerid);
311 if (query == nullptr) {
312 LOGE("Out of memory");
313 return PMINFO_R_ERROR;
315 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
316 new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
317 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
318 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
321 pkgmgr_client::PkgInfoClient client(parcelable, uid,
322 pkgmgr_common::ReqType::QUERY);
323 if (!client.SendRequest()) {
324 return PMINFO_R_ERROR;
327 auto ptr = client.GetResultParcel();
328 if (ptr == nullptr) {
329 LOG(ERROR) << "Fail to get return parcelable";
330 return PMINFO_R_ERROR;
333 if (ptr->GetRequestResult() != PMINFO_R_OK) {
334 LOG(ERROR) << "Request fail";
335 return ptr->GetRequestResult();
338 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
339 LOG(ERROR) << "Invalid parcelable type";
340 return PMINFO_R_ERROR;
343 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
344 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
347 // result_list is vector of string vector
348 auto result_list = return_parcel->GetResult();
349 if (result_list.size() == 0)
350 return PMINFO_R_ENOENT;
351 for (auto result : result_list) {
352 if (result.size() != 1)
353 return PMINFO_R_ERROR;
354 if (result.front().empty() || result.front().size() == 0)
355 return PMINFO_R_ERROR;
356 *appid = strdup(result.front().c_str());
357 if (*appid == nullptr) {
358 LOG(ERROR) << "Out of memory";
359 return PMINFO_R_ERROR;
366 extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info(
367 const char *providerid, const char *type, uid_t uid,
368 char **appid, char **trusted) {
369 char *query = nullptr;
370 query = sqlite3_mprintf(
371 "SELECT app_id, trusted FROM package_app_data_control "
372 "WHERE providerid=%Q AND type=%Q", providerid, type);
373 if (query == nullptr) {
374 LOGE("Out of memory");
375 return PMINFO_R_ERROR;
378 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
379 new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
380 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
381 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
384 pkgmgr_client::PkgInfoClient client(parcelable, uid,
385 pkgmgr_common::ReqType::QUERY);
386 if (!client.SendRequest()) {
387 return PMINFO_R_ERROR;
390 auto ptr = client.GetResultParcel();
391 if (ptr == nullptr) {
392 LOG(ERROR) << "Fail to get return parcelable";
393 return PMINFO_R_ERROR;
396 if (ptr->GetRequestResult() != PMINFO_R_OK) {
397 LOG(ERROR) << "Request fail";
398 return ptr->GetRequestResult();
401 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
402 LOG(ERROR) << "Invalid parcelable type";
403 return PMINFO_R_ERROR;
406 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
407 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
410 // result_list is vector of string vector
411 auto result_list = return_parcel->GetResult();
412 if (result_list.size() == 0)
413 return PMINFO_R_ENOENT;
414 for (auto result : result_list) {
415 if (result.size() != 2)
416 return PMINFO_R_ERROR;
417 if (result.front().empty() || result.front().size() == 0 ||
418 result.back().empty() || result.back().size() == 0)
419 return PMINFO_R_ERROR;
420 *appid = strdup(result.front().c_str());
421 *trusted = strdup(result.back().c_str());
422 if (*appid == nullptr || *trusted == nullptr) {
423 LOG(ERROR) << "Out of memory";
424 return PMINFO_R_ERROR;
431 extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges(
432 const char *providerid, const char *type, uid_t uid,
433 GList **privileges) {
434 char *query = nullptr;
435 query = sqlite3_mprintf(
436 "SELECT privilege FROM package_app_data_control_privilege "
437 "WHERE providerid=%Q AND type=%Q", providerid, type);
438 if (query == nullptr) {
439 LOGE("Out of memory");
440 return PMINFO_R_ERROR;
443 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
444 new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
445 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
446 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
449 pkgmgr_client::PkgInfoClient client(parcelable, uid,
450 pkgmgr_common::ReqType::QUERY);
451 if (!client.SendRequest()) {
452 return PMINFO_R_ERROR;
455 auto ptr = client.GetResultParcel();
456 if (ptr == nullptr) {
457 LOG(ERROR) << "Fail to get return parcelable";
458 return PMINFO_R_ERROR;
461 if (ptr->GetRequestResult() != PMINFO_R_OK) {
462 LOG(ERROR) << "Request fail";
463 return ptr->GetRequestResult();
466 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
467 LOG(ERROR) << "Invalid parcelable type";
468 return PMINFO_R_ERROR;
471 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
472 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
475 // result_list is vector of string vector
476 auto result_list = return_parcel->GetResult();
477 if (result_list.size() == 0)
478 return PMINFO_R_ENOENT;
480 for (auto result : result_list) {
481 if (result.size() != 1)
482 return PMINFO_R_ERROR;
483 if (result.front().empty() || result.front().size() == 0)
484 return PMINFO_R_ERROR;
485 char *privilege = strdup(result.front().c_str());
486 if (privilege == nullptr) {
487 LOG(ERROR) << "Out of memory";
488 return PMINFO_R_ERROR;
490 *privileges = g_list_append(*privileges, privilege);
496 extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges(
497 const char *appid, const char *operation, uid_t uid, GList **privileges) {
498 char *query = nullptr;
499 query = sqlite3_mprintf(
500 "SELECT app_control, privilege FROM package_app_app_control_privilege "
501 "WHERE app_id=%Q", appid);
502 if (query == nullptr) {
503 LOG(ERROR) << "Out of memory";
504 return PMINFO_R_ERROR;
507 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
508 new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
509 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
510 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
513 pkgmgr_client::PkgInfoClient client(parcelable, uid,
514 pkgmgr_common::ReqType::QUERY);
515 if (!client.SendRequest()) {
516 return PMINFO_R_ERROR;
519 auto ptr = client.GetResultParcel();
520 if (ptr == nullptr) {
521 LOG(ERROR) << "Fail to get return parcelable";
522 return PMINFO_R_ERROR;
525 if (ptr->GetRequestResult() != PMINFO_R_OK) {
526 LOG(ERROR) << "Request fail";
527 return ptr->GetRequestResult();
530 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
531 LOG(ERROR) << "Invalid parcelable type";
532 return PMINFO_R_ERROR;
535 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
536 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
539 // result_list is vector of string vector
540 auto result_list = return_parcel->GetResult();
541 if (result_list.size() == 0)
542 return PMINFO_R_ENOENT;
544 for (auto result : result_list) {
545 if (result.size() != 2)
546 return PMINFO_R_ERROR;
547 if (result.front().empty() || result.front().size() == 0 ||
548 result.back().empty() || result.back().size() == 0)
549 return PMINFO_R_ERROR;
550 std::string app_control = result.front();
551 std::stringstream ss(app_control);
553 while (std::getline(ss, token, '|')) {
554 if (token.compare(std::string(operation))) {
555 char *privilege = strdup(result.back().c_str());
556 if (privilege == nullptr) {
557 LOG(ERROR) << "Out of memory";
558 return PMINFO_R_ERROR;
560 *privileges = g_list_append(*privileges, privilege);
567 extern "C" EXPORT_API int _plugininfo_get_appids(
568 const char *pkgid, const char *plugin_type,
569 const char *plugin_name, GList **list) {
570 if (!pkgid || !plugin_type || !plugin_name || !list) {
571 LOG(ERROR) << "Invalid parameter";
572 return PMINFO_R_EINVAL;
575 char *query = nullptr;
576 query = sqlite3_mprintf(
578 "package_plugin_info WHERE pkgid=%Q AND "
579 "plugin_type=%Q AND plugin_name=%Q",
580 pkgid, plugin_type, plugin_name);
581 if (query == nullptr) {
582 LOG(ERROR) << "Out of memory";
583 return PMINFO_R_ERROR;
586 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
587 new pkgmgr_common::parcel::QueryParcelable(_getuid(), std::string(query),
588 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
589 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
592 pkgmgr_client::PkgInfoClient client(parcelable, _getuid(),
593 pkgmgr_common::ReqType::QUERY);
594 if (!client.SendRequest()) {
595 return PMINFO_R_ERROR;
598 auto ptr = client.GetResultParcel();
599 if (ptr == nullptr) {
600 LOG(ERROR) << "Fail to get return parcelable";
601 return PMINFO_R_ERROR;
604 if (ptr->GetRequestResult() != PMINFO_R_OK) {
605 LOG(ERROR) << "Request fail";
606 return ptr->GetRequestResult();
609 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
610 LOG(ERROR) << "Invalid parcelable type";
611 return PMINFO_R_ERROR;
614 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
615 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
618 if (return_parcel->GetCol() != 1) {
619 LOG(ERROR) << "Invalid result";
620 return PMINFO_R_ERROR;
622 // result_list is vector of string vector
623 auto& result_list = return_parcel->GetResult();
624 if (result_list.size() == 0)
625 return PMINFO_R_ENOENT;
627 for (auto result : result_list) {
628 if (result.size() != 1) {
629 LOG(ERROR) << "Invalid result";
630 g_list_free_full(*list, free);
631 return PMINFO_R_ERROR;
633 *list = g_list_append(*list, strdup(result[0].c_str()));
639 static int __convert_update_type(const char *type, pkgmgrinfo_updateinfo_update_type *convert_type)
641 if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_NONE,
642 strlen(PMINFO_UPDATEINFO_TYPE_NONE)) == 0)
643 *convert_type = PMINFO_UPDATEINFO_NONE;
644 else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_FORCE,
645 strlen(PMINFO_UPDATEINFO_TYPE_FORCE)) == 0)
646 *convert_type = PMINFO_UPDATEINFO_FORCE;
647 else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_OPTIONAL,
648 strlen(PMINFO_UPDATEINFO_TYPE_OPTIONAL)) == 0)
649 *convert_type = PMINFO_UPDATEINFO_OPTIONAL;
655 static void __free_update_info(gpointer data)
657 updateinfo_x *update_info = (updateinfo_x *)data;
658 if (update_info == nullptr)
661 if (update_info->pkgid)
662 free((void *)update_info->pkgid);
663 if (update_info->version)
664 free((void *)update_info->version);
665 free((void *)update_info);
669 extern "C" EXPORT_API int _get_pkg_updateinfo(const char *pkgid,
670 GSList **update_info_list, uid_t uid)
672 char *query = nullptr;
675 if (pkgid == nullptr)
676 query = sqlite3_mprintf(
677 "SELECT package, update_version, update_type "
678 "FROM package_update_info");
680 query = sqlite3_mprintf(
681 "SELECT package, update_version, update_type "
682 "FROM package_update_info WHERE package=%Q",
684 if (query == nullptr) {
685 LOG(ERROR) << "Out of memory";
686 return PMINFO_R_ERROR;
689 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
690 new pkgmgr_common::parcel::QueryParcelable(uid, std::string(query),
691 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
692 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
695 pkgmgr_client::PkgInfoClient client(parcelable, uid,
696 pkgmgr_common::ReqType::QUERY);
697 if (!client.SendRequest()) {
698 return PMINFO_R_ERROR;
701 auto ptr = client.GetResultParcel();
702 if (ptr == nullptr) {
703 LOG(ERROR) << "Fail to get return parcelable";
704 return PMINFO_R_ERROR;
707 if (ptr->GetRequestResult() != PMINFO_R_OK) {
708 LOG(ERROR) << "Request fail";
709 return ptr->GetRequestResult();
712 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
713 LOG(ERROR) << "Invalid parcelable type";
714 return PMINFO_R_ERROR;
717 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
718 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
721 if (return_parcel->GetCol() != 3) {
722 LOG(ERROR) << "Invalid result";
723 return PMINFO_R_ERROR;
726 auto result_list = return_parcel->GetResult();
727 if (result_list.size() == 0)
728 return PMINFO_R_ENOENT;
730 for (auto result : result_list) {
731 if (result.size() != 3) {
732 LOG(ERROR) << "Invalid result";
733 g_slist_free_full(*update_info_list, __free_update_info);
734 return PMINFO_R_ERROR;
736 updateinfo_x *update_info = reinterpret_cast<updateinfo_x *>(calloc(1, sizeof(updateinfo_x)));
737 if (update_info == nullptr) {
738 LOG(ERROR) << "Out of memory";
739 return PMINFO_R_ERROR;
741 update_info->pkgid = strdup(result[0].c_str());
742 update_info->version = strdup(result[1].c_str());
743 pkgmgrinfo_updateinfo_update_type convert_type;
744 ret = __convert_update_type(result[2].c_str(), &convert_type);
746 __free_update_info(update_info);
747 g_slist_free_full(*update_info_list, __free_update_info);
748 return PMINFO_R_ERROR;
750 update_info->type = static_cast<int>(convert_type);
751 *update_info_list = g_slist_prepend(*update_info_list,
758 extern "C" EXPORT_API int _pkginfo_set_usr_installed_storage(const char *pkgid,
759 INSTALL_LOCATION location, const char *external_pkg_path,
762 char *query = nullptr;
763 const char *location_str;
764 std::vector<std::string> queries;
766 if (location == INSTALL_INTERNAL)
767 location_str = "installed_internal";
768 else if (location == INSTALL_EXTERNAL)
769 location_str = "installed_external";
771 location_str = "installed_extended";
772 /* pkgcakge_info table */
773 query = sqlite3_mprintf(
774 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
775 location_str, external_pkg_path, pkgid);
776 queries.emplace_back(query);
779 /* package_app_info table */
780 query = sqlite3_mprintf(
781 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
782 location_str, external_pkg_path, pkgid);
783 queries.emplace_back(query);
786 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
787 new pkgmgr_common::parcel::QueryParcelable(uid, queries,
788 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
789 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
791 pkgmgr_client::PkgInfoClient client(parcelable, uid,
792 pkgmgr_common::ReqType::QUERY);
793 if (!client.SendRequest()) {
794 return PMINFO_R_ERROR;
797 auto ptr = client.GetResultParcel();
798 if (ptr == nullptr) {
799 LOG(ERROR) << "Fail to get return parcelable";
800 return PMINFO_R_ERROR;
803 if (ptr->GetRequestResult() != PMINFO_R_OK) {
804 LOG(ERROR) << "Request fail";
805 return ptr->GetRequestResult();
808 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
809 LOG(ERROR) << "Invalid parcelable type";
810 return PMINFO_R_ERROR;
816 extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char *l_pkgid,
817 const char *r_pkgid, pkgmgrinfo_cert_compare_result_type_e *result) {
818 char *query = sqlite3_mprintf("SELECT package, "
819 "COALESCE(author_signer_cert, -1) FROM package_cert_info WHERE "
820 "package IN (%Q, %Q)", l_pkgid, r_pkgid);
821 if (query == nullptr) {
822 LOG(ERROR) << "Out of memory";
823 return PMINFO_R_ERROR;
825 std::vector<std::string> queries;
826 queries.emplace_back(query);
829 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
830 new pkgmgr_common::parcel::QueryParcelable(0, queries,
831 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_CERTDB,
832 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
833 pkgmgr_client::PkgInfoClient client(parcelable, 0,
834 pkgmgr_common::ReqType::QUERY);
835 if (!client.SendRequest()) {
836 return PMINFO_R_ERROR;
839 auto ptr = client.GetResultParcel();
840 if (ptr == nullptr) {
841 LOG(ERROR) << "Fail to get return parcelable";
842 return PMINFO_R_ERROR;
845 if (ptr->GetRequestResult() != PMINFO_R_OK) {
846 LOG(ERROR) << "Request fail";
847 return ptr->GetRequestResult();
850 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
851 LOG(ERROR) << "Invalid parcelable type";
852 return PMINFO_R_ERROR;
855 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
856 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
859 auto certinfo_list = return_parcel->GetResult();
860 if (certinfo_list.size() != 2)
861 return PMINFO_R_ERROR;
863 std::map<std::string, std::string> result_map;
864 for (auto& certinfo : certinfo_list)
865 result_map.insert(make_pair(certinfo.front(), certinfo.back()));
867 if (result_map.find(std::string(l_pkgid))->second == "-1" &&
868 result_map.find(std::string(r_pkgid))->second == "-1")
869 *result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
870 else if (result_map.find(std::string(l_pkgid))->second == "-1")
871 *result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
872 else if (result_map.find(std::string(r_pkgid))->second == "-1")
873 *result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
874 else if (result_map.find(std::string(l_pkgid))->second ==
875 result_map.find(std::string(r_pkgid))->second)
876 *result = PMINFO_CERT_COMPARE_MATCH;
878 *result = PMINFO_CERT_COMPARE_MISMATCH;
883 extern "C" EXPORT_API int _certinfo_compare_app_certinfo(uid_t uid,
884 const char *l_appid, const char *r_appid,
885 pkgmgrinfo_cert_compare_result_type_e *result) {
886 char* query = sqlite3_mprintf("SELECT app_id, package FROM "
887 "package_app_info WHERE app_id IN (%Q, %Q)", l_appid, r_appid);
888 if (query == nullptr) {
889 LOG(ERROR) << "Out of memory";
890 return PMINFO_R_ERROR;
892 std::vector<std::string> queries;
893 queries.emplace_back(query);
896 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
897 new pkgmgr_common::parcel::QueryParcelable(uid, queries,
898 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
899 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
900 pkgmgr_client::PkgInfoClient client(parcelable, uid,
901 pkgmgr_common::ReqType::QUERY);
902 if (!client.SendRequest()) {
903 return PMINFO_R_ERROR;
906 auto ptr = client.GetResultParcel();
907 if (ptr == nullptr) {
908 LOG(ERROR) << "Fail to get return parcelable";
909 return PMINFO_R_ERROR;
912 if (ptr->GetRequestResult() != PMINFO_R_OK) {
913 LOG(ERROR) << "Request fail";
914 return ptr->GetRequestResult();
917 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
918 LOG(ERROR) << "Invalid parcelable type";
919 return PMINFO_R_ERROR;
922 std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
923 std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
926 auto pkgid_list = return_parcel->GetResult();
927 std::map<std::string, std::string> result_map;
928 for (auto& pkgid : pkgid_list)
929 result_map.insert(make_pair(pkgid.front(), pkgid.back()));
931 if (result_map.find(std::string(l_appid)) == result_map.end()) {
932 LOG(ERROR) << "Cannot find pkgid of app " << l_appid
933 << " for uid " << uid;
934 return PMINFO_R_ENOENT;
935 } else if (result_map.find(std::string(r_appid)) == result_map.end()) {
936 LOG(ERROR) << "Cannot find pkgid of app " << r_appid
937 << " for uid " << uid;
938 return PMINFO_R_ENOENT;
941 const char* l_pkgid = result_map.find(
942 std::string(l_appid))->second.c_str();
943 const char* r_pkgid = result_map.find(
944 std::string(r_appid))->second.c_str();
946 return _certinfo_compare_pkg_certinfo(l_pkgid, r_pkgid, result);
949 extern "C" EXPORT_API int _parser_execute_write_query(const char *query, uid_t uid)
951 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
952 new pkgmgr_common::parcel::QueryParcelable(uid, query,
953 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
954 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
956 pkgmgr_client::PkgInfoClient client(parcelable, uid,
957 pkgmgr_common::ReqType::QUERY);
958 if (!client.SendRequest()) {
962 auto ptr = client.GetResultParcel();
963 if (ptr == nullptr) {
964 LOG(ERROR) << "Fail to get return parcelable";
968 if (ptr->GetRequestResult() != PMINFO_R_OK) {
969 LOG(ERROR) << "Request fail";
973 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
974 LOG(ERROR) << "Invalid parcelable type";
981 extern "C" EXPORT_API int _parser_execute_write_queries(const char **queries, int len, uid_t uid)
983 std::vector<std::string> query_vt;
984 for (int i = 0; i < len; ++i)
985 query_vt.emplace_back(queries[i]);
987 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
988 new pkgmgr_common::parcel::QueryParcelable(uid, query_vt,
989 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_PKGDB,
990 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
992 pkgmgr_client::PkgInfoClient client(parcelable, uid,
993 pkgmgr_common::ReqType::QUERY);
994 if (!client.SendRequest()) {
998 auto ptr = client.GetResultParcel();
999 if (ptr == nullptr) {
1000 LOG(ERROR) << "Fail to get return parcelable";
1004 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1005 LOG(ERROR) << "Request fail";
1009 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
1010 LOG(ERROR) << "Invalid parcelable type";
1017 extern "C" EXPORT_API int _parser_insert_manifest_info(manifest_x *mfx, uid_t uid)
1019 std::vector<package_x *> vt { mfx };
1021 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1022 new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Insert));
1024 pkgmgr_client::PkgInfoClient client(parcelable, uid,
1025 pkgmgr_common::ReqType::SET_PKG_INFO);
1026 if (!client.SendRequest()) {
1030 auto ptr = client.GetResultParcel();
1031 if (ptr == nullptr) {
1032 LOG(ERROR) << "Fail to get return parcelable";
1036 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1037 LOG(ERROR) << "Request fail";
1041 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
1042 LOG(ERROR) << "Invalid parcelable type";
1049 extern "C" EXPORT_API int _parser_update_manifest_info(manifest_x *mfx, uid_t uid)
1051 std::vector<package_x *> vt { mfx };
1053 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1054 new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Update));
1056 pkgmgr_client::PkgInfoClient client(parcelable, uid,
1057 pkgmgr_common::ReqType::SET_PKG_INFO);
1058 if (!client.SendRequest()) {
1062 auto ptr = client.GetResultParcel();
1063 if (ptr == nullptr) {
1064 LOG(ERROR) << "Fail to get return parcelable";
1068 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1069 LOG(ERROR) << "Request fail";
1073 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
1074 LOG(ERROR) << "Invalid parcelable type";
1081 extern "C" EXPORT_API int _parser_delete_manifest_info(manifest_x *mfx, uid_t uid)
1083 std::vector<package_x *> vt { mfx };
1085 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1086 new pkgmgr_common::parcel::PkgInfoParcelable(uid, std::move(vt), WriteType::Delete));
1088 pkgmgr_client::PkgInfoClient client(parcelable, uid,
1089 pkgmgr_common::ReqType::SET_PKG_INFO);
1090 if (!client.SendRequest()) {
1094 auto ptr = client.GetResultParcel();
1095 if (ptr == nullptr) {
1096 LOG(ERROR) << "Fail to get return parcelable";
1100 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1101 LOG(ERROR) << "Request fail";
1105 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
1106 LOG(ERROR) << "Invalid parcelable type";
1113 extern "C" EXPORT_API int _pkginfo_insert_certinfo(const char* pkgid,
1114 pkgmgr_certinfo_x* cert, uid_t uid)
1116 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1117 new pkgmgr_common::parcel::CertInfoParcelable(uid, cert));
1119 pkgmgr_client::PkgInfoClient client(parcelable, uid,
1120 pkgmgr_common::ReqType::SET_CERT_INFO);
1121 if (!client.SendRequest())
1122 return PMINFO_R_ERROR;
1124 auto ptr = client.GetResultParcel();
1125 if (ptr == nullptr) {
1126 LOG(ERROR) << "Fail to get return parcelable";
1127 return PMINFO_R_ERROR;
1130 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1131 LOG(ERROR) << "Request fail";
1132 return ptr->GetRequestResult();
1135 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
1136 LOG(ERROR) << "Invalid parcelable type";
1137 return PMINFO_R_ERROR;
1143 extern "C" EXPORT_API int _pkginfo_get_certinfo(const char *pkgid,
1144 pkgmgr_certinfo_x* cert, uid_t uid)
1146 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1147 new pkgmgr_common::parcel::CertInfoParcelable(uid,
1148 std::string(pkgid)));
1150 pkgmgr_client::PkgInfoClient client(parcelable, uid,
1151 pkgmgr_common::ReqType::GET_CERT_INFO);
1152 if (!client.SendRequest())
1153 return PMINFO_R_ERROR;
1155 auto ptr = client.GetResultParcel();
1156 if (ptr == nullptr) {
1157 LOG(ERROR) << "Fail to get return parcelable";
1158 return PMINFO_R_ERROR;
1161 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1162 LOG(ERROR) << "Request fail";
1163 return ptr->GetRequestResult();
1166 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::CertInfo) {
1167 LOG(ERROR) << "Invalid parcelable type";
1168 return PMINFO_R_ERROR;
1171 std::shared_ptr<pkgmgr_common::parcel::CertInfoParcelable> return_parcel(
1172 std::static_pointer_cast<pkgmgr_common::parcel::CertInfoParcelable>(
1175 const pkgmgr_certinfo_x *certinfo = return_parcel->GetCertInfo();
1176 if (certinfo == nullptr)
1177 return PMINFO_R_ERROR;
1179 cert->for_all_users = certinfo->for_all_users;
1180 cert->pkgid = certinfo->pkgid;
1181 cert->cert_value = certinfo->cert_value;
1182 for (int i = 0; i < MAX_CERT_TYPE; i++)
1183 cert->cert_info[i] = certinfo->cert_info[i];
1184 for (int i = 0; i < MAX_CERT_TYPE; i++)
1185 cert->cert_id[i] = certinfo->cert_id[i];
1190 extern "C" EXPORT_API int _pkginfo_delete_certinfo(const char *pkgid)
1192 char* query = sqlite3_mprintf("UPDATE package_cert_info SET "
1193 "package_count = package_count - 1 WHERE package=%Q", pkgid);
1194 if (query == nullptr) {
1195 LOG(ERROR) << "Out of memory";
1196 return PMINFO_R_ERROR;
1199 std::vector<std::string> queries;
1200 queries.emplace_back(query);
1201 sqlite3_free(query);
1203 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1204 new pkgmgr_common::parcel::QueryParcelable(0, queries,
1205 pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_CERTDB,
1206 pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_WRITE));
1208 pkgmgr_client::PkgInfoClient client(parcelable, 0,
1209 pkgmgr_common::ReqType::QUERY);
1210 if (!client.SendRequest()) {
1211 return PMINFO_R_ERROR;
1214 auto ptr = client.GetResultParcel();
1215 if (ptr == nullptr) {
1216 LOG(ERROR) << "Fail to get return parcelable";
1217 return PMINFO_R_ERROR;
1220 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1221 LOG(ERROR) << "Request fail";
1222 return PMINFO_R_ERROR;
1225 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
1226 LOG(ERROR) << "Invalid parcelable type";
1227 return PMINFO_R_ERROR;
1233 extern "C" EXPORT_API int _parser_clear_cache_memory_db(uid_t uid)
1235 std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
1236 new pkgmgr_common::parcel::CommandParcelable(uid, CommandType::RemoveCache));
1238 pkgmgr_client::PkgInfoClient client(parcelable, uid,
1239 pkgmgr_common::ReqType::COMMAND);
1241 if (!client.SendRequest()) {
1242 return PMINFO_R_ERROR;
1245 auto ptr = client.GetResultParcel();
1246 if (ptr == nullptr) {
1247 LOG(ERROR) << "Fail to get return parcelable";
1248 return PMINFO_R_ERROR;
1251 if (ptr->GetRequestResult() != PMINFO_R_OK) {
1252 LOG(ERROR) << "Request fail";
1253 return PMINFO_R_ERROR;
1256 if (ptr->GetType() != pkgmgr_common::parcel::ParcelableType::Result) {
1257 LOG(ERROR) << "Invalid parcelable type";
1258 return PMINFO_R_ERROR;