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

Merge pull request #17661 from benpicco/vfs_mount_path

sys/vfs: add vfs_mount_by_path()
This commit is contained in:
chrysn 2022-02-16 23:42:51 +01:00 committed by GitHub
commit 395d031cd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 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

@ -34,6 +34,16 @@
#define SHELL_VFS_BUFSIZE 256
static uint8_t _shell_vfs_data_buffer[SHELL_VFS_BUFSIZE];
/**
* @brief Auto-Mount array
*/
XFA_USE_CONST(vfs_mount_t, vfs_mountpoints_xfa);
/**
* @brief Number of automatic mountpoints
*/
#define MOUNTPOINTS_NUMOF XFA_LEN(vfs_mount_t, vfs_mountpoints_xfa)
static void _ls_usage(char **argv)
{
printf("%s <path>\n", argv[0]);
@ -49,6 +59,9 @@ static void _vfs_usage(char **argv)
printf("%s mv <src> <dest>\n", argv[0]);
printf("%s rm <file>\n", argv[0]);
printf("%s df [path]\n", argv[0]);
if (MOUNTPOINTS_NUMOF > 0) {
printf("%s mount [path]\n", argv[0]);
}
puts("r: Read [bytes] bytes at [offset] in file <path>");
puts("w: Write (<a>: append, <o> overwrite) <ascii> or <hex> string <data> in file <path>");
puts("ls: list files in <path>");
@ -135,6 +148,19 @@ static int _df_handler(int argc, char **argv)
return 0;
}
static int _mount_handler(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s [path]\n", argv[0]);
puts("mount pre-configured mount point");
return -1;
}
int res = vfs_mount_by_path(argv[1]);
puts(strerror(res));
return res;
}
static int _read_handler(int argc, char **argv)
{
uint8_t buf[16];
@ -589,6 +615,9 @@ int _vfs_handler(int argc, char **argv)
else if (strcmp(argv[1], "df") == 0) {
return _df_handler(argc - 1, &argv[1]);
}
else if (MOUNTPOINTS_NUMOF > 0 && strcmp(argv[1], "mount") == 0) {
return _mount_handler(argc - 1, &argv[1]);
}
else {
printf("vfs: unsupported sub-command \"%s\"\n", argv[1]);
return 1;

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;
}
/** @} */