Fix undo() of StepCopyStorageDirectories
[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
24 }  // namespace
25
26 namespace common_installer {
27 namespace filesystem {
28
29 bool StepCopyStorageDirectories::MoveAppStorage(
30     const bf::path& in_src,
31     const bf::path& in_dst,
32     const char *key,
33     bool merge_dirs) {
34   bf::path src = in_src / key;
35   bf::path dst = in_dst / key;
36   return common_installer::MoveDir(src, dst,
37       merge_dirs ? common_installer::FS_MERGE_SKIP
38                  : common_installer::FS_NONE);
39 }
40
41 common_installer::Step::Status StepCopyStorageDirectories::precheck() {
42   backup_path_ =
43       common_installer::GetBackupPathForPackagePath(context_->pkg_path.get());
44
45   bs::error_code error_code;
46   if (!bf::exists(backup_path_, error_code)) {
47     LOG(DEBUG) << "Cannot restore storage directories from: " << backup_path_;
48     return Status::INVALID_VALUE;
49   }
50
51   return Status::OK;
52 }
53
54 common_installer::Step::Status StepCopyStorageDirectories::process() {
55   if (context_->request_mode.get() == RequestMode::GLOBAL)
56     return Status::OK;
57   if (!MoveAppStorage(backup_path_,
58                       context_->pkg_path.get(),
59                       kDataLocation, true)) {
60     LOG(ERROR) << "Failed to restore private directory for widget in update";
61     return Status::APP_DIR_ERROR;
62   }
63
64   if (!MoveAppStorage(backup_path_,
65                       context_->pkg_path.get(),
66                       kSharedResLocation, true)) {
67     LOG(ERROR) << "Failed to restore shared directory for widget in update";
68     return Status::APP_DIR_ERROR;
69   }
70
71   if (!CacheDir())
72     return Status::APP_DIR_ERROR;
73
74   return Status::OK;
75 }
76
77 common_installer::Step::Status StepCopyStorageDirectories::undo() {
78   if (context_->request_mode.get() == RequestMode::GLOBAL)
79     return Status::OK;
80   common_installer::Step::Status ret = Status::OK;
81   if (!MoveAppStorage(context_->pkg_path.get(),
82                       backup_path_,
83                       kDataLocation)) {
84     LOG(ERROR) << "Failed to restore private directory for package in update";
85     ret = Status::APP_DIR_ERROR;
86   }
87
88   if (!MoveAppStorage(context_->pkg_path.get(),
89                       backup_path_,
90                       kSharedResLocation)) {
91     LOG(ERROR) << "Failed to restore shared directory for package in update";
92     ret = Status::APP_DIR_ERROR;
93   }
94
95   return ret;
96 }
97
98 bool StepCopyStorageDirectories::CacheDir() {
99   bs::error_code error_code;
100   bf::path cache_path = context_->pkg_path.get() / kCache;
101   bf::create_directory(cache_path, error_code);
102   if (error_code) {
103     LOG(ERROR) << "Failed to create cache directory for package";
104     return false;
105   }
106   return true;
107 }
108
109 }  // namespace filesystem
110 }  // namespace common_installer