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 kRecoveryUnknownString[] = "UNKNOWN";
40 const std::map<std::string, ci::RequestType> kStringToRequestMap = {
41 {kRecoveryInstallString, ci::RequestType::Install},
42 {kRecoveryUpdateString, ci::RequestType::Update},
43 {kRecoveryUninstallationString, ci::RequestType::Uninstall},
44 {kRecoveryRdsString, ci::RequestType::Reinstall},
45 {kRecoveryDeltaString, ci::RequestType::Delta},
46 {kRecoveryMountInstallString, ci::RequestType::MountInstall},
47 {kRecoveryMountUpdateString, ci::RequestType::MountUpdate},
48 {kRecoveryReadonlyUpdateInstallString,
49 ci::RequestType::ReadonlyUpdateInstall},
52 std::string TruncateNewLine(const char* data) {
53 int length = strlen(data);
54 if (data[length - 1] == '\n')
56 return std::string(data, length);
61 namespace common_installer {
64 std::unique_ptr<RecoveryFile> RecoveryFile::CreateRecoveryFile(
65 const boost::filesystem::path& path, RequestType type) {
66 if (bf::exists(path)) {
67 LOG(ERROR) << "Recovery file already exists!";
70 std::unique_ptr<RecoveryFile> file(new RecoveryFile(path, type, false));
71 if (file->is_detached()) {
72 LOG(ERROR) << "Failed to access file";
78 std::unique_ptr<RecoveryFile> RecoveryFile::OpenRecoveryFile(
79 const boost::filesystem::path& path) {
80 if (!bf::exists(path)) {
81 LOG(ERROR) << "Cannot open recovery file";
84 std::unique_ptr<RecoveryFile> file(new RecoveryFile(path,
85 RequestType::Unknown, true));
86 if (file->is_detached()) {
87 LOG(ERROR) << "Failed to read recovery file";
93 RecoveryFile::RecoveryFile(const bf::path& path, RequestType type, bool load)
94 : type_(type), path_(path), backup_done_(false), cleanup_(false) {
95 backup_path_ = path_.string() + ".bck";
97 if (!ReadFileContent()) {
103 if (!WriteAndCommitFileContent()) {
107 LOG(DEBUG) << "Recovery file " << path_ << " created";
111 RecoveryFile::~RecoveryFile() {
113 LOG(DEBUG) << "Recovery file " << path_ << " removed";
114 if (Remove(backup_path_))
115 LOG(DEBUG) << "Recovery file " << backup_path_ << " removed";
118 void RecoveryFile::Detach() {
122 bool RecoveryFile::is_detached() const {
123 return path_.empty();
126 void RecoveryFile::set_unpacked_dir(
127 boost::filesystem::path unpacked_dir) {
128 unpacked_dir_ = std::move(unpacked_dir);
131 void RecoveryFile::set_pkgid(std::string pkgid) {
132 pkgid_ = std::move(pkgid);
136 void RecoveryFile::set_backup_done(bool backup_done) {
137 backup_done_ = backup_done;
140 void RecoveryFile::set_cleanup(bool cleanup) {
144 const boost::filesystem::path& RecoveryFile::unpacked_dir() const {
145 return unpacked_dir_;
148 const std::string& RecoveryFile::pkgid() const {
152 RequestType RecoveryFile::type() const {
156 bool RecoveryFile::backup_done() const {
160 bool RecoveryFile::cleanup() const {
164 bool RecoveryFile::ReadFileContent() {
165 FILE* handle = fopen(path_.c_str(), "r");
167 LOG(ERROR) << "Cannot read recovery file";
170 std::array<char, 200> data;
172 if (!fgets(data.data(), data.size(), handle)) {
173 type_ = RequestType::Unknown;
177 std::string mode(TruncateNewLine(data.data()));
178 auto iter = kStringToRequestMap.find(mode);
179 if (iter == kStringToRequestMap.end()) {
180 type_ = RequestType::Unknown;
182 type_ = iter->second;
185 if (!fgets(data.data(), data.size(), handle)) {
189 unpacked_dir_ = TruncateNewLine(data.data());
190 if (!fgets(data.data(), data.size(), handle)) {
194 pkgid_ = TruncateNewLine(data.data());
195 if (!fgets(data.data(), data.size(), handle)) {
199 std::string backup_flag = TruncateNewLine(data.data());
200 if (backup_flag == "true")
203 backup_done_ = false;
204 if (!fgets(data.data(), data.size(), handle)) {
208 std::string cleanup_flag = TruncateNewLine(data.data());
209 if (cleanup_flag == "cleanup")
217 bool RecoveryFile::WriteAndCommitFileContent() {
218 if (bf::exists(path_)) {
219 bs::error_code error;
220 bf::rename(path_, backup_path_, error);
222 LOG(ERROR) << "Cannot backup recovery file:" << path_ <<
223 ", error: " << error;
228 bi::stream<bi::file_descriptor_sink> ofs(path_);
230 LOG(ERROR) << "Cannot write recovery file";
235 case RequestType::Install:
236 ofs << kRecoveryInstallString << std::endl;
238 case RequestType::Update:
239 ofs << kRecoveryUpdateString << std::endl;
241 case RequestType::Uninstall:
242 ofs << kRecoveryUninstallationString << std::endl;
244 case RequestType::Reinstall:
245 ofs << kRecoveryRdsString << std::endl;
247 case RequestType::Delta:
248 ofs << kRecoveryDeltaString << std::endl;
250 case RequestType::MountInstall:
251 ofs << kRecoveryMountInstallString << std::endl;
253 case RequestType::MountUpdate:
254 ofs << kRecoveryMountUpdateString << std::endl;
256 case RequestType::ReadonlyUpdateInstall:
257 ofs << kRecoveryReadonlyUpdateInstallString << std::endl;
260 ofs << kRecoveryUnknownString << std::endl;
263 ofs << unpacked_dir_.c_str() << std::endl;
264 ofs << pkgid_ << std::endl;
265 ofs << (backup_done_ ? "true" : "false") << std::endl;
266 ofs << (cleanup_ ? "cleanup" : "rollback") << std::endl;
268 ::fsync(ofs->handle());
271 Remove(backup_path_);
275 } // namespace recovery
276 } // namespace common_installer