Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_update_storage_directories.cc
1 // Copyright (c) 2017 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/filesystem/step_update_storage_directories.h"
6
7 #include <filesystem>
8 #include <string>
9 #include <vector>
10
11 #include "common/utils/paths.h"
12 #include "common/privileges.h"
13 #include "common/security_registration.h"
14 #include "common/shared_dirs.h"
15 #include "common/utils/file_util.h"
16 #include "common/utils/glist_range.h"
17 #include "common/utils/user_util.h"
18
19 namespace fs = std::filesystem;
20 namespace ci = common_installer;
21
22 namespace {
23
24 const char kSharedDataDir[] = "shared/data";
25 const char kSkelAppDir[] = "skel/apps_rw";
26
27 bool ShouldCreateSharedDataDir(manifest_x* manifest) {
28   if (ci::ShouldSupportLegacySharedDataDir(manifest->api_version))
29     return true;
30
31   for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
32     if (!strcmp(priv->value, ci::privileges::kPrivForSharedData))
33       return true;
34   }
35
36   return false;
37 }
38
39 }  // namespace
40
41 namespace common_installer {
42 namespace filesystem {
43
44 bool StepUpdateStorageDirectories::UpdatePerUserStorageDirectories() {
45   if (should_create_shared_data_)
46     return ci::CreatePerUserSharedDataDir(context_->pkgid.get());
47   else if (old_shared_data_exists_ && !should_create_shared_data_)
48     return ci::BackupPerUserSharedDataDir(context_->pkgid.get());
49   return true;
50 }
51
52 bool StepUpdateStorageDirectories::UpdateStorageDirectories() {
53   if (should_create_shared_data_)
54     return ci::CreateSharedDataDir(context_->pkgid.get(), context_->uid.get());
55   else if (old_shared_data_exists_ && !should_create_shared_data_)
56     return ci::BackupSharedDataDir(context_->pkgid.get(), context_->uid.get());
57   return true;
58 }
59
60 bool StepUpdateStorageDirectories::CleanUpdatePerUserStorageDirectories() {
61   if (old_shared_data_exists_ && !should_create_shared_data_)
62     return ci::RemoveBackupPerUserSharedDataDir(context_->pkgid.get());
63   return true;
64 }
65
66 bool StepUpdateStorageDirectories::CleanUpdateStorageDirectories() {
67   if (old_shared_data_exists_ && !should_create_shared_data_)
68     return ci::RemoveBackupSharedDataDir(context_->pkgid.get(),
69         context_->uid.get());
70   return true;
71 }
72
73 bool StepUpdateStorageDirectories::UndoUpdatePerUserStorageDirectories() {
74   if (!old_shared_data_exists_)
75     return ci::DeletePerUserSharedDataDir(context_->pkgid.get());
76   else if (old_shared_data_exists_ && !should_create_shared_data_)
77     return ci::RestorePerUserSharedDataDir(context_->pkgid.get());
78   return true;
79 }
80
81 bool StepUpdateStorageDirectories::UndoUpdateStorageDirectories() {
82   if (!old_shared_data_exists_)
83     return ci::DeleteSharedDataDir(context_->pkgid.get(), context_->uid.get());
84   else if (old_shared_data_exists_ && !should_create_shared_data_)
85     return ci::RestoreSharedDataDir(context_->pkgid.get(), context_->uid.get());
86   return true;
87 }
88
89 Step::Status StepUpdateStorageDirectories::precheck() {
90   if (!context_->manifest_data.get())
91     return Status::INVALID_VALUE;
92
93   fs::path path;
94   if (context_->request_mode.get() == RequestMode::GLOBAL)
95     path = fs::path(tzplatform_getenv(TZ_SYS_ETC)) / fs::path(kSkelAppDir) /
96         context_->pkgid.get() / kSharedDataDir;
97   else
98     path = fs::path(ci::GetRootAppPath(false, context_->uid.get())) /
99         context_->pkgid.get() / kSharedDataDir;
100
101   old_shared_data_exists_ = fs::exists(path);
102   should_create_shared_data_ = ShouldCreateSharedDataDir(
103       context_->manifest_data.get());
104
105   return Status::OK;
106 }
107
108 Step::Status StepUpdateStorageDirectories::process() {
109   if (context_->request_mode.get() == RequestMode::GLOBAL) {
110     if (!UpdatePerUserStorageDirectories())
111       return Status::APP_DIR_ERROR;
112   } else {
113     RequestType req_type = context_->request_type.get();
114     if (req_type == RequestType::ManifestDirectUpdate ||
115         req_type == RequestType::ManifestPartialUpdate)
116       return Status::OK;
117
118     if (!UpdateStorageDirectories())
119       return Status::APP_DIR_ERROR;
120   }
121
122   return Status::OK;
123 }
124
125 Step::Status StepUpdateStorageDirectories::clean() {
126   if (context_->request_mode.get() == RequestMode::GLOBAL)
127     CleanUpdatePerUserStorageDirectories();
128   else
129     CleanUpdateStorageDirectories();
130   return Status::OK;
131 }
132
133 Step::Status StepUpdateStorageDirectories::undo() {
134   if (context_->request_mode.get() == RequestMode::GLOBAL) {
135     if (!UndoUpdatePerUserStorageDirectories())
136       return Status::APP_DIR_ERROR;
137   } else {
138     if (!UndoUpdateStorageDirectories())
139       return Status::APP_DIR_ERROR;
140   }
141   return Status::OK;
142 }
143
144 }  // namespace filesystem
145 }  // namespace common_installer