Rename invalid WRT_SMACK_LABEL macro to WRT_SMACK_ENABLED
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / task_update_files.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_update_files.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task update files
21  */
22
23 #include <unistd.h>
24 #include <utility>
25 #include <vector>
26 #include <string>
27 #include <algorithm>
28 #include <sys/stat.h>
29 #include <dirent.h>
30
31 #include <widget_install/task_update_files.h>
32 #include <dpl/assert.h>
33 #include <dpl/foreach.h>
34 #include <dpl/utils/wrt_utility.h>
35
36 #include <widget_install/widget_install_context.h>
37 #include <widget_install/widget_install_errors.h>
38 #include <widget_install/job_widget_install.h>
39 #include <widget_install/directory_api.h>
40
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/exception.h>
43 #include <dpl/errno_string.h>
44
45 #include <installer_log.h>
46
47 using namespace WrtDB;
48
49 namespace {
50 inline const char* GetWidgetBackupDirPath()
51 {
52     return "backup";
53 }
54 }
55
56 namespace Jobs {
57 namespace WidgetInstall {
58 TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
59     DPL::TaskDecl<TaskUpdateFiles>(this),
60     m_context(context)
61 {
62     AddStep(&TaskUpdateFiles::StartStep);
63     AddStep(&TaskUpdateFiles::StepBackupDirectory);
64     AddStep(&TaskUpdateFiles::EndStep);
65
66     AddAbortStep(&TaskUpdateFiles::StepAbortBackupDirectory);
67 }
68
69 void TaskUpdateFiles::StepBackupDirectory()
70 {
71     _D("StepCreateBackupFolder");
72
73     std::string backPath = m_context.locations->getBackupDir();
74     _D("Backup resource directory path : %s", backPath.c_str());
75     std::string pkgPath = m_context.locations->getPackageInstallationDir();
76
77     if (0 == access(backPath.c_str(), F_OK)) {
78         if (!WrtUtilRemove(backPath)) {
79             ThrowMsg(Exceptions::RemovingFolderFailure,
80                     "Error occurs during removing backup resource directory");
81         }
82     }
83     _D("copy : %s to %s", pkgPath.c_str(), backPath.c_str());
84     if ((rename(pkgPath.c_str(), backPath.c_str())) != 0) {
85         _E("Failed to rename %s to %s", pkgPath.c_str(), backPath.c_str());
86         ThrowMsg(Exceptions::BackupFailed,
87                 "Error occurs during rename file");
88     }
89
90     WrtUtilMakeDir(pkgPath);
91 }
92
93 void TaskUpdateFiles::StepAbortBackupDirectory()
94 {
95     _D("StepAbortCopyFiles");
96
97     std::string backPath = m_context.locations->getBackupDir();
98     std::string pkgPath = m_context.locations->getPackageInstallationDir();
99
100     _D("Backup Folder %s to %s", backPath.c_str(), pkgPath.c_str());
101
102     if (!WrtUtilRemove(pkgPath)) {
103         _E("Failed to remove %s", pkgPath.c_str());
104     }
105
106     if (rename(backPath.c_str(), pkgPath.c_str()) != 0) {
107         _E("Failed to rename %s to %s", backPath.c_str(), pkgPath.c_str());
108     }
109 }
110
111 void TaskUpdateFiles::StartStep()
112 {
113     _D("--------- <TaskUpdateFiles> : START ----------");
114 }
115
116 void TaskUpdateFiles::EndStep()
117 {
118     m_context.job->UpdateProgress(
119         InstallerContext::INSTALL_CREATE_BACKUP_DIR,
120         "Backup directory created for update");
121
122     _D("--------- <TaskUpdateFiles> : END ----------");
123 }
124 } //namespace WidgetInstall
125 } //namespace Jobs