File locking deprecation replacements (#593)
This commit is contained in:
committed by
GitHub
parent
d1f06cb774
commit
58a529cc44
@@ -30,13 +30,11 @@ static bool loadVersionFromFile(const char* path, AssetVersion& version) {
|
||||
// Read file content
|
||||
std::string content;
|
||||
{
|
||||
auto lock = file::getLock(path);
|
||||
lock->lock(portMAX_DELAY);
|
||||
file::FileMutexGuard guard(path);
|
||||
|
||||
FILE* fp = fopen(path, "r");
|
||||
if (!fp) {
|
||||
LOG_E(TAG, "Failed to open version file: %s", path);
|
||||
lock->unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -44,7 +42,6 @@ static bool loadVersionFromFile(const char* path, AssetVersion& version) {
|
||||
size_t bytesRead = fread(buffer, 1, sizeof(buffer) - 1, fp);
|
||||
bool readError = ferror(fp) != 0;
|
||||
fclose(fp);
|
||||
lock->unlock();
|
||||
|
||||
if (readError) {
|
||||
LOG_E(TAG, "Error reading version file: %s", path);
|
||||
@@ -117,9 +114,8 @@ static bool saveVersionToFile(const char* path, const AssetVersion& version) {
|
||||
// Write to file
|
||||
bool success = false;
|
||||
{
|
||||
auto lock = file::getLock(path);
|
||||
lock->lock(portMAX_DELAY);
|
||||
|
||||
file::FileMutexGuard guard(path);
|
||||
|
||||
FILE* fp = fopen(path, "w");
|
||||
if (fp) {
|
||||
size_t len = strlen(jsonString);
|
||||
@@ -139,7 +135,6 @@ static bool saveVersionToFile(const char* path, const AssetVersion& version) {
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
lock->unlock();
|
||||
}
|
||||
|
||||
cJSON_free(jsonString);
|
||||
|
||||
@@ -1703,8 +1703,7 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
httpd_resp_set_type(request, "image/png");
|
||||
httpd_resp_set_hdr(request, "Cache-Control", "public, max-age=86400");
|
||||
|
||||
auto lock = file::getLock(faviconPath);
|
||||
lock->lock(portMAX_DELAY);
|
||||
file::FileMutexGuard guard(faviconPath);
|
||||
|
||||
FILE* fp = fopen(faviconPath, "rb");
|
||||
if (fp) {
|
||||
@@ -1713,17 +1712,14 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
while ((bytesRead = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
|
||||
if (httpd_resp_send_chunk(request, buffer, bytesRead) != ESP_OK) {
|
||||
fclose(fp);
|
||||
lock->unlock();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
lock->unlock();
|
||||
httpd_resp_send_chunk(request, nullptr, 0);
|
||||
LOG_I(TAG, "[200] %s (favicon)", uri);
|
||||
return ESP_OK;
|
||||
}
|
||||
lock->unlock();
|
||||
}
|
||||
// If favicon not found, return 404 silently (browsers handle this gracefully)
|
||||
httpd_resp_send_err(request, HTTPD_404_NOT_FOUND, "Not found");
|
||||
@@ -1752,9 +1748,8 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
httpd_resp_set_type(request, getContentType(dataPath));
|
||||
|
||||
// Read and send file using standard C FILE* operations
|
||||
auto lock = file::getLock(dataPath);
|
||||
lock->lock(portMAX_DELAY);
|
||||
|
||||
file::FileMutexGuard guard(dataPath);
|
||||
|
||||
FILE* fp = fopen(dataPath.c_str(), "rb");
|
||||
if (fp) {
|
||||
char buffer[512];
|
||||
@@ -1762,18 +1757,15 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
while ((bytesRead = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
|
||||
if (httpd_resp_send_chunk(request, buffer, bytesRead) != ESP_OK) {
|
||||
fclose(fp);
|
||||
lock->unlock();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
lock->unlock();
|
||||
|
||||
httpd_resp_send_chunk(request, nullptr, 0); // End of chunks
|
||||
LOG_I(TAG, "[200] %s (from Data)", uri);
|
||||
return ESP_OK;
|
||||
}
|
||||
lock->unlock();
|
||||
}
|
||||
|
||||
// Fallback to SD card
|
||||
@@ -1781,9 +1773,8 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
if (file::isFile(sdPath.c_str())) {
|
||||
httpd_resp_set_type(request, getContentType(sdPath));
|
||||
|
||||
auto lock = file::getLock(sdPath);
|
||||
lock->lock(portMAX_DELAY);
|
||||
|
||||
file::FileMutexGuard guard(sdPath);
|
||||
|
||||
FILE* fp = fopen(sdPath.c_str(), "rb");
|
||||
if (fp) {
|
||||
char buffer[512];
|
||||
@@ -1791,18 +1782,15 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
while ((bytesRead = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
|
||||
if (httpd_resp_send_chunk(request, buffer, bytesRead) != ESP_OK) {
|
||||
fclose(fp);
|
||||
lock->unlock();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
lock->unlock();
|
||||
|
||||
|
||||
httpd_resp_send_chunk(request, nullptr, 0); // End of chunks
|
||||
LOG_I(TAG, "[200] %s (from SD)", uri);
|
||||
return ESP_OK;
|
||||
}
|
||||
lock->unlock();
|
||||
}
|
||||
|
||||
// File not found
|
||||
|
||||
Reference in New Issue
Block a user