AU: Delta Diff Generator

Adds a class that can take two root filesystem image and generate a
delta between them. Currently it's not very well tested, but this will
improve once the diff applicator is written.

Also, an executable to run the generator.

Other changes:
- Stop leaking loop devices in unittests
- extent mapper: support sparse files, ability to get FS block size
- AppendBlockToExtents support sparse files
- subprocess more verbose on errors
- add WriteAll to utils (WriteAll avoids short-write() returns)
- mkstemp wrapper for ease of use
- VectorIndexOf, finds index of an element in a vector

Review URL: http://codereview.chromium.org/891002
diff --git a/generate_delta_main.cc b/generate_delta_main.cc
index 551c6ef..2f21b2a 100644
--- a/generate_delta_main.cc
+++ b/generate_delta_main.cc
@@ -8,13 +8,23 @@
 #include <unistd.h>
 #include <set>
 #include <string>
+#include <gflags/gflags.h>
 #include <glib.h>
+#include "base/command_line.h"
 #include "chromeos/obsolete_logging.h"
 #include "update_engine/delta_diff_generator.h"
 #include "update_engine/subprocess.h"
 #include "update_engine/update_metadata.pb.h"
 #include "update_engine/utils.h"
 
+DEFINE_string(old_dir, "",
+              "Directory where the old rootfs is loop mounted read-only");
+DEFINE_string(new_dir, "",
+              "Directory where the new rootfs is loop mounted read-only");
+DEFINE_string(old_image, "", "Path to the old rootfs");
+DEFINE_string(new_image, "", "Path to the new rootfs");
+DEFINE_string(out_file, "", "Path to output file");
+
 // This file contains a simple program that takes an old path, a new path,
 // and an output file as arguments and the path to an output file and
 // generates a delta that can be sent to Chrome OS clients.
@@ -26,11 +36,6 @@
 
 namespace {
 
-void usage(const char* argv0) {
-  printf("usage: %s old_dir new_dir out_file\n", argv0);
-  exit(1);
-}
-
 bool IsDir(const char* path) {
   struct stat stbuf;
   TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
@@ -39,21 +44,27 @@
 
 int Main(int argc, char** argv) {
   g_thread_init(NULL);
+  google::ParseCommandLineFlags(&argc, &argv, true);
+  CommandLine::Init(argc, argv);
   Subprocess::Init();
-  if (argc != 4) {
-    usage(argv[0]);
-  }
-  logging::InitLogging("",
+  logging::InitLogging("delta_generator.log",
                        logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
                        logging::DONT_LOCK_LOG_FILE,
                        logging::APPEND_TO_OLD_LOG_FILE);
-  const char* old_dir = argv[1];
-  const char* new_dir = argv[2];
-  if ((!IsDir(old_dir)) || (!IsDir(new_dir))) {
-    usage(argv[0]);
+  if (FLAGS_old_dir.empty() || FLAGS_new_dir.empty() ||
+      FLAGS_old_image.empty() || FLAGS_new_image.empty() ||
+      FLAGS_out_file.empty()) {
+    LOG(FATAL) << "Missing required argument(s)";
+  }
+  if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
+    LOG(FATAL) << "old_dir or new_dir not directory";
   }
   
-  // TODO(adlr): generate delta file
+  DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
+                                              FLAGS_old_image,
+                                              FLAGS_new_dir,
+                                              FLAGS_new_image,
+                                              FLAGS_out_file);
 
   return 0;
 }