From 7000d08ba577569076a6b0c6799b7cb71f5cb772 Mon Sep 17 00:00:00 2001 From: Alex Maestas Date: Mon, 5 Aug 2024 23:06:18 +0000 Subject: [PATCH 1/3] add a format command --- movement/filesystem.c | 26 +++++++++++++++++++++++++- movement/filesystem.h | 1 + movement/shell_cmd_list.c | 10 ++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/movement/filesystem.c b/movement/filesystem.c index 9df0a8d29..b5e3fcab4 100644 --- a/movement/filesystem.c +++ b/movement/filesystem.c @@ -127,6 +127,22 @@ bool filesystem_init(void) { return err == LFS_ERR_OK; } +int _filesystem_format(void); +int _filesystem_format(void) { + int err = lfs_unmount(&lfs); + if (err < 0) { + printf("Couldn't unmount - continuing to format, but you should reboot afterwards!\r\n"); + } + + err = lfs_format(&lfs, &cfg); + if (err < 0) return err; + + err = lfs_mount(&lfs, &cfg); + if (err < 0) return err; + printf("Filesystem re-mounted with %ld bytes free.\r\n", filesystem_get_free_space()); + return 0; +} + bool filesystem_file_exists(char *filename) { info.type = 0; lfs_stat(&lfs, filename, &info); @@ -251,6 +267,15 @@ int filesystem_cmd_rm(int argc, char *argv[]) { return 0; } +int filesystem_cmd_format(int argc, char *argv[]) { + (void) argc; + if(strcmp(argv[1], "YES") == 0) { + return _filesystem_format(); + } + return 1; +} + + int filesystem_cmd_echo(int argc, char *argv[]) { (void) argc; @@ -279,4 +304,3 @@ int filesystem_cmd_echo(int argc, char *argv[]) { return 0; } - diff --git a/movement/filesystem.h b/movement/filesystem.h index fa3d9d1ad..472f28122 100644 --- a/movement/filesystem.h +++ b/movement/filesystem.h @@ -100,6 +100,7 @@ int filesystem_cmd_ls(int argc, char *argv[]); int filesystem_cmd_cat(int argc, char *argv[]); int filesystem_cmd_df(int argc, char *argv[]); int filesystem_cmd_rm(int argc, char *argv[]); +int filesystem_cmd_format(int argc, char *argv[]); int filesystem_cmd_echo(int argc, char *argv[]); #endif // FILESYSTEM_H_ diff --git a/movement/shell_cmd_list.c b/movement/shell_cmd_list.c index 0ea08a56e..e2d700b19 100644 --- a/movement/shell_cmd_list.c +++ b/movement/shell_cmd_list.c @@ -85,6 +85,13 @@ shell_command_t g_shell_commands[] = { .max_args = 1, .cb = filesystem_cmd_rm, }, + { + .name = "format", + .help = "usage: format YES", + .min_args = 1, + .max_args = 1, + .cb = filesystem_cmd_format, + }, { .name = "echo", .help = "usage: echo TEXT {>,>>} FILE", @@ -109,7 +116,7 @@ static int help_cmd(int argc, char *argv[]) { printf("Command List:\r\n"); for (size_t i = 0; i < g_num_shell_commands; i++) { - printf(" %s\t%s\r\n", + printf(" %s\t%s\r\n", g_shell_commands[i].name, (g_shell_commands[i].help) ? g_shell_commands[i].help : "" ); @@ -156,4 +163,3 @@ static int stress_cmd(int argc, char *argv[]) { return 0; } - From ca1da33b8241b91446c966babac7617851af53d2 Mon Sep 17 00:00:00 2001 From: Alex Maestas Date: Mon, 5 Aug 2024 23:09:24 +0000 Subject: [PATCH 2/3] in filesystem_init, handle the error code correctly --- movement/filesystem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/movement/filesystem.c b/movement/filesystem.c index b5e3fcab4..a1953bc61 100644 --- a/movement/filesystem.c +++ b/movement/filesystem.c @@ -120,7 +120,7 @@ bool filesystem_init(void) { printf("Ignore that error! Formatting filesystem...\r\n"); err = lfs_format(&lfs, &cfg); if (err < 0) return false; - err = lfs_mount(&lfs, &cfg) == LFS_ERR_OK; + err = lfs_mount(&lfs, &cfg); printf("Filesystem mounted with %ld bytes free.\r\n", filesystem_get_free_space()); } From 44bdc8503eeb53caa4165b432ac04f45fac50431 Mon Sep 17 00:00:00 2001 From: Alex Maestas Date: Mon, 5 Aug 2024 23:11:54 +0000 Subject: [PATCH 3/3] explain usage in format command when arg isn't YES --- movement/filesystem.c | 1 + 1 file changed, 1 insertion(+) diff --git a/movement/filesystem.c b/movement/filesystem.c index a1953bc61..326db74c0 100644 --- a/movement/filesystem.c +++ b/movement/filesystem.c @@ -272,6 +272,7 @@ int filesystem_cmd_format(int argc, char *argv[]) { if(strcmp(argv[1], "YES") == 0) { return _filesystem_format(); } + printf("usage: format YES\r\n"); return 1; }