From: Ilho Kim Date: Mon, 28 Oct 2019 06:45:19 +0000 (+0900) Subject: Refactor Checking Dependency routine X-Git-Tag: accepted/tizen/5.5/unified/20191118.084816~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F34%2F216534%2F8;p=platform%2Fcore%2Fappfw%2Fapp-installers.git Refactor Checking Dependency routine Change-Id: Id335b0475d510a3cce7b1c31d8526b9f19704ca3 Signed-off-by: Ilho Kim --- diff --git a/src/common/pkgmgr_dependency.cc b/src/common/pkgmgr_dependency.cc new file mode 100644 index 0000000..7ee1d8b --- /dev/null +++ b/src/common/pkgmgr_dependency.cc @@ -0,0 +1,83 @@ +// Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by an apache-2.0 license that can be +// found in the LICENSE file. + +#include "common/pkgmgr_dependency.h" + +#include + +#include "common/request.h" +#include "common/utils/glist_range.h" + +namespace { + +const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER); + +int CompareVersion(const std::string& a, const std::string& b) { + utils::VersionNumber a_ver(a); + utils::VersionNumber b_ver(b); + + return a_ver.Compare(b_ver); +} + +} // namespace + +namespace common_installer { + +bool SatisfyDependencyTo(GList* dependencies, const uid_t uid, + const RequestMode mode) { + bool satisfy = true; + for (const auto& dep : GListRange(dependencies)) { + // check only requires pkgs + if (strcmp(dep->type, "requires")) + continue; + PkgQueryInterface pkg_query(dep->depends_on, uid); + if (!pkg_query.IsPackageInstalled(mode)) { + LOG(ERROR) << dep->depends_on << " is required but not installed!"; + satisfy = false; + } else if (dep->required_version && + (CompareVersion(pkg_query.Version(), dep->required_version) < 0)) { + LOG(ERROR) << "Same or higher version (" << dep->required_version + << ") of " << dep->depends_on << " is required " + << "but current version of installed package is: " + << pkg_query.Version(); + satisfy = false; + } + } + + return satisfy; +} + +bool SatisfyDependencyFrom(const std::string pkgid, const std::string version, + const uid_t uid, const bool is_remove) { + PkgQueryInterface pkg_query(pkgid, uid); + std::vector depends_on; + if (!pkg_query.PackagesDependsOn(&depends_on)) { + LOG(ERROR) << "Can't get Package Depends On"; + return false; + } + + bool satisfy = true; + for (const auto& dep : depends_on) { + if (std::get<1>(dep) != pkgid) + continue; + if (std::get<2>(dep) != "requires") + continue; + if (is_remove) { + LOG(ERROR) << std::get<0>(dep) << " requires this package"; + satisfy = false; + } else { + if (CompareVersion(version, std::get<3>(dep)) < 0) { + LOG(ERROR) << std::get<0>(dep) + << " requires same or higher version (" << std::get<3>(dep) + << ") of this package (" << pkgid << ") but the version " + << "of this package trying to update is : " << version; + satisfy = false; + } + } + } + + return satisfy; +} + +} // namespace common_installer diff --git a/src/common/pkgmgr_dependency.h b/src/common/pkgmgr_dependency.h new file mode 100644 index 0000000..27d103a --- /dev/null +++ b/src/common/pkgmgr_dependency.h @@ -0,0 +1,25 @@ +// Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache 2.0 license that can be +// found in the LICENSE file. + +#ifndef COMMON_PKGMGR_DEPENDENCY_H_ +#define COMMON_PKGMGR_DEPENDENCY_H_ + +#include + +#include +#include + +#include "common/installer_context.h" +#include "common/pkgmgr_query.h" + +namespace common_installer { + +bool SatisfyDependencyTo(GList* dependencies, const uid_t uid, + const RequestMode mode); +bool SatisfyDependencyFrom(const std::string pkgid, const std::string version, + const uid_t uid, const bool is_remove); + +} // namespace common_installer + +#endif // COMMON_PKGMGR_DEPENDENCY_H_ diff --git a/src/common/step/pkgmgr/step_check_installable.cc b/src/common/step/pkgmgr/step_check_installable.cc index 7e5f647..da4059b 100644 --- a/src/common/step/pkgmgr/step_check_installable.cc +++ b/src/common/step/pkgmgr/step_check_installable.cc @@ -13,6 +13,7 @@ #include #include +#include "common/pkgmgr_dependency.h" #include "common/pkgmgr_query.h" #include "common/utils/glist_range.h" #include "common/utils/user_util.h" @@ -21,13 +22,6 @@ namespace { const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER); -int CompareVersion(const std::string& a, const std::string& b) { - utils::VersionNumber a_ver(a); - utils::VersionNumber b_ver(b); - - return a_ver.Compare(b_ver); -} - } // namespace namespace common_installer { @@ -62,27 +56,8 @@ Step::Status StepCheckInstallable::process() { if (getuid () == 0) return Status::OK; - bool installable = true; - manifest_x* manifest = context_->manifest_data.get(); - for (const auto& dep : GListRange(manifest->dependencies)) { - // check only requires pkgs - if (strcmp(dep->type, "requires")) - continue; - PkgQueryInterface pkg_query(dep->depends_on, context_->uid.get()); - if (!pkg_query.IsPackageInstalled(context_->request_mode.get())) { - LOG(ERROR) << dep->depends_on << " is required but not installed!"; - installable = false; - } else if (dep->required_version && - (CompareVersion(manifest->version, dep->required_version) < 0)) { - LOG(ERROR) << "Same or higher version (" << dep->required_version - << ") of " << dep->depends_on - << " is required but current version of installed package is: " - << pkg_query.Version(); - installable = false; - } - } - - if (!installable) + if (!SatisfyDependencyTo(context_->manifest_data.get()->dependencies, + context_->uid.get(), context_->request_mode.get())) return Status::OPERATION_NOT_ALLOWED; return Status::OK; diff --git a/src/common/step/pkgmgr/step_check_removable.cc b/src/common/step/pkgmgr/step_check_removable.cc index 70e67a2..93e0a0a 100644 --- a/src/common/step/pkgmgr/step_check_removable.cc +++ b/src/common/step/pkgmgr/step_check_removable.cc @@ -11,6 +11,7 @@ #include #include "common/app_installer.h" +#include "common/pkgmgr_dependency.h" #include "common/pkgmgr_query.h" namespace common_installer { @@ -49,21 +50,8 @@ Step::Status StepCheckRemovable::process() { if (getuid() == 0) return Status::OK; - std::vector depends_on; - if (!pkg_query.PackagesDependsOn(&depends_on)) - return Status::INVALID_VALUE; - - int break_count = 0; - for (const auto& dep : depends_on) { - if (std::get<1>(dep) != context_->pkgid.get()) - continue; - if (std::get<2>(dep) == "requires") { - LOG(ERROR) << std::get<0>(dep) << " requires this package"; - ++break_count; - } - } - - if (break_count) + if (!SatisfyDependencyFrom(context_->pkgid.get(), "", + context_->uid.get(), true)) return Status::OPERATION_NOT_ALLOWED; return Status::OK; diff --git a/src/common/step/pkgmgr/step_check_upgradable.cc b/src/common/step/pkgmgr/step_check_upgradable.cc index aad7a3e..f921c66 100644 --- a/src/common/step/pkgmgr/step_check_upgradable.cc +++ b/src/common/step/pkgmgr/step_check_upgradable.cc @@ -12,19 +12,9 @@ #include #include +#include "common/pkgmgr_dependency.h" #include "common/pkgmgr_query.h" -namespace { - -int CompareVersion(const std::string& a, const std::string& b) { - utils::VersionNumber a_ver(a); - utils::VersionNumber b_ver(b); - - return a_ver.Compare(b_ver); -} - -} // namespace - namespace common_installer { namespace pkgmgr { @@ -40,25 +30,9 @@ Step::Status StepCheckUpgradable::process() { if (getuid() == 0) return Status::OK; - std::vector depends_on; - if (!pkg_query.PackagesDependsOn(&depends_on)) - return Status::INVALID_VALUE; - - int break_count = 0; - manifest_x* manifest = context_->manifest_data.get(); - for (const auto& dep : depends_on) { - if (std::get<1>(dep) != context_->pkgid.get()) - continue; - if (std::get<2>(dep) == "requires") { - if (CompareVersion(manifest->version, std::get<3>(dep)) < 0) { - LOG(ERROR) << std::get<0>(dep) - << " requires higher version of this package"; - ++break_count; - } - } - } - - if (break_count) + if (!SatisfyDependencyFrom(context_->pkgid.get(), + context_->manifest_data.get()->version, + context_->uid.get(), false)) return Status::OPERATION_NOT_ALLOWED; return Status::OK;