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