AU: Class to perform delta updates.

A class to perform delta updates and the associated unittests. Also,
change the delta diff generator executable to be able to apply a
delta, which is handy for debugging.

TEST=Attached unit test, hand-tested on real build images
BUG=552

Review URL: http://codereview.chromium.org/1718001
diff --git a/utils.cc b/utils.cc
index ff3c9db..7f05ffb 100644
--- a/utils.cc
+++ b/utils.cc
@@ -35,7 +35,7 @@
   return true;
 }
 
-bool WriteAll(int fd, const void *buf, size_t count) {
+bool WriteAll(int fd, const void* buf, size_t count) {
   const char* c_buf = static_cast<const char*>(buf);
   ssize_t bytes_written = 0;
   while (bytes_written < static_cast<ssize_t>(count)) {
@@ -46,6 +46,36 @@
   return true;
 }
 
+bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) {
+  const char* c_buf = static_cast<const char*>(buf);
+  ssize_t bytes_written = 0;
+  while (bytes_written < static_cast<ssize_t>(count)) {
+    ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written,
+                        offset + bytes_written);
+    TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
+    bytes_written += rc;
+  }
+  return true;
+}
+
+bool PReadAll(int fd, void* buf, size_t count, off_t offset,
+              ssize_t* out_bytes_read) {
+  char* c_buf = static_cast<char*>(buf);
+  ssize_t bytes_read = 0;
+  while (bytes_read < static_cast<ssize_t>(count)) {
+    ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read,
+                       offset + bytes_read);
+    TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
+    if (rc == 0) {
+      break;
+    }
+    bytes_read += rc;
+  }
+  *out_bytes_read = bytes_read;
+  return true;
+  
+}
+
 bool ReadFile(const std::string& path, std::vector<char>* out) {
   CHECK(out);
   FILE* fp = fopen(path.c_str(), "r");
@@ -233,6 +263,19 @@
   return true;
 }
 
+bool MakeTempDirectory(const std::string& dirname_template,
+                       std::string* dirname) {
+  DCHECK(dirname);
+  vector<char> buf(dirname_template.size() + 1);
+  memcpy(&buf[0], dirname_template.data(), dirname_template.size());
+  buf[dirname_template.size()] = '\0';
+  
+  char* return_code = mkdtemp(&buf[0]);
+  TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL);
+  *dirname = &buf[0];
+  return true;
+}
+
 bool StringHasSuffix(const std::string& str, const std::string& suffix) {
   if (suffix.size() > str.size())
     return false;
@@ -273,8 +316,9 @@
 }
 
 bool MountFilesystem(const string& device,
-                     const string& mountpoint) {
-  int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", 0, NULL);
+                     const string& mountpoint,
+                     unsigned long mountflags) {
+  int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", mountflags, NULL);
   if (rc < 0) {
     string msg = ErrnoNumberAsString(errno);
     LOG(ERROR) << "Unable to mount destination device: " << msg << ". "