File locking and DevelopmentService improvements (#358)

- Moved `file::getlock(path)` from `Tactility` to `TactilityCore`
- Changed all existing `file::*` functions to implement locking by default
- Removed all manual locking where `file::*` functions were used
- When `DevelopmentService` receives a file, it doesn't try to allocate it all in memory. This fixes going out-of-memory on devices without PSRAM.
- Fix for TactilityC include
This commit is contained in:
Ken Van Hoeylandt
2025-10-05 16:16:55 +02:00
committed by GitHub
parent a05a6afaaf
commit 3802679de4
18 changed files with 224 additions and 180 deletions
@@ -1,3 +1,7 @@
/**
* 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!
*/
#pragma once
#include "Tactility/TactilityCore.h"
@@ -40,6 +44,28 @@ struct FileCloser {
}
};
typedef std::function<std::shared_ptr<Lock>(const std::string&)> FindLockFunction;
/**
* @param[in] path the path to get a lock for
* @return a lock instance (never null)
*/
std::shared_ptr<Lock> getLock(const std::string& path);
void setFindLockFunction(const FindLockFunction& function);
/**
* Acquires a lock, calls the function, then releases the lock.
* @param[in] path the path to find a lock for
* @param[in] fn the code to execute while the lock is acquired
*/
template<typename ReturnType>
ReturnType withLock(const std::string& path, std::function<ReturnType()> fn) {
const auto lock = getLock(path)->asScopedLock();
lock.lock();
return fn();
}
long getSize(FILE* file);
/** Read a file and return its data.
@@ -73,6 +99,10 @@ bool findOrCreateParentDirectory(const std::string& path, mode_t mode);
bool deleteRecursively(const std::string& path);
bool deleteFile(const std::string& path);
bool deleteDirectory(const std::string& path);
/**
* Concatenate a child path with a parent path, ensuring proper slash inbetween
* @param basePath an absolute path with or without trailing "/"