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"
30 #include <dpl/string.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>
39 namespace WidgetInstall {
41 const char* const KEY_DELETE = "#delete";
42 const char* const KEY_ADD = "#add";
43 const char* const KEY_MODIFY = "#modify";
44 std::list<std::string> keyList = {KEY_DELETE, KEY_ADD, KEY_MODIFY};
46 void verifyFile(const std::string &filePath)
48 if (access(filePath.c_str(), F_OK) != 0) {
49 ThrowMsg(Exceptions::RDSDeltaFailure, "File is missed " << filePath);
54 TaskPrepareReinstall::TaskPrepareReinstall(InstallerContext& context) :
55 DPL::TaskDecl<TaskPrepareReinstall>(this),
58 AddStep(&TaskPrepareReinstall::StartStep);
59 AddStep(&TaskPrepareReinstall::StepPrepare);
60 AddStep(&TaskPrepareReinstall::StepParseRDSDelta);
61 AddStep(&TaskPrepareReinstall::StepVerifyRDSDelta);
62 AddStep(&TaskPrepareReinstall::StepAddFile);
63 AddStep(&TaskPrepareReinstall::StepDeleteFile);
64 AddStep(&TaskPrepareReinstall::StepModifyFile);
65 AddStep(&TaskPrepareReinstall::EndStep);
68 void TaskPrepareReinstall::StepPrepare()
71 m_sourcePath = m_context.locations->getTemporaryPackageDir();
74 m_installedPath = m_context.locations->getPackageInstallationDir();
75 m_installedPath += "/";
78 void TaskPrepareReinstall::StepParseRDSDelta()
80 LogDebug("parse RDS delta");
81 std::string rdsDeltaPath = m_sourcePath;
82 rdsDeltaPath += ".rds_delta";
83 std::ifstream delta(rdsDeltaPath);
85 if (!delta.is_open()) {
86 // TODO throw exception
87 ThrowMsg(Exceptions::RDSDeltaFailure, "rds_delta file is missed");
93 while (std::getline(delta, line) &&!delta.eof()) {
94 FOREACH(keyIt, keyList) {
96 LogDebug("find key = [" << line << "]");
101 if (key == line || line.empty() || line == "\n") {
104 if (key == KEY_DELETE) {
105 m_deleteFileList.push_back(line);
106 LogDebug("line = [" << line << "]");
107 } else if (key == KEY_ADD) {
108 m_addFileList.push_back(line);
109 LogDebug("line = [" << line << "]");
110 } else if (key == KEY_MODIFY) {
111 m_modifyFileList.push_back(line);
112 LogDebug("line = [" << line << "]");
117 void TaskPrepareReinstall::StepVerifyRDSDelta()
119 LogDebug("verify RDS delta");
121 FOREACH(file, m_addFileList) {
122 std::string addFilePath = m_sourcePath;
123 addFilePath += *file;
124 verifyFile(addFilePath);
126 // Verify DELETE file
127 FOREACH(file, m_deleteFileList) {
128 std::string deleteFilePath = m_installedPath;
129 deleteFilePath += *file;
130 verifyFile(deleteFilePath);
132 // Verify MODIFY file
133 FOREACH(file, m_modifyFileList) {
134 std::string newFilePath = m_sourcePath;
135 newFilePath += *file;
136 verifyFile(newFilePath);
138 std::string existingFilePath = m_installedPath;
139 existingFilePath += *file;
140 verifyFile(existingFilePath);
142 LogDebug("Finished veify RDS Delta");
144 m_context.job->UpdateProgress(
145 InstallerContext::INSTALL_RDS_DELTA_CHECK,
146 "RDS delta verify finished");
149 void TaskPrepareReinstall::StepAddFile()
151 LogDebug("Add file");
152 FOREACH(file, m_addFileList) {
153 std::string newfile = m_sourcePath;
155 std::string destPath = m_installedPath;
157 if (rename(newfile.c_str(), destPath.c_str()) != 0) {
158 ThrowMsg(Exceptions::RDSDeltaFailure,
159 "Fail to ADD file " << newfile);
161 LogDebug("Copy " << newfile << " to " << destPath);
165 void TaskPrepareReinstall::StepDeleteFile()
167 LogDebug("Delete file");
168 FOREACH(file, m_deleteFileList) {
169 std::string deleteFilePath = m_installedPath;
170 deleteFilePath += *file;
171 if (remove(deleteFilePath.c_str()) != 0) {
172 ThrowMsg(Exceptions::RDSDeltaFailure,
173 "Fail to DELETE file " << deleteFilePath);
175 LogDebug("Delete " << deleteFilePath);
179 void TaskPrepareReinstall::StepModifyFile()
181 LogDebug("Modify file");
182 FOREACH(file, m_modifyFileList) {
183 std::string destPath = m_installedPath;
185 if (remove(destPath.c_str()) != 0) {
186 ThrowMsg(Exceptions::RDSDeltaFailure,
187 "Fail to delete existing file " << destPath);
190 std::string newfile = m_sourcePath;
192 if (rename(newfile.c_str(), destPath.c_str()) != 0) {
193 ThrowMsg(Exceptions::RDSDeltaFailure,
194 "Fail to move new file" << destPath);
196 LogDebug("Replace " << newfile << " to " << destPath);
200 void TaskPrepareReinstall::StartStep()
202 LogDebug("--------- <TaskPrepareReinstall> : START ----------");
205 void TaskPrepareReinstall::EndStep()
207 m_context.job->UpdateProgress(
208 InstallerContext::INSTALL_RDS_PREPARE,
209 "RDS prepare finished");
211 LogDebug("--------- <TaskPrepareReinstall> : END ----------");
214 } //namespace WidgetInstall