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.
5 #include "common/step/filesystem/step_recover_storage_directories.h"
6 #include "common/step/filesystem/step_remove_per_user_storage_directories.h"
8 #include <pkgmgrinfo_basic.h>
12 #include "common/utils/paths.h"
13 #include "common/privileges.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 ci = common_installer;
20 namespace fs = std::filesystem;
24 const char kDataLocation[] = "data";
25 const char kSharedResLocation[] = "shared";
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 Step::Status StepRecoverStorageDirectories::RecoveryNew() {
45 StepRemovePerUserStorageDirectories step(context_);
46 Status status = step.precheck();
47 if (status != Status::OK)
49 return step.process();
52 bool StepRecoverStorageDirectories::MoveAppStorage(
53 const fs::path& in_src,
54 const fs::path& in_dst,
56 fs::path src = in_src / key;
59 fs::path dst = in_dst / key;
60 return common_installer::MoveDir(src, dst);
63 Step::Status StepRecoverStorageDirectories::RecoveryUpdate() {
64 if (!context_->GetPkgPath().empty()) {
65 fs::path backup_path = common_installer::GetBackupPathForPackagePath(
66 context_->GetPkgPath());
67 if (fs::exists(backup_path)) {
68 MoveAppStorage(context_->GetPkgPath(), backup_path, kDataLocation);
69 MoveAppStorage(context_->GetPkgPath(), backup_path, kSharedResLocation);
72 manifest_x* manifest = context_->manifest_data.get();
74 LOG(WARNING) << "Cannot find manifest data. "
75 "Some directories may not be recovered.";
79 if (ShouldCreateSharedDataDir(manifest)) {
80 if (context_->request_mode.get() == RequestMode::GLOBAL) {
81 if (!ci::RestorePerUserSharedDataDir(context_->pkgid.get()))
82 return Status::APP_DIR_ERROR;
84 if (!ci::RestoreSharedDataDir(context_->pkgid.get(),
86 return Status::APP_DIR_ERROR;
89 if (context_->request_mode.get() == RequestMode::GLOBAL) {
90 if (!ci::DeletePerUserSharedDataDir(context_->pkgid.get()))
91 return Status::APP_DIR_ERROR;
93 if (!ci::DeleteSharedDataDir(context_->pkgid.get(),
95 return Status::APP_DIR_ERROR;
102 Step::Status StepRecoverStorageDirectories::RecoveryMountUpdate() {
106 } // namespace filesystem
107 } // namespace common_installer