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-rw/widget_dao.h>
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/utils/wrt_utility.h>
43 #include <dpl/exception.h>
45 using namespace WrtDB;
49 inline const char* GetWidgetBackupDirPath()
56 namespace WidgetInstall {
58 TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
59 DPL::TaskDecl<TaskUpdateFiles>(this),
62 AddStep(&TaskUpdateFiles::StepCreateBackupFolder);
63 AddStep(&TaskUpdateFiles::StepResourceFilesBackup);
64 AddStep(&TaskUpdateFiles::StepExecFileBackup);
65 AddStep(&TaskUpdateFiles::StepUpdateExternalFiles);
67 AddAbortStep(&TaskUpdateFiles::StepAbortCreateBackupFolder);
68 AddAbortStep(&TaskUpdateFiles::StepAbortExecFileBackup);
69 AddAbortStep(&TaskUpdateFiles::StepAbortResourceFilesBackup);
72 void TaskUpdateFiles::StepCreateBackupFolder()
74 LogDebug("StepCreateBackupFolder");
75 std::ostringstream backDirPath;
77 std::string srcBuPath = m_context.locations->getBackupSourceDir();
78 LogDebug("backup resource directory path : " << srcBuPath);
79 if(!WrtUtilMakeDir(srcBuPath)) {
80 ThrowMsg(Exceptions::BackupFailed, "Error occurs during create \
84 std::string binBuPath = m_context.locations->getBackupBinaryDir();
85 LogDebug("backup execution directory path : " << binBuPath);
86 if(!WrtUtilMakeDir(binBuPath)) {
87 ThrowMsg(Exceptions::BackupFailed, "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, "Error occurs during read \
106 struct dirent* dirent;
107 struct stat statInfo;
109 if ((dirent = readdir(pkgDir))) {
110 std::string dirName = dirent->d_name;
111 std::string absFileName = dirPath + "/" + dirName;
112 if (stat(absFileName.c_str(), &statInfo) != 0) {
113 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
116 if (S_ISDIR(statInfo.st_mode)) {
117 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
121 ReadDirList(absFileName, list, subLen);
124 list.insert(absFileName.substr(subLen));
130 void TaskUpdateFiles::StepResourceFilesBackup()
132 LogDebug("StepCopyFiles");
134 ExistFileList resList;
135 ExistFileList tempList;
137 std::string pkgSrc = m_context.locations->getSourceDir();
138 ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str())+1);
141 std::string tempSrc = m_context.locations->getTemporaryPackageDir();
142 ReadDirList(tempSrc, tempList, strlen(tempSrc.c_str())+1);
144 FOREACH(it, tempList) {
145 std::set<std::string>::iterator res;
146 res = resList.find(*it);
147 std::string resFile = pkgSrc + "/" + (*it);
148 std::string newFile = tempSrc + "/" +(*it);
150 if (res != resList.end()) {
151 std::string backupFile = m_context.locations->getBackupSourceDir() +
155 if (stat(resFile.c_str(), &sInfo) != 0) {
156 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
159 if (S_ISDIR(sInfo.st_mode)) {
160 LogDebug(resFile << " is a directory. so create a folder : " <<
162 WrtUtilMakeDir(backupFile);
164 if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
165 LogError("Failed to rename " << resFile << " to " << backupFile);
166 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
170 LogDebug("backup : " << resFile << " to " << backupFile);
172 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
173 LogError("Failed to rename " << newFile << " to " << resFile);
174 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
177 LogDebug("copy : " << newFile << " to " << resFile);
181 if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
182 LogError("Failed to rename " << newFile << " to " << resFile);
183 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
186 LogDebug("only copy : " << newFile << " to " << resFile);
190 if (resList.empty() != 0) {
191 FOREACH(remain, resList) {
192 std::string pkgFile = pkgSrc + "/" + (*remain);
193 std::string backFile = tempSrc + "/" + (*remain);
194 if ((rename(pkgFile.c_str(), backFile.c_str())) != 0) {
195 LogError("Failed to backup : " << pkgFile << " to " << backFile);
196 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
199 LogDebug("only backup : " << pkgFile << " to " << backFile);
203 m_context.job->UpdateProgress(
204 InstallerContext::INSTALL_BACKUP_RES_FILES,
205 "Backup resource file for update");
208 void TaskUpdateFiles::StepExecFileBackup()
210 std::string execFile = m_context.locations->getExecFile();
212 LogDebug(" source : " << execFile);
214 std::string tempSource= m_context.locations->getBackupExecFile();
215 LogDebug(" source : " << tempSource);
217 if ((rename(execFile.c_str(), tempSource.c_str())) != 0) {
218 LogError("Failed to rename " << execFile << " to " <<
220 ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
223 LogDebug("Backup : " << execFile << " to " << tempSource);
225 std::string clientPath = GlobalConfig::GetWrtClientExec();
227 LogInfo("link -s " << clientPath << " " << execFile);
228 symlink(clientPath.c_str(), execFile.c_str());
230 m_context.job->UpdateProgress(
231 InstallerContext::INSTALL_BACKUP_EXEC,
232 "Backup execution file for update");
235 void TaskUpdateFiles::StepAbortResourceFilesBackup()
237 LogDebug("StepAbortCopyFiles");
238 std::string srcPath = m_context.locations->getSourceDir();
239 std::string srcBuPath = m_context.locations->getBackupSourceDir();
241 LogDebug("Backup Folder " << srcBuPath << " to " << srcPath);
243 if(!WrtUtilRemove(srcPath)) {
244 LogError("Failed to remove " << srcPath);
247 if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
248 LogError("Failed to rename " << srcBuPath << " to " << srcPath);
252 void TaskUpdateFiles::StepAbortExecFileBackup()
254 LogDebug("StepAbortExecFileBackup");
255 std::string binPath = m_context.locations->getBinaryDir();
257 if(!WrtUtilRemove(binPath)) {
258 LogError("Failed to remove " << binPath);
261 std::string binBuPath = m_context.locations->getBackupBinaryDir();
262 if (rename(binBuPath.c_str(), binPath.c_str()) != 0) {
263 LogError("Failed to rename " << binBuPath << " to " << binPath);
265 LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
268 void TaskUpdateFiles::StepAbortCreateBackupFolder()
270 LogDebug("StepAbortCreateBackupFolder");
271 std::ostringstream path;
272 path << m_context.locations->getBackupDir();
273 LogDebug("Remove backup directory : " << path.str());
275 if(!WrtUtilRemove(path.str())) {
276 LogError("Failed to remove " << path);
280 void TaskUpdateFiles::StepUpdateExternalFiles()
282 WidgetDAO dao(m_context.locations->getPkgname());
283 WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
284 WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
285 LogDebug("Removing external files:");
286 FOREACH(file, externalLocationsDB)
288 if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
290 if(WrtUtilFileExists(*file))
292 LogDebug(" -> " << *file);
293 remove(file->c_str());
295 else if(WrtUtilDirExists(*file))
297 LogDebug(" -> " << *file);
298 if(!WrtUtilRemove(*file)){
299 ThrowMsg(Exceptions::RemovingFolderFailure,
300 "Failed to remove external directory");
305 LogWarning(" -> " << *file << "(no such a path)");
309 dao.unregisterAllExternalLocations();
310 LogDebug("Registering(update) external files:");
311 FOREACH(file, externalLocationsUpdate)
313 LogDebug(" -> " << *file);
315 dao.registerExternalLocations(externalLocationsUpdate);
318 } //namespace WidgetInstall