2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_update_files.cpp
18 * @author Soyoung Kim (sy037.kim@samsung.com)
20 * @brief Implementation file for installer task update files
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>
37 #include <widget_install/widget_install_context.h>
38 #include <widget_install/widget_install_errors.h>
39 #include <widget_install/job_widget_install.h>
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/exception.h>
43 #include <dpl/errno_string.h>
45 using namespace WrtDB;
48 inline const char* GetWidgetBackupDirPath()
55 namespace WidgetInstall {
56 TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
57 DPL::TaskDecl<TaskUpdateFiles>(this),
60 AddStep(&TaskUpdateFiles::StepCreateBackupFolder);
61 AddStep(&TaskUpdateFiles::StepResourceFilesBackup);
62 AddStep(&TaskUpdateFiles::StepExecFileBackup);
64 AddAbortStep(&TaskUpdateFiles::StepAbortCreateBackupFolder);
65 AddAbortStep(&TaskUpdateFiles::StepAbortExecFileBackup);
66 AddAbortStep(&TaskUpdateFiles::StepAbortResourceFilesBackup);
69 void TaskUpdateFiles::StepCreateBackupFolder()
71 LogDebug("StepCreateBackupFolder");
73 std::string srcBuPath = m_context.locations->getBackupSourceDir();
74 LogDebug("backup resource directory path : " << srcBuPath);
75 if (!WrtUtilMakeDir(srcBuPath)) {
77 Exceptions::BackupFailed,
78 "Error occurs during create \
82 std::string binBuPath = m_context.locations->getBackupBinaryDir();
83 LogDebug("backup execution directory path : " << binBuPath);
84 if (!WrtUtilMakeDir(binBuPath)) {
86 Exceptions::BackupFailed,
87 "Error occurs during create backup \
91 m_context.job->UpdateProgress(
92 InstallerContext::INSTALL_CREATE_BACKUP_DIR,
93 "Backup directory created for update");
96 void TaskUpdateFiles::ReadDirList(std::string dirPath, ExistFileList &list,
99 DIR* pkgDir = opendir(dirPath.c_str());
101 LogError("Package directory " << dirPath << " doesn't exist");
102 ThrowMsg(Exceptions::InternalError,
103 "Error occurs during read \
107 struct stat statInfo;
108 struct dirent dirent;
109 struct dirent *result;
112 for (return_code = readdir_r(pkgDir, &dirent, &result);
113 result != NULL && return_code == 0;
114 return_code = readdir_r(pkgDir, &dirent, &result))
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");
122 if (S_ISDIR(statInfo.st_mode)) {
123 if (strcmp(dirent.d_name, ".") == 0 || strcmp(dirent.d_name,
128 ReadDirList(absFileName, list, subLen);
131 list.insert(absFileName.substr(subLen));
133 if (return_code != 0 || errno != 0) {
134 LogError("readdir_r() failed with " << DPL::GetErrnoString());
137 //closing the directory
138 if (-1 == TEMP_FAILURE_RETRY(closedir(pkgDir))) {
139 LogError("Failed to close dir: " << dirPath << " with error: "
140 << DPL::GetErrnoString());
144 void TaskUpdateFiles::StepResourceFilesBackup()
146 LogDebug("StepCopyFiles");
148 ExistFileList resList;
149 ExistFileList tempList;
151 std::string pkgSrc = m_context.locations->getSourceDir();
152 ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str()) + 1);
154 std::string tempSrc = m_context.locations->getTemporaryPackageDir();
155 ReadDirList(tempSrc, tempList, strlen(tempSrc.c_str()) + 1);
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);
163 if (res != resList.end()) {
164 std::string backupFile =
165 m_context.locations->getBackupSourceDir() +
169 if (stat(resFile.c_str(), &sInfo) != 0) {
170 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
173 if (S_ISDIR(sInfo.st_mode)) {
174 LogDebug(resFile << " is a directory. so create a folder : " <<
176 WrtUtilMakeDir(backupFile);
178 if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
180 "Failed to rename " << resFile << " to " <<
183 Exceptions::BackupFailed,
184 "Error occurs during \
188 LogDebug("backup : " << resFile << " to " << backupFile);
190 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
192 "Failed to rename " << newFile << " to " << resFile);
194 Exceptions::BackupFailed,
195 "Error occurs during \
198 LogDebug("copy : " << newFile << " to " << resFile);
202 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
203 LogError("Failed to rename " << newFile << " to " << resFile);
205 Exceptions::BackupFailed,
206 "Error occurs during \
209 LogDebug("only copy : " << newFile << " to " << resFile);
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);
220 Exceptions::BackupFailed,
221 "Error occurs during \
224 LogDebug("only backup : " << pkgFile << " to " << backFile);
228 m_context.job->UpdateProgress(
229 InstallerContext::INSTALL_BACKUP_RES_FILES,
230 "Backup resource file for update");
233 void TaskUpdateFiles::StepExecFileBackup()
235 std::string execFile = m_context.locations->getExecFile();
237 LogDebug(" source : " << execFile);
239 std::string tempSource = m_context.locations->getBackupExecFile();
240 LogDebug(" source : " << tempSource);
242 if ((rename(execFile.c_str(), tempSource.c_str())) != 0) {
243 LogError("Failed to rename " << execFile << " to " <<
245 ThrowMsg(Exceptions::BackupFailed,
246 "Error occurs during \
249 LogDebug("Backup : " << execFile << " to " << tempSource);
251 std::string clientPath = GlobalConfig::GetWrtClientExec();
253 LogInfo("link -s " << clientPath << " " << execFile);
256 if( symlink(clientPath.c_str(), execFile.c_str()) != 0 )
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");
265 m_context.job->UpdateProgress(
266 InstallerContext::INSTALL_BACKUP_EXEC,
267 "Backup execution file for update");
270 void TaskUpdateFiles::StepAbortResourceFilesBackup()
272 LogDebug("StepAbortCopyFiles");
273 std::string srcPath = m_context.locations->getSourceDir();
274 std::string srcBuPath = m_context.locations->getBackupSourceDir();
276 LogDebug("Backup Folder " << srcBuPath << " to " << srcPath);
278 if (!WrtUtilRemove(srcPath)) {
279 LogError("Failed to remove " << srcPath);
282 if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
283 LogError("Failed to rename " << srcBuPath << " to " << srcPath);
287 void TaskUpdateFiles::StepAbortExecFileBackup()
289 LogDebug("StepAbortExecFileBackup");
290 std::string binPath = m_context.locations->getBinaryDir();
292 if (!WrtUtilRemove(binPath)) {
293 LogError("Failed to remove " << binPath);
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);
300 LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
303 void TaskUpdateFiles::StepAbortCreateBackupFolder()
305 LogDebug("StepAbortCreateBackupFolder");
306 std::ostringstream path;
307 path << m_context.locations->getBackupDir();
308 LogDebug("Remove backup directory : " << path.str());
310 if (!WrtUtilRemove(path.str())) {
311 LogError("Failed to remove " << path);
314 } //namespace WidgetInstall