]> git.earman.xyz Git - sensor-watch.git/commitdiff
add a format command
authorAlex Maestas <git@se30.xyz>
Mon, 5 Aug 2024 23:06:18 +0000 (23:06 +0000)
committerAlex Maestas <git@se30.xyz>
Mon, 5 Aug 2024 23:06:18 +0000 (23:06 +0000)
movement/filesystem.c
movement/filesystem.h
movement/shell_cmd_list.c

index 9df0a8d29077b2193139cbb011a2f35ec4e15c9b..b5e3fcab4ffe7119172882aa447350928b4ba620 100644 (file)
@@ -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;
 }
-
index fa3d9d1ad19e52acf8b18bc97524993683b933a0..472f28122fe7700a9e06163bb90cd771c317f308 100644 (file)
@@ -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_
index 0ea08a56ea6285752a6c279dad6bf3273a1d9f9d..e2d700b1947a137dbae6549c666d728c3a42cca0 100644 (file)
@@ -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;
 }
-