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.
5 #include "common/recovery_file.h"
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>
12 #include <manifest_parser/utils/logging.h>
20 #include "common/installer_context.h"
21 #include "common/utils/file_util.h"
23 namespace bf = boost::filesystem;
24 namespace bs = boost::system;
25 namespace bi = boost::iostreams;
26 namespace ci = common_installer;
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";
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},
55 std::string TruncateNewLine(const char* data) {
56 int length = strlen(data);
57 if (data[length - 1] == '\n')
59 return std::string(data, length);
64 namespace common_installer {
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!";
73 std::unique_ptr<RecoveryFile> file(new RecoveryFile(path, type, false));
74 if (file->is_detached()) {
75 LOG(ERROR) << "Failed to access file";
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";
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";
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";
100 if (!ReadFileContent()) {
106 if (!WriteAndCommitFileContent()) {
110 LOG(DEBUG) << "Recovery file " << path_ << " created";
114 RecoveryFile::~RecoveryFile() {
116 LOG(DEBUG) << "Recovery file " << path_ << " removed";
117 if (Remove(backup_path_))
118 LOG(DEBUG) << "Recovery file " << backup_path_ << " removed";
121 void RecoveryFile::Detach() {
125 bool RecoveryFile::is_detached() const {
126 return path_.empty();
129 void RecoveryFile::set_unpacked_dir(
130 boost::filesystem::path unpacked_dir) {
131 unpacked_dir_ = std::move(unpacked_dir);
134 void RecoveryFile::set_pkgid(std::string pkgid) {
135 pkgid_ = std::move(pkgid);
139 void RecoveryFile::set_backup_done(bool backup_done) {
140 backup_done_ = backup_done;
143 void RecoveryFile::set_cleanup(bool cleanup) {
147 const boost::filesystem::path& RecoveryFile::unpacked_dir() const {
148 return unpacked_dir_;
151 const std::string& RecoveryFile::pkgid() const {
155 RequestType RecoveryFile::type() const {
159 bool RecoveryFile::backup_done() const {
163 bool RecoveryFile::cleanup() const {
167 bool RecoveryFile::ReadFileContent() {
168 FILE* handle = fopen(path_.c_str(), "r");
170 LOG(ERROR) << "Cannot read recovery file";
173 std::array<char, 200> data;
175 if (!fgets(data.data(), data.size(), handle)) {
176 type_ = RequestType::Unknown;
180 std::string mode(TruncateNewLine(data.data()));
181 auto iter = kStringToRequestMap.find(mode);
182 if (iter == kStringToRequestMap.end()) {
183 type_ = RequestType::Unknown;
185 type_ = iter->second;
188 if (!fgets(data.data(), data.size(), handle)) {
192 unpacked_dir_ = TruncateNewLine(data.data());
193 if (!fgets(data.data(), data.size(), handle)) {
197 pkgid_ = TruncateNewLine(data.data());
198 if (!fgets(data.data(), data.size(), handle)) {
202 std::string backup_flag = TruncateNewLine(data.data());
203 if (backup_flag == "true")
206 backup_done_ = false;
207 if (!fgets(data.data(), data.size(), handle)) {
211 std::string cleanup_flag = TruncateNewLine(data.data());
212 if (cleanup_flag == "cleanup")
220 bool RecoveryFile::WriteAndCommitFileContent() {
221 if (bf::exists(path_)) {
222 bs::error_code error;
223 bf::rename(path_, backup_path_, error);
225 LOG(ERROR) << "Cannot backup recovery file:" << path_ <<
226 ", error: " << error;
231 bi::stream<bi::file_descriptor_sink> ofs(path_);
233 LOG(ERROR) << "Cannot write recovery file";
238 case RequestType::Install:
239 ofs << kRecoveryInstallString << std::endl;
241 case RequestType::Update:
242 ofs << kRecoveryUpdateString << std::endl;
244 case RequestType::Uninstall:
245 ofs << kRecoveryUninstallationString << std::endl;
247 case RequestType::Reinstall:
248 ofs << kRecoveryRdsString << std::endl;
250 case RequestType::Delta:
251 ofs << kRecoveryDeltaString << std::endl;
253 case RequestType::MountInstall:
254 ofs << kRecoveryMountInstallString << std::endl;
256 case RequestType::MountUpdate:
257 ofs << kRecoveryMountUpdateString << std::endl;
259 case RequestType::ReadonlyUpdateInstall:
260 ofs << kRecoveryReadonlyUpdateInstallString << std::endl;
262 case RequestType::ReadonlyUpdateUninstall:
263 ofs << kRecoveryReadonlyUpdateUninstallString << std::endl;
266 ofs << kRecoveryUnknownString << std::endl;
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;
274 ::fsync(ofs->handle());
277 Remove(backup_path_);
281 } // namespace recovery
282 } // namespace common_installer