Add a binary to generate .map files from filesystem image am: 83b594bb04

Original change: https://android-review.googlesource.com/c/platform/system/update_engine/+/2605629

Change-Id: I8691d9e6feddf3b5852d403bd786f43aa70ae659
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/Android.bp b/Android.bp
index 86add6a..b3634f3 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1318,3 +1318,23 @@
         "update_metadata-protos",
     ],
 }
+
+cc_binary_host {
+    name: "map_file_generator",
+    defaults: [
+        "ue_defaults",
+        "libpayload_generator_exports",
+    ],
+    srcs: [
+        "aosp/map_file_generator.cc",
+    ],
+    static_libs: [
+        "liblog",
+        "libbase",
+        "libpayload_generator",
+        "libpayload_extent_ranges",
+        "libpayload_extent_utils",
+        "libgflags",
+        "update_metadata-protos",
+    ],
+}
diff --git a/aosp/map_file_generator.cc b/aosp/map_file_generator.cc
new file mode 100644
index 0000000..1943a66
--- /dev/null
+++ b/aosp/map_file_generator.cc
@@ -0,0 +1,89 @@
+//
+// Copyright (C) 2023 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "android-base/stringprintf.h"
+#include "android-base/unique_fd.h"
+#include "common/utils.h"
+#include "update_engine/payload_generator/ext2_filesystem.h"
+#include "update_engine/payload_generator/erofs_filesystem.h"
+#include "update_engine/payload_generator/filesystem_interface.h"
+
+namespace chromeos_update_engine {
+
+int WriteBlockMap(const char* img,
+                  const FilesystemInterface* fs,
+                  const char* output_file) {
+  std::vector<FilesystemInterface::File> files;
+  if (!fs->GetFiles(&files)) {
+    LOG(ERROR) << "Failed to parse file info in " << img;
+    return -2;
+  }
+  android::base::unique_fd fd(
+      open(output_file, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
+  if (fd < 0) {
+    PLOG(ERROR) << "Failed to open " << output_file;
+    return -errno;
+  }
+  for (const auto& file : files) {
+    if (file.extents.empty()) {
+      continue;
+    }
+    std::string output_line;
+    output_line.append(file.name);
+    for (const auto& extent : file.extents) {
+      if (extent.num_blocks() <= 0) {
+        continue;
+      }
+      output_line.append(" ");
+      if (extent.num_blocks() == 1) {
+        output_line.append(std::to_string(extent.start_block()));
+        continue;
+      }
+      const auto extent_str = android::base::StringPrintf(
+          "%lu-%lu",
+          extent.start_block(),
+          extent.start_block() + extent.num_blocks() - 1);
+      output_line.append(extent_str);
+    }
+    output_line.append("\n");
+    if (!utils::WriteAll(fd.get(), output_line.data(), output_line.size())) {
+      PLOG(ERROR) << "Failed to write to " << output_file;
+      return -errno;
+    }
+  }
+  return 0;
+}
+
+int Main(int argc, const char* argv[]) {
+  const char* img = argv[1];
+  const char* output_file = argv[2];
+  std::unique_ptr<FilesystemInterface> fs;
+  fs = ErofsFilesystem::CreateFromFile(img);
+  if (fs != nullptr) {
+    return WriteBlockMap(img, fs.get(), output_file);
+  }
+  fs = Ext2Filesystem::CreateFromFile(img);
+  if (fs != nullptr) {
+    return WriteBlockMap(img, fs.get(), output_file);
+  }
+  LOG(ERROR) << "Failed to parse " << img;
+  return -1;
+}
+}  // namespace chromeos_update_engine
+
+int main(int argc, const char* argv[]) {
+  return chromeos_update_engine::Main(argc, argv);
+}
\ No newline at end of file