libmetrics: Support partial writes.

BUG=chromium-os:11125
TEST=unit tests, tested on device

Change-Id: If9066988b86f61cb5bae413b7250d5426854f31b

Review URL: http://codereview.chromium.org/6592019
diff --git a/metrics_library.cc b/metrics_library.cc
index 3bc8007..8c98696 100644
--- a/metrics_library.cc
+++ b/metrics_library.cc
@@ -12,7 +12,7 @@
 #include <cstdio>
 #include <cstring>
 
-#include <base/eintr_wrapper.h>
+#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 @@
   }
 }
 
+// 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 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;
   }