f23ea707d3bee835c9f9e3174d3d6a02567274ba
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_remove_backup.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_remove_backup.cpp
18  * @author  Soyoung kim(sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task backup files remove
21  */
22 #include <widget_install/task_remove_backup.h>
23
24 #include <sys/stat.h>
25 #include <string>
26 #include <sstream>
27 #include <dpl/wrt-dao-rw/widget_dao.h>
28 #include <dpl/wrt-dao-ro/global_config.h>
29 #include <dpl/foreach.h>
30 #include <dpl/log/log.h>
31 #include <dpl/assert.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <widget_install/job_widget_install.h>
34 #include <widget_install/widget_install_errors.h>
35 #include <widget_install/widget_install_context.h>
36
37 using namespace WrtDB;
38
39 namespace Jobs {
40 namespace WidgetInstall {
41 TaskRemoveBackupFiles::TaskRemoveBackupFiles(InstallerContext& context) :
42     DPL::TaskDecl<TaskRemoveBackupFiles>(this),
43     m_context(context)
44 {
45     AddStep(&TaskRemoveBackupFiles::StepDeleteDB);
46     AddStep(&TaskRemoveBackupFiles::StepRemoveBackupFiles);
47 }
48
49 void TaskRemoveBackupFiles::StepDeleteDB()
50 {
51     LogInfo("Delete old widget DB");
52     Try
53     {
54         // If there is existing model, remove its database data
55         if (true == m_context.existingWidgetInfo.isExist) {
56             WidgetHandle old = m_context.existingWidgetInfo.existingHandle;
57             LogInfo("Unregistering widget...: " << old);
58             WidgetDAO::unregisterWidget(old);
59             LogInfo("Widget unregistered");
60         }
61     }
62     Catch(DPL::DB::SqlConnection::Exception::Base)
63     {
64         LogError("Failed to clean up old widget DB!");
65         ReThrowMsg(Exceptions::RemoveBackupFailed, "Database failure!");
66     }
67
68     m_context.job->UpdateProgress(
69         InstallerContext::INSTALL_DELETE_OLD_DB,
70         "Backup widget db delete Finished");
71 }
72
73 void TaskRemoveBackupFiles::StepRemoveBackupFiles()
74 {
75     std::ostringstream backupDir;
76     backupDir << m_context.locations->getBackupDir();
77
78     if (WrtUtilRemove(backupDir.str())) {
79         LogDebug("Success to remove backup files : " << backupDir.str());
80     } else {
81         LogError("Failed to remove backup directory : " << backupDir.str());
82         ThrowMsg(Exceptions::RemoveBackupFailed,
83                  "Error occurs during removing existing folder");
84     }
85
86     std::string tmp = m_context.locations->getTemporaryPackageDir();
87     if (WrtUtilRemove(tmp)) {
88         LogDebug("Success to remove temp directory : " << tmp);
89     } else {
90         LogError("Failed to remove temp directory : " << tmp);
91         ThrowMsg(Exceptions::RemoveBackupFailed,
92                  "Error occurs during removing existing folder");
93     }
94
95     m_context.job->UpdateProgress(
96         InstallerContext::INSTALL_REMOVE_BACKUP_FILE,
97         "Backup widget file delete Finished");
98 }
99
100 } //namespace WidgetInstall
101 } //namespace Jobs