[Release] wrt-installer_0.1.114
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_prepare_reinstall.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    task_prepare_reinstall.cpp
18  * @author  Jihoon Chung(jihoon.chung@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task prepare reinstalling
21  */
22
23 #include "task_prepare_reinstall.h"
24
25 #include <stdio.h>
26 #include <fstream>
27 #include <unistd.h>
28
29 #include <dpl/task.h>
30 #include <dpl/string.h>
31 #include <dpl/log/log.h>
32 #include <dpl/foreach.h>
33
34 #include <widget_install/widget_install_context.h>
35 #include <widget_install/widget_install_errors.h>
36 #include <widget_install/job_widget_install.h>
37
38 namespace Jobs {
39 namespace WidgetInstall {
40 namespace {
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};
45
46 void verifyFile(const std::string &filePath)
47 {
48     if (access(filePath.c_str(), F_OK) != 0) {
49         ThrowMsg(Exceptions::RDSDeltaFailure, "File is missed " << filePath);
50     }
51 }
52 }
53
54 TaskPrepareReinstall::TaskPrepareReinstall(InstallerContext& context) :
55     DPL::TaskDecl<TaskPrepareReinstall>(this),
56     m_context(context)
57 {
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);
66 }
67
68 void TaskPrepareReinstall::StepPrepare()
69 {
70     LogDebug("Prepare");
71     m_sourcePath = m_context.locations->getTemporaryPackageDir();
72     m_sourcePath += "/";
73
74     m_installedPath = m_context.locations->getPackageInstallationDir();
75     m_installedPath += "/";
76 }
77
78 void TaskPrepareReinstall::StepParseRDSDelta()
79 {
80     LogDebug("parse RDS delta");
81     std::string rdsDeltaPath = m_sourcePath;
82     rdsDeltaPath += ".rds_delta";
83     std::ifstream delta(rdsDeltaPath);
84
85     if (!delta.is_open()) {
86         // TODO throw exception
87         ThrowMsg(Exceptions::RDSDeltaFailure, "rds_delta file is missed");
88         return;
89     }
90
91     std::string line;
92     std::string key;
93     while (std::getline(delta, line) &&!delta.eof()) {
94         FOREACH(keyIt, keyList) {
95             if (line == *keyIt) {
96                 LogDebug("find key = [" << line << "]");
97                 key = line;
98                 break;
99             }
100         }
101         if (key == line || line.empty() || line == "\n") {
102             continue;
103         }
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 << "]");
113         }
114     }
115 }
116
117 void TaskPrepareReinstall::StepVerifyRDSDelta()
118 {
119     LogDebug("verify RDS delta");
120     // Verify ADD file
121     FOREACH(file, m_addFileList) {
122         std::string addFilePath = m_sourcePath;
123         addFilePath += *file;
124         verifyFile(addFilePath);
125     }
126     // Verify DELETE file
127     FOREACH(file, m_deleteFileList) {
128         std::string deleteFilePath = m_installedPath;
129         deleteFilePath += *file;
130         verifyFile(deleteFilePath);
131     }
132     // Verify MODIFY file
133     FOREACH(file, m_modifyFileList) {
134         std::string newFilePath = m_sourcePath;
135         newFilePath += *file;
136         verifyFile(newFilePath);
137
138         std::string existingFilePath = m_installedPath;
139         existingFilePath += *file;
140         verifyFile(existingFilePath);
141     }
142     LogDebug("Finished veify RDS Delta");
143
144     m_context.job->UpdateProgress(
145         InstallerContext::INSTALL_RDS_DELTA_CHECK,
146         "RDS delta verify finished");
147 }
148
149 void TaskPrepareReinstall::StepAddFile()
150 {
151     LogDebug("Add file");
152     FOREACH(file, m_addFileList) {
153         std::string newfile = m_sourcePath;
154         newfile += *file;
155         std::string destPath = m_installedPath;
156         destPath += *file;
157         if (rename(newfile.c_str(), destPath.c_str()) != 0) {
158             ThrowMsg(Exceptions::RDSDeltaFailure,
159                 "Fail to ADD file " << newfile);
160         }
161         LogDebug("Copy " << newfile << " to " << destPath);
162     }
163 }
164
165 void TaskPrepareReinstall::StepDeleteFile()
166 {
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);
174         }
175         LogDebug("Delete " << deleteFilePath);
176     }
177 }
178
179 void TaskPrepareReinstall::StepModifyFile()
180 {
181     LogDebug("Modify  file");
182     FOREACH(file, m_modifyFileList) {
183         std::string destPath = m_installedPath;
184         destPath += *file;
185         if (remove(destPath.c_str()) != 0) {
186             ThrowMsg(Exceptions::RDSDeltaFailure,
187                 "Fail to delete existing file " << destPath);
188         }
189
190         std::string newfile = m_sourcePath;
191         newfile += *file;
192         if (rename(newfile.c_str(), destPath.c_str()) != 0) {
193             ThrowMsg(Exceptions::RDSDeltaFailure,
194                 "Fail to move new file" << destPath);
195         }
196         LogDebug("Replace " << newfile << " to " << destPath);
197     }
198 }
199
200 void TaskPrepareReinstall::StartStep()
201 {
202     LogDebug("--------- <TaskPrepareReinstall> : START ----------");
203 }
204
205 void TaskPrepareReinstall::EndStep()
206 {
207     m_context.job->UpdateProgress(
208         InstallerContext::INSTALL_RDS_PREPARE,
209         "RDS prepare finished");
210
211     LogDebug("--------- <TaskPrepareReinstall> : END ----------");
212 }
213
214 } //namespace WidgetInstall
215 } //namespace Jobs