1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

vfs: Provide generic stat implementation (and use in fatfs)

When a file system has `fstat` and `open` implemented, `stat` can still
be missing. The new function is a generic implementation, and used in
fatfs to provide a `stat`.
This commit is contained in:
chrysn 2020-10-25 17:36:50 +01:00
parent 2fc6b07d03
commit b6392d63ef
3 changed files with 34 additions and 0 deletions

View File

@ -445,6 +445,7 @@ static const vfs_file_system_ops_t fatfs_fs_ops = {
.unlink = _unlink,
.mkdir = _mkdir,
.rmdir = _rmdir,
.stat = vfs_sysop_stat_from_fstat,
};
static const vfs_file_ops_t fatfs_file_ops = {

View File

@ -969,6 +969,20 @@ const vfs_mount_t *vfs_iterate_mounts(const vfs_mount_t *cur);
*/
const vfs_file_t *vfs_file_get(int fd);
/** @brief Implementation of `stat` using `fstat`
*
* This helper can be used by file system drivers that do not have any more
* efficient implementation of `fs_op::stat` than opening the file and running
* `f_op::fstat` on it.
*
* It can be set as `fs_op::stat` by a file system driver, provided it
* implements `f_op::open` and `f_op::fstat` and `f_op::close`, and its `open`
* accepts `NULL` in the `abs_path` position.
*/
int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp,
const char *restrict path,
struct stat *restrict buf);
#ifdef __cplusplus
}
#endif

View File

@ -1000,4 +1000,23 @@ static inline int _fd_is_valid(int fd)
return 0;
}
int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
{
const vfs_file_ops_t * f_op = mountp->fs->f_op;
vfs_file_t opened = {
.mp = mountp,
/* As per definition of the `vfsfile_ops::open` field */
.f_op = f_op,
.private_data = { .ptr = NULL },
.pos = 0,
};
int err = f_op->open(&opened, path, 0, 0, NULL);
if (err < 0) {
return err;
}
err = f_op->fstat(&opened, buf);
f_op->close(&opened);
return err;
}
/** @} */