1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/vfs: add vfs_mount_by_path()

This commit is contained in:
Benjamin Valentin 2022-02-14 17:03:21 +01:00
parent 78e4f6b557
commit 54d5f43aff
2 changed files with 53 additions and 17 deletions

View File

@ -871,6 +871,21 @@ int vfs_format(vfs_mount_t *mountp);
*/
int vfs_mount(vfs_mount_t *mountp);
/**
* @brief Mount a file system with a pre-configured mount path
*
* @note This assumes mount points have been configured with @ref VFS_AUTO_MOUNT.
*
* @warning If the @ref pseudomodule_vfs_auto_format is used a format attempt will
* be made if the mount fails.
*
* @param[in] path Path of the pre-configured mount point
*
* @return 0 on success
* @return <0 on error
*/
int vfs_mount_by_path(const char *path);
/**
* @brief Rename a file
*

View File

@ -1051,27 +1051,48 @@ int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp, const char *restrict path, st
return err;
}
static int _auto_mount(vfs_mount_t *mountp, unsigned i)
{
(void) i;
DEBUG("vfs%u: mounting as '%s'\n", i, mountp->mount_point);
int res = vfs_mount(mountp);
if (res == 0) {
return 0;
}
if (IS_ACTIVE(MODULE_VFS_AUTO_FORMAT)) {
DEBUG("vfs%u: formatting…\n", i);
res = vfs_format(mountp);
if (res) {
DEBUG("vfs%u: format: error %d\n", i, res);
return res;
}
res = vfs_mount(mountp);
}
if (res) {
DEBUG("vfs%u mount: error %d\n", i, res);
}
return res;
}
void auto_init_vfs(void)
{
for (unsigned i = 0; i < MOUNTPOINTS_NUMOF; ++i) {
DEBUG("vfs%u: mounting as '%s'\n", i, vfs_mountpoints_xfa[i].mount_point);
int res = vfs_mount(&vfs_mountpoints_xfa[i]);
if (res) {
if (IS_ACTIVE(MODULE_VFS_AUTO_FORMAT)) {
DEBUG("vfs%u: formatting…\n", i);
res = vfs_format(&vfs_mountpoints_xfa[i]);
if (res) {
DEBUG("vfs%u: format: error %d\n", i, res);
continue;
}
res = vfs_mount(&vfs_mountpoints_xfa[i]);
}
if (res) {
DEBUG("vfs%u mount: error %d\n", i, res);
}
}
_auto_mount(&vfs_mountpoints_xfa[i], i);
}
}
int vfs_mount_by_path(const char *path)
{
for (unsigned i = 0; i < MOUNTPOINTS_NUMOF; ++i) {
if (strcmp(path, vfs_mountpoints_xfa[i].mount_point) == 0) {
return _auto_mount(&vfs_mountpoints_xfa[i], i);
}
}
return -ENOENT;
}
/** @} */