File locking deprecation replacements (#593)

This commit is contained in:
Ken Van Hoeylandt
2026-07-27 23:48:02 +02:00
committed by GitHub
parent d1f06cb774
commit 58a529cc44
16 changed files with 231 additions and 220 deletions
+25 -6
View File
@@ -1,20 +1,24 @@
/**
* All functions in this file can be safely called without manually applying file locks.
* For calls to C stdlib APIs such as fopen(), always call file::getLock(path) first!
* For calls to C stdlib APIs such as fopen(), always lock with file::FileMutexGuard(path) first!
*/
#pragma once
#include <Tactility/TactilityCore.h>
#include <Tactility/Lock.h>
#include <tactility/filesystem/file_mutex.h>
#include <cstdio>
#include <dirent.h>
#include <functional>
#include <memory>
#include <string>
#include <sys/stat.h>
#include <vector>
/**
* @warning SD card access requires a locking mechanism:
* @warning When using this in the Tactility main project, use `file::getLock()` or `file::withLock()`
* @warning When using this in the Tactility main project, use `file::FileMutexGuard`
*/
namespace tt::file {
@@ -46,10 +50,25 @@ struct FileCloser {
};
/**
* @param[in] path the path to get a lock for
* @return a lock instance (never null)
* RAII lock over TactilityKernel's file_mutex.h for the file system mount that owns `path`.
* Locks in the constructor, unlocks in the destructor - no heap allocation, no virtual dispatch.
*/
std::shared_ptr<Lock> getLock(const std::string& path) __attribute__((deprecated("Use file_mutex.h from TactilityKernel")));
class FileMutexGuard final {
FileMutex mutex {};
public:
explicit FileMutexGuard(const std::string& path) {
file_mutex_get(&mutex, path.c_str());
file_mutex_lock(&mutex);
}
~FileMutexGuard() {
file_mutex_unlock(&mutex);
}
FileMutexGuard(const FileMutexGuard&) = delete;
FileMutexGuard& operator=(const FileMutexGuard&) = delete;
};
long getSize(FILE* file);
@@ -2,12 +2,12 @@
#include <Tactility/file/File.h>
#include <tactility/filesystem/file_mutex.h>
#include <string>
#include "FileLock.h"
/**
* @warning The functionality below does NOT safely acquire file locks. Use file::getLock() or file::withLock() when using the functionality below.
* @warning The functionality below does NOT safely acquire file locks. Use file::FileMutexGuard when using the functionality below.
*/
namespace tt::file {
@@ -45,7 +45,7 @@ class ObjectFileWriter {
const uint32_t recordSize;
const uint32_t recordVersion;
const bool append;
const std::shared_ptr<Lock> lock;
FileMutex mutex {};
std::unique_ptr<FILE, FileCloser> file;
uint32_t recordsWritten = 0;
@@ -56,9 +56,10 @@ public:
filePath(std::move(filePath)),
recordSize(recordSize),
recordVersion(recordVersion),
append(append),
lock(getLock(filePath))
{}
append(append)
{
file_mutex_get(&mutex, this->filePath.c_str());
}
~ObjectFileWriter() {