cd1b6326bd528c397d129c9f912c5b940126dd6a
[platform/core/appfw/app-installers.git] / src / common / recovery_file.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/recovery_file.h"
6
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/iostreams/stream.hpp>
9 #include <boost/iostreams/device/file_descriptor.hpp>
10 #include <boost/system/error_code.hpp>
11
12 #include <manifest_parser/utils/logging.h>
13
14 #include <array>
15 #include <cstring>
16 #include <string>
17 #include <map>
18 #include <utility>
19
20 #include "common/installer_context.h"
21 #include "common/utils/file_util.h"
22
23 namespace bf = boost::filesystem;
24 namespace bs = boost::system;
25 namespace bi = boost::iostreams;
26 namespace ci = common_installer;
27
28 namespace {
29
30 const char kRecoveryInstallString[] = "NEW";
31 const char kRecoveryUpdateString[] = "UPDATE";
32 const char kRecoveryUninstallationString[] = "UNINSTALL";
33 const char kRecoveryRdsString[] = "RDS";
34 const char kRecoveryDeltaString[] = "DELTA";
35 const char kRecoveryMountInstallString[] = "MOUNTINSTALL";
36 const char kRecoveryMountUpdateString[] = "MOUNTUPDATE";
37 const char kRecoveryReadonlyUpdateInstallString[] = "READONLYUPDATEINSTALL";
38 const char kRecoveryReadonlyUpdateUninstallString[] = "READONLYUPDATEUNINSTALL";
39 const char kRecoveryUnknownString[] = "UNKNOWN";
40
41 const std::map<std::string, ci::RequestType> kStringToRequestMap = {
42   {kRecoveryInstallString, ci::RequestType::Install},
43   {kRecoveryUpdateString, ci::RequestType::Update},
44   {kRecoveryUninstallationString, ci::RequestType::Uninstall},
45   {kRecoveryRdsString, ci::RequestType::Reinstall},
46   {kRecoveryDeltaString, ci::RequestType::Delta},
47   {kRecoveryMountInstallString, ci::RequestType::MountInstall},
48   {kRecoveryMountUpdateString, ci::RequestType::MountUpdate},
49   {kRecoveryReadonlyUpdateInstallString,
50       ci::RequestType::ReadonlyUpdateInstall},
51   {kRecoveryReadonlyUpdateUninstallString,
52       ci::RequestType::ReadonlyUpdateUninstall},
53 };
54
55 std::string TruncateNewLine(const char* data) {
56   int length = strlen(data);
57   if (data[length - 1] == '\n')
58       --length;
59   return std::string(data, length);
60 }
61
62 }  // namespace
63
64 namespace common_installer {
65 namespace recovery {
66
67 std::unique_ptr<RecoveryFile> RecoveryFile::CreateRecoveryFile(
68     const boost::filesystem::path& path, RequestType type) {
69   if (bf::exists(path)) {
70     LOG(ERROR) << "Recovery file already exists!";
71     return nullptr;
72   }
73   std::unique_ptr<RecoveryFile> file(new RecoveryFile(path, type, false));
74   if (file->is_detached()) {
75     LOG(ERROR) << "Failed to access file";
76     return nullptr;
77   }
78   return file;
79 }
80
81 std::unique_ptr<RecoveryFile> RecoveryFile::OpenRecoveryFile(
82     const boost::filesystem::path& path) {
83   if (!bf::exists(path)) {
84     LOG(ERROR) << "Cannot open recovery file";
85     return nullptr;
86   }
87   std::unique_ptr<RecoveryFile> file(new RecoveryFile(path,
88       RequestType::Unknown, true));
89   if (file->is_detached()) {
90     LOG(ERROR) << "Failed to read recovery file";
91     return nullptr;
92   }
93   return file;
94 }
95
96 RecoveryFile::RecoveryFile(const bf::path& path, RequestType type, bool load)
97     : type_(type), path_(path), backup_done_(false), cleanup_(false) {
98   backup_path_ = path_.string() + ".bck";
99   if (load) {
100     if (!ReadFileContent()) {
101       path_.clear();
102       return;
103     }
104   } else {
105     // create file
106     if (!WriteAndCommitFileContent()) {
107       path_.clear();
108       return;
109     }
110     LOG(DEBUG) << "Recovery file " << path_ << " created";
111   }
112 }
113
114 RecoveryFile::~RecoveryFile() {
115   if (Remove(path_))
116     LOG(DEBUG) << "Recovery file " << path_ << " removed";
117   if (Remove(backup_path_))
118     LOG(DEBUG) << "Recovery file " << backup_path_ << " removed";
119 }
120
121 void RecoveryFile::Detach() {
122   path_.clear();
123 }
124
125 bool RecoveryFile::is_detached() const {
126   return path_.empty();
127 }
128
129 void RecoveryFile::set_unpacked_dir(
130     boost::filesystem::path unpacked_dir) {
131   unpacked_dir_ = std::move(unpacked_dir);
132 }
133
134 void RecoveryFile::set_pkgid(std::string pkgid) {
135   pkgid_ = std::move(pkgid);
136 }
137
138
139 void RecoveryFile::set_backup_done(bool backup_done) {
140   backup_done_ = backup_done;
141 }
142
143 void RecoveryFile::set_cleanup(bool cleanup) {
144   cleanup_ = cleanup;
145 }
146
147 const boost::filesystem::path& RecoveryFile::unpacked_dir() const {
148   return unpacked_dir_;
149 }
150
151 const std::string& RecoveryFile::pkgid() const {
152   return pkgid_;
153 }
154
155 RequestType RecoveryFile::type() const {
156   return type_;
157 }
158
159 bool RecoveryFile::backup_done() const {
160   return backup_done_;
161 }
162
163 bool RecoveryFile::cleanup() const {
164   return cleanup_;
165 }
166
167 bool RecoveryFile::ReadFileContent() {
168   FILE* handle = fopen(path_.c_str(), "r");
169   if (!handle) {
170     LOG(ERROR) << "Cannot read recovery file";
171     return false;
172   }
173   std::array<char, 200> data;
174   data[0] = '\0';
175   if (!fgets(data.data(), data.size(), handle)) {
176     type_ = RequestType::Unknown;
177     fclose(handle);
178     return true;
179   }
180   std::string mode(TruncateNewLine(data.data()));
181   auto iter = kStringToRequestMap.find(mode);
182   if (iter == kStringToRequestMap.end()) {
183     type_ = RequestType::Unknown;
184   } else {
185     type_ = iter->second;
186   }
187
188   if (!fgets(data.data(), data.size(), handle)) {
189     fclose(handle);
190     return true;
191   }
192   unpacked_dir_ = TruncateNewLine(data.data());
193   if (!fgets(data.data(), data.size(), handle)) {
194     fclose(handle);
195     return true;
196   }
197   pkgid_ = TruncateNewLine(data.data());
198   if (!fgets(data.data(), data.size(), handle)) {
199     fclose(handle);
200     return true;
201   }
202   std::string backup_flag = TruncateNewLine(data.data());
203   if (backup_flag == "true")
204     backup_done_ = true;
205   else
206     backup_done_ = false;
207   if (!fgets(data.data(), data.size(), handle)) {
208     fclose(handle);
209     return true;
210   }
211   std::string cleanup_flag = TruncateNewLine(data.data());
212   if (cleanup_flag == "cleanup")
213     cleanup_ = true;
214   else
215     cleanup_ = false;
216   fclose(handle);
217   return true;
218 }
219
220 bool RecoveryFile::WriteAndCommitFileContent() {
221   if (bf::exists(path_))  {
222     bs::error_code error;
223     bf::rename(path_, backup_path_, error);
224     if (error) {
225       LOG(ERROR) << "Cannot backup recovery file:" << path_ <<
226           ", error: " << error;
227       return false;
228     }
229   }
230
231   bi::stream<bi::file_descriptor_sink> ofs(path_);
232   if (!ofs) {
233     LOG(ERROR) << "Cannot write recovery file";
234     return false;
235   }
236
237   switch (type_) {
238   case RequestType::Install:
239     ofs << kRecoveryInstallString << std::endl;
240     break;
241   case RequestType::Update:
242     ofs << kRecoveryUpdateString << std::endl;
243     break;
244   case RequestType::Uninstall:
245     ofs << kRecoveryUninstallationString << std::endl;
246     break;
247   case RequestType::Reinstall:
248     ofs << kRecoveryRdsString << std::endl;
249     break;
250   case RequestType::Delta:
251     ofs << kRecoveryDeltaString << std::endl;
252     break;
253   case RequestType::MountInstall:
254     ofs << kRecoveryMountInstallString << std::endl;
255     break;
256   case RequestType::MountUpdate:
257     ofs << kRecoveryMountUpdateString << std::endl;
258     break;
259   case RequestType::ReadonlyUpdateInstall:
260     ofs << kRecoveryReadonlyUpdateInstallString << std::endl;
261     break;
262   case RequestType::ReadonlyUpdateUninstall:
263     ofs << kRecoveryReadonlyUpdateUninstallString << std::endl;
264     break;
265   default:
266     ofs << kRecoveryUnknownString << std::endl;
267     break;
268   }
269   ofs << unpacked_dir_.c_str() << std::endl;
270   ofs << pkgid_ << std::endl;
271   ofs << (backup_done_ ? "true" : "false") << std::endl;
272   ofs << (cleanup_ ? "cleanup" : "rollback") << std::endl;
273   ofs.flush();
274   ::fsync(ofs->handle());
275   ofs.close();
276
277   Remove(backup_path_);
278   return true;
279 }
280
281 }  // namespace recovery
282 }  // namespace common_installer
283