tizen 2.4 release
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_install_ospsvc.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    task_install_ospsvc.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task  install osp service
21  */
22 #include "task_install_ospsvc.h"
23
24 #include <unistd.h>
25 #include <string>
26
27 #include <pkgmgr/pkgmgr_parser.h>
28 #include <pkgmgr-info.h>
29 #include <fstream>
30 #include <dpl/errno_string.h>
31 #include <dpl/foreach.h>
32 #include <dpl/wrt-dao-ro/global_config.h>
33 #include <dpl/utils/bash_utils.h>
34 #include <privilege-control.h>
35
36 #include <widget_install/widget_install_context.h>
37 #include <widget_install/widget_install_errors.h>
38 #include <dpl/log/secure_log.h>
39
40 using namespace WrtDB;
41
42 namespace {
43 const int MAX_BUF_SIZE = 128;
44 const char* OSP_INSTALL_STR1 = "/usr/etc/package-manager/backend/tpk -iv ";
45 const char* OSP_INSTALL_STR2 = " -p ";
46
47 #if USE(CORE_HYBRID_PKG)
48 const char* CORE_INSTALL_STR1 = "/usr/etc/package-manager/backend/rpm -iv ";
49 const char* CORE_INSTALL_STR2 = " -p ";
50 const char* const CORE_MANIFEST_XML = "tizen-manifest.xml";
51 #endif
52
53 }
54
55 namespace Jobs {
56 namespace WidgetInstall {
57 TaskInstallOspsvc::TaskInstallOspsvc(JobWidgetInstall * const &jobContext) :
58     DPL::TaskDecl<TaskInstallOspsvc>(this),
59     m_jobContext(jobContext)
60 {
61     AddStep(&TaskInstallOspsvc::StartStep);
62     AddStep(&TaskInstallOspsvc::StepUninstallSmack);
63     AddStep(&TaskInstallOspsvc::StepInstallOspService);
64     AddStep(&TaskInstallOspsvc::EndStep);
65 }
66
67 void TaskInstallOspsvc::StepUninstallSmack()
68 {
69     std::string pkgId = DPL::ToUTF8String(m_jobContext->m_installerContext.widgetConfig.tzPkgid);
70     if (m_jobContext->m_installerContext.isUpdateMode) {
71         _D("StepUninstallSmack");
72         if (PC_OPERATION_SUCCESS != perm_app_uninstall(pkgId.c_str())) {
73             _E("failure in removing smack rules file");
74             ThrowMsg(Exceptions::NotAllowed, "Update failure. "
75                     "failure in delete smack rules file before update.");
76         }
77     }
78 }
79
80 void TaskInstallOspsvc::StepInstallOspService()
81 {
82     _D("Step: installation for hybrid service");
83
84     std::ostringstream commStr;
85
86 #if USE(CORE_HYBRID_PKG)
87     bool isCore = false;
88     try {
89         bf::path corePath(m_jobContext->m_installerContext.locations->getPackageInstallationDir());
90         corePath /= "/";
91         corePath /= CORE_MANIFEST_XML;
92         if (bf::exists(corePath)) {
93             isCore = true;
94         }
95     } catch (const bf::filesystem_error& ex) {
96         _E("Not a hybrid app");
97     }
98 #endif
99
100 #if USE(CORE_HYBRID_PKG)
101     if( isCore ){
102         commStr << CORE_INSTALL_STR1 << BashUtils::escape_arg(
103             m_jobContext->m_installerContext.locations->getPackageInstallationDir())
104             << CORE_INSTALL_STR2 << m_jobContext->m_installerContext.certLevel;
105     }else{
106 #endif
107     commStr << OSP_INSTALL_STR1 << BashUtils::escape_arg(
108         m_jobContext->m_installerContext.locations->getPackageInstallationDir())
109         << OSP_INSTALL_STR2 << m_jobContext->m_installerContext.certLevel;
110 #if USE(CORE_HYBRID_PKG)
111     }
112 #endif
113
114     _D("hybrid install command : %s", commStr.str().c_str());
115
116     char readBuf[MAX_BUF_SIZE];
117     FILE *fd;
118     fd = popen(commStr.str().c_str(), "r");
119     if (NULL == fd) {
120         _E("Failed to installtion hybrid service");
121         ThrowMsg(Exceptions::InstallOspsvcFailed,
122                  "Error occurs during\
123                 install hybrid service");
124     }
125
126     if (fgets(readBuf, MAX_BUF_SIZE, fd) == NULL)
127     {
128         _E("Failed to installtion hybrid service.\
129                 Inability of reading file.");
130         ThrowMsg(Exceptions::InstallOspsvcFailed,
131                 "Error occurs during\
132                 install hybrid service");
133     }
134     _D("return value : %s", readBuf);
135
136     int result = atoi(readBuf);
137     if (0 != result) {
138         ThrowMsg(Exceptions::InstallOspsvcFailed,
139                  "Error occurs during\
140                 install hybrid service");
141     }
142
143     pclose(fd);
144 }
145
146 void TaskInstallOspsvc::StartStep()
147 {
148     LOGI("--------- <TaskInstallOspsvc> : START ----------");
149 }
150
151 void TaskInstallOspsvc::EndStep()
152 {
153     m_jobContext->UpdateProgress(
154         InstallerContext::INSTALL_INSTALL_OSPSVC,
155         "Installed Osp servcie");
156
157     LOGI("--------- <TaskInstallOspsvc> : END ----------");
158 }
159 } //namespace WidgetInstall
160 } //namespace Jobs