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
30 #include <widget_install/task_update_files.h>
31 #include <dpl/assert.h>
32 #include <dpl/log/log.h>
33 #include <dpl/foreach.h>
34 #include <dpl/utils/wrt_utility.h>
36 #include <widget_install/widget_install_context.h>
37 #include <widget_install/widget_install_errors.h>
38 #include <widget_install/job_widget_install.h>
40 #include <dpl/wrt-dao-ro/global_config.h>
41 #include <dpl/exception.h>
42 #include <dpl/errno_string.h>
44 using namespace WrtDB;
48 inline const char* GetWidgetBackupDirPath()
55 namespace WidgetInstall {
57 TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
58 DPL::TaskDecl<TaskUpdateFiles>(this),
61 AddStep(&TaskUpdateFiles::StepCreateBackupFolder);
62 AddStep(&TaskUpdateFiles::StepResourceFilesBackup);
63 AddStep(&TaskUpdateFiles::StepExecFileBackup);
65 AddAbortStep(&TaskUpdateFiles::StepAbortCreateBackupFolder);
66 AddAbortStep(&TaskUpdateFiles::StepAbortExecFileBackup);
67 AddAbortStep(&TaskUpdateFiles::StepAbortResourceFilesBackup);
70 void TaskUpdateFiles::StepCreateBackupFolder()
72 LogDebug("StepCreateBackupFolder");
73 std::ostringstream backDirPath;
75 std::string srcBuPath = m_context.locations->getBackupSourceDir();
76 LogDebug("backup resource directory path : " << srcBuPath);
77 if(!WrtUtilMakeDir(srcBuPath)) {
78 ThrowMsg(Exceptions::BackupFailed, "Error occurs during create \
82 std::string binBuPath = m_context.locations->getBackupBinaryDir();
83 LogDebug("backup execution directory path : " << binBuPath);
84 if(!WrtUtilMakeDir(binBuPath)) {
85 ThrowMsg(Exceptions::BackupFailed, "Error occurs during create backup \
89 m_context.job->UpdateProgress(
90 InstallerContext::INSTALL_CREATE_BACKUP_DIR,
91 "Backup directory created for update");
94 void TaskUpdateFiles::ReadDirList(std::string dirPath, ExistFileList &list,
97 DIR* pkgDir = opendir(dirPath.c_str());
99 LogError("Package directory " << dirPath << " doesn't exist");
100 ThrowMsg(Exceptions::InternalError, "Error occurs during read \
104 struct dirent* dirent;
105 struct stat statInfo;
107 if ((dirent = readdir(pkgDir))) {
108 std::string dirName = dirent->d_name;
109 std::string absFileName = dirPath + "/" + dirName;
110 if (stat(absFileName.c_str(), &statInfo) != 0) {
111 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
114 if (S_ISDIR(statInfo.st_mode)) {
115 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
119 ReadDirList(absFileName, list, subLen);
122 list.insert(absFileName.substr(subLen));
126 //closing the directory
127 if (-1 == TEMP_FAILURE_RETRY(closedir(pkgDir))) {
128 LogError("Failed to close dir: " << dirPath << " with error: "
129 << DPL::GetErrnoString());
133 void TaskUpdateFiles::StepResourceFilesBackup()
135 LogDebug("StepCopyFiles");
137 ExistFileList resList;
138 ExistFileList tempList;
140 std::string pkgSrc = m_context.locations->getSourceDir();
141 ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str())+1);
144 std::string tempSrc = m_context.locations->getTemporaryPackageDir();
145 ReadDirList(tempSrc, tempList, strlen(tempSrc.c_str())+1);
147 FOREACH(it, tempList) {
148 std::set<std::string>::iterator res;
149 res = resList.find(*it);
150 std::string resFile = pkgSrc + "/" + (*it);
151 std::string newFile = tempSrc + "/" +(*it);
153 if (res != resList.end()) {
154 std::string backupFile = m_context.locations->getBackupSourceDir() +
158 if (stat(resFile.c_str(), &sInfo) != 0) {
159 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
162 if (S_ISDIR(sInfo.st_mode)) {
163 LogDebug(resFile << " is a directory. so create a folder : " <<
165 WrtUtilMakeDir(backupFile);
167 if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
168 LogError("Failed to rename " << resFile << " to " << backupFile);
169 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
173 LogDebug("backup : " << resFile << " to " << backupFile);
175 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
176 LogError("Failed to rename " << newFile << " to " << resFile);
177 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
180 LogDebug("copy : " << newFile << " to " << resFile);
184 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
185 LogError("Failed to rename " << newFile << " to " << resFile);
186 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
189 LogDebug("only copy : " << newFile << " to " << resFile);
193 if (resList.empty() != 0) {
194 FOREACH(remain, resList) {
195 std::string pkgFile = pkgSrc + "/" + (*remain);
196 std::string backFile = tempSrc + "/" + (*remain);
197 if ((rename(pkgFile.c_str(), backFile.c_str())) != 0) {
198 LogError("Failed to backup : " << pkgFile << " to " << backFile);
199 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
202 LogDebug("only backup : " << pkgFile << " to " << backFile);
206 m_context.job->UpdateProgress(
207 InstallerContext::INSTALL_BACKUP_RES_FILES,
208 "Backup resource file for update");
211 void TaskUpdateFiles::StepExecFileBackup()
213 std::string execFile = m_context.locations->getExecFile();
215 LogDebug(" source : " << execFile);
217 std::string tempSource= m_context.locations->getBackupExecFile();
218 LogDebug(" source : " << tempSource);
220 if ((rename(execFile.c_str(), tempSource.c_str())) != 0) {
221 LogError("Failed to rename " << execFile << " to " <<
223 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
226 LogDebug("Backup : " << execFile << " to " << tempSource);
228 std::string clientPath = GlobalConfig::GetWrtClientExec();
230 LogInfo("link -s " << clientPath << " " << execFile);
231 symlink(clientPath.c_str(), execFile.c_str());
233 m_context.job->UpdateProgress(
234 InstallerContext::INSTALL_BACKUP_EXEC,
235 "Backup execution file for update");
238 void TaskUpdateFiles::StepAbortResourceFilesBackup()
240 LogDebug("StepAbortCopyFiles");
241 std::string srcPath = m_context.locations->getSourceDir();
242 std::string srcBuPath = m_context.locations->getBackupSourceDir();
244 LogDebug("Backup Folder " << srcBuPath << " to " << srcPath);
246 if(!WrtUtilRemove(srcPath)) {
247 LogError("Failed to remove " << srcPath);
250 if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
251 LogError("Failed to rename " << srcBuPath << " to " << srcPath);
255 void TaskUpdateFiles::StepAbortExecFileBackup()
257 LogDebug("StepAbortExecFileBackup");
258 std::string binPath = m_context.locations->getBinaryDir();
260 if(!WrtUtilRemove(binPath)) {
261 LogError("Failed to remove " << binPath);
264 std::string binBuPath = m_context.locations->getBackupBinaryDir();
265 if (rename(binBuPath.c_str(), binPath.c_str()) != 0) {
266 LogError("Failed to rename " << binBuPath << " to " << binPath);
268 LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
271 void TaskUpdateFiles::StepAbortCreateBackupFolder()
273 LogDebug("StepAbortCreateBackupFolder");
274 std::ostringstream path;
275 path << m_context.locations->getBackupDir();
276 LogDebug("Remove backup directory : " << path.str());
278 if(!WrtUtilRemove(path.str())) {
279 LogError("Failed to remove " << path);
283 } //namespace WidgetInstall