Remove rw directories when request mode is global
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_copy_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_copy_storage_directories.h"
6
7 #include <boost/filesystem/path.hpp>
8 #include <boost/system/error_code.hpp>
9
10 #include <string>
11
12 #include "common/paths.h"
13 #include "utils/file_util.h"
14
15 namespace bf = boost::filesystem;
16 namespace bs = boost::system;
17
18 namespace {
19
20 const char kCache[] = "cache";
21 const char kDataLocation[] = "data";
22 const char kSharedResLocation[] = "shared";
23 const char kSharedCache[] = "shared/cache";
24 const char kSharedData[] = "shared/data";
25 const char kSharedTrusted[] = "shared/trusted";
26
27 }  // namespace
28
29 namespace common_installer {
30 namespace filesystem {
31
32 bool StepCopyStorageDirectories::MoveAppStorage(
33     const bf::path& in_src,
34     const bf::path& in_dst,
35     const char *key,
36     bool merge_dirs) {
37   bf::path src = in_src / key;
38   bf::path dst = in_dst / key;
39   return common_installer::MoveDir(src, dst,
40       merge_dirs ? common_installer::FS_MERGE_SKIP
41                  : common_installer::FS_NONE);
42 }
43
44 common_installer::Step::Status StepCopyStorageDirectories::precheck() {
45   backup_path_ =
46       common_installer::GetBackupPathForPackagePath(context_->pkg_path.get());
47
48   bs::error_code error_code;
49   if (!bf::exists(backup_path_, error_code)) {
50     LOG(DEBUG) << "Cannot restore storage directories from: " << backup_path_;
51     return Status::INVALID_VALUE;
52   }
53
54   return Status::OK;
55 }
56
57 common_installer::Step::Status StepCopyStorageDirectories::process() {
58   if (context_->request_mode.get() == RequestMode::GLOBAL) {
59     RemoveAll();
60     return Status::OK;
61   }
62
63   if (!MoveAppStorage(backup_path_,
64                       context_->pkg_path.get(),
65                       kDataLocation, true)) {
66     LOG(ERROR) << "Failed to restore private directory for widget in update";
67     return Status::APP_DIR_ERROR;
68   }
69
70   if (!MoveAppStorage(backup_path_,
71                       context_->pkg_path.get(),
72                       kSharedResLocation, true)) {
73     LOG(ERROR) << "Failed to restore shared directory for widget in update";
74     return Status::APP_DIR_ERROR;
75   }
76
77   if (!CacheDir())
78     return Status::APP_DIR_ERROR;
79
80   return Status::OK;
81 }
82
83 common_installer::Step::Status StepCopyStorageDirectories::undo() {
84   if (context_->request_mode.get() == RequestMode::GLOBAL)
85     return Status::OK;
86   common_installer::Step::Status ret = Status::OK;
87   if (!MoveAppStorage(context_->pkg_path.get(),
88                       backup_path_,
89                       kDataLocation)) {
90     LOG(ERROR) << "Failed to restore private directory for package in update";
91     ret = Status::APP_DIR_ERROR;
92   }
93
94   if (!MoveAppStorage(context_->pkg_path.get(),
95                       backup_path_,
96                       kSharedResLocation)) {
97     LOG(ERROR) << "Failed to restore shared directory for package in update";
98     ret = Status::APP_DIR_ERROR;
99   }
100
101   return ret;
102 }
103
104 bool StepCopyStorageDirectories::CacheDir() {
105   bs::error_code error_code;
106   bf::path cache_path = context_->pkg_path.get() / kCache;
107   bf::create_directory(cache_path, error_code);
108   if (error_code) {
109     LOG(ERROR) << "Failed to create cache directory for package";
110     return false;
111   }
112   return true;
113 }
114
115 void StepCopyStorageDirectories::RemoveAll() {
116   bs::error_code error_code;
117   bf::path data_path = context_->pkg_path.get() / kDataLocation;
118   bf::path cache_path = context_->pkg_path.get() / kCache;
119   bf::path shared_data_path = context_->pkg_path.get() / kSharedData;
120   bf::path shared_cache_path = context_->pkg_path.get() / kSharedCache;
121   bf::path shared_trusted_path = context_->pkg_path.get() / kSharedTrusted;
122
123   bf::remove_all(data_path, error_code);
124   bf::remove_all(cache_path, error_code);
125   bf::remove_all(shared_data_path, error_code);
126   bf::remove_all(shared_cache_path, error_code);
127   bf::remove_all(shared_trusted_path, error_code);
128 }
129
130 }  // namespace filesystem
131 }  // namespace common_installer