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.
5 #include "common/step/filesystem/step_update_tep.h"
7 #include <pkgmgr-info.h>
12 #include <system_error>
14 #include "common/utils/paths.h"
15 #include "common/pkgmgr_registration.h"
16 #include "common/utils/file_util.h"
18 namespace fs = std::filesystem;
20 namespace common_installer {
21 namespace filesystem {
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;
28 return StepCopyTep::precheck();
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)
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);
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)
51 if (!context_->external_storage) {
52 fs::path new_path = context_->manifest_data.get()->tep_name;
53 fs::path backup_path =
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);
61 LOG(ERROR) << "Cannot recreate directory for tep file";
62 return Status::APP_DIR_ERROR;
65 if (!CopyFile(backup_path, new_path)) {
66 LOG(ERROR) << "Failed to preserve tep file during update";
67 return Status::APP_DIR_ERROR;
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;
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());
93 } // namespace filesystem
94 } // namespace common_installer