[Release] wrt-installer_0.1.114
[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/log/log.h>
34 #include <dpl/foreach.h>
35 #include <dpl/utils/wrt_utility.h>
36
37 #include <widget_install/widget_install_context.h>
38 #include <widget_install/widget_install_errors.h>
39 #include <widget_install/job_widget_install.h>
40 #include <widget_install/directory_api.h>
41
42 #include <dpl/wrt-dao-ro/global_config.h>
43 #include <dpl/exception.h>
44 #include <dpl/errno_string.h>
45
46 using namespace WrtDB;
47
48 namespace {
49 inline const char* GetWidgetBackupDirPath()
50 {
51     return "backup";
52 }
53 }
54
55 namespace Jobs {
56 namespace WidgetInstall {
57 TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
58     DPL::TaskDecl<TaskUpdateFiles>(this),
59     m_context(context)
60 {
61     AddStep(&TaskUpdateFiles::StartStep);
62     AddStep(&TaskUpdateFiles::StepBackupDirectory);
63     AddStep(&TaskUpdateFiles::EndStep);
64
65     AddAbortStep(&TaskUpdateFiles::StepAbortBackupDirectory);
66 }
67
68 void TaskUpdateFiles::StepBackupDirectory()
69 {
70     LogDebug("StepCreateBackupFolder");
71
72     std::string backPath = m_context.locations->getBackupDir();
73     LogDebug("Backup resource directory path : " << backPath);
74     std::string pkgPath = m_context.locations->getPackageInstallationDir();
75
76     if (0 == access(backPath.c_str(), F_OK)) {
77         if (!WrtUtilRemove(backPath)) {
78             ThrowMsg(Exceptions::RemovingFolderFailure,
79                     "Error occurs during removing backup resource directory");
80         }
81     }
82     LogDebug("copy : " << pkgPath << " to " << backPath);
83     if ((rename(pkgPath.c_str(), backPath.c_str())) != 0) {
84         LogError("Failed to rename " << pkgPath << " to " << backPath);
85         ThrowMsg(Exceptions::BackupFailed,
86                 "Error occurs during rename file");
87     }
88
89     WrtUtilMakeDir(pkgPath);
90 }
91
92 void TaskUpdateFiles::StepAbortBackupDirectory()
93 {
94     LogDebug("StepAbortCopyFiles");
95
96     std::string backPath = m_context.locations->getBackupDir();
97     std::string pkgPath = m_context.locations->getPackageInstallationDir();
98
99     LogDebug("Backup Folder " << backPath << " to " << pkgPath);
100
101     if (!WrtUtilRemove(pkgPath)) {
102         LogError("Failed to remove " << pkgPath);
103     }
104
105     if (rename(backPath.c_str(), pkgPath.c_str()) != 0) {
106         LogError("Failed to rename " << backPath << " to " << pkgPath);
107     }
108 }
109
110 void TaskUpdateFiles::StartStep()
111 {
112     LogDebug("--------- <TaskUpdateFiles> : START ----------");
113 }
114
115 void TaskUpdateFiles::EndStep()
116 {
117     m_context.job->UpdateProgress(
118         InstallerContext::INSTALL_CREATE_BACKUP_DIR,
119         "Backup directory created for update");
120
121     LogDebug("--------- <TaskUpdateFiles> : END ----------");
122 }
123 } //namespace WidgetInstall
124 } //namespace Jobs