Skip to content

Commit

Permalink
Extract read_exactly as reusable utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWiederhake authored and joto committed Dec 3, 2023
1 parent 5e07fce commit 4ce5fb6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
27 changes: 2 additions & 25 deletions include/osmium/io/detail/pbf_input_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,37 +114,14 @@ namespace osmium {
return size;
}

/**
* Read exactly size bytes from fd into buffer.
*
* @pre Value in size parameter must fit in unsigned int
* @returns true if size bytes could be read
* false if EOF was encountered
*/
bool read_exactly(char* buffer, std::size_t size) {
std::size_t to_read = size;

while (to_read > 0) {
auto const read_size = osmium::io::detail::reliable_read(m_fd, buffer + (size - to_read), static_cast<unsigned int>(to_read));
if (read_size == 0) { // EOF
return false;
}
to_read -= read_size;
}

*m_offset_ptr += size;

return true;
}

/**
* Read 4 bytes in network byte order from file. They contain
* the length of the following BlobHeader.
*/
uint32_t read_blob_header_size_from_file() {
if (m_fd != -1) {
std::array<char, sizeof(uint32_t)> buffer{};
if (!read_exactly(buffer.data(), buffer.size())) {
if (!osmium::io::detail::read_exactly(m_fd, buffer.data(), static_cast<unsigned int>(buffer.size()))) {
return 0; // EOF
}
return check_size(get_size_in_network_byte_order(buffer.data()));
Expand Down Expand Up @@ -230,7 +207,7 @@ namespace osmium {
if (m_fd != -1) {
buffer.resize(size);

if (!read_exactly(&*buffer.begin(), size)) {
if (!osmium::io::detail::read_exactly(m_fd, &*buffer.begin(), static_cast<unsigned int>(size))) {
throw osmium::pbf_error{"unexpected EOF"};
}
} else {
Expand Down
24 changes: 24 additions & 0 deletions include/osmium/io/detail/read_write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ DEALINGS IN THE SOFTWARE.
#include <osmium/io/writer_options.hpp>
#include <osmium/util/file.hpp>

#include <cassert>
#include <cerrno>
#include <cstddef>
#include <fcntl.h>
Expand Down Expand Up @@ -201,6 +202,29 @@ namespace osmium {
return nread;
}

/**
* Read exactly size bytes from fd into buffer. In contrast to reliable_read,
* this function will continue reading until either EOF or an error is encountered.
*
* @pre buffer Buffer for data to be read. Must be at least size bytes long.
* @returns true if size bytes could be read
* false if EOF was encountered
*/
inline bool read_exactly(int fd, char* buffer, unsigned int size) {
unsigned int to_read = size;

while (to_read > 0) {
auto const read_size = reliable_read(fd, buffer + (size - to_read), to_read);
if (read_size == 0) { // EOF
return false;
}
assert(read_size <= to_read);
to_read -= read_size;
}

return true;
}

inline void reliable_fsync(const int fd) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
Expand Down

0 comments on commit 4ce5fb6

Please sign in to comment.