Fixed update web app
[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/log/log.h>
31 #include <dpl/errno_string.h>
32 #include <dpl/foreach.h>
33 #include <dpl/wrt-dao-ro/global_config.h>
34 #include <dpl/utils/bash_utils.h>
35
36 #include <widget_install/job_widget_install.h>
37 #include <widget_install/widget_install_context.h>
38 #include <widget_install/widget_install_errors.h>
39
40 using namespace WrtDB;
41
42 namespace {
43 const int MAX_BUF_SIZE = 128;
44 const char* OSP_INSTALL_STR = "/usr/etc/package-manager/backend/tpk -iv ";
45 }
46
47 namespace Jobs {
48 namespace WidgetInstall {
49 TaskInstallOspsvc::TaskInstallOspsvc(InstallerContext& context) :
50     DPL::TaskDecl<TaskInstallOspsvc>(this),
51     m_context(context)
52 {
53     AddStep(&TaskInstallOspsvc::StepInstallOspService);
54     AddStep(&TaskInstallOspsvc::StepUpdateManifestFile);
55 }
56
57 void TaskInstallOspsvc::StepInstallOspService()
58 {
59     LogInfo("Step: installation for osp service");
60
61     std::ostringstream commStr;
62     commStr << OSP_INSTALL_STR << BashUtils::escape_arg(
63         m_context.locations->getPackageInstallationDir());
64     //commStr << " 2>&1";
65     LogDebug("osp install command : " << commStr.str());
66
67     char readBuf[MAX_BUF_SIZE];
68     FILE *fd;
69     fd = popen(commStr.str().c_str(), "r");
70     if (NULL == fd) {
71         LogError("Failed to installtion osp service");
72         ThrowMsg(Exceptions::InstallOspsvcFailed,
73                  "Error occurs during\
74                 install osp service");
75     }
76
77     if (fgets(readBuf, MAX_BUF_SIZE, fd) == NULL)
78     {
79         LogError("Failed to installtion osp service.\
80                 Inability of reading file.");
81         ThrowMsg(Exceptions::InstallOspsvcFailed,
82                 "Error occurs during\
83                 install osp service");
84     }
85     LogDebug("return value : " << readBuf);
86
87     int result = atoi(readBuf);
88     if (0 != result) {
89         ThrowMsg(Exceptions::InstallOspsvcFailed,
90                  "Error occurs during\
91                 install osp service");
92     }
93
94     pclose(fd);
95
96     m_context.job->UpdateProgress(
97         InstallerContext::INSTALL_INSTALL_OSPSVC,
98         "Installed Osp servcie");
99 }
100
101 void TaskInstallOspsvc::StepUpdateManifestFile()
102 {
103     std::string pkgid = DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
104     pkgmgrinfo_pkginfo_h handle;
105
106     int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid.c_str(), &handle);
107     if (ret != PMINFO_R_OK) {
108         LogDebug("StepUpdateManifestFile");
109         std::ostringstream manifest_file;
110         if (m_context.mode.rootPath == InstallMode::RootPath::RO) {
111             manifest_file << "/usr/share/packages/"; //TODO constant with path
112         } else {
113             manifest_file << "/opt/share/packages/"; //TODO constant with path
114         }
115         manifest_file << pkgid;
116         manifest_file << ".xml";
117         LogDebug("manifest file : " << manifest_file.str());
118
119         int ret = pkgmgr_parser_parse_manifest_for_uninstallation(
120                 manifest_file.str().c_str(), NULL);
121
122         if (ret != 0) {
123             LogError("Manifest parser error: " << ret);
124         }
125
126         int code = pkgmgr_parser_parse_manifest_for_installation(
127                 manifest_file.str().c_str(), NULL);
128
129         if (code != 0) {
130             LogError("Manifest parser error: " << code);
131         }
132     }
133 }
134 } //namespace WidgetInstall
135 } //namespace Jobs