check null value of extra icon path.
[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/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     bs::error_code error;
28     bf::remove(pair.first, error);
29   }
30   LOG(INFO) << "Icons recovery done";
31   return Status::OK;
32 }
33
34 Step::Status StepRecoverIcons::RecoveryUpdate() {
35   if (!TryGatherIcons()) {
36     LOG(DEBUG) << "Icons recovery not needed";
37     return Status::OK;
38   }
39   for (auto& pair : icons_) {
40     if (bf::exists(pair.first)) {
41       bs::error_code error;
42       bf::remove(pair.first, error);
43       if (error) {
44         LOG(ERROR) << "Cannot move icon to restore its correct location";
45         return Status::RECOVERY_ERROR;
46       }
47     }
48     (void) MoveFile(pair.second, pair.first);
49   }
50   LOG(INFO) << "Icons recovery done";
51   return Status::OK;
52 }
53
54 bool StepRecoverIcons::TryGatherIcons() {
55   if (!context_->manifest_data.get())
56     return false;
57
58   // gather icon info
59   const char* extra_icon_path = getIconPath(context_->uid.get(),
60       context_->is_preload_request.get());
61   if (!extra_icon_path)
62     return true;
63   for (auto iter = bf::directory_iterator(extra_icon_path);
64       iter != bf::directory_iterator(); ++iter) {
65     if (!bf::is_regular_file(iter->path()))
66       continue;
67     for (application_x* app : GListRange<application_x*>(
68         context_->manifest_data.get()->application)) {
69       if (app->icon) {
70         bf::path p = iter->path();
71         std::string filename = iter->path().filename().string();
72         if (filename.find(app->appid) == 0) {
73           bf::path icon;
74           bf::path icon_backup;
75           if (p.extension() == GetIconFileBackupExtension()) {
76             icon_backup = p;
77             icon = p.replace_extension("");
78           } else {
79             icon = p;
80             icon_backup = GetBackupPathForIconFile(iter->path());
81           }
82           icons_.insert(std::make_pair(icon, icon_backup));
83         }
84       }
85     }
86   }
87   return true;
88 }
89
90 }  // namespace filesystem
91 }  // namespace common_installer
92