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