refactor: Removed redundant FsFile close() calls (#1434)

## Summary

**What is the goal of this PR?** (e.g., Implements the new feature for
file uploading.)

`DESTRUCTOR_CLOSES_FILE=1` is set in platformio.ini, which makes SdFat's
FsBaseFile destructor call close() automatically when a file goes out of
scope.

Three categories of file close calls remain untouched:
1. Close before Storage.remove() on the same path: ScreenshotUtil.cpp
closes the file before deleting it on write error. The remove might fail
if the file is still open.
2. Close before reopening the same variable: Epub.cpp writes a temp
NCX/nav file, closes it, then reopens it for reading. The
RecentBooksStore.cpp close before saveToFile() is the same pattern, it
rewrites the same file.
3. Close on member variables: BookMetadataCache.cpp (bookFile,
spineFile, tocFile), Section.cpp (file), XtcParser.cpp (m_file),
ZipFile.cpp (file). These persist beyond any single function scope, so
the destructor timing doesn't match the intended close point.

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**PARTIALLY**_
This commit is contained in:
Zach Nelson
2026-04-14 16:41:05 -05:00
committed by GitHub
parent 075ad7d021
commit 23aad213fc
24 changed files with 46 additions and 104 deletions
+15 -9
View File
@@ -155,6 +155,7 @@ bool Epub::parseTocNcxFile() const {
return false;
}
readItemContentsToStream(tocNcxItem, tempNcxFile, 1024);
// Explicitly close() file before reopening for reading
tempNcxFile.close();
if (!Storage.openFileForRead("EBP", tmpNcxPath, tempNcxFile)) {
return false;
@@ -165,14 +166,12 @@ bool Epub::parseTocNcxFile() const {
if (!ncxParser.setup()) {
LOG_ERR("EBP", "Could not setup toc ncx parser");
tempNcxFile.close();
return false;
}
const auto ncxBuffer = static_cast<uint8_t*>(malloc(1024));
if (!ncxBuffer) {
LOG_ERR("EBP", "Could not allocate memory for toc ncx parser");
tempNcxFile.close();
return false;
}
@@ -184,12 +183,12 @@ bool Epub::parseTocNcxFile() const {
if (processedSize != readSize) {
LOG_ERR("EBP", "Could not process all toc ncx data");
free(ncxBuffer);
tempNcxFile.close();
return false;
}
}
free(ncxBuffer);
// Explicitly close() file before calling Storage.remove()
tempNcxFile.close();
Storage.remove(tmpNcxPath.c_str());
@@ -212,6 +211,7 @@ bool Epub::parseTocNavFile() const {
return false;
}
readItemContentsToStream(tocNavItem, tempNavFile, 1024);
// Explicitly close() file before reopening for reading
tempNavFile.close();
if (!Storage.openFileForRead("EBP", tmpNavPath, tempNavFile)) {
return false;
@@ -241,12 +241,12 @@ bool Epub::parseTocNavFile() const {
if (processedSize != readSize) {
LOG_ERR("EBP", "Could not process all toc nav data");
free(navBuffer);
tempNavFile.close();
return false;
}
}
free(navBuffer);
// Explicitly close() file before calling Storage.remove()
tempNavFile.close();
Storage.remove(tmpNavPath.c_str());
@@ -304,10 +304,12 @@ void Epub::parseCssFiles() const {
}
if (!readItemContentsToStream(cssPath, tempCssFile, 1024)) {
LOG_ERR("EBP", "Could not read CSS file: %s", cssPath.c_str());
// Explicitly close() file before calling Storage.remove()
tempCssFile.close();
Storage.remove(tmpCssPath.c_str());
continue;
}
// Explicitly close() file before reopening for reading
tempCssFile.close();
// Parse the CSS file
@@ -317,6 +319,7 @@ void Epub::parseCssFiles() const {
continue;
}
cssParser->loadFromStream(tempCssFile);
// Explicitly close() file before calling Storage.remove()
tempCssFile.close();
Storage.remove(tmpCssPath.c_str());
}
@@ -547,6 +550,7 @@ bool Epub::generateCoverBmp(bool cropped) const {
return false;
}
readItemContentsToStream(coverImageHref, coverJpg, 1024);
// Explicitly close() file before reopening for reading
coverJpg.close();
if (!Storage.openFileForRead("EBP", coverJpgTempPath, coverJpg)) {
@@ -555,10 +559,10 @@ bool Epub::generateCoverBmp(bool cropped) const {
FsFile coverBmp;
if (!Storage.openFileForWrite("EBP", getCoverBmpPath(cropped), coverBmp)) {
coverJpg.close();
return false;
}
const bool success = JpegToBmpConverter::jpegFileToBmpStream(coverJpg, coverBmp, cropped);
// Explicitly close() files before calling Storage.remove()
coverJpg.close();
coverBmp.close();
Storage.remove(coverJpgTempPath.c_str());
@@ -580,6 +584,7 @@ bool Epub::generateCoverBmp(bool cropped) const {
return false;
}
readItemContentsToStream(coverImageHref, coverPng, 1024);
// Explicitly close() file before reopening for reading
coverPng.close();
if (!Storage.openFileForRead("EBP", coverPngTempPath, coverPng)) {
@@ -588,10 +593,10 @@ bool Epub::generateCoverBmp(bool cropped) const {
FsFile coverBmp;
if (!Storage.openFileForWrite("EBP", getCoverBmpPath(cropped), coverBmp)) {
coverPng.close();
return false;
}
const bool success = PngToBmpConverter::pngFileToBmpStream(coverPng, coverBmp, cropped);
// Explicitly close() files before calling Storage.remove()
coverPng.close();
coverBmp.close();
Storage.remove(coverPngTempPath.c_str());
@@ -634,6 +639,7 @@ bool Epub::generateThumbBmp(int height) const {
return false;
}
readItemContentsToStream(coverImageHref, coverJpg, 1024);
// Explicitly close() file before reopening for reading
coverJpg.close();
if (!Storage.openFileForRead("EBP", coverJpgTempPath, coverJpg)) {
@@ -642,7 +648,6 @@ bool Epub::generateThumbBmp(int height) const {
FsFile thumbBmp;
if (!Storage.openFileForWrite("EBP", getThumbBmpPath(height), thumbBmp)) {
coverJpg.close();
return false;
}
// Use smaller target size for Continue Reading card (half of screen: 240x400)
@@ -651,6 +656,7 @@ bool Epub::generateThumbBmp(int height) const {
int THUMB_TARGET_HEIGHT = height;
const bool success = JpegToBmpConverter::jpegFileTo1BitBmpStreamWithSize(coverJpg, thumbBmp, THUMB_TARGET_WIDTH,
THUMB_TARGET_HEIGHT);
// Explicitly close() files before calling Storage.remove()
coverJpg.close();
thumbBmp.close();
Storage.remove(coverJpgTempPath.c_str());
@@ -670,6 +676,7 @@ bool Epub::generateThumbBmp(int height) const {
return false;
}
readItemContentsToStream(coverImageHref, coverPng, 1024);
// Explicitly close() file before reopening for reading
coverPng.close();
if (!Storage.openFileForRead("EBP", coverPngTempPath, coverPng)) {
@@ -678,13 +685,13 @@ bool Epub::generateThumbBmp(int height) const {
FsFile thumbBmp;
if (!Storage.openFileForWrite("EBP", getThumbBmpPath(height), thumbBmp)) {
coverPng.close();
return false;
}
int THUMB_TARGET_WIDTH = height * 0.6;
int THUMB_TARGET_HEIGHT = height;
const bool success =
PngToBmpConverter::pngFileTo1BitBmpStreamWithSize(coverPng, thumbBmp, THUMB_TARGET_WIDTH, THUMB_TARGET_HEIGHT);
// Explicitly close() files before calling Storage.remove()
coverPng.close();
thumbBmp.close();
Storage.remove(coverPngTempPath.c_str());
@@ -702,7 +709,6 @@ bool Epub::generateThumbBmp(int height) const {
// Write an empty bmp file to avoid generation attempts in the future
FsFile thumbBmp;
Storage.openFileForWrite("EBP", getThumbBmpPath(height), thumbBmp);
thumbBmp.close();
return false;
}