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
+8 -47
View File
@@ -4,9 +4,7 @@
#include <fstream>
#include <unistd.h>
#include <tactility/filesystem/file_mutex.h>
#include <tactility/log.h>
#include <Tactility/Mutex.h>
#include <Tactility/StringUtils.h>
namespace tt::hal::sdcard {
@@ -17,27 +15,6 @@ namespace tt::file {
constexpr auto* TAG = "file";
class FileMutexLock final : public Lock {
FileMutex mutex;
public:
explicit FileMutexLock(const std::string& path) {
file_mutex_get(&mutex, path.c_str());
}
bool lock(TickType_t timeout) const override {
return file_mutex_try_lock(&mutex, timeout);
}
void unlock() const override {
file_mutex_unlock(&mutex);
}
};
std::shared_ptr<Lock> getLock(const std::string& path) {
return std::make_shared<FileMutexLock>(path);
}
std::string getChildPath(const std::string& basePath, const std::string& childPath) {
// Postfix with "/" when the current path isn't "/"
if (basePath.length() != 1) {
@@ -65,9 +42,7 @@ bool listDirectory(
const std::string& path,
std::function<void(const dirent&)> onEntry
) {
auto lockable = getLock(path);
auto lock = lockable->asScopedLock();
lock.lock();
FileMutexGuard guard(path);
LOG_I(TAG, "listDir start %s", path.c_str());
DIR* dir = opendir(path.c_str());
@@ -93,9 +68,7 @@ int scandir(
ScandirFilter filterMethod,
ScandirSort sortMethod
) {
auto lockable = getLock(path);
auto lock = lockable->asScopedLock();
lock.lock();
FileMutexGuard guard(path);
LOG_I(TAG, "scandir start");
DIR* dir = opendir(path.c_str());
@@ -220,9 +193,7 @@ bool writeString(const std::string& filepath, const std::string& content) {
}
static bool findOrCreateDirectoryInternal(std::string path, mode_t mode) {
auto lockable = getLock(path);
auto lock = lockable->asScopedLock();
lock.lock();
FileMutexGuard guard(path);
struct stat dir_stat;
if (mkdir(path.c_str(), mode) == 0) {
@@ -336,38 +307,28 @@ bool deleteRecursively(const std::string& path) {
}
bool deleteFile(const std::string& path) {
auto lockable = getLock(path);
auto lock = lockable->asScopedLock();
lock.lock();
FileMutexGuard guard(path);
return remove(path.c_str()) == 0;
}
bool deleteDirectory(const std::string& path) {
auto lockable = getLock(path);
auto lock = lockable->asScopedLock();
lock.lock();
FileMutexGuard guard(path);
return rmdir(path.c_str()) == 0;
}
bool isFile(const std::string& path) {
auto lockable = getLock(path);
auto lock = lockable->asScopedLock();
lock.lock();
FileMutexGuard guard(path);
return access(path.c_str(), F_OK) == 0;
}
bool isDirectory(const std::string& path) {
auto lockable = getLock(path);
auto lock = lockable->asScopedLock();
lock.lock();
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) {
auto lockable = getLock(filePath);
auto lock = lockable->asScopedLock();
lock.lock();
FileMutexGuard guard(filePath);
auto* file = fopen(filePath.c_str(), "r");
if (file == nullptr) {
+11 -14
View File
@@ -2,7 +2,6 @@
#include <Tactility/StringUtils.h>
#include <Tactility/file/File.h>
#include <Tactility/file/FileLock.h>
#include <tactility/log.h>
namespace tt::file {
@@ -55,22 +54,20 @@ bool loadPropertiesFile(const std::string& filePath, std::map<std::string, std::
}
bool savePropertiesFile(const std::string& filePath, const std::map<std::string, std::string>& properties) {
bool result = false;
getLock(filePath)->withLock([&result, filePath, &properties] {
LOG_I(TAG, "Saving properties file %s", filePath.c_str());
FileMutexGuard guard(filePath);
FILE* file = fopen(filePath.c_str(), "w");
if (file == nullptr) {
LOG_E(TAG, "Failed to open %s", filePath.c_str());
return;
}
LOG_I(TAG, "Saving properties file %s", filePath.c_str());
for (const auto& [key, value]: properties) { fprintf(file, "%s=%s\n", key.c_str(), value.c_str()); }
FILE* file = fopen(filePath.c_str(), "w");
if (file == nullptr) {
LOG_E(TAG, "Failed to open %s", filePath.c_str());
return false;
}
fclose(file);
result = true;
});
return result;
for (const auto& [key, value]: properties) { fprintf(file, "%s=%s\n", key.c_str(), value.c_str()); }
fclose(file);
return true;
}
}