Skip to content

Commit

Permalink
Merge pull request #429 from theAlexes/theAlexes/format-command
Browse files Browse the repository at this point in the history
add a format command
  • Loading branch information
theAlexes authored Aug 7, 2024
2 parents e732afb + 44bdc85 commit 0ad5fa4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
29 changes: 27 additions & 2 deletions movement/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,29 @@ 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());
}

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);
Expand Down Expand Up @@ -251,6 +267,16 @@ 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();
}
printf("usage: format YES\r\n");
return 1;
}


int filesystem_cmd_echo(int argc, char *argv[]) {
(void) argc;

Expand Down Expand Up @@ -279,4 +305,3 @@ int filesystem_cmd_echo(int argc, char *argv[]) {

return 0;
}

1 change: 1 addition & 0 deletions movement/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
10 changes: 8 additions & 2 deletions movement/shell_cmd_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 : ""
);
Expand Down Expand Up @@ -156,4 +163,3 @@ static int stress_cmd(int argc, char *argv[]) {

return 0;
}

0 comments on commit 0ad5fa4

Please sign in to comment.