1 // Copyright (c) 2020 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/global_recovery_file.h"
7 #include <manifest_parser/utils/logging.h>
13 #include <system_error>
16 #include "common/utils/file_util.h"
17 #include "common/pkgmgr_interface.h"
19 namespace ci = common_installer;
20 namespace fs = std::filesystem;
24 const char kGlobalTypeName[] = "unified";
26 fs::path Backup(const fs::path& filepath) {
27 fs::path backup_path(filepath);
28 backup_path += ".bck";
30 if (!ci::MoveFile(filepath, backup_path) ||
31 !ci::CopyFile(backup_path, filepath)) {
32 LOG(ERROR) << "Failed to backup";
41 namespace common_installer {
43 GlobalRecoveryFile::GlobalRecoveryFile(
44 std::shared_ptr<PkgMgrInterface> pkgmgr) : pkgmgr_(pkgmgr) {
47 GlobalRecoveryFile::~GlobalRecoveryFile() {
49 if (fs::exists(recovery_filepath_, ec))
50 ci::Remove(recovery_filepath_);
51 if (fs::exists(backup_path_, ec))
52 ci::Remove(backup_path_);
56 bool GlobalRecoveryFile::Init() {
57 std::filesystem::path root_path = ci::GetRootAppPath(
58 pkgmgr_->GetIsPreloadRequest(), pkgmgr_->GetUid());
59 recovery_filepath_ = GenerateRecoveryFilePath(root_path, kGlobalTypeName);
60 if (recovery_filepath_.empty())
66 std::string GlobalRecoveryFile::AddPathWithType(
67 const std::string& pkg_type) {
68 std::filesystem::path root_path = ci::GetRootAppPath(
69 pkgmgr_->GetIsPreloadRequest(), pkgmgr_->GetUid());
70 fs::path recovery_filepath = GenerateRecoveryFilePath(root_path, pkg_type);
71 if (!AppendString(recovery_filepath.string()))
74 recovery_list_.emplace_back(recovery_filepath.string());
75 return recovery_filepath.string();
78 bool GlobalRecoveryFile::AppendPath(
79 const fs::path& additional_path) {
80 return AppendString(additional_path.string());
83 bool GlobalRecoveryFile::AppendCleanUp() {
84 return AppendString("cleanup");
87 bool GlobalRecoveryFile::AppendString(const std::string& val) {
88 if (recovery_filepath_.empty())
91 if (fs::exists(recovery_filepath_)) {
92 backup_path_ = Backup(recovery_filepath_);
93 if (backup_path_.empty()) {
94 LOG(ERROR) << "Failed to backup";
99 FILE* handle = fopen(recovery_filepath_.c_str(), "a");
101 LOG(ERROR) << "Failed to open file :" << recovery_filepath_;
105 fputs(val.c_str(), handle);
110 ci::Remove(backup_path_);
114 fs::path GlobalRecoveryFile::GenerateRecoveryFilePath(
115 const fs::path& path, const std::string& type) {
116 fs::path pattern = path;
119 pattern += "-recovery-%%%%%%";
122 std::vector<std::string>::iterator iter;
124 tmp_path = ci::GenerateUniquePathString(pattern);
125 iter = std::find(recovery_list_.begin(), recovery_list_.end(),
127 } while (std::filesystem::exists(tmp_path) ||
128 iter != recovery_list_.end());
133 } // namespace common_installer