Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / pkgmgr / step_unregister_app.cc
1 // Copyright (c) 2014 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/pkgmgr/step_unregister_app.h"
6
7 #include <unistd.h>
8
9 #include <pkgmgr_installer.h>
10 #include <vcore/Certificate.h>
11
12 #include <cassert>
13 #include <filesystem>
14 #include <string>
15
16 #include "common/pkgmgr_registration.h"
17 #include "common/utils/pkgmgr_query.h"
18 #include "common/utils/file_util.h"
19
20 namespace common_installer {
21 namespace pkgmgr {
22
23 Step::Status StepUnregisterApplication::precheck() {
24   if (context_->pkgid.get().empty()) {
25     LOG(ERROR) << "pkgid attribute is empty";
26     return Status::PACKAGE_NOT_FOUND;
27   }
28
29   if (context_->force_clean_from_db.get())
30     return Step::Status::OK;
31
32   if (context_->xml_path.get().empty()) {
33     LOG(ERROR) << "xml_path attribute is empty";
34     return Status::MANIFEST_NOT_FOUND;
35   }
36
37   if (!std::filesystem::exists(context_->xml_path.get())) {
38     LOG(ERROR) << "xml_path ("
39                << context_->xml_path.get()
40                << ") path does not exist";
41     return Status::MANIFEST_NOT_FOUND;
42   }
43
44   return Step::Status::OK;
45 }
46
47 bool StepUnregisterApplication::BackupCertInfo() {
48   std::string base64 = QueryCertificateAuthorCertificate(context_->pkgid.get(),
49                                                          context_->uid.get());
50   if (base64.empty())
51     return true;
52
53   CertificateInfo certificate_info;
54   try {
55     certificate_info.auth_cert.set(ValidationCore::CertificatePtr(
56         new ValidationCore::Certificate(
57             base64,
58             ValidationCore::Certificate::FormType::FORM_BASE64)));
59   } catch (const ValidationCore::Certificate::Exception::Base &e) {
60     LOG(ERROR) << "Exception in cert-svc-vcore Certificate "
61                 << "Dump : " << e.DumpToString();
62     return false;
63   }
64   context_->certificate_info.set(certificate_info);
65
66   return true;
67 }
68
69 Step::Status StepUnregisterApplication::process() {
70   // Prepare certificate info for rollback operations
71   if (!BackupCertInfo())
72     LOG(ERROR) << "Failed to backup cert info";
73
74   if (!UnregisterAppInPkgmgr(context_->manifest_data.get(),
75                              context_->pkgid.get(),
76                              context_->uid.get(),
77                              context_->request_mode.get())) {
78     LOG(ERROR) << "Failed to unregister application from database";
79     return Step::Status::REGISTER_ERROR;
80   }
81
82   LOG(DEBUG) << "Successfully unregister the application";
83   return Status::OK;
84 }
85
86 Step::Status StepUnregisterApplication::undo() {
87   if (!RegisterAppInPkgmgr(context_->manifest_data.get(),
88                            context_->pkgid.get(),
89                            context_->certificate_info.get(),
90                            context_->uid.get(),
91                            context_->storage.get(),
92                            context_->request_mode.get(),
93                            context_->tep_path.get())) {
94     LOG(ERROR) << "Failed to restore application registration";
95     return Step::Status::REGISTER_ERROR;
96   }
97
98   LOG(INFO) << "Successfully restored application registration";
99   return Status::OK;
100 }
101
102 }  // namespace pkgmgr
103 }  // namespace common_installer