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.
5 #include "wgt/step/filesystem/step_wgt_update_package_directory.h"
7 #include <boost/filesystem/path.hpp>
8 #include <boost/filesystem/operations.hpp>
9 #include <boost/system/error_code.hpp>
11 #include <common/paths.h>
12 #include <common/utils/file_util.h>
14 namespace bf = boost::filesystem;
15 namespace bs = boost::system;
16 namespace ci = common_installer;
20 const char* kBackupEntries[] = {
25 bool MoveCreateDir(const bf::path& source, const bf::path& destination) {
26 if (!bf::exists(destination.parent_path())) {
28 bf::create_directories(destination.parent_path(), error);
30 LOG(ERROR) << "Cannot create directory: " << destination.parent_path();
34 if (!ci::MoveDir(source, destination)) {
35 LOG(ERROR) << "Failed to move directory " << destination;
44 namespace filesystem {
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))
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 "
59 return Status::APP_DIR_ERROR;
66 StepWgtUpdatePackageDirectory::RecoverBackupOfDirectories() {
67 bf::path backup_path =
68 ci::GetBackupPathForPackagePath(context_->GetPkgPath());
70 // skip if there is no backup of directories
71 if (!bf::exists(backup_path))
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))
79 LOG(DEBUG) << "Recover directory entry: " << entry;
81 bf::remove_all(directory, error);
83 LOG(ERROR) << "Failed to remove fail-update directory: " << directory;
84 return Status::APP_DIR_ERROR;
86 if (!ci::MoveDir(directory_backup, directory)) {
87 LOG(ERROR) << "Failed to restore directory backup "
89 return Status::APP_DIR_ERROR;
95 ci::Step::Status StepWgtUpdatePackageDirectory::process() {
96 auto status = CreateBackupOfDirectories();
97 if (status != Status::OK)
99 LOG(DEBUG) << "Directory backups created";
101 status = StepWgtPreparePackageDirectory::process();
102 if (status != Status::OK)
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);
116 ci::Step::Status StepWgtUpdatePackageDirectory::undo() {
117 Status status = RecoverBackupOfDirectories();
118 if (status != Status::OK)
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);
126 LOG(ERROR) << "Failed to remove backup directories";
127 return Status::APP_DIR_ERROR;
133 } // namespace filesystem