From 9c4ca01e246243f9eb1e8924aab9a7b8c5d1e4c6 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 6 May 2024 15:43:50 +0200 Subject: [PATCH] sys/vfs_util: add vfs_file_exists() --- sys/include/vfs_util.h | 12 ++++++++++++ sys/vfs_util/vfs_util.c | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/sys/include/vfs_util.h b/sys/include/vfs_util.h index f2e11b95d0..d0eb06434e 100644 --- a/sys/include/vfs_util.h +++ b/sys/include/vfs_util.h @@ -20,6 +20,9 @@ #ifndef VFS_UTIL_H #define VFS_UTIL_H +#include +#include + #ifdef __cplusplus extern "C" { #endif @@ -115,6 +118,15 @@ int vfs_file_sha256(const char* file, void *digest, */ int vfs_is_dir(const char *path); +/** + @brief Checks if @p path is a file and can be read. + * + * @param[in] path Path to check + * + * @return true if the file exists, false otherwise + */ +bool vfs_file_exists(const char *path); + /** * @brief Behaves like `rm -r @p root`. * diff --git a/sys/vfs_util/vfs_util.c b/sys/vfs_util/vfs_util.c index c056934ed5..2998f5242a 100644 --- a/sys/vfs_util/vfs_util.c +++ b/sys/vfs_util/vfs_util.c @@ -171,6 +171,17 @@ int vfs_is_dir(const char *path) return ((stat.st_mode & S_IFMT) == S_IFDIR); } +bool vfs_file_exists(const char *path) +{ + int res = vfs_open(path, O_RDONLY, 0); + if (res < 0) { + return false; + } + + vfs_close(res); + return true; +} + /** * @brief Removes additional "/" slashes from @p path *