diff --git a/metrics/metrics_library.cc b/metrics/metrics_library.cc index 3bc800716..8c98696c1 100644 --- a/metrics/metrics_library.cc +++ b/metrics/metrics_library.cc @@ -12,7 +12,7 @@ #include #include -#include +#include "base/eintr_wrapper.h" // HANDLE_EINTR macro, no libbase required. #define READ_WRITE_ALL_FILE_FLAGS \ (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) @@ -42,6 +42,22 @@ static void PrintError(const char* message, const char* file, } } +// Copied from libbase to avoid pulling in all of libbase just for libmetrics. +static int WriteFileDescriptor(const int fd, const char* data, int size) { + // Allow for partial writes. + ssize_t bytes_written_total = 0; + for (ssize_t bytes_written_partial = 0; bytes_written_total < size; + bytes_written_total += bytes_written_partial) { + bytes_written_partial = + HANDLE_EINTR(write(fd, data + bytes_written_total, + size - bytes_written_total)); + if (bytes_written_partial < 0) + return -1; + } + + return bytes_written_total; +} + MetricsLibrary::MetricsLibrary() : uma_events_file_(NULL), consent_file_(kConsentFile) {} @@ -155,7 +171,7 @@ bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) { } bool success = true; - if (HANDLE_EINTR(write(chrome_fd, message, length)) != length) { + if (WriteFileDescriptor(chrome_fd, message, length) != length) { PrintError("write", uma_events_file_, errno); success = false; }