From 57d1ab50a8d4093678ff52d82e044fdbf4337231 Mon Sep 17 00:00:00 2001 From: Mathias Laurin Date: Thu, 16 Jan 2025 17:24:16 +0100 Subject: [PATCH] Let RRDUDSSocket read bytes for FETCHBIN CMK-18929 Change-Id: I40322faa303ccb798f0bb2d00dc202ef8ea52841 --- .../livestatus/include/livestatus/RRDUDSSocket.h | 1 + packages/livestatus/src/RRDUDSSocket.cc | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/packages/livestatus/include/livestatus/RRDUDSSocket.h b/packages/livestatus/include/livestatus/RRDUDSSocket.h index 533c110ac39..6bcaf08178e 100644 --- a/packages/livestatus/include/livestatus/RRDUDSSocket.h +++ b/packages/livestatus/include/livestatus/RRDUDSSocket.h @@ -24,6 +24,7 @@ class RRDUDSSocket { void connect(); [[nodiscard]] Logger *logger() const { return logger_; } [[nodiscard]] std::string readLine() const; + [[nodiscard]] std::string read(std::size_t count) const; [[nodiscard]] ssize_t write(std::string_view text, std::chrono::milliseconds timeout) const; [[nodiscard]] bool isConnected() const { return socket_ != -1; } diff --git a/packages/livestatus/src/RRDUDSSocket.cc b/packages/livestatus/src/RRDUDSSocket.cc index ca457ee77d1..8f6f702734b 100644 --- a/packages/livestatus/src/RRDUDSSocket.cc +++ b/packages/livestatus/src/RRDUDSSocket.cc @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include "livestatus/Logger.h" @@ -53,6 +55,18 @@ std::string RRDUDSSocket::readLine() const { return mk::rstrip(answer); } +std::string RRDUDSSocket::read(std::size_t count) const { + std::array answer{}; + // NOLINTBEGIN(cppcoreguidelines-pro-bounds-array-to-pointer-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic) + const auto rd = ::fread(answer.data(), sizeof(decltype(answer)::value_type), + std::min(count, answer.size()), file_); + if (feof(file_) != 0 || ferror(file_) != 0) { + throw generic_error("cannot read reply"); + } + return {answer.begin(), answer.begin() + rd}; + // NOLINTEND(cppcoreguidelines-pro-bounds-array-to-pointer-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic) +} + ssize_t RRDUDSSocket::write(std::string_view text, std::chrono::milliseconds timeout) const { if (!isConnected()) {