[Recovery] StepRecoverIcons 05/45705/4
authorTomasz Iwanek <t.iwanek@samsung.com>
Mon, 3 Aug 2015 14:11:30 +0000 (16:11 +0200)
committerPawel Sikorski <p.sikorski@samsung.com>
Tue, 11 Aug 2015 08:30:51 +0000 (01:30 -0700)
For recovering new installation, any existing icons are removed.
For recovering update installation, all icons are moved to
original locations.

Change-Id: I80e34748a4e753450d8a4cbdf8f81dbbeb5b44ae

src/common/CMakeLists.txt
src/common/step/step_recover_icons.cc [new file with mode: 0644]
src/common/step/step_recover_icons.h [new file with mode: 0644]

index 4c382df..2bd05c1 100644 (file)
@@ -19,6 +19,7 @@ SET(SRCS
   step/step_copy_storage_directories.cc
   step/step_create_storage_directories.cc
   step/step_generate_xml.cc
+  step/step_recover_icons.cc
   step/step_recover_manifest.cc
   step/step_recovery.cc
   step/step_register_app.cc
diff --git a/src/common/step/step_recover_icons.cc b/src/common/step/step_recover_icons.cc
new file mode 100644 (file)
index 0000000..34ace90
--- /dev/null
@@ -0,0 +1,73 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#include "common/step/step_recover_icons.h"
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/system/error_code.hpp>
+#include <pkgmgr-info.h>
+
+#include "common/utils/clist_helpers.h"
+#include "common/utils/file_util.h"
+
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
+
+namespace common_installer {
+namespace filesystem {
+
+Step::Status StepRecoverIcons::RecoveryNew() {
+  if (!TryGatherIcons()) {
+    LOG(DEBUG) << "Icons recovery not needed";
+    return Status::OK;
+  }
+  for (auto& pair : icons_) {
+    bs::error_code error;
+    bf::remove(pair.first, error);
+  }
+  LOG(INFO) << "Icons recovery done";
+  return Status::OK;
+}
+
+Step::Status StepRecoverIcons::RecoveryUpdate() {
+  if (!TryGatherIcons()) {
+    LOG(DEBUG) << "Icons recovery not needed";
+    return Status::OK;
+  }
+  for (auto& pair : icons_) {
+    if (bf::exists(pair.first)) {
+      bs::error_code error;
+      bf::remove(pair.first, error);
+      if (error) {
+        LOG(ERROR) << "Cannot move icon to restore its correct location";
+        return Status::ERROR;
+      }
+    }
+    (void) MoveFile(pair.second, pair.first);
+  }
+  LOG(INFO) << "Icons recovery done";
+  return Status::OK;
+}
+
+bool StepRecoverIcons::TryGatherIcons() {
+  if (!context_->manifest_data.get())
+    return false;
+  uiapplication_x* ui = nullptr;
+  PKGMGR_LIST_MOVE_NODE_TO_HEAD(context_->manifest_data.get()->uiapplication,
+                                ui);
+  for (; ui != nullptr; ui = ui->next) {
+    bf::path app_icon = bf::path(getIconPath(context_->uid.get()))
+      / bf::path(ui->appid);
+    app_icon += ".png";
+    bf::path icon_backup = app_icon;
+    icon_backup += ".bck";
+    if (bf::exists(icon_backup) || bf::exists(app_icon))
+        icons_.emplace_back(app_icon, icon_backup);
+  }
+  return true;
+}
+
+}  // namespace filesystem
+}  // namespace common_installer
+
diff --git a/src/common/step/step_recover_icons.h b/src/common/step/step_recover_icons.h
new file mode 100644 (file)
index 0000000..d003584
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef COMMON_STEP_STEP_RECOVER_ICONS_H_
+#define COMMON_STEP_STEP_RECOVER_ICONS_H_
+
+#include <boost/filesystem/path.hpp>
+#include <utility>
+#include <vector>
+
+#include "common/context_installer.h"
+#include "common/step/step_recovery.h"
+#include "common/utils/logging.h"
+
+namespace common_installer {
+namespace filesystem {
+
+/**
+ * @brief The StepRecoverIcons class
+ *        Fixes state of platform icon files in recovery mode.
+ *
+ * For recovery of new installation, all icons files are removed.
+ * For recovery of update installation, all icons of applications of package are
+ * restored to its previous locations.
+ */
+class StepRecoverIcons : public recovery::StepRecovery {
+ public:
+  using StepRecovery::StepRecovery;
+
+  Status RecoveryNew() override;
+  Status RecoveryUpdate() override;
+
+ private:
+  bool TryGatherIcons();
+
+  std::vector<std::pair<boost::filesystem::path, boost::filesystem::path>>
+      icons_;
+
+  SCOPE_LOG_TAG(RecoverIcons)
+};
+
+}  // namespace filesystem
+}  // namespace common_installer
+
+#endif  // COMMON_STEP_STEP_RECOVER_ICONS_H_