2ab1e2f1dbeed2646301c7562716e559d2b4cc19
[platform/core/appfw/app-installers.git] / src / common / step / pkgmgr / step_check_removable.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/pkgmgr/step_check_removable.h"
6
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include <string>
11 #include <vector>
12
13 #include "common/installer/app_installer.h"
14 #include "common/pkgmgr_dependency.h"
15 #include "common/utils/pkgmgr_query.h"
16
17 namespace common_installer {
18 namespace pkgmgr {
19
20 Step::Status StepCheckRemovable::process() {
21   if (context_->force_remove.get())
22       return Status::OK;
23
24   PkgQueryInterface pkg_query(context_->pkgid.get().c_str(),
25       context_->uid.get());
26   if (!pkg_query.IsValid()) {
27     LOG(ERROR) << "This package is not installed";
28     return Status::INVALID_VALUE;
29   }
30   bool removable = pkg_query.IsRemovablePackage();
31   if (pkg_query.GetLastOperationError() != PMINFO_R_OK)
32     return Status::INVALID_VALUE;
33
34   if (!removable) {
35     LOG(ERROR) << "This package is not removable";
36     return Status::OPERATION_NOT_ALLOWED;
37   }
38
39   std::string pkg_type = pkg_query.Type();
40   // rpm package installed by tpk-backend
41   if (pkg_type == "rpm")
42     pkg_type = "tpk";
43   if (pkg_type != context_->pkg_type.get()) {
44     LOG(ERROR) << "Wrong use of backend : package's type [" << pkg_query.Type()
45                << "] mismatched backend [" << context_->pkg_type.get() << "]";
46     return Status::OPERATION_NOT_ALLOWED;
47   }
48
49   return Status::OK;
50 }
51
52 Step::Status StepCheckRemovable::precheck() {
53   if (context_->pkgid.get().empty())
54     return Status::INVALID_VALUE;
55   return Status::OK;
56 }
57
58 }  // namespace pkgmgr
59 }  // namespace common_installer