Refactor `ReadFromEeprom` to return `absl::StatusOr`.
previously`if(!bytes_read.ok)` is never true since ReadFromEeprom returns int64_t(even with -1 on error), which means we always succeeded with the old logic
```
absl::StatusOr<int64_t> bytes_read =
ReadFromEeprom(file, offset, length, outbuf);
if (!bytes_read.ok()) {
return -1;
}
return bytes_read.value();
```
Update `ReadFromEeprom` to return `absl::StatusOr<int64_t>` to provide more detailed error information, returning `absl::InternalError` with specific error messages on `lseek` or `read` failures.
PiperOrigin-RevId: 822256104
Change-Id: Ide04f66277c9b87c956e05dcc687b8f9e6fe1176
diff --git a/tlbmc/hal/fru_scanner_i2c.cc b/tlbmc/hal/fru_scanner_i2c.cc
index 37eade4..45c19a9 100644
--- a/tlbmc/hal/fru_scanner_i2c.cc
+++ b/tlbmc/hal/fru_scanner_i2c.cc
@@ -80,13 +80,21 @@
return bus_list;
}
-int64_t ReadFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf) {
+// Reads from eeprom file at given offset and returns the number of bytes
+// read.
+absl::StatusOr<int64_t> ReadFromEeprom(int fd, off_t offset, size_t len,
+ uint8_t* buf) {
auto result = lseek(fd, offset, SEEK_SET);
if (result < 0) {
LOG(ERROR) << "Failed to seek to offset " << offset << " in eeprom file";
- return -1;
+ return absl::InternalError("Failed to seek to offset");
};
- return read(fd, buf, len);
+ auto bytes_read = read(fd, buf, len);
+ if (bytes_read < 0) {
+ LOG(ERROR) << "Failed to read from eeprom file";
+ return absl::InternalError("Failed to read from eeprom file");
+ }
+ return bytes_read;
}
std::string GetEepromPath(size_t bus, size_t address,