[go: nahoru, domu]

Code Health: Add span variant of base::ReadFile()

Callers within //base have been migrated. Other callers will be
migrated in follow-up CLs

Bug: 1333521, 1490484
Change-Id: If00d50f9c3be126662ea9df6ea09133c49b3a79d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5138261
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Austin Sullivan <asully@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1244165}
diff --git a/base/files/file_util.cc b/base/files/file_util.cc
index 4828866..c84190d4 100644
--- a/base/files/file_util.cc
+++ b/base/files/file_util.cc
@@ -441,6 +441,18 @@
   return true;
 }
 
+int ReadFile(const FilePath& filename, char* data, int max_size) {
+  if (max_size < 0) {
+    return -1;
+  }
+  std::optional<uint64_t> result =
+      ReadFile(filename, make_span(data, static_cast<uint32_t>(max_size)));
+  if (!result) {
+    return -1;
+  }
+  return checked_cast<int>(result.value());
+}
+
 bool WriteFile(const FilePath& filename, span<const uint8_t> data) {
   int size = checked_cast<int>(data.size());
   return WriteFile(filename, reinterpret_cast<const char*>(data.data()),