Relocate some utility files
[platform/core/appfw/app-installers.git] / src / pkg_recovery / pkg_recovery.cc
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.
4
5 #include <boost/exception/diagnostic_information.hpp>
6 #include <boost/filesystem/operations.hpp>
7 #include <boost/filesystem/path.hpp>
8
9 #include <glib.h>
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>
17
18 #include <regex>
19 #include <string>
20 #include <vector>
21
22 namespace bf = boost::filesystem;
23 namespace ci = common_installer;
24
25 namespace {
26
27 typedef std::pair<std::string, std::string> RecoverEntry;
28
29 const char kRecoveryFilePattern[] = "^(.*)-recovery-(.){6}$";
30 const char kBackupFilePattern[] = "^(.*)-recovery-(.){6}\\.bck$";
31 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
32
33 std::string TruncateNewLine(const char* data) {
34   int length = strlen(data);
35   if (data[length - 1] == '\n')
36       --length;
37   return std::string(data, length);
38 }
39
40 std::vector<std::string> ParseRecoveryFile(const char* file) {
41   FILE* handle = fopen(file, "r");
42   if (!handle) {
43     LOG(ERROR) << "Failed to open recovery file :" << file;
44     return {};
45   }
46
47   std::vector<std::string> arguments;
48   std::array<char, 200> data;
49   data[0] = '\0';
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");
54       break;
55     }
56
57     if (!boost::filesystem::exists(line_data))
58       continue;
59
60     arguments.emplace_back(line_data);
61   }
62   fclose(handle);
63
64   return arguments;
65 }
66
67 class PkgRecoveryService {
68  public:
69   PkgRecoveryService();
70   ~PkgRecoveryService();
71   void Run();
72
73  private:
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);
78 };
79
80 PkgRecoveryService::PkgRecoveryService() {
81 }
82
83 PkgRecoveryService::~PkgRecoveryService() {
84 }
85
86 bool PkgRecoveryService::RunBackend(uid_t uid, const char* type,
87     const char* file) {
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));
95
96     arguments.emplace(arguments.begin(), "-b");
97     arguments.emplace_back("-u");
98     arguments.emplace_back(str_uid.c_str());
99
100     backend.RunWithArgs(arguments);
101   } else {
102     backend.Run("-b", file, "-u", str_uid.c_str());
103   }
104   int status = backend.Wait();
105   if (WIFSIGNALED(status) || WEXITSTATUS(status))
106     return false;
107
108   ci::Remove(bf::path(file));
109
110   return true;
111 }
112
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);
119
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);
128   }
129 }
130
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();
135       ++iter) {
136     try {
137       std::string file = iter->path().filename().string();
138       std::regex backup_regex(kBackupFilePattern);
139       std::smatch match;
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);
145       }
146     } catch (...) {
147       LOG(WARNING) << "Exception occurred: "
148                    << boost::current_exception_diagnostic_information();
149       continue;
150     }
151   }
152 }
153
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();
160       ++iter) {
161     try {
162       std::string file = iter->path().filename().string();
163       std::regex recovery_regex(kRecoveryFilePattern);
164       std::smatch match;
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());
170         else
171           list.emplace_back(type, iter->path().string());
172       }
173     } catch (...) {
174       LOG(WARNING) << "Exception occurred: "
175                    << boost::current_exception_diagnostic_information();
176       continue;
177     }
178   }
179
180   return list;
181 }
182
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))
190       continue;
191
192     if (!RunBackend(uid, type, file))
193       LOG(ERROR) << "Recovery process for " << file << " failed";
194   }
195 }
196
197 }  // namespace
198
199 int main() {
200   PkgRecoveryService service;
201   service.Run();
202   return 0;
203 }