Merge "[Prevent] Handle return value."
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / task_database.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_new_db_insert.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @author  Soyoung kim(sy037.kim@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for installer task database updating for widget update
22  */
23 #include <time.h>
24 #include <sys/stat.h>
25 #include <widget_install/task_database.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30 #include <dpl/foreach.h>
31 #include <dpl/utils/wrt_utility.h>
32 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
34 #include <string>
35 #include <sstream>
36 #include <ace_api_install.h>
37 #include <ace_registration.h>
38 #include <errno.h>
39 #include <string.h>
40
41 using namespace WrtDB;
42
43 namespace Jobs {
44 namespace WidgetInstall {
45 TaskDatabase::TaskDatabase(InstallerContext& context) :
46     DPL::TaskDecl<TaskDatabase>(this),
47     m_context(context),
48     m_handleToRemove(INVALID_WIDGET_HANDLE),
49     m_handle(INVALID_WIDGET_HANDLE)
50 {
51     AddStep(&TaskDatabase::StepRegisterExternalFiles);
52     AddStep(&TaskDatabase::StepWrtDBInsert);
53     AddStep(&TaskDatabase::StepAceDBInsert);
54     AddStep(&TaskDatabase::StepRemoveExternalFiles);
55
56     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
57 }
58
59 void TaskDatabase::StepWrtDBInsert()
60 {
61     Try
62     {
63         /* Set install Time */
64         time(&m_context.widgetConfig.installedTime);
65
66         if (m_context.existingWidgetInfo.isExist) //update
67         {
68             m_handleToRemove = WidgetDAOReadOnly::getHandle(
69                 m_context.locations->getPkgname());
70             LogInfo("Registering widget... (update)");
71             WidgetDAO::registerOrUpdateWidget(
72                     m_context.locations->getPkgname(),
73                     m_context.widgetConfig,
74                     m_context.wacSecurity);
75             m_handle = WidgetDAOReadOnly::getHandle(
76                 m_context.locations->getPkgname());
77         }
78         else //new installation
79         {
80             LogInfo("Registering widget...");
81             WidgetDAO::registerWidget(
82                     m_context.locations->getPkgname(),
83                     m_context.widgetConfig,
84                     m_context.wacSecurity);
85             m_handle = WidgetDAOReadOnly::getHandle(
86                 m_context.locations->getPkgname());
87         }
88
89         FOREACH (cap, m_context.staticPermittedDevCaps) {
90             LogInfo("staticPermittedDevCaps : " << cap->first
91                     << " smack status: " << cap->second);
92         }
93
94         LogInfo("Widget registered");
95     }
96     Catch(WidgetDAO::Exception::DatabaseError)
97     {
98         LogError("Database failure!");
99         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
100     }
101     Catch(DPL::DB::SqlConnection::Exception::Base)
102     {
103         LogError("Database failure!");
104         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
105     }
106 }
107
108 void TaskDatabase::StepAceDBInsert()
109 {
110     LogDebug("Inserting Ace database entry. New handle: " << m_handle);
111     if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
112         LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
113         if (ACE_OK != ace_unregister_widget(
114                 static_cast<ace_widget_handle_t>(m_handleToRemove)))
115         {
116             LogWarning("Error while removing ace entry for previous insallation");
117         };
118     }
119
120     if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
121                                   m_context.wacSecurity.getCertificateList()))
122     {
123         LogError("ace database insert failed");
124         ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
125     }
126     LogDebug("Ace data inserted");
127
128     m_context.job->UpdateProgress(
129         InstallerContext::INSTALL_NEW_DB_INSERT,
130         "New Widget DB UPDATE Finished");
131 }
132
133 void TaskDatabase::StepRegisterExternalFiles()
134 {
135     WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
136     if (m_context.existingWidgetInfo.isExist) //update
137     {
138         WidgetDAO dao(m_context.locations->getPkgname());
139         WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
140         FOREACH(file, externalLocationsDB)
141         {
142             if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
143             {
144                 m_externalLocationsToRemove.push_back(*file);
145             }
146         }
147     }
148     LogDebug("Registering external files:");
149     FOREACH(file, externalLocationsUpdate)
150     {
151         LogDebug("  -> " << *file);
152     }
153
154     //set external locations to be registered
155     m_context.widgetConfig.externalLocations = externalLocationsUpdate;
156 }
157
158 void TaskDatabase::StepRemoveExternalFiles()
159 {
160     if(!m_externalLocationsToRemove.empty())
161     {
162         LogDebug("Removing external files:");
163     }
164
165     FOREACH(file, m_externalLocationsToRemove)
166     {
167         if(WrtUtilFileExists(*file))
168         {
169             LogDebug("  -> " << *file);
170             remove(file->c_str());
171         }
172         else if(WrtUtilDirExists(*file))
173         {
174             LogDebug("  -> " << *file);
175             if(!WrtUtilRemove(*file)){
176                 ThrowMsg(Exceptions::RemovingFolderFailure,
177                         "Failed to remove external directory");
178             }
179         }
180         else
181         {
182             LogWarning("  -> " << *file << "(no such a path)");
183         }
184     }
185 }
186
187 void TaskDatabase::StepAbortDBInsert()
188 {
189     LogWarning("[DB Update Task] Aborting... (DB Clean)");
190     Try
191     {
192         WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
193         LogDebug("Cleaning DB successful!");
194     }
195     Catch(DPL::DB::SqlConnection::Exception::Base)
196     {
197         LogError("Failed to handle StepAbortDBClean!");
198     }
199
200     ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
201     // Remove also old one. If it was already updated nothing wrong will happen,
202     // but if not old widget will be removed.
203     if (INVALID_WIDGET_HANDLE != m_handleToRemove)
204         ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
205 }
206
207 } //namespace WidgetInstall
208 } //namespace Jobs