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