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_copy_storage_directories.h"
7 #include <boost/filesystem/path.hpp>
8 #include <boost/system/error_code.hpp>
12 #include "common/paths.h"
13 #include "utils/file_util.h"
15 namespace bf = boost::filesystem;
16 namespace bs = boost::system;
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";
29 namespace common_installer {
30 namespace filesystem {
32 bool StepCopyStorageDirectories::MoveAppStorage(
33 const bf::path& in_src,
34 const bf::path& in_dst,
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);
44 common_installer::Step::Status StepCopyStorageDirectories::precheck() {
46 common_installer::GetBackupPathForPackagePath(context_->pkg_path.get());
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;
57 common_installer::Step::Status StepCopyStorageDirectories::process() {
58 if (context_->request_mode.get() == RequestMode::GLOBAL) {
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;
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;
78 return Status::APP_DIR_ERROR;
83 common_installer::Step::Status StepCopyStorageDirectories::undo() {
84 if (context_->request_mode.get() == RequestMode::GLOBAL)
86 common_installer::Step::Status ret = Status::OK;
87 if (!MoveAppStorage(context_->pkg_path.get(),
90 LOG(ERROR) << "Failed to restore private directory for package in update";
91 ret = Status::APP_DIR_ERROR;
94 if (!MoveAppStorage(context_->pkg_path.get(),
96 kSharedResLocation)) {
97 LOG(ERROR) << "Failed to restore shared directory for package in update";
98 ret = Status::APP_DIR_ERROR;
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);
109 LOG(ERROR) << "Failed to create cache directory for package";
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;
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);
130 } // namespace filesystem
131 } // namespace common_installer