From: Tomasz Iwanek Date: Wed, 28 Oct 2015 08:28:38 +0000 (+0100) Subject: Delta info file parser X-Git-Tag: submit/tizen/20151104.130422^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d9e7bb4f06ea409555b6cd4df29d45c75d5cf12c;p=platform%2Fcore%2Fappfw%2Fmanifest-parser.git Delta info file parser Change-Id: Ia2b22ecdc6becdc1435412cdc25edad3ce16b9f0 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index d495555..780ed50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ SET(CMAKE_CXX_FLAGS_CCOV "-O0 -std=c++11 -g --coverage") # Targets SET(TARGET_LIBNAME_MANIFEST_HANDLERS "manifest-handlers") SET(TARGET_LIBNAME_TPK_MANIFEST_HANDLERS "tpk-manifest-handlers") +SET(TARGET_LIBNAME_DELTA_MANIFEST_HANDLERS "delta-manifest-handlers") SET(TARGET_LIBNAME_PARSER "manifest-parser") SET(TARGET_LIBNAME_UTILS "manifest-parser-utils") SET(TARGET_EXAMPLES "parser-example") diff --git a/packaging/manifest-parser.spec b/packaging/manifest-parser.spec index e79728c..078fb3b 100644 --- a/packaging/manifest-parser.spec +++ b/packaging/manifest-parser.spec @@ -75,6 +75,7 @@ make %{?_smp_mflags} %{_libdir}/libmanifest-parser-utils.so* %{_libdir}/libmanifest-handlers.so* %{_libdir}/libtpk-manifest-handlers.so* +%{_libdir}/libdelta-manifest-handlers.so* %license LICENSE LICENSE-xwalk %files devel diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f9963f5..0e25727 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,3 +4,4 @@ ADD_SUBDIRECTORY(tpk_manifest_handlers) ADD_SUBDIRECTORY(unit_tests) ADD_SUBDIRECTORY(utils) ADD_SUBDIRECTORY(examples) +ADD_SUBDIRECTORY(delta) diff --git a/src/delta/CMakeLists.txt b/src/delta/CMakeLists.txt new file mode 100644 index 0000000..5b8f0c7 --- /dev/null +++ b/src/delta/CMakeLists.txt @@ -0,0 +1,26 @@ +# Target - sources +SET(SRCS + delta_handler.cc + delta_parser.cc +) +# Target - definition +ADD_LIBRARY(${TARGET_LIBNAME_DELTA_MANIFEST_HANDLERS} SHARED ${SRCS}) +# Target - includes +TARGET_INCLUDE_DIRECTORIES(${TARGET_LIBNAME_DELTA_MANIFEST_HANDLERS} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../") + +# Extra +SET_TARGET_PROPERTIES(${TARGET_LIBNAME_MANIFEST_HANDLERS} PROPERTIES VERSION ${VERSION}) +SET_TARGET_PROPERTIES(${TARGET_LIBNAME_MANIFEST_HANDLERS} PROPERTIES SOVERSION ${VERSION_MAJOR}) + +# Install +INSTALL(TARGETS ${TARGET_LIBNAME_DELTA_MANIFEST_HANDLERS} DESTINATION ${LIB_INSTALL_DIR}) + +# Install includes +INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION ${INCLUDEDIR} + FILES_MATCHING PATTERN "*.h") + +# Configure and install pkgconfig file +SET(PKG_FILE delta-manifest-handlers.pc) +CONFIGURE_FILE(${PKG_FILE}.in ${PKG_FILE} @ONLY) +INSTALL(FILES ${PKG_FILE} DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) diff --git a/src/delta/delta-manifest-handlers.pc.in b/src/delta/delta-manifest-handlers.pc.in new file mode 100644 index 0000000..c070202 --- /dev/null +++ b/src/delta/delta-manifest-handlers.pc.in @@ -0,0 +1,11 @@ +prefix=@PREFIX@ +exec_prefix=@PREFIX@ +libdir=@LIBDIR@ +includedir=@INCLUDEDIR@ + +Name: delta-manifest-handlers +Description: Delta manifest handlers for manifest parser +Version: @VERSION@ +Requires: manifest-parser +Libs: -L${libdir} -ldelta-manifest-handlers +Cflags: -I${includedir} diff --git a/src/delta/delta_handler.cc b/src/delta/delta_handler.cc new file mode 100644 index 0000000..6617d62 --- /dev/null +++ b/src/delta/delta_handler.cc @@ -0,0 +1,75 @@ +// 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 "delta/delta_handler.h" + +namespace { + +const char kAddFilesKey[] = "add-files"; +const char kModifyFilesKey[] = "modify-files"; +const char kRemoveFilesKey[] = "remove-files"; +const char kFileNameKey[] = "@name"; +const char kFileKey[] = "file"; + +bool GetFilesForKey(const parser::DictionaryValue* dict, const std::string& key, + std::vector* files, std::string* error) { + for (const auto& item : parser::GetOneOrMany(dict, key, "")) { + for (const auto& file : parser::GetOneOrMany(item, kFileKey, "")) { + std::string position; + if (!file->GetString(kFileNameKey, &position)) { + *error = std::string("Malformed tag: ") + key; + return false; + } + files->push_back(position); + } + } + return true; +} + +} // namespace + +namespace delta { + +const char kDeltaInfoKey[] = "delta"; + +bool DeltaHandler::Parse( + const parser::Manifest& manifest, + std::shared_ptr* output, + std::string* error) { + parser::Value* value = nullptr; + if (!manifest.Get(kDeltaInfoKey, &value)) { + *error = "Cannot find root element "; + return false; + } + const parser::DictionaryValue* dict = nullptr; + if (!value->GetAsDictionary(&dict)) { + *error = "Root element malformed"; + return false; + } + std::shared_ptr delta_info(new DeltaInfo()); + + std::vector added; + if (!GetFilesForKey(dict, kAddFilesKey, &added, error)) + return false; + delta_info->set_added(added); + + std::vector modified; + if (!GetFilesForKey(dict, kModifyFilesKey, &modified, error)) + return false; + delta_info->set_modified(modified); + + std::vector removed; + if (!GetFilesForKey(dict, kRemoveFilesKey, &removed, error)) + return false; + delta_info->set_removed(removed); + + *output = std::static_pointer_cast(delta_info); + return true; +} + +std::string DeltaHandler::Key() const { + return kDeltaInfoKey; +} + +} // namespace delta diff --git a/src/delta/delta_handler.h b/src/delta/delta_handler.h new file mode 100644 index 0000000..96e1c7a --- /dev/null +++ b/src/delta/delta_handler.h @@ -0,0 +1,60 @@ +// 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 DELTA_DELTA_HANDLER_H_ +#define DELTA_DELTA_HANDLER_H_ + +#include +#include + +#include "manifest_parser/manifest_handler.h" + +namespace delta { + +extern const char kDeltaInfoKey[]; + +class DeltaInfo : public parser::ManifestData { + public: + void set_added(const std::vector& added) { + added_ = added; + } + const std::vector& added() const { + return added_; + } + void set_modified(const std::vector& modified) { + modified_ = modified; + } + const std::vector& modified() const { + return modified_; + } + void set_removed(const std::vector& removed) { + removed_ = removed; + } + const std::vector& removed() const { + return removed_; + } + + private: + std::vector added_; + std::vector modified_; + std::vector removed_; +}; + +/** + * @brief The DeltaHandler class + * + * Main handler for all tags in delta file + */ +class DeltaHandler : public parser::ManifestHandler { + public: + bool Parse( + const parser::Manifest& manifest, + std::shared_ptr* output, + std::string* error) override; + std::string Key() const override; +}; + +} // namespace delta + +#endif // DELTA_DELTA_HANDLER_H_ diff --git a/src/delta/delta_parser.cc b/src/delta/delta_parser.cc new file mode 100644 index 0000000..66e2c71 --- /dev/null +++ b/src/delta/delta_parser.cc @@ -0,0 +1,43 @@ +// 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 "delta/delta_parser.h" + +#include + +#include "delta/delta_handler.h" +#include "manifest_parser/manifest_handler.h" + +namespace delta { + +DeltaParser::DeltaParser() { + std::vector handlers = { + new DeltaHandler, + }; + + std::unique_ptr registry( + new parser::ManifestHandlerRegistry(handlers)); + + parser_.reset(new parser::ManifestParser(std::move(registry))); +} + +std::shared_ptr DeltaParser::GetManifestData( + const std::string& key) { + return parser_->GetManifestData(key); +} + +const std::string& DeltaParser::GetErrorMessage() const { + if (!error_.empty()) + return error_; + return parser_->GetErrorMessage(); +} + +bool DeltaParser::ParseManifest(const boost::filesystem::path& path) { + if (!parser_->ParseManifest(path)) + return false; + + return true; +} + +} // namespace delta diff --git a/src/delta/delta_parser.h b/src/delta/delta_parser.h new file mode 100644 index 0000000..1e520b9 --- /dev/null +++ b/src/delta/delta_parser.h @@ -0,0 +1,49 @@ +// 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 DELTA_DELTA_PARSER_H_ +#define DELTA_DELTA_PARSER_H_ + +#include + +#include +#include + +#include "manifest_parser/manifest_parser.h" + +namespace delta { + +/** + * @brief The DeltaParser class + * Parser class of delta info file. + * + * Instance of this class may be used to parse delta file. + * Depending on boolean result of @ref ParseManifest method, client code may + * call: + * - on success -> @ref GetManifestData(), passing the key of ManifestData + * instance that it is interested in. + * - on failure -> @ref GetErrorMessage(), to get value of error which was set + * during the processing of config.xml + * + * To investigate which key do you need to get certain parsed piece of data, + * check the key reported by handler's @ref ManifestHandler::Key() method. + * Key returned by this method is the key to access data set by handler. + */ +class DeltaParser { + public: + DeltaParser(); + + std::shared_ptr GetManifestData( + const std::string& key); + const std::string& GetErrorMessage() const; + bool ParseManifest(const boost::filesystem::path& path); + + private: + std::unique_ptr parser_; + std::string error_; +}; + +} // namespace delta + +#endif // DELTA_DELTA_PARSER_H_