Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / backup / step_backup_manifest.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/backup/step_backup_manifest.h"
6
7 #include <pkgmgr-info.h>
8 #include <pkgmgr_installer.h>
9
10 #include <algorithm>
11 #include <filesystem>
12 #include <string>
13 #include <system_error>
14
15 #include "common/utils/paths.h"
16 #include "common/utils/file_util.h"
17
18 namespace ci = common_installer;
19 namespace fs = std::filesystem;
20
21 namespace common_installer {
22 namespace backup {
23
24 Step::Status StepBackupManifest::precheck() {
25   if (!fs::exists(context_->xml_path.get())) {
26     LOG(ERROR) << "Xml manifest file does not exist";
27     return Status::MANIFEST_NOT_FOUND;
28   }
29
30   return Status::OK;
31 }
32
33 Step::Status StepBackupManifest::process() {
34   // set backup file path
35   fs::path backup_xml_path =
36       GetBackupPathForManifestFile(context_->xml_path.get());
37   context_->backup_xml_path.set(backup_xml_path);
38   if (!MoveFile(context_->xml_path.get(), backup_xml_path, true) ||
39       !CopyFile(backup_xml_path, context_->xml_path.get())) {
40     LOG(ERROR) << "Failed to make a copy of xml manifest file";
41     return Status::MANIFEST_ERROR;
42   }
43
44   LOG(DEBUG) << "Manifest backup created";
45   return Status::OK;
46 }
47
48 Step::Status StepBackupManifest::clean() {
49   LOG(DEBUG) << "Remove manifest backup";
50   ci::Remove(context_->backup_xml_path.get());
51
52   return Status::OK;
53 }
54
55 Step::Status StepBackupManifest::undo() {
56   if (!fs::exists(context_->backup_xml_path.get()))
57     return Status::OK;
58
59   std::error_code error;
60   fs::remove(context_->xml_path.get(), error);
61   if (error) {
62     LOG(ERROR) << "Failed to remove newly generated xml file in revert";
63     return Status::MANIFEST_ERROR;
64   }
65   if (!MoveFile(context_->backup_xml_path.get(),
66       context_->xml_path.get())) {
67     LOG(ERROR) << "Failed to revert a content of xml manifest file";
68     return Status::MANIFEST_ERROR;
69   }
70   LOG(DEBUG) << "Manifest reverted from backup";
71
72   return Status::OK;
73 }
74
75 }  // namespace backup
76 }  // namespace common_installer