Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_recover_storage_directories.cc
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.
4
5 #include "common/step/filesystem/step_recover_storage_directories.h"
6 #include "common/step/filesystem/step_remove_per_user_storage_directories.h"
7
8 #include <pkgmgrinfo_basic.h>
9
10 #include <filesystem>
11
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"
18
19 namespace ci = common_installer;
20 namespace fs = std::filesystem;
21
22 namespace {
23
24 const char kDataLocation[] = "data";
25 const char kSharedResLocation[] = "shared";
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 Step::Status StepRecoverStorageDirectories::RecoveryNew() {
45   StepRemovePerUserStorageDirectories step(context_);
46   Status status = step.precheck();
47   if (status != Status::OK)
48     return status;
49   return step.process();
50 }
51
52 bool StepRecoverStorageDirectories::MoveAppStorage(
53     const fs::path& in_src,
54     const fs::path& in_dst,
55     const char *key) {
56   fs::path src = in_src / key;
57   if (!fs::exists(src))
58     return false;
59   fs::path dst = in_dst / key;
60   return common_installer::MoveDir(src, dst);
61 }
62
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);
70     }
71
72     manifest_x* manifest = context_->manifest_data.get();
73     if (!manifest) {
74       LOG(WARNING) << "Cannot find manifest data. "
75                       "Some directories may not be recovered.";
76       return Status::OK;
77     }
78
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;
83       } else {
84         if (!ci::RestoreSharedDataDir(context_->pkgid.get(),
85                 context_->uid.get()))
86           return Status::APP_DIR_ERROR;
87       }
88     } else {
89       if (context_->request_mode.get() == RequestMode::GLOBAL) {
90         if (!ci::DeletePerUserSharedDataDir(context_->pkgid.get()))
91           return Status::APP_DIR_ERROR;
92       } else {
93         if (!ci::DeleteSharedDataDir(context_->pkgid.get(),
94                 context_->uid.get()))
95           return Status::APP_DIR_ERROR;
96       }
97     }
98   }
99   return Status::OK;
100 }
101
102 Step::Status StepRecoverStorageDirectories::RecoveryMountUpdate() {
103   return Status::OK;
104 }
105
106 }  // namespace filesystem
107 }  // namespace common_installer