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_prepare_reinstall.cpp
18 * @author Jihoon Chung(jihoon.chung@samsung.com)
20 * @brief Implementation file for installer task prepare reinstalling
23 #include "task_prepare_reinstall.h"
29 #include <dpl/string.h>
30 #include <dpl/log/log.h>
31 #include <dpl/foreach.h>
33 #include <widget_install/widget_install_context.h>
34 #include <widget_install/widget_install_errors.h>
35 #include <widget_install/job_widget_install.h>
38 namespace WidgetInstall {
40 const char* const KEY_DELETE = "#delete";
41 const char* const KEY_ADD = "#add";
42 const char* const KEY_MODIFY = "#modify";
43 std::list<std::string> keyList = {KEY_DELETE, KEY_ADD, KEY_MODIFY};
45 void verifyFile(const std::string &filePath)
47 if (access(filePath.c_str(), F_OK) != 0) {
48 ThrowMsg(Exceptions::AceCheckFailed, "File is missed " << filePath);
53 TaskPrepareReinstall::TaskPrepareReinstall(InstallerContext& context) :
54 DPL::TaskDecl<TaskPrepareReinstall>(this),
57 AddStep(&TaskPrepareReinstall::StepPrepare);
58 AddStep(&TaskPrepareReinstall::StepParseRDSDelta);
59 AddStep(&TaskPrepareReinstall::StepVerifyRDSDelta);
60 AddStep(&TaskPrepareReinstall::StepAddFile);
61 AddStep(&TaskPrepareReinstall::StepDeleteFile);
62 AddStep(&TaskPrepareReinstall::StepModifyFile);
65 void TaskPrepareReinstall::StepPrepare()
68 m_sourcePath = m_context.locations->getTemporaryPackageDir();
71 m_installedPath = m_context.locations->getPackageInstallationDir();
72 m_installedPath += "/";
75 void TaskPrepareReinstall::StepParseRDSDelta()
77 LogInfo("parse RDS delta");
78 std::string rdsDeltaPath = m_sourcePath;
79 rdsDeltaPath += ".rds_delta";
80 std::ifstream delta(rdsDeltaPath);
82 if (!delta.is_open()) {
83 // TODO throw exception
84 ThrowMsg(Exceptions::RDSDeltaFailure, "rds_delta file is missed");
90 while (std::getline(delta, line) &&!delta.eof()) {
91 FOREACH(keyIt, keyList) {
93 LogInfo("find key = [" << line << "]");
98 if (key == line || line.empty() || line == "\n") {
101 if (key == KEY_DELETE) {
102 m_deleteFileList.push_back(line);
103 LogInfo("line = [" << line << "]");
104 } else if (key == KEY_ADD) {
105 m_addFileList.push_back(line);
106 LogInfo("line = [" << line << "]");
107 } else if (key == KEY_MODIFY) {
108 m_modifyFileList.push_back(line);
109 LogInfo("line = [" << line << "]");
114 void TaskPrepareReinstall::StepVerifyRDSDelta()
116 LogInfo("verify RDS delta");
118 FOREACH(file, m_addFileList) {
119 std::string addFilePath = m_sourcePath;
120 addFilePath += *file;
121 verifyFile(addFilePath);
123 // Verify DELETE file
124 FOREACH(file, m_deleteFileList) {
125 std::string deleteFilePath = m_installedPath;
126 deleteFilePath += *file;
127 verifyFile(deleteFilePath);
129 // Verify MODIFY file
130 FOREACH(file, m_modifyFileList) {
131 std::string newFilePath = m_sourcePath;
132 newFilePath += *file;
133 verifyFile(newFilePath);
135 std::string existingFilePath = m_installedPath;
136 existingFilePath += *file;
137 verifyFile(existingFilePath);
139 LogInfo("Finished veify RDS Delta");
141 m_context.job->UpdateProgress(
142 InstallerContext::INSTALL_RDS_DELTA_CHECK,
143 "RDS delta verify finished");
146 void TaskPrepareReinstall::StepAddFile()
149 FOREACH(file, m_addFileList) {
150 std::string newfile = m_sourcePath;
152 std::string destPath = m_installedPath;
154 if (rename(newfile.c_str(), destPath.c_str()) != 0) {
155 ThrowMsg(Exceptions::RDSDeltaFailure,
156 "Fail to ADD file " << newfile);
158 LogInfo("Copy " << newfile << " to " << destPath);
162 void TaskPrepareReinstall::StepDeleteFile()
164 LogInfo("Delete file");
165 FOREACH(file, m_deleteFileList) {
166 std::string deleteFilePath = m_installedPath;
167 deleteFilePath += *file;
168 if (remove(deleteFilePath.c_str()) != 0) {
169 ThrowMsg(Exceptions::RDSDeltaFailure,
170 "Fail to DELETE file " << deleteFilePath);
172 LogInfo("Delete " << deleteFilePath);
176 void TaskPrepareReinstall::StepModifyFile()
178 LogInfo("Modify file");
179 FOREACH(file, m_modifyFileList) {
180 std::string destPath = m_installedPath;
182 if (remove(destPath.c_str()) != 0) {
183 ThrowMsg(Exceptions::RDSDeltaFailure,
184 "Fail to delete existing file " << destPath);
187 std::string newfile = m_sourcePath;
189 if (rename(newfile.c_str(), destPath.c_str()) != 0) {
190 ThrowMsg(Exceptions::RDSDeltaFailure,
191 "Fail to move new file" << destPath);
193 LogInfo("Replace " << newfile << " to " << destPath);
195 m_context.job->UpdateProgress(
196 InstallerContext::INSTALL_RDS_PREPARE,
197 "RDS prepare finished");
200 } //namespace WidgetInstall