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");
72 std::ostringstream backDirPath;
74 std::string srcBuPath = m_context.locations->getBackupSourceDir();
75 LogDebug("backup resource directory path : " << srcBuPath);
76 if (!WrtUtilMakeDir(srcBuPath)) {
78 Exceptions::BackupFailed,
79 "Error occurs during create \
83 std::string binBuPath = m_context.locations->getBackupBinaryDir();
84 LogDebug("backup execution directory path : " << binBuPath);
85 if (!WrtUtilMakeDir(binBuPath)) {
87 Exceptions::BackupFailed,
88 "Error occurs during create backup \
92 m_context.job->UpdateProgress(
93 InstallerContext::INSTALL_CREATE_BACKUP_DIR,
94 "Backup directory created for update");
97 void TaskUpdateFiles::ReadDirList(std::string dirPath, ExistFileList &list,
100 DIR* pkgDir = opendir(dirPath.c_str());
102 LogError("Package directory " << dirPath << " doesn't exist");
103 ThrowMsg(Exceptions::InternalError,
104 "Error occurs during read \
108 struct dirent* dirent;
109 struct stat statInfo;
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");
118 if (S_ISDIR(statInfo.st_mode)) {
119 if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
124 ReadDirList(absFileName, list, subLen);
127 list.insert(absFileName.substr(subLen));
130 //closing the directory
131 if (-1 == TEMP_FAILURE_RETRY(closedir(pkgDir))) {
132 LogError("Failed to close dir: " << dirPath << " with error: "
133 << DPL::GetErrnoString());
137 void TaskUpdateFiles::StepResourceFilesBackup()
139 LogDebug("StepCopyFiles");
141 ExistFileList resList;
142 ExistFileList tempList;
144 std::string pkgSrc = m_context.locations->getSourceDir();
145 ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str()) + 1);
147 std::string tempSrc = m_context.locations->getTemporaryPackageDir();
148 ReadDirList(tempSrc, tempList, strlen(tempSrc.c_str()) + 1);
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);
156 if (res != resList.end()) {
157 std::string backupFile =
158 m_context.locations->getBackupSourceDir() +
162 if (stat(resFile.c_str(), &sInfo) != 0) {
163 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
166 if (S_ISDIR(sInfo.st_mode)) {
167 LogDebug(resFile << " is a directory. so create a folder : " <<
169 WrtUtilMakeDir(backupFile);
171 if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
173 "Failed to rename " << resFile << " to " <<
176 Exceptions::BackupFailed,
177 "Error occurs during \
181 LogDebug("backup : " << resFile << " to " << backupFile);
183 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
185 "Failed to rename " << newFile << " to " << resFile);
187 Exceptions::BackupFailed,
188 "Error occurs during \
191 LogDebug("copy : " << newFile << " to " << resFile);
195 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
196 LogError("Failed to rename " << newFile << " to " << resFile);
198 Exceptions::BackupFailed,
199 "Error occurs during \
202 LogDebug("only copy : " << newFile << " to " << resFile);
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);
213 Exceptions::BackupFailed,
214 "Error occurs during \
217 LogDebug("only backup : " << pkgFile << " to " << backFile);
221 m_context.job->UpdateProgress(
222 InstallerContext::INSTALL_BACKUP_RES_FILES,
223 "Backup resource file for update");
226 void TaskUpdateFiles::StepExecFileBackup()
228 std::string execFile = m_context.locations->getExecFile();
230 LogDebug(" source : " << execFile);
232 std::string tempSource = m_context.locations->getBackupExecFile();
233 LogDebug(" source : " << tempSource);
235 if ((rename(execFile.c_str(), tempSource.c_str())) != 0) {
236 LogError("Failed to rename " << execFile << " to " <<
238 ThrowMsg(Exceptions::BackupFailed,
239 "Error occurs during \
242 LogDebug("Backup : " << execFile << " to " << tempSource);
244 std::string clientPath = GlobalConfig::GetWrtClientExec();
246 LogInfo("link -s " << clientPath << " " << execFile);
247 symlink(clientPath.c_str(), execFile.c_str());
249 m_context.job->UpdateProgress(
250 InstallerContext::INSTALL_BACKUP_EXEC,
251 "Backup execution file for update");
254 void TaskUpdateFiles::StepAbortResourceFilesBackup()
256 LogDebug("StepAbortCopyFiles");
257 std::string srcPath = m_context.locations->getSourceDir();
258 std::string srcBuPath = m_context.locations->getBackupSourceDir();
260 LogDebug("Backup Folder " << srcBuPath << " to " << srcPath);
262 if (!WrtUtilRemove(srcPath)) {
263 LogError("Failed to remove " << srcPath);
266 if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
267 LogError("Failed to rename " << srcBuPath << " to " << srcPath);
271 void TaskUpdateFiles::StepAbortExecFileBackup()
273 LogDebug("StepAbortExecFileBackup");
274 std::string binPath = m_context.locations->getBinaryDir();
276 if (!WrtUtilRemove(binPath)) {
277 LogError("Failed to remove " << binPath);
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);
284 LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
287 void TaskUpdateFiles::StepAbortCreateBackupFolder()
289 LogDebug("StepAbortCreateBackupFolder");
290 std::ostringstream path;
291 path << m_context.locations->getBackupDir();
292 LogDebug("Remove backup directory : " << path.str());
294 if (!WrtUtilRemove(path.str())) {
295 LogError("Failed to remove " << path);
298 } //namespace WidgetInstall