#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <string>
+#include <memory>
#include "common/utils/profile_util.h"
#include "common/installer_context.h"
}
common_installer::Step::Status StepManifestAdjustment::process() {
- xmlDocPtr doc = xmlParseFile(xml_path_.c_str());
+ std::unique_ptr<xmlDoc, decltype(xmlFreeDoc)*> doc(
+ xmlParseFile(xml_path_.c_str()), xmlFreeDoc);
- if (doc == nullptr) {
+ if (doc == nullptr || doc.get() == nullptr) {
LOG(ERROR) << "Could not parse file";
return Step::Status::ERROR;
}
- xmlNodePtr node = xmlDocGetRootElement(doc);
+ xmlNodePtr node = xmlDocGetRootElement(doc.get());
+ if (node == nullptr) {
+ LOG(ERROR) << "Could not get root element";
+ return Step::Status::ERROR;
+ }
if (!SetProperty(node, "type", context_->manifest_data.get()->type)) {
- xmlFreeDoc(doc);
return Step::Status::ERROR;
}
if (!SetProperty(node, "readonly",
context_->manifest_data.get()->readonly)) {
- xmlFreeDoc(doc);
return Step::Status::ERROR;
}
if (!SetProperty(node, "preload",
context_->manifest_data.get()->preload)) {
- xmlFreeDoc(doc);
return Step::Status::ERROR;
}
if (!SetProperty(node, "removable",
context_->manifest_data.get()->removable)) {
- xmlFreeDoc(doc);
return Step::Status::ERROR;
}
if (IsNonPrivilegedPackage() && IsAdjustmentNecessary()) {
- xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc);
+ xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(doc.get());
if (!xpath_ctx) {
LOG(ERROR) << "Failed to create XPath context";
- xmlFreeDoc(doc);
return Step::Status::ERROR;
}
!SetManifestElement(&(app->onboot), "false")) {
xmlXPathFreeObject(xpath_obj);
xmlXPathFreeContext(xpath_ctx);
- xmlFreeDoc(doc);
return Step::Status::ERROR;
}
xmlXPathFreeContext(xpath_ctx);
}
- if (xmlSaveFile(xml_path_.c_str(), doc) == -1) {
+ if (xmlSaveFile(xml_path_.c_str(), doc.get()) == -1) {
LOG(ERROR) << "Failed to modify xml file";
- xmlFreeDoc(doc);
return Step::Status::ERROR;
}
- xmlFreeDoc(doc);
-
return Step::Status::OK;
}