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
29 #include <widget_install/task_update_files.h>
30 #include <dpl/assert.h>
31 #include <dpl/log/log.h>
32 #include <dpl/foreach.h>
34 #include <widget_install/widget_install_context.h>
35 #include <widget_install/widget_install_errors.h>
36 #include <widget_install/job_widget_install.h>
38 #include <dpl/wrt-dao-rw/widget_dao.h>
39 #include <dpl/wrt-dao-ro/global_config.h>
40 #include <dpl/utils/wrt_utility.h>
41 #include <dpl/exception.h>
43 using namespace WrtDB;
47 inline const char* GetWidgetBackupDirPath()
54 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 backDirPath << m_context.locations->getBackupDir();
75 m_srcBuPath = backDirPath.str() + GlobalConfig::GetWidgetSrcPath();
76 LogDebug("backup resource directory path : " << m_srcBuPath);
77 if(!_WrtMakeDir(m_srcBuPath.c_str(), 0755, WRT_FILEUTILS_RECUR)) {
78 ThrowMsg(Exceptions::BackupFailed, "Error occurs during create \
82 m_binBuPath = backDirPath.str() + GlobalConfig::GetUserWidgetExecPath();
83 LogDebug("backup execution directory path : " << m_binBuPath);
84 if(!_WrtMakeDir(m_binBuPath.c_str(), 0755, WRT_FILEUTILS_RECUR)) {
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 LogDebug("Package directory 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));
128 void TaskUpdateFiles::StepResourceFilesBackup()
130 LogDebug("StepCopyFiles");
132 ExistFileList resList;
133 ExistFileList tempList;
135 std::string pkgSrc = m_pkgPath + "/" + GlobalConfig::GetWidgetSrcPath();
136 ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str())+1);
139 std::string tempSrc = m_context.locations->getTemporaryPackageDir();
140 ReadDirList(tempSrc, tempList, strlen(tempSrc.c_str())+1);
142 FOREACH(it, tempList) {
143 std::set<std::string>::iterator res;
144 res = resList.find(*it);
145 std::string resFile = pkgSrc + "/" + (*it);
146 std::string newFile = tempSrc + "/" +(*it);
148 if (res != resList.end()) {
149 std::string backupFile = m_srcBuPath + "/"+ (*it);
152 if (stat(resFile.c_str(), &sInfo) != 0) {
153 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
156 if (S_ISDIR(sInfo.st_mode)) {
157 LogDebug(resFile << " is a directory. so create a folder : " <<
159 _WrtMakeDir(backupFile.c_str(), 0755, WRT_FILEUTILS_RECUR);
161 if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
162 LogError("Failed to rename " << resFile << " to " << backupFile);
163 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
167 LogDebug("backup : " << resFile << " to " << backupFile);
169 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
170 LogError("Failed to rename " << newFile << " to " << resFile);
171 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
174 LogDebug("copy : " << newFile << " to " << resFile);
178 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
179 LogError("Failed to rename " << newFile << " to " << resFile);
180 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
183 LogDebug("only copy : " << newFile << " to " << resFile);
187 if (resList.empty() != 0) {
188 FOREACH(remain, resList) {
189 std::string pkgFile = pkgSrc + "/" + (*remain);
190 std::string backFile = tempSrc + "/" + (*remain);
191 if ((rename(pkgFile.c_str(), backFile.c_str())) != 0) {
192 LogError("Failed to backup : " << pkgFile << " to " << backFile);
193 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
196 LogDebug("only backup : " << pkgFile << " to " << backFile);
200 m_context.job->UpdateProgress(
201 InstallerContext::INSTALL_BACKUP_RES_FILES,
202 "Backup resource file for update");
205 void TaskUpdateFiles::StepExecFileBackup()
207 std::ostringstream source;
208 source << m_pkgPath << "/" << GlobalConfig::GetUserWidgetExecPath();
209 source << "/" << m_context.existingWidgetInfo.existingHandle;
211 LogDebug(" source : " << source.str());
213 std::ostringstream tempSource;
214 tempSource << m_binBuPath << "/" <<
215 m_context.existingWidgetInfo.existingHandle;
216 LogDebug(" source : " << tempSource.str());
218 if ((rename(source.str().c_str(), tempSource.str().c_str())) != 0) {
219 LogError("Failed to rename " << source.str() << " to " <<
221 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
224 LogDebug("Backup : " << source.str() << " to " << tempSource.str());
226 std::ostringstream newClient;
227 newClient << m_pkgPath << "/" << GlobalConfig::GetUserWidgetExecPath();
228 newClient << "/" << m_context.widgetHandle;
230 std::string clientPath = GlobalConfig::GetWrtClientExec();
232 LogInfo("link -s " << clientPath << " " << newClient.str());
233 symlink(clientPath.c_str(), newClient.str().c_str());
235 m_context.job->UpdateProgress(
236 InstallerContext::INSTALL_BACKUP_EXEC,
237 "Backup execution file for update");
240 void TaskUpdateFiles::StepAbortResourceFilesBackup()
242 LogDebug("StepAbortCopyFiles");
243 std::string srcPath = m_pkgPath + "/" + GlobalConfig::GetWidgetSrcPath();
245 LogDebug("Backup Folder " << m_srcBuPath << " to " << srcPath);
247 if(!_WrtUtilRemoveDir(srcPath.c_str())) {
248 LogError("Failed to remove " << srcPath);
251 if (rename(m_srcBuPath.c_str(), srcPath.c_str()) != 0) {
252 LogError("Failed to rename " << m_srcBuPath << " to " << srcPath);
256 void TaskUpdateFiles::StepAbortExecFileBackup()
258 LogDebug("StepAbortExecFileBackup");
259 std::string binPath = m_pkgPath + "/" +
260 GlobalConfig::GetUserWidgetExecPath();
262 if(!_WrtUtilRemoveDir(binPath.c_str())) {
263 LogError("Failed to remove " << binPath);
266 if (rename(m_binBuPath.c_str(), binPath.c_str()) != 0) {
267 LogError("Failed to rename " << m_binBuPath << " to " << binPath);
269 LogDebug("Backup Folder " << m_binBuPath << "move to " << binPath);
272 void TaskUpdateFiles::StepAbortCreateBackupFolder()
274 LogDebug("StepAbortCreateBackupFolder");
275 std::ostringstream path;
276 path << m_context.locations->getBackupDir();
277 LogDebug("Remove backup directory : " << path.str());
279 if(!_WrtUtilRemoveDir(path.str().c_str())) {
280 LogError("Failed to remove " << path);
283 } //namespace WidgetInstall