f109908b1257ea0b113a82e167e7b4331eefa23c
[platform/core/appfw/app-installers.git] / src / common / utils / request.cc
1 // Copyright (c) 2015 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/utils/request.h"
6
7 #include <tzplatform_config.h>
8 #include <unistd.h>
9
10 #include <map>
11 #include <string>
12
13 #include "common/utils/user_util.h"
14
15 namespace bf = boost::filesystem;
16
17 namespace common_installer {
18
19 const std::map<RequestType, const char*> kRequestTypeStringMap = {
20   {RequestType::Unknown, "Unknown"},
21   {RequestType::Install, "Install"},
22   {RequestType::Update, "Update"},
23   {RequestType::Uninstall, "Uninstall"},
24   {RequestType::Reinstall, "Reinstall"},
25   {RequestType::Delta, "Delta"},
26   {RequestType::Move, "Move"},
27   {RequestType::Recovery, "Recovery"},
28   {RequestType::RecoveryUpdate, "RecoveryUpdate"},
29   {RequestType::MountInstall, "MountInstall"},
30   {RequestType::MountUpdate, "MountUpdate"},
31   {RequestType::ManifestDirectInstall, "ManifestDirectInstall"},
32   {RequestType::ManifestDirectUpdate, "ManifestDirectUpdate"},
33   {RequestType::ManifestPartialInstall, "ManifestPartialInstall"},
34   {RequestType::ManifestPartialUpdate, "ManifestPartialUpdate"},
35   {RequestType::PartialUninstall, "PartialUninstall"},
36   {RequestType::ReadonlyUpdateInstall, "ReadonlyUpdateInstall"},
37   {RequestType::ReadonlyUpdateUninstall, "ReadonlyUpdateUninstall"},
38   {RequestType::DisablePkg, "DisablePkg"},
39   {RequestType::EnablePkg, "EnablePkg"},
40   {RequestType::MigrateExtImg, "MigrateExtImg"},
41   {RequestType::RecoverDB, "RecoverDB"},
42 };
43
44 std::string GetRequestTypeString(RequestType request_type) {
45   const auto& it = kRequestTypeStringMap.find(request_type);
46   if (it == kRequestTypeStringMap.end())
47     return "Unknown";
48   else
49     return it->second;
50 }
51
52 RequestMode GetRequestMode(uid_t uid) {
53   return (uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)) ?
54       RequestMode::GLOBAL : RequestMode::USER;
55 }
56
57 // Now, readonly app is always installed RO location.
58 const char* GetRootAppPath(bool is_readonly, uid_t uid) {
59   if (GetRequestMode(uid) == RequestMode::GLOBAL) {
60     return is_readonly ?
61         tzplatform_getenv(TZ_SYS_RO_APP) : tzplatform_getenv(TZ_SYS_RW_APP);
62   } else {
63     tzplatform_set_user(uid);
64     const char* rootpath = tzplatform_getenv(TZ_USER_APP);
65     tzplatform_reset_user();
66     return rootpath;
67   }
68 }
69
70 const char* GetExtendedRootAppPath(uid_t uid) {
71   if (GetRequestMode(uid) == RequestMode::GLOBAL) {
72     return tzplatform_getenv(TZ_SYS_EXTENDEDSD_APP);
73   } else {
74     tzplatform_set_user(uid);
75     const char* rootpath = tzplatform_getenv(TZ_USER_EXTENDEDSD_APP);
76     tzplatform_reset_user();
77     return rootpath;
78   }
79 }
80
81 }  // namespace common_installer