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.
5 #include "common/step/filesystem/step_update_storage_directories.h"
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/filesystem/path.hpp>
9 #include <boost/system/error_code.hpp>
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"
22 namespace bf = boost::filesystem;
23 namespace bs = boost::system;
24 namespace ci = common_installer;
28 const char kSharedDataDir[] = "shared/data";
29 const char kSkelAppDir[] = "skel/apps_rw";
31 bool ShouldCreateSharedDataDir(manifest_x* manifest) {
32 if (ci::ShouldSupportLegacySharedDataDir(manifest->api_version))
35 for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
36 if (!strcmp(priv->value, ci::privileges::kPrivForSharedData))
45 namespace common_installer {
46 namespace filesystem {
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());
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());
64 bool StepUpdateStorageDirectories::CleanUpdatePerUserStorageDirectories() {
65 if (old_shared_data_exists_ && !should_create_shared_data_)
66 return ci::RemoveBackupPerUserSharedDataDir(context_->pkgid.get());
70 bool StepUpdateStorageDirectories::CleanUpdateStorageDirectories() {
71 if (old_shared_data_exists_ && !should_create_shared_data_)
72 return ci::RemoveBackupSharedDataDir(context_->pkgid.get(),
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());
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());
93 Step::Status StepUpdateStorageDirectories::precheck() {
94 if (!context_->manifest_data.get())
95 return Status::INVALID_VALUE;
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;
102 path = bf::path(ci::GetRootAppPath(false, context_->uid.get())) /
103 context_->pkgid.get() / kSharedDataDir;
105 old_shared_data_exists_ = bf::exists(path);
106 should_create_shared_data_ = ShouldCreateSharedDataDir(
107 context_->manifest_data.get());
112 Step::Status StepUpdateStorageDirectories::process() {
113 if (context_->request_mode.get() == RequestMode::GLOBAL) {
114 if (!UpdatePerUserStorageDirectories())
115 return Status::APP_DIR_ERROR;
117 RequestType req_type = context_->request_type.get();
118 if (req_type == RequestType::ManifestDirectUpdate ||
119 req_type == RequestType::ManifestPartialUpdate)
122 if (!UpdateStorageDirectories())
123 return Status::APP_DIR_ERROR;
129 Step::Status StepUpdateStorageDirectories::clean() {
130 if (context_->request_mode.get() == RequestMode::GLOBAL)
131 CleanUpdatePerUserStorageDirectories();
133 CleanUpdateStorageDirectories();
137 Step::Status StepUpdateStorageDirectories::undo() {
138 if (context_->request_mode.get() == RequestMode::GLOBAL) {
139 if (!UndoUpdatePerUserStorageDirectories())
140 return Status::APP_DIR_ERROR;
142 if (!UndoUpdateStorageDirectories())
143 return Status::APP_DIR_ERROR;
148 } // namespace filesystem
149 } // namespace common_installer