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