ff51359446e440adc333012b0ccafca55fd519ce
[platform/core/appfw/app-installers.git] / src / common / step / pkgmgr / step_recover_application.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/pkgmgr/step_recover_application.h"
6
7 #include <boost/filesystem.hpp>
8
9 #include "common/utils/paths.h"
10 #include "common/utils/pkgmgr_query.h"
11 #include "common/pkgmgr_registration.h"
12
13 namespace bf = boost::filesystem;
14
15 namespace common_installer {
16 namespace pkgmgr {
17
18 Step::Status StepRecoverApplication::RecoveryNew() {
19   if (!SetXmlPaths())
20     return Status::OK;
21   UnregisterApplication();
22   return Status::OK;
23 }
24
25 Step::Status StepRecoverApplication::RecoveryUpdate() {
26   if (!SetXmlPaths()) {
27     LOG(ERROR) << "Some parameters are lacking";
28     return Status::ERROR;
29   }
30   PkgQueryInterface pkg_query(context_->pkgid.get(), context_->uid.get());
31   if (pkg_query.IsPreloadPackage())
32     SetExtAttrForPreloadPackage(pkg_query.IsUpdatedPackage(),
33         pkg_query.IsRemovablePackage());
34   UnregisterApplication();
35   if (!RegisterApplication()) {
36     LOG(ERROR) << "Unsuccessful app registration";
37     return Status::RECOVERY_ERROR;
38   }
39   return Status::OK;
40 }
41
42 Step::Status StepRecoverApplication::RecoveryReadonlyUpdateInstall() {
43   if (!SetXmlPaths()) {
44     LOG(ERROR) << "Some parameters are lacking";
45     return Status::ERROR;
46   }
47   SetExtAttrForPreloadPackage(false, false);
48   UnregisterApplication();
49   if (!RegisterApplication()) {
50     LOG(ERROR) << "Unsuccessful app registration";
51     return Status::RECOVERY_ERROR;
52   }
53   return Status::OK;
54 }
55
56 bool StepRecoverApplication::SetXmlPaths() {
57   if (context_->pkgid.get().empty())
58     return false;
59   bf::path xml_path =
60       bf::path(getUserManifestPath(context_->uid.get(),
61           context_->is_readonly_package.get()))
62       / context_->pkgid.get();
63   xml_path += ".xml";
64   context_->xml_path.set(xml_path);
65   context_->backup_xml_path.set(GetBackupPathForManifestFile(xml_path));
66   return true;
67 }
68
69 void StepRecoverApplication::SetExtAttrForPreloadPackage(
70     bool is_updated, bool is_removable) {
71   manifest_x* manifest_data = context_->manifest_data.get();
72   free(manifest_data->preload);
73   manifest_data->preload = strdup("true");
74   free(manifest_data->update);
75   manifest_data->update = strdup(is_updated ? "true" : "false");
76   free(manifest_data->system);
77   manifest_data->system = strdup(is_removable ? "false" : "true");
78   free(manifest_data->readonly);
79   manifest_data->readonly = strdup(is_removable ? "false" : "true");
80   free(manifest_data->removable);
81   manifest_data->removable = strdup(is_removable ? "true" : "false");
82 }
83
84 bool StepRecoverApplication::UnregisterApplication() {
85   if (context_->manifest_data.get())
86     return UnregisterAppInPkgmgr(context_->manifest_data.get(),
87                                  context_->pkgid.get(),
88                                  context_->uid.get(),
89                                  context_->request_mode.get());
90   else
91     return UnregisterAppInPkgmgrForPkgId(context_->pkgid.get(),
92                                          context_->uid.get(),
93                                          context_->request_mode.get());
94 }
95
96 bool StepRecoverApplication::RegisterApplication() {
97   return RegisterAppInPkgmgr(context_->manifest_data.get(),
98                              context_->pkgid.get(),
99                              context_->certificate_info.get(),
100                              context_->uid.get(),
101                              context_->storage.get(),
102                              context_->request_mode.get(),
103                              context_->tep_path.get());
104 }
105
106 }  // namespace pkgmgr
107 }  // namespace common_installer