Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vfs: introduce vfs_format_by_path() #18447

Merged
merged 3 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions sys/include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,21 @@ int vfs_closedir(vfs_DIR *dirp);
*/
int vfs_format(vfs_mount_t *mountp);

/**
* @brief Format a file system
*
* The file system must not be mounted in order to be formatted.
* Call @ref vfs_unmount_by_path first if necessary.
*
* @note This assumes mount points have been configured with @ref VFS_AUTO_MOUNT.
*
* @param[in] path Path of the pre-configured mount point
*
* @return 0 on success
* @return <0 on error
*/
int vfs_format_by_path(const char *path);

/**
* @brief Mount a file system
*
Expand Down
48 changes: 42 additions & 6 deletions sys/shell/commands/sc_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ static void _vfs_usage(char **argv)
if (MOUNTPOINTS_NUMOF > 0) {
printf("%s remount [path]\n", argv[0]);
}
if (MOUNTPOINTS_NUMOF > 0) {
printf("%s format [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>");
Expand Down Expand Up @@ -112,6 +115,7 @@ static int _errno_string(int err, char *buf, size_t buflen)
err = -err;
}
switch (err) {
_case_snprintf_errno_name(EBUSY);
_case_snprintf_errno_name(EACCES);
_case_snprintf_errno_name(ENOENT);
_case_snprintf_errno_name(EINVAL);
Expand Down Expand Up @@ -185,9 +189,13 @@ static int _mount_handler(int argc, char **argv)
return -1;
}

uint8_t buf[16];
char buf[16];
int res = vfs_mount_by_path(argv[1]);
_errno_string(res, (char *)buf, sizeof(buf));
if (res < 0) {
_errno_string(res, buf, sizeof(buf));
puts(buf);
}

return res;
}

Expand All @@ -199,10 +207,13 @@ static int _umount_handler(int argc, char **argv)
return -1;
}

uint8_t buf[16];
char buf[16];
int res = vfs_unmount_by_path(argv[1]);
if (res < 0) {
_errno_string(res, buf, sizeof(buf));
puts(buf);
}

_errno_string(res, (char *)buf, sizeof(buf));
return res;
}

Expand All @@ -214,10 +225,32 @@ static int _remount_handler(int argc, char **argv)
return -1;
}

uint8_t buf[16];
char buf[16];
vfs_unmount_by_path(argv[1]);
int res = vfs_mount_by_path(argv[1]);
_errno_string(res, (char *)buf, sizeof(buf));
if (res < 0) {
_errno_string(res, buf, sizeof(buf));
puts(buf);
}

return res;
}

static int _format_handler(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s [path]\n", argv[0]);
puts("format pre-configured mount point");
return -1;
}

char buf[16];
int res = vfs_format_by_path(argv[1]);
if (res < 0) {
_errno_string(res, buf, sizeof(buf));
puts(buf);
}

return res;
}

Expand Down Expand Up @@ -719,6 +752,9 @@ static int _vfs_handler(int argc, char **argv)
else if (MOUNTPOINTS_NUMOF > 0 && strcmp(argv[1], "remount") == 0) {
return _remount_handler(argc - 1, &argv[1]);
}
else if (MOUNTPOINTS_NUMOF > 0 && strcmp(argv[1], "format") == 0) {
return _format_handler(argc - 1, &argv[1]);
}
else {
printf("vfs: unsupported sub-command \"%s\"\n", argv[1]);
return 1;
Expand Down
11 changes: 11 additions & 0 deletions sys/vfs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1208,4 +1208,15 @@ int vfs_unmount_by_path(const char *path)
return -ENOENT;
}

int vfs_format_by_path(const char *path)
{
for (unsigned i = 0; i < MOUNTPOINTS_NUMOF; ++i) {
if (strcmp(path, vfs_mountpoints_xfa[i].mount_point) == 0) {
return vfs_format(&vfs_mountpoints_xfa[i]);
}
}

return -ENOENT;
}

/** @} */