reader.hpp
Go to the documentation of this file.
#ifndef OSMIUM_IO_READER_HPP
#define OSMIUM_IO_READER_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cerrno>
#include <cstdlib>
#include <fcntl.h>
#include <future>
#include <memory>
#include <string>
#include <system_error>
#include <thread>
#include <utility>
#ifndef _WIN32
# include <sys/wait.h>
#endif
#ifndef _MSC_VER
# include <unistd.h>
#endif
#include <osmium/io/compression.hpp>
#include <osmium/io/detail/input_format.hpp>
#include <osmium/io/detail/read_thread.hpp>
#include <osmium/io/detail/read_write.hpp>
#include <osmium/io/detail/queue_util.hpp>
#include <osmium/io/error.hpp>
#include <osmium/io/file.hpp>
#include <osmium/io/header.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm/entity_bits.hpp>
#include <osmium/thread/util.hpp>
namespace osmium {
namespace io {
class Reader {
static constexpr size_t max_input_queue_size = 20; // XXX
static constexpr size_t max_osmdata_queue_size = 20; // XXX
osmium::io::File m_file;
osmium::osm_entity_bits::type m_read_which_entities;
enum class status {
okay = 0, // normal reading
error = 1, // some error occurred while reading
closed = 2, // close() called successfully after eof
eof = 3 // eof of file was reached without error
} m_status;
int m_childpid;
detail::future_string_queue_type m_input_queue;
std::unique_ptr<osmium::io::Decompressor> m_decompressor;
osmium::io::detail::ReadThreadManager m_read_thread_manager;
detail::future_buffer_queue_type m_osmdata_queue;
detail::queue_wrapper<osmium::memory::Buffer> m_osmdata_queue_wrapper;
std::future<osmium::io::Header> m_header_future;
osmium::io::Header m_header;
osmium::thread::thread_handler m_thread;
// This function will run in a separate thread.
static void parser_thread(const osmium::io::File& file,
detail::future_string_queue_type& input_queue,
detail::future_buffer_queue_type& osmdata_queue,
std::promise<osmium::io::Header>&& header_promise,
osmium::osm_entity_bits::type read_which_entities) {
std::promise<osmium::io::Header> promise = std::move(header_promise);
auto creator = detail::ParserFactory::instance().get_creator_function(file);
auto parser = creator(input_queue, osmdata_queue, promise, read_which_entities);
parser->parse();
}
#ifndef _WIN32
static int execute(const std::string& command, const std::string& filename, int* childpid) {
int pipefd[2];
if (pipe(pipefd) < 0) {
throw std::system_error(errno, std::system_category(), "opening pipe failed");
}
pid_t pid = fork();
if (pid < 0) {
throw std::system_error(errno, std::system_category(), "fork failed");
}
if (pid == 0) { // child
// close all file descriptors except one end of the pipe
for (int i = 0; i < 32; ++i) {
if (i != pipefd[1]) {
::close(i);
}
}
if (dup2(pipefd[1], 1) < 0) { // put end of pipe as stdout/stdin
exit(1);
}
::open("/dev/null", O_RDONLY); // stdin
::open("/dev/null", O_WRONLY); // stderr
// hack: -g switches off globbing in curl which allows [] to be used in file names
// this is important for XAPI URLs
// in theory this execute() function could be used for other commands, but it is
// only used for curl at the moment, so this is okay.
if (::execlp(command.c_str(), command.c_str(), "-g", filename.c_str(), nullptr) < 0) {
exit(1);
}
}
// parent
*childpid = pid;
::close(pipefd[1]);
return pipefd[0];
}
#endif
static int open_input_file_or_url(const std::string& filename, int* childpid) {
std::string protocol = filename.substr(0, filename.find_first_of(':'));
if (protocol == "http" || protocol == "https" || protocol == "ftp" || protocol == "file") {
#ifndef _WIN32
return execute("curl", filename, childpid);
#else
throw io_error("Reading OSM files from the network currently not supported on Windows.");
#endif
} else {
return osmium::io::detail::open_for_reading(filename);
}
}
public:
explicit Reader(const osmium::io::File& file, osmium::osm_entity_bits::type read_which_entities = osmium::osm_entity_bits::all) :
m_file(file.check()),
m_read_which_entities(read_which_entities),
m_status(status::okay),
m_childpid(0),
m_input_queue(max_input_queue_size, "raw_input"),
m_decompressor(m_file.buffer() ?
osmium::io::CompressionFactory::instance().create_decompressor(file.compression(), m_file.buffer(), m_file.buffer_size()) :
osmium::io::CompressionFactory::instance().create_decompressor(file.compression(), open_input_file_or_url(m_file.filename(), &m_childpid))),
m_read_thread_manager(*m_decompressor, m_input_queue),
m_osmdata_queue(max_osmdata_queue_size, "parser_results"),
m_osmdata_queue_wrapper(m_osmdata_queue),
m_header_future(),
m_header(),
m_thread() {
std::promise<osmium::io::Header> header_promise;
m_header_future = header_promise.get_future();
m_thread = osmium::thread::thread_handler{parser_thread, std::ref(m_file), std::ref(m_input_queue), std::ref(m_osmdata_queue), std::move(header_promise), read_which_entities};
}
explicit Reader(const std::string& filename, osmium::osm_entity_bits::type read_types = osmium::osm_entity_bits::all) :
Reader(osmium::io::File(filename), read_types) {
}
explicit Reader(const char* filename, osmium::osm_entity_bits::type read_types = osmium::osm_entity_bits::all) :
Reader(osmium::io::File(filename), read_types) {
}
Reader(const Reader&) = delete;
Reader& operator=(const Reader&) = delete;
Reader(Reader&&) = default;
Reader& operator=(Reader&&) = default;
~Reader() noexcept {
try {
close();
} catch (...) {
// Ignore any exceptions because destructor must not throw.
}
}
void close() {
m_status = status::closed;
m_read_thread_manager.stop();
m_osmdata_queue_wrapper.drain();
try {
m_read_thread_manager.close();
} catch (...) {
// Ignore any exceptions.
}
#ifndef _WIN32
if (m_childpid) {
int status;
pid_t pid = ::waitpid(m_childpid, &status, 0);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
throw std::system_error(errno, std::system_category(), "subprocess returned error");
}
#pragma GCC diagnostic pop
m_childpid = 0;
}
#endif
}
osmium::io::Header header() {
if (m_status == status::error) {
throw io_error("Can not get header from reader when in status 'error'");
}
try {
if (m_header_future.valid()) {
m_header = m_header_future.get();
if (m_read_which_entities == osmium::osm_entity_bits::nothing) {
m_status = status::eof;
}
}
} catch (...) {
close();
m_status = status::error;
throw;
}
return m_header;
}
osmium::memory::Buffer read() {
osmium::memory::Buffer buffer;
if (m_status != status::okay ||
m_read_which_entities == osmium::osm_entity_bits::nothing) {
throw io_error("Can not read from reader when in status 'closed', 'eof', or 'error'");
}
try {
// m_input_format.read() can return an invalid buffer to signal EOF,
// or a valid buffer with or without data. A valid buffer
// without data is not an error, it just means we have to
// keep getting the next buffer until there is one with data.
while (true) {
buffer = m_osmdata_queue_wrapper.pop();
if (detail::at_end_of_data(buffer)) {
m_status = status::eof;
m_read_thread_manager.close();
return buffer;
}
if (buffer.committed() > 0) {
return buffer;
}
}
} catch (...) {
close();
m_status = status::error;
throw;
}
}
bool eof() const {
return m_status == status::eof || m_status == status::closed;
}
}; // class Reader
template <typename... TArgs>
osmium::memory::Buffer read_file(TArgs&&... args) {
osmium::memory::Buffer buffer(1024*1024, osmium::memory::Buffer::auto_grow::yes);
Reader reader(std::forward<TArgs>(args)...);
while (osmium::memory::Buffer read_buffer = reader.read()) {
buffer.add_buffer(read_buffer);
buffer.commit();
}
return buffer;
}
} // namespace io
} // namespace osmium
#endif // OSMIUM_IO_READER_HPP
osmium::io::Reader::m_osmdata_queue_wrapper
detail::queue_wrapper< osmium::memory::Buffer > m_osmdata_queue_wrapper
Definition: reader.hpp:100
osmium::io::Reader::status
status
Definition: reader.hpp:84
osmium::io::Reader::read
osmium::memory::Buffer read()
Definition: reader.hpp:314
osmium::io::Reader::status::eof
osmium::io::Reader::Reader
Reader(const osmium::io::File &file, osmium::osm_entity_bits::type read_which_entities=osmium::osm_entity_bits::all)
Definition: reader.hpp:200
osmium::osm_entity_bits::type
type
Definition: entity_bits.hpp:60
util.hpp
osmium::io::Reader::m_childpid
int m_childpid
Definition: reader.hpp:91
entity_bits.hpp
osmium::io::Reader::m_header_future
std::future< osmium::io::Header > m_header_future
Definition: reader.hpp:102
osmium::io::read_file
osmium::memory::Buffer read_file(TArgs &&...args)
Definition: reader.hpp:364
osmium::memory::Buffer::auto_grow::yes
osmium::io::Reader::status::okay
osmium::io::Reader::m_status
enum osmium::io::Reader::status m_status
osmium::io::Reader::m_decompressor
std::unique_ptr< osmium::io::Decompressor > m_decompressor
Definition: reader.hpp:95
osmium::io::Reader::operator=
Reader & operator=(const Reader &)=delete
osmium::io::Reader::max_osmdata_queue_size
static constexpr size_t max_osmdata_queue_size
Definition: reader.hpp:79
osmium::io::Reader::eof
bool eof() const
Definition: reader.hpp:349
osmium::osm_entity_bits::all
object or changeset
Definition: entity_bits.hpp:71
osmium::io::Reader::m_input_queue
detail::future_string_queue_type m_input_queue
Definition: reader.hpp:93
file.hpp
osmium::io::Reader::m_file
osmium::io::File m_file
Definition: reader.hpp:81
osmium::io::File
Definition: file.hpp:74
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
compression.hpp
osmium::memory::Buffer::add_buffer
void add_buffer(const Buffer &buffer)
Definition: buffer.hpp:474
header.hpp
osmium::io::Reader::m_read_thread_manager
osmium::io::detail::ReadThreadManager m_read_thread_manager
Definition: reader.hpp:97
osmium::io::Reader::status::closed
osmium::io::Reader::execute
static int execute(const std::string &command, const std::string &filename, int *childpid)
Definition: reader.hpp:131
osmium::io::Reader
Definition: reader.hpp:76
osmium::io::Reader::~Reader
~Reader() noexcept
Definition: reader.hpp:234
osmium::io_error
Definition: error.hpp:44
osmium::io::Reader::parser_thread
static void parser_thread(const osmium::io::File &file, detail::future_string_queue_type &input_queue, detail::future_buffer_queue_type &osmdata_queue, std::promise< osmium::io::Header > &&header_promise, osmium::osm_entity_bits::type read_which_entities)
Definition: reader.hpp:108
osmium::io::Reader::header
osmium::io::Header header()
Definition: reader.hpp:284
osmium::io::Reader::status::error
osmium::memory::Buffer::committed
size_t committed() const noexcept
Definition: buffer.hpp:241
error.hpp
osmium::memory::Buffer
Definition: buffer.hpp:97
osmium::io::Reader::m_thread
osmium::thread::thread_handler m_thread
Definition: reader.hpp:105
osmium::io::Reader::open_input_file_or_url
static int open_input_file_or_url(const std::string &filename, int *childpid)
Definition: reader.hpp:176
osmium::io::Reader::m_osmdata_queue
detail::future_buffer_queue_type m_osmdata_queue
Definition: reader.hpp:99
buffer.hpp
osmium::io::Reader::m_header
osmium::io::Header m_header
Definition: reader.hpp:103
osmium::osm_entity_bits::nothing
Definition: entity_bits.hpp:62
osmium::io::Reader::Reader
Reader(const char *filename, osmium::osm_entity_bits::type read_types=osmium::osm_entity_bits::all)
Definition: reader.hpp:224
osmium::io::CompressionFactory
Definition: compression.hpp:117
osmium::util::Options::get
std::string get(const std::string &key, const std::string &default_value="") const noexcept
Definition: options.hpp:124
osmium::io::Reader::max_input_queue_size
static constexpr size_t max_input_queue_size
Definition: reader.hpp:78
osmium::io::Header
Definition: header.hpp:49
osmium::io::Reader::close
void close()
Definition: reader.hpp:250
osmium::io::Reader::m_read_which_entities
osmium::osm_entity_bits::type m_read_which_entities
Definition: reader.hpp:82
osmium::memory::Buffer::commit
size_t commit()
Definition: buffer.hpp:335
osmium::thread::thread_handler
Definition: util.hpp:83
osmium::io::Reader::Reader
Reader(const std::string &filename, osmium::osm_entity_bits::type read_types=osmium::osm_entity_bits::all)
Definition: reader.hpp:220