File locking refactored (#631)
Remove FileMutex and wire locking into driver subsystems.
This commit is contained in:
committed by
GitHub
parent
92ca046681
commit
020aa471e2
@@ -61,7 +61,6 @@
|
||||
#include <tactility/drivers/rtc.h>
|
||||
#include <tactility/drivers/trackball.h>
|
||||
#include <tactility/drivers/uart_controller.h>
|
||||
#include <tactility/filesystem/file_mutex.h>
|
||||
#include <tactility/filesystem/file_system.h>
|
||||
#include <tactility/kernel_init.h>
|
||||
#include <tactility/log.h>
|
||||
@@ -73,9 +72,6 @@ constexpr auto* TAG = "Tactility";
|
||||
|
||||
static DispatcherHandle_t mainDispatcherHandle = dispatcher_alloc();
|
||||
|
||||
void initFileMutexForLvgl();
|
||||
void deinitFileMutexForLvgl();
|
||||
|
||||
namespace {
|
||||
|
||||
void mainDispatcherTrampoline(void* context) {
|
||||
@@ -321,19 +317,12 @@ void createTempDirectory() {
|
||||
auto data_path = getDataPath();
|
||||
auto temp_path = std::format("{}/tmp", data_path);
|
||||
if (!file::isDirectory(temp_path)) {
|
||||
FileMutex mutex;
|
||||
file_mutex_get(&mutex, data_path.c_str());
|
||||
if (file_mutex_try_lock(&mutex, 1000 / portTICK_PERIOD_MS)) {
|
||||
if (!file::findOrCreateParentDirectory(temp_path, 0777)) {
|
||||
LOG_E(TAG, "Failed to create %s", data_path.c_str());
|
||||
} else if (mkdir(temp_path.c_str(), 0777) == 0) {
|
||||
LOG_I(TAG, "Created %s", temp_path.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to create %s", temp_path.c_str());
|
||||
}
|
||||
file_mutex_unlock(&mutex);
|
||||
if (!file::findOrCreateParentDirectory(temp_path, 0777)) {
|
||||
LOG_E(TAG, "Failed to create %s", data_path.c_str());
|
||||
} else if (mkdir(temp_path.c_str(), 0777) == 0) {
|
||||
LOG_I(TAG, "Created %s", temp_path.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, data_path.c_str());
|
||||
LOG_E(TAG, "Failed to create %s", temp_path.c_str());
|
||||
}
|
||||
} else {
|
||||
LOG_I(TAG, "Found existing %s", temp_path.c_str());
|
||||
@@ -419,8 +408,6 @@ static void applySavedTouchCalibration() {
|
||||
#endif // CONFIG_TT_TOUCH_CALIBRATION_SUPPORTED
|
||||
|
||||
static void onLvglStarted() {
|
||||
initFileMutexForLvgl();
|
||||
|
||||
window_manager_configure(windowManagerScreenInit);
|
||||
check(module_ensure_started(&lvgl_window_manager_module) == ERROR_NONE);
|
||||
|
||||
@@ -451,14 +438,6 @@ static void onLvglStarted() {
|
||||
}
|
||||
|
||||
static void onLvglStopped() {
|
||||
deinitFileMutexForLvgl();
|
||||
|
||||
if (softwareKeyboard.object != nullptr) {
|
||||
lvgl_software_keyboard_destruct(&softwareKeyboard);
|
||||
}
|
||||
|
||||
module_stop(&lvgl_window_manager_module);
|
||||
|
||||
lvgl::stopKeyboardDeviceListener();
|
||||
lvgl::stopUsbHidInput();
|
||||
|
||||
@@ -474,6 +453,12 @@ static void onLvglStopped() {
|
||||
check(service::removeService(service::memorychecker::manifest.id));
|
||||
check(service::removeService(service::statusbar::manifest.id));
|
||||
|
||||
if (softwareKeyboard.object != nullptr) {
|
||||
lvgl_software_keyboard_destruct(&softwareKeyboard);
|
||||
}
|
||||
|
||||
module_stop(&lvgl_window_manager_module);
|
||||
|
||||
memory_print_stats();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,8 +21,6 @@ static bool parseEntry(const cJSON* object, AppHubEntry& entry) {
|
||||
}
|
||||
|
||||
bool parseJson(const std::string& filePath, AppHubEntryList& entries) {
|
||||
file::FileMutexGuard guard(filePath);
|
||||
|
||||
auto data = file::readString(filePath);
|
||||
if (data == nullptr) {
|
||||
LOG_E(TAG, "Failed to read %s", filePath.c_str());
|
||||
|
||||
@@ -110,7 +110,6 @@ void writeCrashLogFile(const CrashData& crashData) {
|
||||
}
|
||||
|
||||
std::string path = std::string(root) + "/crash.txt";
|
||||
file::FileMutexGuard guard(path);
|
||||
if (!file::writeString(path, formatCrashData(crashData))) {
|
||||
LOG_E(TAG, "Failed to write %s", path.c_str());
|
||||
}
|
||||
|
||||
@@ -13,17 +13,17 @@
|
||||
#include <Tactility/file/File.h>
|
||||
#include <Tactility/Platform.h>
|
||||
#include <Tactility/StringUtils.h>
|
||||
#include <Tactility/Tactility.h>
|
||||
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/usb_host_msc.h>
|
||||
#include <tactility/filesystem/file_mutex.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace tt::app::files {
|
||||
@@ -106,31 +106,13 @@ static void onPastePressedCallback(lv_event_t* event) {
|
||||
// region File helpers
|
||||
|
||||
static bool copyFileContents(const std::string& src, const std::string& dst) {
|
||||
FileMutex src_mutex;
|
||||
file_mutex_get(&src_mutex, src.c_str());
|
||||
FileMutex dst_mutex;
|
||||
file_mutex_get(&dst_mutex, dst.c_str());
|
||||
const bool same_lock = (src_mutex.lock == dst_mutex.lock &&
|
||||
src_mutex.try_lock == dst_mutex.try_lock &&
|
||||
src_mutex.unlock == dst_mutex.unlock);
|
||||
|
||||
auto unlock_all = [&] {
|
||||
if (!same_lock) file_mutex_unlock(&dst_mutex);
|
||||
file_mutex_unlock(&src_mutex);
|
||||
};
|
||||
|
||||
file_mutex_lock(&src_mutex);
|
||||
if (!same_lock) file_mutex_lock(&dst_mutex);
|
||||
|
||||
FILE* in = fopen(src.c_str(), "rb");
|
||||
if (in == nullptr) {
|
||||
unlock_all();
|
||||
return false;
|
||||
}
|
||||
FILE* out = fopen(dst.c_str(), "wb");
|
||||
if (out == nullptr) {
|
||||
fclose(in);
|
||||
unlock_all();
|
||||
return false;
|
||||
}
|
||||
uint8_t buf[512];
|
||||
@@ -152,7 +134,6 @@ static bool copyFileContents(const std::string& src, const std::string& dst) {
|
||||
if (!success) {
|
||||
remove(dst.c_str());
|
||||
}
|
||||
unlock_all();
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -162,33 +143,23 @@ static bool copyRecursive(const std::string& src, const std::string& dst) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Process one entry at a time: release the device lock between iterations
|
||||
// so other SPI bus users aren't starved, and stop immediately on failure.
|
||||
FileMutex mutex;
|
||||
file_mutex_get(&mutex, src.c_str());
|
||||
file_mutex_lock(&mutex);
|
||||
DIR* dir = opendir(src.c_str());
|
||||
if (!dir) {
|
||||
file_mutex_unlock(&mutex);
|
||||
file::deleteRecursively(dst);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool success = true;
|
||||
while (success) {
|
||||
struct dirent* entry = readdir(dir);
|
||||
dirent* entry = readdir(dir);
|
||||
if (!entry) break;
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
|
||||
|
||||
std::string name = entry->d_name; // copy before releasing lock
|
||||
file_mutex_unlock(&mutex);
|
||||
|
||||
success = copyRecursive(file::getChildPath(src, name), file::getChildPath(dst, name));
|
||||
|
||||
file_mutex_lock(&mutex);
|
||||
}
|
||||
closedir(dir);
|
||||
file_mutex_unlock(&mutex);
|
||||
|
||||
if (!success) {
|
||||
file::deleteRecursively(dst);
|
||||
@@ -608,7 +579,6 @@ void View::onResult(uint32_t launchId, int32_t result) {
|
||||
LOG_W(TAG, "Failed to delete %s", filepath.c_str());
|
||||
}
|
||||
} else if (file::isFile(filepath)) {
|
||||
file::FileMutexGuard guard(filepath);
|
||||
if (remove(filepath.c_str()) != 0) {
|
||||
LOG_W(TAG, "Failed to delete %s", filepath.c_str());
|
||||
}
|
||||
@@ -623,20 +593,17 @@ void View::onResult(uint32_t launchId, int32_t result) {
|
||||
std::string new_name = resultText;
|
||||
if (!new_name.empty() && new_name != state->getSelectedChildEntry()) {
|
||||
std::string rename_to = file::getChildPath(state->getCurrentPath(), new_name);
|
||||
{
|
||||
file::FileMutexGuard guard(filepath);
|
||||
struct stat st;
|
||||
if (stat(rename_to.c_str(), &st) == 0) {
|
||||
LOG_W(TAG, "Rename: destination already exists: \"%s\"", rename_to.c_str());
|
||||
state->setPendingAction(State::ActionNone);
|
||||
alertdialog::start(appInstanceId, "Rename failed", "\"" + new_name + "\" already exists.");
|
||||
break;
|
||||
}
|
||||
if (rename(filepath.c_str(), rename_to.c_str()) == 0) {
|
||||
LOG_I(TAG, "Renamed \"%s\" to \"%s\"", filepath.c_str(), rename_to.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to rename \"%s\" to \"%s\"", filepath.c_str(), rename_to.c_str());
|
||||
}
|
||||
struct stat st;
|
||||
if (stat(rename_to.c_str(), &st) == 0) {
|
||||
LOG_W(TAG, "Rename: destination already exists: \"%s\"", rename_to.c_str());
|
||||
state->setPendingAction(State::ActionNone);
|
||||
alertdialog::start(appInstanceId, "Rename failed", "\"" + new_name + "\" already exists.");
|
||||
break;
|
||||
}
|
||||
if (rename(filepath.c_str(), rename_to.c_str()) == 0) {
|
||||
LOG_I(TAG, "Renamed \"%s\" to \"%s\"", filepath.c_str(), rename_to.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to rename \"%s\" to \"%s\"", filepath.c_str(), rename_to.c_str());
|
||||
}
|
||||
|
||||
state->setEntriesForPath(state->getCurrentPath());
|
||||
@@ -649,22 +616,21 @@ void View::onResult(uint32_t launchId, int32_t result) {
|
||||
if (!filename.empty()) {
|
||||
std::string new_file_path = file::getChildPath(state->getCurrentPath(), filename);
|
||||
|
||||
{
|
||||
file::FileMutexGuard guard(new_file_path);
|
||||
|
||||
struct stat st;
|
||||
if (stat(new_file_path.c_str(), &st) == 0) {
|
||||
LOG_W(TAG, "File already exists: \"%s\"", new_file_path.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
FILE* new_file = fopen(new_file_path.c_str(), "w");
|
||||
// O_CREAT | O_EXCL makes creation+existence-check one atomic operation, unlike a separate stat() before fopen()
|
||||
int fd = open(new_file_path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0644);
|
||||
if (fd >= 0) {
|
||||
FILE* new_file = fdopen(fd, "w");
|
||||
if (new_file) {
|
||||
fclose(new_file);
|
||||
LOG_I(TAG, "Created file \"%s\"", new_file_path.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to create file \"%s\"", new_file_path.c_str());
|
||||
close(fd);
|
||||
}
|
||||
LOG_I(TAG, "Created file \"%s\"", new_file_path.c_str());
|
||||
} else if (errno == EEXIST) {
|
||||
LOG_W(TAG, "File already exists: \"%s\"", new_file_path.c_str());
|
||||
break;
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to create file \"%s\"", new_file_path.c_str());
|
||||
}
|
||||
|
||||
state->setEntriesForPath(state->getCurrentPath());
|
||||
@@ -677,20 +643,16 @@ void View::onResult(uint32_t launchId, int32_t result) {
|
||||
if (!foldername.empty()) {
|
||||
std::string new_folder_path = file::getChildPath(state->getCurrentPath(), foldername);
|
||||
|
||||
{
|
||||
file::FileMutexGuard guard(new_folder_path);
|
||||
struct stat st;
|
||||
if (stat(new_folder_path.c_str(), &st) == 0) {
|
||||
LOG_W(TAG, "Folder already exists: \"%s\"", new_folder_path.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
if (stat(new_folder_path.c_str(), &st) == 0) {
|
||||
LOG_W(TAG, "Folder already exists: \"%s\"", new_folder_path.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
if (mkdir(new_folder_path.c_str(), 0755) == 0) {
|
||||
LOG_I(TAG, "Created folder \"%s\"", new_folder_path.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to create folder \"%s\"", new_folder_path.c_str());
|
||||
}
|
||||
if (mkdir(new_folder_path.c_str(), 0755) == 0) {
|
||||
LOG_I(TAG, "Created folder \"%s\"", new_folder_path.c_str());
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to create folder \"%s\"", new_folder_path.c_str());
|
||||
}
|
||||
|
||||
state->setEntriesForPath(state->getCurrentPath());
|
||||
@@ -709,12 +671,9 @@ void View::onResult(uint32_t launchId, int32_t result) {
|
||||
// Revalidate right before the destructive delete so we only ever
|
||||
// remove the exact file the user agreed to overwrite.
|
||||
bool dst_unchanged;
|
||||
{
|
||||
file::FileMutexGuard guard(dst);
|
||||
struct stat current_stat {};
|
||||
dst_unchanged = (stat(dst.c_str(), ¤t_stat) == 0) &&
|
||||
state->pendingPasteDstMatches(current_stat);
|
||||
}
|
||||
struct stat current_stat {};
|
||||
dst_unchanged = (stat(dst.c_str(), ¤t_stat) == 0) &&
|
||||
state->pendingPasteDstMatches(current_stat);
|
||||
state->clearPendingPasteDstStat();
|
||||
|
||||
if (!dst_unchanged) {
|
||||
@@ -780,13 +739,6 @@ void View::onPastePressed() {
|
||||
std::string entry_name = file::getLastPathSegment(src);
|
||||
std::string dst = file::getChildPath(state->getCurrentPath(), entry_name);
|
||||
|
||||
// Note: FileMutexGuard(src) guards the source path; the existence check below is
|
||||
// against dst, so there is a TOCTOU gap between this check and the write inside
|
||||
// doPaste. When dst exists, the overwrite-confirm path below re-validates dst's
|
||||
// stat immediately before the destructive delete (see ActionPaste in onResult),
|
||||
// closing the window that matters (the dialog being open). When dst does not
|
||||
// exist here, doPaste's write can still race a concurrent creator; acceptable on
|
||||
// a single-user embedded device.
|
||||
if (src == dst) {
|
||||
LOG_I(TAG, "Paste: source and destination are the same path, skipping");
|
||||
return;
|
||||
@@ -794,12 +746,9 @@ void View::onPastePressed() {
|
||||
|
||||
bool dst_exists;
|
||||
struct stat dst_stat {};
|
||||
{
|
||||
file::FileMutexGuard guard(src);
|
||||
dst_exists = (stat(dst.c_str(), &dst_stat) == 0);
|
||||
}
|
||||
|
||||
if (dst_exists) {
|
||||
// If dst exists...
|
||||
if (stat(dst.c_str(), &dst_stat) == 0) {
|
||||
state->setPendingPasteDst(dst);
|
||||
state->setPendingPasteDstStat(dst_stat);
|
||||
state->setPendingAction(State::ActionPaste);
|
||||
@@ -815,11 +764,7 @@ void View::doPaste(const std::string& src, bool is_cut, const std::string& dst)
|
||||
bool success = false;
|
||||
bool src_delete_failed = false;
|
||||
if (is_cut) {
|
||||
{
|
||||
file::FileMutexGuard guard(src);
|
||||
success = (rename(src.c_str(), dst.c_str()) == 0);
|
||||
}
|
||||
if (!success) {
|
||||
if (rename(src.c_str(), dst.c_str()) != 0) {
|
||||
// Fallback for cross-filesystem moves: copy then delete.
|
||||
// Only mark success if both halves succeed — if the source removal
|
||||
// fails we leave success=false so the clipboard is preserved and
|
||||
|
||||
@@ -46,8 +46,6 @@ void resetFileContent(Context* ctx) {
|
||||
}
|
||||
|
||||
void openFile(Context* ctx, const std::string& path) {
|
||||
// We might be reading from the SD card, which could share a SPI bus with other devices (display)
|
||||
file::FileMutexGuard guard(path);
|
||||
auto data = file::readString(path);
|
||||
if (data != nullptr) {
|
||||
lvgl_lock();
|
||||
@@ -60,15 +58,11 @@ void openFile(Context* ctx, const std::string& path) {
|
||||
}
|
||||
|
||||
bool saveFile(Context* ctx, const std::string& path) {
|
||||
// We might be writing to SD card, which could share a SPI bus with other devices (display)
|
||||
bool result = false;
|
||||
{
|
||||
file::FileMutexGuard guard(path);
|
||||
if (file::writeString(path, ctx->saveBuffer.c_str())) {
|
||||
LOG_I(TAG, "Saved to %s", path.c_str());
|
||||
ctx->filePath = path;
|
||||
result = true;
|
||||
}
|
||||
if (file::writeString(path, ctx->saveBuffer.c_str())) {
|
||||
LOG_I(TAG, "Saved to %s", path.c_str());
|
||||
ctx->filePath = path;
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@ bool isCompleted() {
|
||||
LOG_E(TAG, "Setup path not found");
|
||||
return false;
|
||||
}
|
||||
file::FileMutexGuard guard(path);
|
||||
return file::isFile(path);
|
||||
}
|
||||
|
||||
@@ -69,7 +68,6 @@ void markCompleted() {
|
||||
if (!getCompletedMarkerPath(path)) {
|
||||
return;
|
||||
}
|
||||
file::FileMutexGuard guard(path);
|
||||
file::writeString(path, "");
|
||||
}
|
||||
|
||||
|
||||
@@ -42,8 +42,6 @@ bool listDirectory(
|
||||
const std::string& path,
|
||||
std::function<void(const dirent&)> onEntry
|
||||
) {
|
||||
FileMutexGuard guard(path);
|
||||
|
||||
LOG_I(TAG, "listDir start %s", path.c_str());
|
||||
DIR* dir = opendir(path.c_str());
|
||||
if (dir == nullptr) {
|
||||
@@ -68,8 +66,6 @@ int scandir(
|
||||
ScandirFilter filterMethod,
|
||||
ScandirSort sortMethod
|
||||
) {
|
||||
FileMutexGuard guard(path);
|
||||
|
||||
LOG_I(TAG, "scandir start");
|
||||
DIR* dir = opendir(path.c_str());
|
||||
if (dir == nullptr) {
|
||||
@@ -193,8 +189,6 @@ bool writeString(const std::string& filepath, const std::string& content) {
|
||||
}
|
||||
|
||||
static bool findOrCreateDirectoryInternal(std::string path, mode_t mode) {
|
||||
FileMutexGuard guard(path);
|
||||
|
||||
struct stat dir_stat;
|
||||
if (mkdir(path.c_str(), mode) == 0) {
|
||||
return true;
|
||||
@@ -310,29 +304,23 @@ bool deleteRecursively(const std::string& path) {
|
||||
}
|
||||
|
||||
bool deleteFile(const std::string& path) {
|
||||
FileMutexGuard guard(path);
|
||||
return remove(path.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool deleteDirectory(const std::string& path) {
|
||||
FileMutexGuard guard(path);
|
||||
return rmdir(path.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool isFile(const std::string& path) {
|
||||
FileMutexGuard guard(path);
|
||||
return access(path.c_str(), F_OK) == 0;
|
||||
}
|
||||
|
||||
bool isDirectory(const std::string& path) {
|
||||
FileMutexGuard guard(path);
|
||||
struct stat stat_result;
|
||||
return stat(path.c_str(), &stat_result) == 0 && S_ISDIR(stat_result.st_mode);
|
||||
}
|
||||
|
||||
bool readLines(const std::string& filePath, bool stripNewLine, std::function<void(const char* line)> callback) {
|
||||
FileMutexGuard guard(filePath);
|
||||
|
||||
auto* file = fopen(filePath.c_str(), "r");
|
||||
if (file == nullptr) {
|
||||
return false;
|
||||
|
||||
@@ -1,135 +0,0 @@
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/display.h>
|
||||
#include <tactility/drivers/sdcard.h>
|
||||
#include <tactility/drivers/spi_controller.h>
|
||||
#include <tactility/filesystem/file_mutex.h>
|
||||
#include <tactility/filesystem/file_system.h>
|
||||
#include <lvgl/lvgl.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
constexpr auto* TAG = "file_mutex_lvgl";
|
||||
|
||||
struct Device;
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<FileMutexId> registered_ids;
|
||||
|
||||
void wrapped_lvgl_lock() {
|
||||
if (!lvgl_is_running()) return;
|
||||
lvgl_lock();
|
||||
}
|
||||
|
||||
bool wrapped_lvgl_try_lock(uint32_t timeout) {
|
||||
// Return lock success, so the file operation can continue when LVGL is not running
|
||||
// lvgl_try_lock() fails to lock if LVGL is not running
|
||||
if (!lvgl_is_running()) return true;
|
||||
return lvgl_try_lock(timeout);
|
||||
}
|
||||
void wrapped_lvgl_unlock() {
|
||||
if (!lvgl_is_running()) return;
|
||||
lvgl_unlock();
|
||||
}
|
||||
|
||||
const FileMutex lvgl_mutex = {
|
||||
.lock = wrapped_lvgl_lock,
|
||||
.try_lock = wrapped_lvgl_try_lock,
|
||||
.unlock = wrapped_lvgl_unlock,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace tt {
|
||||
|
||||
/**
|
||||
* Finds file systems with a device (e.g. sd card) that is owned by a SPI controller.
|
||||
* If the SPI controller has a display on the bus, we create an LVGL lock for the file system path.
|
||||
*/
|
||||
void initFileMutexForLvgl() {
|
||||
file_system_for_each(®istered_ids, [](FileSystem* fs, void* context) {
|
||||
char mount_path[64];
|
||||
if (file_system_get_path(fs, mount_path, sizeof(mount_path)) != ERROR_NONE) {
|
||||
return true;
|
||||
}
|
||||
LOG_D(TAG, "Mount path %s", mount_path);
|
||||
|
||||
// We only care about file system with a Device (owner)
|
||||
auto* owner = file_system_get_owner(fs);
|
||||
if (owner == nullptr) {
|
||||
LOG_D(TAG, "Owner: none");
|
||||
return true;
|
||||
}
|
||||
|
||||
LOG_D(TAG, "Owner: %s", owner->name);
|
||||
|
||||
// Ignore devices without a parent (root)
|
||||
auto* parent = device_get_parent(owner);
|
||||
if (parent == nullptr) {
|
||||
LOG_D(TAG, "Owner: no parent");
|
||||
return true;
|
||||
}
|
||||
|
||||
LOG_D(TAG, "Owner: parent %s", parent->name);
|
||||
|
||||
// If the FileSystem is on a SPI bus and there's more than 1 device, we assume the other one is the display.
|
||||
auto* type = device_get_type(parent);
|
||||
if (type != &SPI_CONTROLLER_TYPE || device_get_child_count(parent) <= 1) {
|
||||
LOG_D(TAG, "Owner parent not SPI controller or not enough children");
|
||||
return true;
|
||||
}
|
||||
|
||||
struct Context {
|
||||
const char* mountPath;
|
||||
std::vector<FileMutexId>* registeredIds;
|
||||
};
|
||||
Context ctx = { .mountPath = mount_path, .registeredIds = static_cast<std::vector<FileMutexId>*>(context) };
|
||||
|
||||
device_for_each_child(parent, &ctx, [](Device* child, void* context) -> bool {
|
||||
Context* ctx = static_cast<Context*>(context);
|
||||
if (device_get_type(child) == &DISPLAY_TYPE) {
|
||||
LOG_I(TAG, "Adding file mutex for %s as it shares a bus with a display", ctx->mountPath);
|
||||
ctx->registeredIds->push_back(file_mutex_add(&lvgl_mutex, ctx->mountPath));
|
||||
return false;
|
||||
} else {
|
||||
LOG_D(TAG, "child of parent, %s: not DISPLAY_TYPE", child->name);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
// SDMMC-backed SD cards aren't parented under SPI_CONTROLLER_TYPE, so the pass above never
|
||||
// sees them - but on some chips (classic ESP32) SDMMC and SPI still contend for DMA/bus
|
||||
// access. Lock every SD card mount if a display exists anywhere, regardless of bus topology.
|
||||
if (!device_exists_of_type(&DISPLAY_TYPE)) {
|
||||
return;
|
||||
}
|
||||
|
||||
file_system_for_each(®istered_ids, [](FileSystem* fs, void* context) {
|
||||
char mount_path[64];
|
||||
if (file_system_get_path(fs, mount_path, sizeof(mount_path)) != ERROR_NONE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto* owner = file_system_get_owner(fs);
|
||||
if (owner == nullptr || device_get_type(owner) != &SDCARD_TYPE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
LOG_I(TAG, "Adding file mutex for %s (SD card) - a display is present and may contend for bus/DMA resources", mount_path);
|
||||
auto* ids = static_cast<std::vector<FileMutexId>*>(context);
|
||||
ids->push_back(file_mutex_add(&lvgl_mutex, mount_path));
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
void deinitFileMutexForLvgl() {
|
||||
for (FileMutexId id : registered_ids) {
|
||||
file_mutex_remove(id);
|
||||
}
|
||||
registered_ids.clear();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,12 +4,7 @@
|
||||
namespace tt::lvgl {
|
||||
|
||||
bool label_set_text_file(lv_obj_t* label, const char* filepath) {
|
||||
std::unique_ptr<uint8_t[]> text;
|
||||
{
|
||||
file::FileMutexGuard guard(filepath);
|
||||
text = file::readString(filepath);
|
||||
}
|
||||
|
||||
std::unique_ptr<uint8_t[]> text = file::readString(filepath);
|
||||
if (text != nullptr) {
|
||||
lv_label_set_text(label, reinterpret_cast<const char*>(text.get()));
|
||||
return true;
|
||||
|
||||
@@ -67,8 +67,7 @@ void download(
|
||||
|
||||
auto bytes_left = client->getContentLength();
|
||||
|
||||
file::FileMutexGuard guard(downloadFilePath);
|
||||
LOG_I(TAG, "opening %s", downloadFilePath.c_str());
|
||||
LOG_I(TAG, "Opening %s", downloadFilePath.c_str());
|
||||
auto* file = fopen(downloadFilePath.c_str(), "wb");
|
||||
if (file == nullptr) {
|
||||
onError("Failed to open file");
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include <Tactility/StringUtils.h>
|
||||
#include <Tactility/network/HttpdReq.h>
|
||||
|
||||
#include <tactility/filesystem/file_mutex.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <memory>
|
||||
@@ -186,16 +185,7 @@ size_t receiveFile(httpd_req_t* request, size_t length, const std::string& fileP
|
||||
char buffer[BUFFER_SIZE];
|
||||
size_t bytes_received = 0;
|
||||
|
||||
// Locked only around each actual disk I/O call below, not across the httpd_req_recv() waits
|
||||
// in between - this file's mutex may resolve to lvgl_lock() (see FileMutexLvgl.cpp), and
|
||||
// holding that for the whole (potentially multi-second) network transfer starves LVGL's own
|
||||
// task for the entire upload instead of just for each brief write.
|
||||
FileMutex mutex {};
|
||||
file_mutex_get(&mutex, filePath.c_str());
|
||||
|
||||
file_mutex_lock(&mutex);
|
||||
auto* file = fopen(filePath.c_str(), "wb");
|
||||
file_mutex_unlock(&mutex);
|
||||
if (file == nullptr) {
|
||||
LOG_E(TAG, "Failed to open file for writing: %s", filePath.c_str());
|
||||
return 0;
|
||||
@@ -226,19 +216,14 @@ size_t receiveFile(httpd_req_t* request, size_t length, const std::string& fileP
|
||||
timeout_retries = 0;
|
||||
size_t receive_chunk_size = (size_t)received;
|
||||
|
||||
file_mutex_lock(&mutex);
|
||||
bool write_ok = fwrite(buffer, 1, receive_chunk_size, file) == receive_chunk_size;
|
||||
file_mutex_unlock(&mutex);
|
||||
if (!write_ok) {
|
||||
if (fwrite(buffer, 1, receive_chunk_size, file) != receive_chunk_size) {
|
||||
LOG_E(TAG, "Failed to write all bytes");
|
||||
break;
|
||||
}
|
||||
bytes_received += receive_chunk_size;
|
||||
}
|
||||
|
||||
file_mutex_lock(&mutex);
|
||||
fclose(file);
|
||||
file_mutex_unlock(&mutex);
|
||||
return bytes_received;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,32 +29,28 @@ static bool loadVersionFromFile(const char* path, AssetVersion& version) {
|
||||
|
||||
// Read file content
|
||||
std::string content;
|
||||
{
|
||||
file::FileMutexGuard guard(path);
|
||||
|
||||
FILE* fp = fopen(path, "r");
|
||||
if (!fp) {
|
||||
LOG_E(TAG, "Failed to open version file: %s", path);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
size_t bytesRead = fread(buffer, 1, sizeof(buffer) - 1, fp);
|
||||
bool readError = ferror(fp) != 0;
|
||||
fclose(fp);
|
||||
|
||||
if (readError) {
|
||||
LOG_E(TAG, "Error reading version file: %s", path);
|
||||
return false;
|
||||
}
|
||||
if (bytesRead == 0) {
|
||||
LOG_E(TAG, "Version file is empty: %s", path);
|
||||
return false;
|
||||
}
|
||||
buffer[bytesRead] = '\0';
|
||||
content = buffer;
|
||||
FILE* fp = fopen(path, "r");
|
||||
if (!fp) {
|
||||
LOG_E(TAG, "Failed to open version file: %s", path);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
size_t bytesRead = fread(buffer, 1, sizeof(buffer) - 1, fp);
|
||||
bool readError = ferror(fp) != 0;
|
||||
fclose(fp);
|
||||
|
||||
if (readError) {
|
||||
LOG_E(TAG, "Error reading version file: %s", path);
|
||||
return false;
|
||||
}
|
||||
if (bytesRead == 0) {
|
||||
LOG_E(TAG, "Version file is empty: %s", path);
|
||||
return false;
|
||||
}
|
||||
buffer[bytesRead] = '\0';
|
||||
content = buffer;
|
||||
|
||||
// Parse JSON
|
||||
cJSON* json = cJSON_Parse(content.c_str());
|
||||
if (json == nullptr) {
|
||||
@@ -113,30 +109,26 @@ static bool saveVersionToFile(const char* path, const AssetVersion& version) {
|
||||
|
||||
// Write to file
|
||||
bool success = false;
|
||||
{
|
||||
file::FileMutexGuard guard(path);
|
||||
|
||||
FILE* fp = fopen(path, "w");
|
||||
if (fp) {
|
||||
size_t len = strlen(jsonString);
|
||||
size_t written = fwrite(jsonString, 1, len, fp);
|
||||
success = (written == len);
|
||||
if (success) {
|
||||
if (fflush(fp) != 0) {
|
||||
LOG_E(TAG, "Failed to flush version file: %s", path);
|
||||
FILE* fp = fopen(path, "w");
|
||||
if (fp) {
|
||||
size_t len = strlen(jsonString);
|
||||
size_t written = fwrite(jsonString, 1, len, fp);
|
||||
success = (written == len);
|
||||
if (success) {
|
||||
if (fflush(fp) != 0) {
|
||||
LOG_E(TAG, "Failed to flush version file: %s", path);
|
||||
success = false;
|
||||
} else {
|
||||
int fd = fileno(fp);
|
||||
if (fd >= 0 && fsync(fd) != 0) {
|
||||
LOG_E(TAG, "Failed to fsync version file: %s", path);
|
||||
success = false;
|
||||
} else {
|
||||
int fd = fileno(fp);
|
||||
if (fd >= 0 && fsync(fd) != 0) {
|
||||
LOG_E(TAG, "Failed to fsync version file: %s", path);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
cJSON_free(jsonString);
|
||||
cJSON_Delete(json);
|
||||
|
||||
|
||||
@@ -1700,8 +1700,6 @@ 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");
|
||||
|
||||
file::FileMutexGuard guard(faviconPath);
|
||||
|
||||
FILE* fp = fopen(faviconPath, "rb");
|
||||
if (fp) {
|
||||
char buffer[512];
|
||||
@@ -1743,9 +1741,6 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
// Try to serve from Data partition first
|
||||
if (file::isFile(dataPath.c_str())) {
|
||||
httpd_resp_set_type(request, getContentType(dataPath));
|
||||
|
||||
// Read and send file using standard C FILE* operations
|
||||
file::FileMutexGuard guard(dataPath);
|
||||
|
||||
FILE* fp = fopen(dataPath.c_str(), "rb");
|
||||
if (fp) {
|
||||
@@ -1769,8 +1764,6 @@ esp_err_t WebServerService::handleAssets(httpd_req_t* request) {
|
||||
std::string sdPath = std::string("/sdcard/tactility/webserver") + requestedPath;
|
||||
if (file::isFile(sdPath.c_str())) {
|
||||
httpd_resp_set_type(request, getContentType(sdPath));
|
||||
|
||||
file::FileMutexGuard guard(sdPath);
|
||||
|
||||
FILE* fp = fopen(sdPath.c_str(), "rb");
|
||||
if (fp) {
|
||||
|
||||
Reference in New Issue
Block a user