bcbd2f33ad2aca9c504f28f101794987c031ee8d
[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
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/exception.h>
43 #include <dpl/errno_string.h>
44
45 using namespace WrtDB;
46
47 namespace {
48 inline const char* GetWidgetBackupDirPath()
49 {
50     return "backup";
51 }
52 }
53
54 namespace Jobs {
55 namespace WidgetInstall {
56 TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
57     DPL::TaskDecl<TaskUpdateFiles>(this),
58     m_context(context)
59 {
60     AddStep(&TaskUpdateFiles::StepCreateBackupFolder);
61     AddStep(&TaskUpdateFiles::StepResourceFilesBackup);
62     AddStep(&TaskUpdateFiles::StepExecFileBackup);
63
64     AddAbortStep(&TaskUpdateFiles::StepAbortCreateBackupFolder);
65     AddAbortStep(&TaskUpdateFiles::StepAbortExecFileBackup);
66     AddAbortStep(&TaskUpdateFiles::StepAbortResourceFilesBackup);
67 }
68
69 void TaskUpdateFiles::StepCreateBackupFolder()
70 {
71     LogDebug("StepCreateBackupFolder");
72     std::ostringstream backDirPath;
73
74     std::string srcBuPath = m_context.locations->getBackupSourceDir();
75     LogDebug("backup resource directory path : " << srcBuPath);
76     if (!WrtUtilMakeDir(srcBuPath)) {
77         ThrowMsg(
78             Exceptions::BackupFailed,
79             "Error occurs during create \
80                 backup directory.");
81     }
82
83     std::string binBuPath = m_context.locations->getBackupBinaryDir();
84     LogDebug("backup execution directory path : " << binBuPath);
85     if (!WrtUtilMakeDir(binBuPath)) {
86         ThrowMsg(
87             Exceptions::BackupFailed,
88             "Error occurs during create backup \
89                 directory.");
90     }
91
92     m_context.job->UpdateProgress(
93         InstallerContext::INSTALL_CREATE_BACKUP_DIR,
94         "Backup directory created for update");
95 }
96
97 void TaskUpdateFiles::ReadDirList(std::string dirPath, ExistFileList &list,
98                                   size_t subLen)
99 {
100     DIR* pkgDir = opendir(dirPath.c_str());
101     if (!pkgDir) {
102         LogError("Package directory " << dirPath << " doesn't exist");
103         ThrowMsg(Exceptions::InternalError,
104                  "Error occurs during read \
105                 directory");
106     }
107
108     struct dirent* dirent;
109     struct stat statInfo;
110     do {
111         if ((dirent = readdir(pkgDir))) {
112             std::string dirName = dirent->d_name;
113             std::string absFileName = dirPath + "/" + dirName;
114             if (stat(absFileName.c_str(), &statInfo) != 0) {
115                 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
116             }
117
118             if (S_ISDIR(statInfo.st_mode)) {
119                 if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
120                                                                "..") == 0)
121                 {
122                     continue;
123                 }
124                 ReadDirList(absFileName, list, subLen);
125             }
126
127             list.insert(absFileName.substr(subLen));
128         }
129     } while (dirent);
130     //closing the directory
131     if (-1 == TEMP_FAILURE_RETRY(closedir(pkgDir))) {
132         LogError("Failed to close dir: " << dirPath << " with error: "
133                                          << DPL::GetErrnoString());
134     }
135 }
136
137 void TaskUpdateFiles::StepResourceFilesBackup()
138 {
139     LogDebug("StepCopyFiles");
140
141     ExistFileList resList;
142     ExistFileList tempList;
143
144     std::string pkgSrc = m_context.locations->getSourceDir();
145     ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str()) + 1);
146
147     std::string tempSrc = m_context.locations->getTemporaryPackageDir();
148     ReadDirList(tempSrc, tempList, strlen(tempSrc.c_str()) + 1);
149
150     FOREACH(it, tempList) {
151         std::set<std::string>::iterator res;
152         res = resList.find(*it);
153         std::string resFile = pkgSrc + "/" + (*it);
154         std::string newFile = tempSrc + "/" + (*it);
155
156         if (res != resList.end()) {
157             std::string backupFile =
158                 m_context.locations->getBackupSourceDir() +
159                 "/" + (*it);
160
161             struct stat sInfo;
162             if (stat(resFile.c_str(), &sInfo) != 0) {
163                 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
164             }
165
166             if (S_ISDIR(sInfo.st_mode)) {
167                 LogDebug(resFile << " is a directory. so create a folder : " <<
168                          backupFile);
169                 WrtUtilMakeDir(backupFile);
170             } else {
171                 if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
172                     LogError(
173                         "Failed to rename " << resFile << " to " <<
174                         backupFile);
175                     ThrowMsg(
176                         Exceptions::BackupFailed,
177                         "Error occurs during \
178                             rename file");
179                 }
180
181                 LogDebug("backup : " << resFile << " to " << backupFile);
182
183                 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
184                     LogError(
185                         "Failed to rename " << newFile << " to " << resFile);
186                     ThrowMsg(
187                         Exceptions::BackupFailed,
188                         "Error occurs during \
189                             rename file");
190                 }
191                 LogDebug("copy : " << newFile << " to " << resFile);
192             }
193             resList.erase(res);
194         } else {
195             if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
196                 LogError("Failed to rename " << newFile << " to " << resFile);
197                 ThrowMsg(
198                     Exceptions::BackupFailed,
199                     "Error occurs during \
200                         rename file");
201             }
202             LogDebug("only copy : " << newFile << " to " << resFile);
203         }
204     }
205
206     if (resList.empty() != 0) {
207         FOREACH(remain, resList) {
208             std::string pkgFile = pkgSrc + "/" + (*remain);
209             std::string backFile = tempSrc + "/" + (*remain);
210             if ((rename(pkgFile.c_str(), backFile.c_str())) != 0) {
211                 LogError("Failed to backup : " << pkgFile << " to " << backFile);
212                 ThrowMsg(
213                     Exceptions::BackupFailed,
214                     "Error occurs during \
215                         rename file");
216             }
217             LogDebug("only backup : " << pkgFile << " to " << backFile);
218         }
219     }
220
221     m_context.job->UpdateProgress(
222         InstallerContext::INSTALL_BACKUP_RES_FILES,
223         "Backup resource file for update");
224 }
225
226 void TaskUpdateFiles::StepExecFileBackup()
227 {
228     std::string execFile = m_context.locations->getExecFile();
229
230     LogDebug(" source : " << execFile);
231
232     std::string tempSource = m_context.locations->getBackupExecFile();
233     LogDebug(" source : " << tempSource);
234
235     if ((rename(execFile.c_str(), tempSource.c_str())) != 0) {
236         LogError("Failed to rename " << execFile << " to " <<
237                  tempSource);
238         ThrowMsg(Exceptions::BackupFailed,
239                  "Error occurs during \
240                 rename file");
241     }
242     LogDebug("Backup : " << execFile << " to " << tempSource);
243
244     std::string clientPath = GlobalConfig::GetWrtClientExec();
245
246     LogInfo("link -s " << clientPath << " " << execFile);
247     symlink(clientPath.c_str(), execFile.c_str());
248
249     m_context.job->UpdateProgress(
250         InstallerContext::INSTALL_BACKUP_EXEC,
251         "Backup execution file for update");
252 }
253
254 void TaskUpdateFiles::StepAbortResourceFilesBackup()
255 {
256     LogDebug("StepAbortCopyFiles");
257     std::string srcPath = m_context.locations->getSourceDir();
258     std::string srcBuPath = m_context.locations->getBackupSourceDir();
259
260     LogDebug("Backup Folder " << srcBuPath << " to " << srcPath);
261
262     if (!WrtUtilRemove(srcPath)) {
263         LogError("Failed to remove " << srcPath);
264     }
265
266     if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
267         LogError("Failed to rename " << srcBuPath << " to " << srcPath);
268     }
269 }
270
271 void TaskUpdateFiles::StepAbortExecFileBackup()
272 {
273     LogDebug("StepAbortExecFileBackup");
274     std::string binPath = m_context.locations->getBinaryDir();
275
276     if (!WrtUtilRemove(binPath)) {
277         LogError("Failed to remove " << binPath);
278     }
279
280     std::string binBuPath = m_context.locations->getBackupBinaryDir();
281     if (rename(binBuPath.c_str(), binPath.c_str()) != 0) {
282         LogError("Failed to rename " << binBuPath << " to " << binPath);
283     }
284     LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
285 }
286
287 void TaskUpdateFiles::StepAbortCreateBackupFolder()
288 {
289     LogDebug("StepAbortCreateBackupFolder");
290     std::ostringstream path;
291     path << m_context.locations->getBackupDir();
292     LogDebug("Remove backup directory : " << path.str());
293
294     if (!WrtUtilRemove(path.str())) {
295         LogError("Failed to remove " << path);
296     }
297 }
298 } //namespace WidgetInstall
299 } //namespace Jobs