Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_recover_icons.cc
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.
4
5 #include "common/step/filesystem/step_recover_icons.h"
6
7 #include <pkgmgr-info.h>
8
9 #include <filesystem>
10
11 #include "common/utils/paths.h"
12 #include "common/utils/file_util.h"
13 #include "common/utils/glist_range.h"
14
15 namespace fs = std::filesystem;
16
17 namespace common_installer {
18 namespace filesystem {
19
20 Step::Status StepRecoverIcons::RecoveryNew() {
21   if (!TryGatherIcons()) {
22     LOG(DEBUG) << "Icons recovery not needed";
23     return Status::OK;
24   }
25   for (auto& pair : icons_) {
26     Remove(pair.first);
27   }
28   LOG(INFO) << "Icons recovery done";
29   return Status::OK;
30 }
31
32 Step::Status StepRecoverIcons::RecoveryUpdate() {
33   if (!TryGatherIcons()) {
34     LOG(DEBUG) << "Icons recovery not needed";
35     return Status::OK;
36   }
37   for (auto& pair : icons_) {
38     if (fs::exists(pair.second)) {
39       if (!Remove(pair.first)) {
40         LOG(ERROR) << "Cannot move icon to restore its correct location";
41         return Status::RECOVERY_ERROR;
42       }
43     }
44     (void) MoveFile(pair.second, pair.first);
45   }
46   LOG(INFO) << "Icons recovery done";
47   return Status::OK;
48 }
49
50 bool StepRecoverIcons::TryGatherIcons() {
51   if (!context_->manifest_data.get())
52     return false;
53
54   // gather icon info
55   const char* extra_icon_path = getIconPath(context_->uid.get(),
56       context_->is_readonly_package.get());
57   if (!extra_icon_path)
58     return true;
59   for (auto iter = fs::directory_iterator(extra_icon_path);
60       iter != fs::directory_iterator(); ++iter) {
61     if (!fs::is_regular_file(iter->path()))
62       continue;
63     for (application_x* app : GListRange<application_x*>(
64         context_->manifest_data.get()->application)) {
65       if (app->icon) {
66         fs::path p = iter->path();
67         std::string filename = iter->path().filename().string();
68         if (filename.find(app->appid) == 0) {
69           fs::path icon;
70           fs::path icon_backup;
71           if (p.extension() == GetIconFileBackupExtension()) {
72             icon_backup = p;
73             icon = p.replace_extension("");
74           } else {
75             icon = p;
76             icon_backup = GetBackupPathForIconFile(iter->path());
77           }
78           icons_.insert(std::make_pair(icon, icon_backup));
79         }
80       }
81     }
82   }
83   return true;
84 }
85
86 }  // namespace filesystem
87 }  // namespace common_installer