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