Rename invalid WRT_SMACK_LABEL macro to WRT_SMACK_ENABLED
[platform/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-ro/global_config.h>
28 #include <dpl/foreach.h>
29 #include <dpl/assert.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <dpl/wrt-dao-rw/widget_dao.h>
32 #include <widget_install/job_widget_install.h>
33 #include <widget_install/widget_install_errors.h>
34 #include <widget_install/widget_install_context.h>
35 #include <wrt-commons/widget-interface-dao/widget_interface_dao.h>
36 #include <installer_log.h>
37
38 using namespace WrtDB;
39
40 namespace Jobs {
41 namespace WidgetInstall {
42 TaskRemoveBackupFiles::TaskRemoveBackupFiles(InstallerContext& context) :
43     DPL::TaskDecl<TaskRemoveBackupFiles>(this),
44     m_context(context)
45 {
46     AddStep(&TaskRemoveBackupFiles::StartStep);
47     if (m_context.mode.extension != InstallMode::ExtensionType::DIR)
48     {
49         AddStep(&TaskRemoveBackupFiles::StepRemoveBackupFiles);
50     }
51     AddStep(&TaskRemoveBackupFiles::StepDeleteBackupDB);
52     AddStep(&TaskRemoveBackupFiles::StepDeleteBackupWidgetInterfaceDB);
53     AddStep(&TaskRemoveBackupFiles::EndStep);
54 }
55
56 void TaskRemoveBackupFiles::StepRemoveBackupFiles()
57 {
58     std::ostringstream backupDir;
59     backupDir << m_context.locations->getBackupDir();
60
61     if (WrtUtilRemove(backupDir.str())) {
62         _D("Success to remove backup files : %s", backupDir.str().c_str());
63     } else {
64         _E("Failed to remove backup directory : %s", backupDir.str().c_str());
65         ThrowMsg(Exceptions::RemoveBackupFailed,
66                  "Error occurs during removing existing folder");
67     }
68
69     std::string tmp = m_context.locations->getTemporaryPackageDir();
70     if (WrtUtilRemove(tmp)) {
71         _D("Success to remove temp directory : %s", tmp.c_str());
72     } else {
73         _E("Failed to remove temp directory : %s", tmp.c_str());
74     }
75 }
76
77 void TaskRemoveBackupFiles::StepDeleteBackupDB()
78 {
79     _D("StepDeleteBackupDB");
80     std::string oldAppid =
81         DPL::ToUTF8String(m_context.widgetConfig.tzAppid) + ".backup";
82
83     Try
84     {
85         WidgetDAO::unregisterWidget(DPL::FromUTF8String(oldAppid));
86     }
87     Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
88     {
89         _E("Fail to delete old version db information");
90     }
91 }
92
93 void TaskRemoveBackupFiles::StepDeleteBackupWidgetInterfaceDB()
94 {
95     _D("StepDeleteBackupWidgetInterfaceDB");
96     using namespace WidgetInterfaceDB;
97     using namespace WrtDB;
98
99     DbWidgetHandle handle =
100         WidgetDAOReadOnly::getHandle(m_context.widgetConfig.tzAppid);
101     std::string backupDbPath = WidgetInterfaceDAO::databaseFileName(handle);
102     backupDbPath += GlobalConfig::GetBackupDatabaseSuffix();
103
104     // remove backup database
105     if (remove(backupDbPath.c_str()) != 0) {
106         _W("Fail to remove");
107     }
108 }
109
110 void TaskRemoveBackupFiles::StartStep()
111 {
112     _D("--------- <TaskRemoveBackupFiles> : START ----------");
113 }
114
115 void TaskRemoveBackupFiles::EndStep()
116 {
117     _D("--------- <TaskRemoveBackupFiles> : END ----------");
118 }
119 } //namespace WidgetInstall
120 } //namespace Jobs