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"
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"
19 namespace fs = std::filesystem;
20 namespace ci = common_installer;
24 const char kSharedDataDir[] = "shared/data";
25 const char kSkelAppDir[] = "skel/apps_rw";
27 bool ShouldCreateSharedDataDir(manifest_x* manifest) {
28 if (ci::ShouldSupportLegacySharedDataDir(manifest->api_version))
31 for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
32 if (!strcmp(priv->value, ci::privileges::kPrivForSharedData))
41 namespace common_installer {
42 namespace filesystem {
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());
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());
60 bool StepUpdateStorageDirectories::CleanUpdatePerUserStorageDirectories() {
61 if (old_shared_data_exists_ && !should_create_shared_data_)
62 return ci::RemoveBackupPerUserSharedDataDir(context_->pkgid.get());
66 bool StepUpdateStorageDirectories::CleanUpdateStorageDirectories() {
67 if (old_shared_data_exists_ && !should_create_shared_data_)
68 return ci::RemoveBackupSharedDataDir(context_->pkgid.get(),
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());
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());
89 Step::Status StepUpdateStorageDirectories::precheck() {
90 if (!context_->manifest_data.get())
91 return Status::INVALID_VALUE;
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;
98 path = fs::path(ci::GetRootAppPath(false, context_->uid.get())) /
99 context_->pkgid.get() / kSharedDataDir;
101 old_shared_data_exists_ = fs::exists(path);
102 should_create_shared_data_ = ShouldCreateSharedDataDir(
103 context_->manifest_data.get());
108 Step::Status StepUpdateStorageDirectories::process() {
109 if (context_->request_mode.get() == RequestMode::GLOBAL) {
110 if (!UpdatePerUserStorageDirectories())
111 return Status::APP_DIR_ERROR;
113 RequestType req_type = context_->request_type.get();
114 if (req_type == RequestType::ManifestDirectUpdate ||
115 req_type == RequestType::ManifestPartialUpdate)
118 if (!UpdateStorageDirectories())
119 return Status::APP_DIR_ERROR;
125 Step::Status StepUpdateStorageDirectories::clean() {
126 if (context_->request_mode.get() == RequestMode::GLOBAL)
127 CleanUpdatePerUserStorageDirectories();
129 CleanUpdateStorageDirectories();
133 Step::Status StepUpdateStorageDirectories::undo() {
134 if (context_->request_mode.get() == RequestMode::GLOBAL) {
135 if (!UndoUpdatePerUserStorageDirectories())
136 return Status::APP_DIR_ERROR;
138 if (!UndoUpdateStorageDirectories())
139 return Status::APP_DIR_ERROR;
144 } // namespace filesystem
145 } // namespace common_installer