1 // Copyright (c) 2017 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 <boost/exception/diagnostic_information.hpp>
6 #include <boost/filesystem/operations.hpp>
7 #include <boost/filesystem/path.hpp>
10 #include <common/utils/file_util.h>
11 #include <common/utils/request.h>
12 #include <common/utils/subprocess.h>
13 #include <common/utils/user_util.h>
14 #include <manifest_parser/utils/logging.h>
15 #include <sys/types.h>
16 #include <tzplatform_config.h>
22 namespace bf = boost::filesystem;
23 namespace ci = common_installer;
27 typedef std::pair<std::string, std::string> RecoverEntry;
29 const char kRecoveryFilePattern[] = "^(.*)-recovery-(.){6}$";
30 const char kBackupFilePattern[] = "^(.*)-recovery-(.){6}\\.bck$";
31 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
33 std::string TruncateNewLine(const char* data) {
34 int length = strlen(data);
35 if (data[length - 1] == '\n')
37 return std::string(data, length);
40 std::vector<std::string> ParseRecoveryFile(const char* file) {
41 FILE* handle = fopen(file, "r");
43 LOG(ERROR) << "Failed to open recovery file :" << file;
47 std::vector<std::string> arguments;
48 std::array<char, 200> data;
50 while (fgets(data.data(), data.size(), handle)) {
51 std::string line_data = TruncateNewLine(data.data());
52 if (line_data == "cleanup") {
53 arguments.emplace_back("--recovery-cleanup");
57 if (!boost::filesystem::exists(line_data))
60 arguments.emplace_back(line_data);
67 class PkgRecoveryService {
70 ~PkgRecoveryService();
74 void SearchBackupFiles(uid_t uid);
75 std::vector<RecoverEntry> SearchRecoveryFiles(uid_t uid);
76 void ProcessRecovery(uid_t uid, const std::vector<RecoverEntry>& entries);
77 bool RunBackend(uid_t uid, const char* type, const char* file);
80 PkgRecoveryService::PkgRecoveryService() {
83 PkgRecoveryService::~PkgRecoveryService() {
86 bool PkgRecoveryService::RunBackend(uid_t uid, const char* type,
88 std::string backend_cmd = "/usr/bin/" + std::string(type) + "-backend";
89 ci::Subprocess backend(backend_cmd);
90 std::string str_uid = std::to_string(uid);
91 if (std::string(type) == "unified") {
92 auto arguments = ParseRecoveryFile(file);
93 if (!arguments.size())
94 return ci::Remove(bf::path(file));
96 arguments.emplace(arguments.begin(), "-b");
97 arguments.emplace_back("-u");
98 arguments.emplace_back(str_uid.c_str());
100 backend.RunWithArgs(arguments);
102 backend.Run("-b", file, "-u", str_uid.c_str());
104 int status = backend.Wait();
105 if (WIFSIGNALED(status) || WEXITSTATUS(status))
108 ci::Remove(bf::path(file));
113 void PkgRecoveryService::Run() {
114 // recover global packages
115 SearchBackupFiles(kGlobalUserUid);
116 LOG(INFO) << "Searching recovery files for user " << kGlobalUserUid;
117 std::vector<RecoverEntry> globalentries = SearchRecoveryFiles(kGlobalUserUid);
118 ProcessRecovery(kGlobalUserUid, globalentries);
120 // recover normal user packages
121 ci::UserList list = ci::GetUserList();
122 for (auto userinfo : list) {
123 uid_t uid = std::get<0>(userinfo);
124 LOG(INFO) << "Searching recovery files for user " << std::get<0>(userinfo);
125 SearchBackupFiles(uid);
126 std::vector<RecoverEntry> entries = SearchRecoveryFiles(uid);
127 ProcessRecovery(uid, entries);
131 void PkgRecoveryService::SearchBackupFiles(uid_t uid) {
132 const bf::path recovery_dir = ci::GetRootAppPath(false, uid);
133 for (bf::directory_iterator iter(recovery_dir);
134 iter != bf::directory_iterator();
137 std::string file = iter->path().filename().string();
138 std::regex backup_regex(kBackupFilePattern);
140 if (std::regex_search(file, match, backup_regex)) {
141 bf::path orig_file(iter->path().parent_path() / iter->path().stem());
142 if (bf::exists(orig_file))
143 bf::remove(orig_file);
144 bf::rename(iter->path(), orig_file);
147 LOG(WARNING) << "Exception occurred: "
148 << boost::current_exception_diagnostic_information();
154 std::vector<RecoverEntry> PkgRecoveryService::SearchRecoveryFiles(uid_t uid) {
155 std::vector<RecoverEntry> list;
156 const bf::path recovery_dir = ci::GetRootAppPath(false, uid);
157 LOG(INFO) << "RootAppPath: " << recovery_dir;
158 for (bf::directory_iterator iter(recovery_dir);
159 iter != bf::directory_iterator();
162 std::string file = iter->path().filename().string();
163 std::regex recovery_regex(kRecoveryFilePattern);
165 if (std::regex_search(file, match, recovery_regex)) {
166 LOG(INFO) << "Found recovery file: " << file;
167 std::string type(match[1]);
168 if (type == "unified")
169 list.emplace(list.begin(), type, iter->path().string());
171 list.emplace_back(type, iter->path().string());
174 LOG(WARNING) << "Exception occurred: "
175 << boost::current_exception_diagnostic_information();
183 void PkgRecoveryService::ProcessRecovery(uid_t uid,
184 const std::vector<RecoverEntry>& entries) {
185 LOG(INFO) << "Process recovery for user " << uid;
186 for (auto entry : entries) {
187 const char* type = entry.first.c_str();
188 const char* file = entry.second.c_str();
189 if (!bf::exists(file))
192 if (!RunBackend(uid, type, file))
193 LOG(ERROR) << "Recovery process for " << file << " failed";
200 PkgRecoveryService service;