#include <widget_install/widget_install_context.h>
#include <widget_install/widget_install_errors.h>
#include <widget_install/job_widget_install.h>
+#include <widget_install/directory_api.h>
#include <dpl/wrt-dao-ro/global_config.h>
#include <dpl/exception.h>
DPL::TaskDecl<TaskUpdateFiles>(this),
m_context(context)
{
- AddStep(&TaskUpdateFiles::StepCreateBackupFolder);
- AddStep(&TaskUpdateFiles::StepResourceFilesBackup);
- AddStep(&TaskUpdateFiles::StepExecFileBackup);
+ AddStep(&TaskUpdateFiles::StepBackupDirectory);
- AddAbortStep(&TaskUpdateFiles::StepAbortCreateBackupFolder);
- AddAbortStep(&TaskUpdateFiles::StepAbortExecFileBackup);
- AddAbortStep(&TaskUpdateFiles::StepAbortResourceFilesBackup);
+ AddAbortStep(&TaskUpdateFiles::StepAbortBackupDirectory);
}
-void TaskUpdateFiles::StepCreateBackupFolder()
+void TaskUpdateFiles::StepBackupDirectory()
{
LogDebug("StepCreateBackupFolder");
- std::ostringstream backDirPath;
- std::string srcBuPath = m_context.locations->getBackupSourceDir();
- LogDebug("backup resource directory path : " << srcBuPath);
- if (!WrtUtilMakeDir(srcBuPath)) {
- ThrowMsg(
- Exceptions::BackupFailed,
- "Error occurs during create \
- backup directory.");
- }
-
- std::string binBuPath = m_context.locations->getBackupBinaryDir();
- LogDebug("backup execution directory path : " << binBuPath);
- if (!WrtUtilMakeDir(binBuPath)) {
- ThrowMsg(
- Exceptions::BackupFailed,
- "Error occurs during create backup \
- directory.");
- }
-
- m_context.job->UpdateProgress(
- InstallerContext::INSTALL_CREATE_BACKUP_DIR,
- "Backup directory created for update");
-}
-
-void TaskUpdateFiles::ReadDirList(std::string dirPath, ExistFileList &list,
- size_t subLen)
-{
- DIR* pkgDir = opendir(dirPath.c_str());
- if (!pkgDir) {
- LogError("Package directory " << dirPath << " doesn't exist");
- ThrowMsg(Exceptions::InternalError,
- "Error occurs during read \
- directory");
- }
-
- struct dirent* dirent;
- struct stat statInfo;
- do {
- if ((dirent = readdir(pkgDir))) {
- std::string dirName = dirent->d_name;
- std::string absFileName = dirPath + "/" + dirName;
- if (stat(absFileName.c_str(), &statInfo) != 0) {
- ThrowMsg(Exceptions::InternalError, "Error occurs read file");
- }
-
- if (S_ISDIR(statInfo.st_mode)) {
- if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
- "..") == 0)
- {
- continue;
- }
- ReadDirList(absFileName, list, subLen);
- }
-
- list.insert(absFileName.substr(subLen));
- }
- } while (dirent);
- //closing the directory
- if (-1 == TEMP_FAILURE_RETRY(closedir(pkgDir))) {
- LogError("Failed to close dir: " << dirPath << " with error: "
- << DPL::GetErrnoString());
- }
-}
-
-void TaskUpdateFiles::StepResourceFilesBackup()
-{
- LogDebug("StepCopyFiles");
-
- ExistFileList resList;
- ExistFileList tempList;
-
- std::string pkgSrc = m_context.locations->getSourceDir();
- ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str()) + 1);
-
- std::string tempSrc = m_context.locations->getTemporaryPackageDir();
- ReadDirList(tempSrc, tempList, strlen(tempSrc.c_str()) + 1);
-
- FOREACH(it, tempList) {
- std::set<std::string>::iterator res;
- res = resList.find(*it);
- std::string resFile = pkgSrc + "/" + (*it);
- std::string newFile = tempSrc + "/" + (*it);
-
- if (res != resList.end()) {
- std::string backupFile =
- m_context.locations->getBackupSourceDir() +
- "/" + (*it);
-
- struct stat sInfo;
- if (stat(resFile.c_str(), &sInfo) != 0) {
- ThrowMsg(Exceptions::InternalError, "Error occurs read file");
- }
-
- if (S_ISDIR(sInfo.st_mode)) {
- LogDebug(resFile << " is a directory. so create a folder : " <<
- backupFile);
- WrtUtilMakeDir(backupFile);
- } else {
- if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
- LogError(
- "Failed to rename " << resFile << " to " <<
- backupFile);
- ThrowMsg(
- Exceptions::BackupFailed,
- "Error occurs during \
- rename file");
- }
+ std::string backPath = m_context.locations->getBackupDir();
+ LogDebug("backup resource directory path : " << backPath);
+ std::string pkgPath = m_context.locations->getPackageInstallationDir();
- LogDebug("backup : " << resFile << " to " << backupFile);
-
- if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
- LogError(
- "Failed to rename " << newFile << " to " << resFile);
- ThrowMsg(
- Exceptions::BackupFailed,
- "Error occurs during \
- rename file");
- }
- LogDebug("copy : " << newFile << " to " << resFile);
- }
- resList.erase(res);
- } else {
- if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
- LogError("Failed to rename " << newFile << " to " << resFile);
- ThrowMsg(
- Exceptions::BackupFailed,
- "Error occurs during \
- rename file");
- }
- LogDebug("only copy : " << newFile << " to " << resFile);
- }
- }
-
- if (resList.empty() != 0) {
- FOREACH(remain, resList) {
- std::string pkgFile = pkgSrc + "/" + (*remain);
- std::string backFile = tempSrc + "/" + (*remain);
- if ((rename(pkgFile.c_str(), backFile.c_str())) != 0) {
- LogError("Failed to backup : " << pkgFile << " to " << backFile);
- ThrowMsg(
- Exceptions::BackupFailed,
- "Error occurs during \
- rename file");
- }
- LogDebug("only backup : " << pkgFile << " to " << backFile);
- }
+ if (0 == access(backPath.c_str(), F_OK)) {
+ WrtUtilRemove(backPath);
}
-
- m_context.job->UpdateProgress(
- InstallerContext::INSTALL_BACKUP_RES_FILES,
- "Backup resource file for update");
-}
-
-void TaskUpdateFiles::StepExecFileBackup()
-{
- std::string execFile = m_context.locations->getExecFile();
-
- LogDebug(" source : " << execFile);
-
- std::string tempSource = m_context.locations->getBackupExecFile();
- LogDebug(" source : " << tempSource);
-
- if ((rename(execFile.c_str(), tempSource.c_str())) != 0) {
- LogError("Failed to rename " << execFile << " to " <<
- tempSource);
+ LogDebug("copy : " << pkgPath << " to " << backPath);
+ if ((rename(pkgPath.c_str(), backPath.c_str())) != 0) {
+ LogError("Failed to rename " << pkgPath << " to " << backPath);
ThrowMsg(Exceptions::BackupFailed,
- "Error occurs during \
- rename file");
+ "Error occurs during rename file");
}
- LogDebug("Backup : " << execFile << " to " << tempSource);
-
- std::string clientPath = GlobalConfig::GetWrtClientExec();
- LogInfo("link -s " << clientPath << " " << execFile);
- symlink(clientPath.c_str(), execFile.c_str());
+ WrtUtilMakeDir(pkgPath);
m_context.job->UpdateProgress(
- InstallerContext::INSTALL_BACKUP_EXEC,
- "Backup execution file for update");
+ InstallerContext::INSTALL_CREATE_BACKUP_DIR,
+ "Backup directory created for update");
}
-void TaskUpdateFiles::StepAbortResourceFilesBackup()
+void TaskUpdateFiles::StepAbortBackupDirectory()
{
LogDebug("StepAbortCopyFiles");
- std::string srcPath = m_context.locations->getSourceDir();
- std::string srcBuPath = m_context.locations->getBackupSourceDir();
-
- LogDebug("Backup Folder " << srcBuPath << " to " << srcPath);
-
- if (!WrtUtilRemove(srcPath)) {
- LogError("Failed to remove " << srcPath);
- }
-
- if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
- LogError("Failed to rename " << srcBuPath << " to " << srcPath);
- }
-}
-void TaskUpdateFiles::StepAbortExecFileBackup()
-{
- LogDebug("StepAbortExecFileBackup");
- std::string binPath = m_context.locations->getBinaryDir();
+ std::string backPath = m_context.locations->getBackupDir();
+ std::string pkgPath = m_context.locations->getPackageInstallationDir();
- if (!WrtUtilRemove(binPath)) {
- LogError("Failed to remove " << binPath);
- }
+ LogDebug("Backup Folder " << backPath << " to " << pkgPath);
- std::string binBuPath = m_context.locations->getBackupBinaryDir();
- if (rename(binBuPath.c_str(), binPath.c_str()) != 0) {
- LogError("Failed to rename " << binBuPath << " to " << binPath);
+ if (!WrtUtilRemove(pkgPath)) {
+ LogError("Failed to remove " << pkgPath);
}
- LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
-}
-
-void TaskUpdateFiles::StepAbortCreateBackupFolder()
-{
- LogDebug("StepAbortCreateBackupFolder");
- std::ostringstream path;
- path << m_context.locations->getBackupDir();
- LogDebug("Remove backup directory : " << path.str());
- if (!WrtUtilRemove(path.str())) {
- LogError("Failed to remove " << path);
+ if (rename(backPath.c_str(), pkgPath.c_str()) != 0) {
+ LogError("Failed to rename " << backPath << " to " << pkgPath);
}
}
} //namespace WidgetInstall