File locking refactored (#631)
Remove FileMutex and wire locking into driver subsystems.
This commit is contained in:
committed by
GitHub
parent
92ca046681
commit
020aa471e2
@@ -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, "");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user