Fix static analysis issue
[platform/core/appfw/app-installers.git] / src / common / step / pkgmgr / step_check_installable.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_installable.h"
6
7 #include <manifest_parser/utils/version_number.h>
8 #include <pkgmgr-info.h>
9 #include <sys/types.h>
10 #include <tzplatform_config.h>
11 #include <unistd.h>
12
13 #include <string>
14 #include <vector>
15
16 #include "common/pkgmgr_dependency.h"
17 #include "common/utils/pkgmgr_query.h"
18 #include "common/utils/glist_range.h"
19 #include "common/utils/user_util.h"
20
21 namespace {
22
23 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
24
25 }  // namespace
26
27 namespace common_installer {
28 namespace pkgmgr {
29
30 Step::Status StepCheckInstallable::process() {
31   switch (context_->request_mode.get()) {
32     case RequestMode::USER: {
33       PkgQueryInterface pkg_query(context_->pkgid.get(), kGlobalUserUid);
34       if (pkg_query.IsPackageInstalled(RequestMode::GLOBAL)) {
35         LOG(ERROR) << "This package is already installed as global package";
36         return Status::OPERATION_NOT_ALLOWED;
37       }
38       break;
39     }
40     case RequestMode::GLOBAL: {
41       UserList list = GetUserList();
42       for (const auto& l : list) {
43         uid_t uid = std::get<0>(l);
44         PkgQueryInterface pkg_query(context_->pkgid.get(), uid);
45         if (pkg_query.IsPackageInstalled(RequestMode::USER)) {
46           LOG(ERROR) << "This package is already installed by user("
47                      << uid << ")";
48           return Status::OPERATION_NOT_ALLOWED;
49         }
50       }
51       break;
52     }
53   }
54
55   return Status::OK;
56 }
57
58 Step::Status StepCheckInstallable::precheck() {
59   if (context_->pkgid.get().empty())
60     return Status::INVALID_VALUE;
61   return Status::OK;
62 }
63
64 }  // namespace pkgmgr
65 }  // namespace common_installer