Use span-based file API in touch_replay.cc

No (intended) functionality changes.

Bug: 435317390
Change-Id: Ia906e0852bc0f0b61ca3a811a7b877298941ef29
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7511731
Auto-Submit: Jan Keitel <jkeitel@google.com>
Reviewed-by: Egor Pasko <pasko@chromium.org>
Commit-Queue: Egor Pasko <pasko@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1577200}
NOKEYCHECK=True
GitOrigin-RevId: 00b338d055e09c2a4de9cda1c9ed250bb98a0f2a
diff --git a/touch_replay/touch_replay.cc b/touch_replay/touch_replay.cc
index 2bbe340..c55419c 100644
--- a/touch_replay/touch_replay.cc
+++ b/touch_replay/touch_replay.cc
@@ -16,13 +16,13 @@
 #include <string>
 #include <type_traits>
 
-#include "base/compiler_specific.h"
 #include "base/containers/span.h"
 #include "base/files/file.h"
 #include "base/files/file_enumerator.h"
 #include "base/files/file_path.h"
 #include "base/logging.h"
 #include "base/posix/eintr_wrapper.h"
+#include "base/strings/cstring_view.h"
 #include "base/strings/string_number_conversions.h"
 
 // Records and replays touch events on Android device.
@@ -238,8 +238,8 @@
 
 bool IsValidFileHeader(base::File& dump_file) {
   char magic_buf[kLogFileHeaderSize];
-  int bytes_read = dump_file.ReadAtCurrentPos(magic_buf, kLogFileHeaderSize);
-  if (bytes_read < kLogFileHeaderSize) {
+  if (!dump_file.ReadAtCurrentPosAndCheck(
+          base::as_writable_byte_span(magic_buf))) {
     PLOG(ERROR) << "read magic";
     return false;
   }
@@ -259,7 +259,7 @@
   char cur;
   int64_t name_bytes_read = 0;
   do {
-    if (!f.Read(offset, base::byte_span_from_ref(cur))) {
+    if (!f.ReadAndCheck(offset, base::byte_span_from_ref(cur))) {
       PLOG(ERROR) << "read device name";
       return false;
     }
@@ -325,9 +325,9 @@
   }
 
   // Write the device file name to the dump (inc. terminating NUL).
-  std::string s = device->path().MaybeAsASCII();
   if (!dump_file.WriteAtCurrentPosAndCheck(
-          base::as_bytes(UNSAFE_TODO(base::span(s.c_str(), s.size() + 1))))) {
+          base::byte_span_with_nul_from_cstring_view(
+              base::cstring_view(device->path().MaybeAsASCII())))) {
     LOG(ERROR) << "Could not write device name";
     return false;
   }
@@ -403,10 +403,8 @@
     LOG(ERROR) << "No events to send";
     return false;
   }
-  const int64_t bytes_to_read = num_records * sizeof(TouchInputEventRecord);
   std::vector<TouchInputEventRecord> records(num_records);
-  if (dump_file.Read(offset, base::as_writable_byte_span(records)) <
-      base::checked_cast<size_t>(bytes_to_read)) {
+  if (!dump_file.ReadAndCheck(offset, base::as_writable_byte_span(records))) {
     PLOG(ERROR) << "Could not read records";
     return false;
   }