Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_update_tep.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_update_tep.h"
6
7 #include <pkgmgr-info.h>
8
9 #include <cstring>
10 #include <filesystem>
11 #include <string>
12 #include <system_error>
13
14 #include "common/utils/paths.h"
15 #include "common/pkgmgr_registration.h"
16 #include "common/utils/file_util.h"
17
18 namespace fs = std::filesystem;
19
20 namespace common_installer {
21 namespace filesystem {
22
23 Step::Status StepUpdateTep::precheck() {
24   if (!context_->old_manifest_data.get()) {
25     LOG(ERROR) << "Old manifest data is not set";
26     return Status::INVALID_VALUE;
27   }
28   return StepCopyTep::precheck();
29 }
30
31 Step::Status StepUpdateTep::process() {
32   // copy new tep file to package path if possible
33   Status status = StepCopyTep::process();
34   if (status != Status::OK)
35     return status;
36
37   // preserve old tep path if no new tep is supplied
38   if (context_->tep_path.get().empty() &&
39       context_->old_manifest_data.get()->tep_name) {
40     if (context_->manifest_data.get()->tep_name)
41       free(context_->manifest_data.get()->tep_name);
42     context_->manifest_data.get()->tep_name =
43         strdup(context_->old_manifest_data.get()->tep_name);
44
45     // Tep doesn't need to be copied in mount update as this mode is not
46     // creating new package directory so tep is in place.
47     if (context_->request_type.get() == RequestType::MountUpdate)
48       return Status::OK;
49
50     // copy if necessary
51     if (!context_->external_storage) {
52       fs::path new_path = context_->manifest_data.get()->tep_name;
53       fs::path backup_path =
54           GetInternalTepPath(
55               GetBackupPathForPackagePath(context_->GetPkgPath()));
56       backup_path /= new_path.filename();
57       if (!fs::exists(new_path.parent_path())) {
58         std::error_code error;
59         fs::create_directory(new_path.parent_path(), error);
60         if (error) {
61           LOG(ERROR) << "Cannot recreate directory for tep file";
62           return Status::APP_DIR_ERROR;
63         }
64       }
65       if (!CopyFile(backup_path, new_path)) {
66         LOG(ERROR) << "Failed to preserve tep file during update";
67         return Status::APP_DIR_ERROR;
68       }
69     }
70   }
71   return Status::OK;
72 }
73
74 Step::Status StepUpdateTep::clean() {
75   if (context_->external_storage) {
76     if (context_->old_manifest_data.get()->tep_name &&
77         strcmp(context_->old_manifest_data.get()->tep_name,
78                context_->manifest_data.get()->tep_name)) {
79       fs::path old_tep = context_->old_manifest_data.get()->tep_name;
80       Remove(old_tep);
81     }
82   }
83   return Status::OK;
84 }
85
86 Step::Status StepUpdateTep::undo() {
87   const fs::path& remove_path = context_->tep_path.get();
88   RemoveAll(context_->old_manifest_data.get()->tep_name ?
89       remove_path : remove_path.parent_path());
90   return Status::OK;
91 }
92
93 }  // namespace filesystem
94 }  // namespace common_installer