Add lz4diff patch routines

Test: th
Bug: 206729162

Change-Id: I76a404e233e7683812bac65520c6c065e9a1ffe2
diff --git a/common/utils.cc b/common/utils.cc
index 8b57075..002e6a0 100644
--- a/common/utils.cc
+++ b/common/utils.cc
@@ -567,7 +567,7 @@
                      const string& fs_mount_options) {
   vector<const char*> fstypes;
   if (type.empty()) {
-    fstypes = {"ext2", "ext3", "ext4", "squashfs"};
+    fstypes = {"ext2", "ext3", "ext4", "squashfs", "erofs"};
   } else {
     fstypes = {type.c_str()};
   }
@@ -931,17 +931,34 @@
   }
   return true;
 }
+bool ReadExtents(const std::string& path,
+                 const vector<Extent>& extents,
+                 brillo::Blob* out_data,
+                 ssize_t out_data_size,
+                 size_t block_size) {
+  FileDescriptorPtr fd = std::make_shared<EintrSafeFileDescriptor>();
+  fd->Open(path.c_str(), O_RDONLY);
+  return ReadExtents(fd, extents, out_data, out_data_size, block_size);
+}
 
-bool ReadExtents(const string& path,
+bool ReadExtents(FileDescriptorPtr fd,
+                 const google::protobuf::RepeatedPtrField<Extent>& extents,
+                 brillo::Blob* out_data,
+                 size_t block_size) {
+  return ReadExtents(fd,
+                     {extents.begin(), extents.end()},
+                     out_data,
+                     utils::BlocksInExtents(extents) * block_size,
+                     block_size);
+}
+
+bool ReadExtents(FileDescriptorPtr fd,
                  const vector<Extent>& extents,
                  brillo::Blob* out_data,
                  ssize_t out_data_size,
                  size_t block_size) {
   brillo::Blob data(out_data_size);
   ssize_t bytes_read = 0;
-  int fd = open(path.c_str(), O_RDONLY);
-  TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
-  ScopedFdCloser fd_closer(&fd);
 
   for (const Extent& extent : extents) {
     ssize_t bytes_read_this_iteration = 0;
diff --git a/common/utils.h b/common/utils.h
index a0a466d..46e41f0 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -312,6 +312,12 @@
                  ssize_t out_data_size,
                  size_t block_size);
 
+bool ReadExtents(FileDescriptorPtr path,
+                 const std::vector<Extent>& extents,
+                 brillo::Blob* out_data,
+                 ssize_t out_data_size,
+                 size_t block_size);
+
 bool WriteExtents(const std::string& path,
                   const google::protobuf::RepeatedPtrField<Extent>& extents,
                   const brillo::Blob& data,
@@ -333,6 +339,11 @@
                  brillo::Blob* out_data,
                  size_t block_size);
 
+bool ReadExtents(FileDescriptorPtr path,
+                 const google::protobuf::RepeatedPtrField<Extent>& extents,
+                 brillo::Blob* out_data,
+                 size_t block_size);
+
 // Read the current boot identifier and store it in |boot_id|. This identifier
 // is constants during the same boot of the kernel and is regenerated after
 // reboot. Returns whether it succeeded getting the boot_id.