Implement StepRpkManifestAdjustment
authorJunghyun Yeon <jungh.yeon@samsung.com>
Wed, 12 May 2021 08:22:51 +0000 (17:22 +0900)
committer연정현/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <jungh.yeon@samsung.com>
Fri, 14 May 2021 00:45:24 +0000 (09:45 +0900)
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/rpk/step/pkgmgr/step_rpk_manifest_adjustment.cc [new file with mode: 0644]
src/rpk/step/pkgmgr/step_rpk_manifest_adjustment.h [new file with mode: 0644]

diff --git a/src/rpk/step/pkgmgr/step_rpk_manifest_adjustment.cc b/src/rpk/step/pkgmgr/step_rpk_manifest_adjustment.cc
new file mode 100644 (file)
index 0000000..6792e1f
--- /dev/null
@@ -0,0 +1,110 @@
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include "rpk/step/pkgmgr/step_rpk_manifest_adjustment.h"
+
+#include <pkgmgr-info.h>
+
+#include <boost/filesystem/path.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+#include <libxml/xpathInternals.h>
+#include <string>
+#include <type_traits>
+
+#include "common/installer_context.h"
+
+namespace bs = boost::system;
+namespace bf = boost::filesystem;
+
+using libxml_char = const unsigned char *;
+
+namespace rpk {
+namespace pkgmgr {
+
+common_installer::Step::Status StepRpkManifestAdjustment::precheck() {
+  xml_path_ = context_->GetPkgPath();
+  xml_path_ /= "tizen-manifest.xml";
+  if (!bf::exists(xml_path_)) {
+    LOG(ERROR) << "Could not find manifest";
+    return Step::Status::ERROR;
+  }
+
+  new_path_ = bf::path(getUserManifestPath(context_->uid.get(),
+      context_->is_readonly_package.get())) / bf::path(context_->pkgid.get());
+  new_path_ += ".xml";
+  if (bf::exists(new_path_)) {
+    backup_path_ = new_path_.string() + ".bck";
+    bs::error_code error;
+    bf::copy_file(new_path_, backup_path_, error);
+    if (error)
+      LOG(WARNING) << "Failed to backup original manifest file " << new_path_
+                   << " due to error: " << error;
+  }
+  return Step::Status::OK;
+}
+
+common_installer::Step::Status StepRpkManifestAdjustment::process() {
+  std::unique_ptr<std::remove_pointer<xmlDocPtr>::type,
+      decltype(xmlFreeDoc)*> doc_auto(
+          xmlParseFile(xml_path_.c_str()), xmlFreeDoc);
+  if (doc_auto == nullptr) {
+    LOG(ERROR) << "Could not parse file";
+    return Step::Status::ERROR;
+  }
+
+  xmlNodePtr node = xmlDocGetRootElement(doc_auto.get());
+
+  std::string pkgtype_attrib = "type";
+  auto attrib = xmlSetProp(node,
+      reinterpret_cast<libxml_char>(pkgtype_attrib.c_str()),
+      reinterpret_cast<libxml_char>(context_->manifest_data.get()->type));
+
+  if (attrib == nullptr) {
+    LOG(ERROR) << "Failed to set attribute pkgtype";
+    return Step::Status::ERROR;
+  }
+
+  std::string readonly_attrib = "readonly";
+  attrib = xmlSetProp(node,
+      reinterpret_cast<libxml_char>(readonly_attrib.c_str()),
+      reinterpret_cast<libxml_char>(context_->manifest_data.get()->readonly));
+
+  if (attrib == nullptr) {
+    LOG(ERROR) << "Failed to set attribute readonly";
+    return Step::Status::ERROR;
+  }
+
+  std::string preload_attrib = "preload";
+  attrib = xmlSetProp(node,
+      reinterpret_cast<libxml_char>(preload_attrib.c_str()),
+      reinterpret_cast<libxml_char>(context_->manifest_data.get()->preload));
+
+  if (attrib == nullptr) {
+    LOG(ERROR) << "Failed to set attribute preload";
+    return Step::Status::ERROR;
+  }
+
+  std::string removable_attrib = "removable";
+  attrib = xmlSetProp(node,
+      reinterpret_cast<libxml_char>(removable_attrib.c_str()),
+      reinterpret_cast<libxml_char>(context_->manifest_data.get()->removable));
+
+  if (attrib == nullptr) {
+    LOG(ERROR) << "Failed to set attribute removable";
+    return Step::Status::ERROR;
+  }
+
+  if (xmlSaveFile(new_path_.c_str(), doc_auto.get()) == -1) {
+    LOG(ERROR) << "Failed to modify xml file";
+    return Step::Status::ERROR;
+  }
+
+  return Step::Status::OK;
+}
+
+}  // namespace pkgmgr
+}  // namespace rpk
diff --git a/src/rpk/step/pkgmgr/step_rpk_manifest_adjustment.h b/src/rpk/step/pkgmgr/step_rpk_manifest_adjustment.h
new file mode 100644 (file)
index 0000000..faf3342
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef RPK_STEP_PKGMGR_STEP_RPK_MANIFEST_ADJUSTMENT_H_
+#define RPK_STEP_PKGMGR_STEP_RPK_MANIFEST_ADJUSTMENT_H_
+
+#include <boost/filesystem/path.hpp>
+
+#include <common/step/step.h>
+
+namespace rpk {
+namespace pkgmgr {
+
+class StepRpkManifestAdjustment : public common_installer::Step {
+ public:
+  using Step::Step;
+
+  Status process() override;
+  Status clean() override { return Status::OK; }
+  Status undo() override { return Status::OK; }
+  Status precheck() override;
+
+ private:
+  boost::filesystem::path xml_path_;
+  boost::filesystem::path new_path_;
+  boost::filesystem::path backup_path_;
+
+  STEP_NAME(RpkManifestAdjustment)
+};
+
+}  // namespace pkgmgr
+}  // namespace rpk
+
+#endif  // RPK_STEP_PKGMGR_STEP_RPK_MANIFEST_ADJUSTMENT_H_