[Release] wrt-installer_0.1.25
[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
28 #include <dpl/task.h>
29 #include <dpl/string.h>
30 #include <dpl/log/log.h>
31 #include <dpl/foreach.h>
32
33 #include <widget_install/widget_install_context.h>
34 #include <widget_install/widget_install_errors.h>
35 #include <widget_install/job_widget_install.h>
36
37 namespace Jobs {
38 namespace WidgetInstall {
39 namespace {
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};
44
45 void verifyFile(const std::string &filePath)
46 {
47     if (access(filePath.c_str(), F_OK) != 0) {
48         ThrowMsg(Exceptions::AceCheckFailed, "File is missed " << filePath);
49     }
50 }
51 }
52
53 TaskPrepareReinstall::TaskPrepareReinstall(InstallerContext& context) :
54     DPL::TaskDecl<TaskPrepareReinstall>(this),
55     m_context(context)
56 {
57     AddStep(&TaskPrepareReinstall::StepPrepare);
58     AddStep(&TaskPrepareReinstall::StepParseRDSDelta);
59     AddStep(&TaskPrepareReinstall::StepVerifyRDSDelta);
60     AddStep(&TaskPrepareReinstall::StepAddFile);
61     AddStep(&TaskPrepareReinstall::StepDeleteFile);
62     AddStep(&TaskPrepareReinstall::StepModifyFile);
63 }
64
65 void TaskPrepareReinstall::StepPrepare()
66 {
67     LogInfo("Prepare");
68     m_sourcePath = m_context.locations->getTemporaryPackageDir();
69     m_sourcePath += "/";
70
71     m_installedPath = m_context.locations->getPackageInstallationDir();
72     m_installedPath += "/";
73 }
74
75 void TaskPrepareReinstall::StepParseRDSDelta()
76 {
77     LogInfo("parse RDS delta");
78     std::string rdsDeltaPath = m_sourcePath;
79     rdsDeltaPath += ".rds_delta";
80     std::ifstream delta(rdsDeltaPath);
81
82     if (!delta.is_open()) {
83         // TODO throw exception
84         ThrowMsg(Exceptions::RDSDeltaFailure, "rds_delta file is missed");
85         return;
86     }
87
88     std::string line;
89     std::string key;
90     while (std::getline(delta, line) &&!delta.eof()) {
91         FOREACH(keyIt, keyList) {
92             if (line == *keyIt) {
93                 LogInfo("find key = [" << line << "]");
94                 key = line;
95                 break;
96             }
97         }
98         if (key == line || line.empty() || line == "\n") {
99             continue;
100         }
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 << "]");
110         }
111     }
112 }
113
114 void TaskPrepareReinstall::StepVerifyRDSDelta()
115 {
116     LogInfo("verify RDS delta");
117     // Verify ADD file
118     FOREACH(file, m_addFileList) {
119         std::string addFilePath = m_sourcePath;
120         addFilePath += *file;
121         verifyFile(addFilePath);
122     }
123     // Verify DELETE file
124     FOREACH(file, m_deleteFileList) {
125         std::string deleteFilePath = m_installedPath;
126         deleteFilePath += *file;
127         verifyFile(deleteFilePath);
128     }
129     // Verify MODIFY file
130     FOREACH(file, m_modifyFileList) {
131         std::string newFilePath = m_sourcePath;
132         newFilePath += *file;
133         verifyFile(newFilePath);
134
135         std::string existingFilePath = m_installedPath;
136         existingFilePath += *file;
137         verifyFile(existingFilePath);
138     }
139     LogInfo("Finished veify RDS Delta");
140
141     m_context.job->UpdateProgress(
142         InstallerContext::INSTALL_RDS_DELTA_CHECK,
143         "RDS delta verify finished");
144 }
145
146 void TaskPrepareReinstall::StepAddFile()
147 {
148     LogInfo("Add file");
149     FOREACH(file, m_addFileList) {
150         std::string newfile = m_sourcePath;
151         newfile += *file;
152         std::string destPath = m_installedPath;
153         destPath += *file;
154         if (rename(newfile.c_str(), destPath.c_str()) != 0) {
155             ThrowMsg(Exceptions::RDSDeltaFailure,
156                 "Fail to ADD file " << newfile);
157         }
158         LogInfo("Copy " << newfile << " to " << destPath);
159     }
160 }
161
162 void TaskPrepareReinstall::StepDeleteFile()
163 {
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);
171         }
172         LogInfo("Delete " << deleteFilePath);
173     }
174 }
175
176 void TaskPrepareReinstall::StepModifyFile()
177 {
178     LogInfo("Modify  file");
179     FOREACH(file, m_modifyFileList) {
180         std::string destPath = m_installedPath;
181         destPath += *file;
182         if (remove(destPath.c_str()) != 0) {
183             ThrowMsg(Exceptions::RDSDeltaFailure,
184                 "Fail to delete existing file " << destPath);
185         }
186
187         std::string newfile = m_sourcePath;
188         newfile += *file;
189         if (rename(newfile.c_str(), destPath.c_str()) != 0) {
190             ThrowMsg(Exceptions::RDSDeltaFailure,
191                 "Fail to move new file" << destPath);
192         }
193         LogInfo("Replace " << newfile << " to " << destPath);
194     }
195     m_context.job->UpdateProgress(
196         InstallerContext::INSTALL_RDS_PREPARE,
197         "RDS prepare finished");
198 }
199
200 } //namespace WidgetInstall
201 } //namespace Jobs