update_engine: Refactor OperationsGenerator into a base class.

This refactor cleans up the interface of the algorithms that generate
the list of rootfs and kernel operations removing the mention of a
Graph from it. The Graph class is only used by the in-place generator
because it requires to keep track of dependencies between operations
reading or writting the same block. The full update generator, using
only REPLACE or REPLACE_BZ doesn't need to use a graph to do that, but
in order to reuse some code, the interface was hacked that way.

This patch now uses two vectors of "AnnotatedOperations", which are
a mere InstallOperation as defined by the .proto file plus a name
used for logging purposes only. Both rootfs and kernel operations
have now the same type on the interface, allowing to share common
functions handling those.

BUG=chromium:331965
TEST=FEATURES=test emerge-link update_engine

Change-Id: I78566bbecb948634b7ecc8d086766ce67a79b43e
Reviewed-on: https://chromium-review.googlesource.com/262281
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Trybot-Ready: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/payload_generator/annotated_operation.cc b/payload_generator/annotated_operation.cc
new file mode 100644
index 0000000..0e73479
--- /dev/null
+++ b/payload_generator/annotated_operation.cc
@@ -0,0 +1,80 @@
+// Copyright 2015 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "update_engine/payload_generator/annotated_operation.h"
+
+#include <base/format_macros.h>
+#include <base/strings/string_number_conversions.h>
+#include <base/strings/stringprintf.h>
+
+using std::string;
+
+namespace chromeos_update_engine {
+
+namespace {
+// Output the list of extents as (start_block, num_blocks) in the passed output
+// stream.
+void OutputExtents(std::ostream* os,
+                   const google::protobuf::RepeatedPtrField<Extent>& extents) {
+  for (const auto& extent : extents) {
+    *os << " (" << extent.start_block() << ", " << extent.num_blocks() << ")";
+  }
+}
+}  // namespace
+
+void AnnotatedOperation::SetNameFromFileAndChunk(
+    const string& filename, off_t chunk_offset, off_t chunk_size) {
+  name = filename;
+  if (chunk_offset != 0 || chunk_size != -1) {
+    if (chunk_size != -1) {
+      base::StringAppendF(&name, " [%" PRId64 ", %" PRId64 ")",
+                          chunk_offset, chunk_offset + chunk_size);
+    } else {
+      base::StringAppendF(&name, " [%" PRId64 ", end)", chunk_offset);
+    }
+  }
+}
+
+string InstallOperationTypeName(
+    DeltaArchiveManifest_InstallOperation_Type op_type) {
+  switch (op_type) {
+    case DeltaArchiveManifest_InstallOperation_Type_BSDIFF:
+      return "BSDIFF";
+    case DeltaArchiveManifest_InstallOperation_Type_MOVE:
+      return "MOVE";
+    case DeltaArchiveManifest_InstallOperation_Type_REPLACE:
+      return "REPLACE";
+    case DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ:
+      return "REPLACE_BZ";
+    case DeltaArchiveManifest_InstallOperation_Type_SOURCE_COPY:
+      return "SOURCE_COPY";
+    case DeltaArchiveManifest_InstallOperation_Type_SOURCE_BSDIFF:
+      return "SOURCE_BSDIFF";
+  }
+  return "UNK";
+}
+
+std::ostream& operator<<(std::ostream& os, const AnnotatedOperation& aop) {
+  // For example, this prints:
+  // REPLACE_BZ 500 @3000
+  //   name: /foo/bar
+  //    dst: (123, 3) (127, 2)
+  os << InstallOperationTypeName(aop.op.type()) << " "  << aop.op.data_length();
+  if (aop.op.data_length() > 0)
+    os << " @" << aop.op.data_offset();
+  if (!aop.name.empty()) {
+    os << std::endl << "  name: " << aop.name;
+  }
+  if (aop.op.src_extents_size() != 0) {
+    os << std::endl << "   src:";
+    OutputExtents(&os, aop.op.src_extents());
+  }
+  if (aop.op.dst_extents_size() != 0) {
+    os << std::endl << "   dst:";
+    OutputExtents(&os, aop.op.dst_extents());
+  }
+  return os;
+}
+
+}  // namespace chromeos_update_engine