Change clean operations not to return failure
[platform/core/appfw/wgt-backend.git] / src / wgt / step / filesystem / step_wgt_update_package_directory.cc
1 // Copyright (c) 2016 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 "wgt/step/filesystem/step_wgt_update_package_directory.h"
6
7 #include <boost/filesystem/path.hpp>
8 #include <boost/filesystem/operations.hpp>
9 #include <boost/system/error_code.hpp>
10
11 #include <common/paths.h>
12 #include <common/utils/file_util.h>
13
14 namespace bf = boost::filesystem;
15 namespace bs = boost::system;
16 namespace ci = common_installer;
17
18 namespace {
19
20 const char* kBackupEntries[] = {
21   "bin",
22   "shared/res"
23 };
24
25 bool MoveCreateDir(const bf::path& source, const bf::path& destination) {
26   if (!bf::exists(destination.parent_path())) {
27     bs::error_code error;
28     bf::create_directories(destination.parent_path(), error);
29     if (error) {
30       LOG(ERROR) << "Cannot create directory: " << destination.parent_path();
31       return false;
32     }
33   }
34   if (!ci::MoveDir(source, destination)) {
35     LOG(ERROR) << "Failed to move directory " << destination;
36     return false;
37   }
38   return true;
39 }
40
41 }  // namespace
42
43 namespace wgt {
44 namespace filesystem {
45
46 ci::Step::Status
47 StepWgtUpdatePackageDirectory::CreateBackupOfDirectories() {
48   bf::path backup_path =
49       ci::GetBackupPathForPackagePath(context_->GetPkgPath());
50   for (auto& entry : kBackupEntries) {
51     bf::path directory = context_->GetPkgPath() / entry;
52     if (!bf::exists(directory))
53       continue;
54     LOG(DEBUG) << "Backup directory entry: " << entry;
55     bf::path directory_backup = backup_path / entry;
56     if (!MoveCreateDir(directory, directory_backup)) {
57       LOG(ERROR) << "Failed to create backup directory "
58                  << directory_backup;
59       return Status::APP_DIR_ERROR;
60     }
61   }
62   return Status::OK;
63 }
64
65 ci::Step::Status
66 StepWgtUpdatePackageDirectory::RecoverBackupOfDirectories() {
67   bf::path backup_path =
68       ci::GetBackupPathForPackagePath(context_->GetPkgPath());
69
70   // skip if there is no backup of directories
71   if (!bf::exists(backup_path))
72     return Status::OK;
73
74   for (auto& entry : kBackupEntries) {
75     bf::path directory = context_->GetPkgPath() / entry;
76     bf::path directory_backup = backup_path / entry;
77     if (!bf::exists(directory_backup))
78         continue;
79     LOG(DEBUG) << "Recover directory entry: " << entry;
80     bs::error_code error;
81     bf::remove_all(directory, error);
82     if (error) {
83       LOG(ERROR) << "Failed to remove fail-update directory: " << directory;
84       return Status::APP_DIR_ERROR;
85     }
86     if (!ci::MoveDir(directory_backup, directory)) {
87       LOG(ERROR) << "Failed to restore directory backup "
88                  << directory_backup;
89       return Status::APP_DIR_ERROR;
90     }
91   }
92   return Status::OK;
93 }
94
95 ci::Step::Status StepWgtUpdatePackageDirectory::process() {
96   auto status = CreateBackupOfDirectories();
97   if (status != Status::OK)
98     return status;
99   LOG(DEBUG) << "Directory backups created";
100
101   status = StepWgtPreparePackageDirectory::process();
102   if (status != Status::OK)
103     return status;
104
105   return Status::OK;
106 }
107
108 ci::Step::Status StepWgtUpdatePackageDirectory::clean() {
109   bf::path backup_path =
110       ci::GetBackupPathForPackagePath(context_->GetPkgPath());
111   if (bf::exists(backup_path))
112     ci::RemoveAll(backup_path);
113   return Status::OK;
114 }
115
116 ci::Step::Status StepWgtUpdatePackageDirectory::undo() {
117   Status status = RecoverBackupOfDirectories();
118   if (status != Status::OK)
119     return status;
120   bf::path backup_path =
121       ci::GetBackupPathForPackagePath(context_->GetPkgPath());
122   if (bf::exists(backup_path)) {
123     bs::error_code error;
124     bf::remove_all(backup_path, error);
125     if (error) {
126       LOG(ERROR) << "Failed to remove backup directories";
127       return Status::APP_DIR_ERROR;
128     }
129   }
130   return Status::OK;
131 }
132
133 }  // namespace filesystem
134 }  // namespace wgt