Fix static analysis issue
[platform/core/appfw/app-installers.git] / src / common / step / backup / step_backup_icons.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/backup/step_backup_icons.h"
6
7 #include <pkgmgr-info.h>
8
9 #include <boost/filesystem.hpp>
10 #include <boost/system/error_code.hpp>
11
12 #include <string>
13
14 #include "common/utils/paths.h"
15 #include "common/utils/file_util.h"
16 #include "common/utils/glist_range.h"
17
18 namespace bf = boost::filesystem;
19 namespace bs = boost::system;
20
21 namespace common_installer {
22 namespace backup {
23
24 StepBackupIcons::StepBackupIcons(InstallerContext* context) : Step(context),
25     extra_icon_path_(nullptr) {}
26
27 Step::Status StepBackupIcons::process() {
28   extra_icon_path_ = getIconPath(context_->uid.get(),
29       context_->is_readonly_package.get());
30   if (!extra_icon_path_)
31     return Status::OK;
32
33   GetIconInfo();
34
35   if (!BackupIcons())
36     return Status::ICON_ERROR;
37
38   LOG(DEBUG) << "Icons backup created";
39   return Status::OK;
40 }
41
42 Step::Status StepBackupIcons::clean() {
43   RemoveBackupIcons();
44   LOG(DEBUG) << "Icons backup removed";
45
46   return Status::OK;
47 }
48
49 Step::Status StepBackupIcons::undo() {
50   for (auto& pair : icons_) {
51     if (!MoveFile(pair.second, pair.first)) {
52       LOG(ERROR) << "Cannot revert icon from backup: " << pair.first;
53       return Status::ICON_ERROR;
54     }
55   }
56   LOG(DEBUG) << "Icons reverted from backup";
57
58   return Status::OK;
59 }
60
61 bool StepBackupIcons::BackupIcons() {
62   for (auto& pair : icons_) {
63     if (!MoveFile(pair.first, pair.second, true)) {
64       LOG(ERROR) << "Cannot create backup for icon: " << pair.first;
65       return false;
66     }
67   }
68
69   return true;
70 }
71
72 void StepBackupIcons::RemoveBackupIcons() {
73   for (auto& pair : icons_)
74     Remove(pair.second);
75 }
76
77 void StepBackupIcons::GetIconInfo() {
78   for (auto iter = bf::directory_iterator(extra_icon_path_);
79       iter != bf::directory_iterator(); ++iter) {
80     if (!bf::is_regular_file(iter->path()))
81       continue;
82
83     AddAppIconToList(iter->path());
84   }
85 }
86
87 void StepBackupIcons::AddAppIconToList(boost::filesystem::path path) {
88   for (application_x* app : GListRange<application_x*>(
89       context_->old_manifest_data.get()->application)) {
90     if (!app->icon)
91       continue;
92
93     bf::path filename = path.filename();
94     filename.replace_extension();
95     std::string id = filename.string();
96     if (id == app->appid) {
97       bf::path icon_backup = GetBackupPathForIconFile(path);
98       icons_.emplace_back(path, icon_backup);
99     }
100   }
101 }
102
103 }  // namespace backup
104 }  // namespace common_installer