Validate file handle and read result for progress.bin

The progress.bin reading code assumed SD.open() always succeeded after
SD.exists() returned true. This could fail due to race conditions or
other SD errors. Now properly checks if the file opened and if the
expected number of bytes were read.
This commit is contained in:
IFAKA 2025-12-19 01:10:20 +01:00
parent d86b3fe134
commit bd2c156bab

View File

@ -33,13 +33,14 @@ void EpubReaderActivity::onEnter() {
epub->setupCacheDir(); epub->setupCacheDir();
if (SD.exists((epub->getCachePath() + "/progress.bin").c_str())) { File f = SD.open((epub->getCachePath() + "/progress.bin").c_str());
File f = SD.open((epub->getCachePath() + "/progress.bin").c_str()); if (f) {
uint8_t data[4]; uint8_t data[4];
f.read(data, 4); if (f.read(data, 4) == 4) {
currentSpineIndex = data[0] + (data[1] << 8); currentSpineIndex = data[0] + (data[1] << 8);
nextPageNumber = data[2] + (data[3] << 8); nextPageNumber = data[2] + (data[3] << 8);
Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber); Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber);
}
f.close(); f.close();
} }