File locking deprecation replacements (#593)
This commit is contained in:
committed by
GitHub
parent
d1f06cb774
commit
58a529cc44
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user