cli: storage: minor subcommand lookup refactor (#3739)

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
hedger 2024-06-30 21:20:12 +03:00 committed by GitHub
parent d36396f35b
commit 14b37a68b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,37 +12,15 @@
#define MAX_NAME_LENGTH 255
static void storage_cli_print_usage(void) {
printf("Usage:\r\n");
printf("storage <cmd> <path> <args>\r\n");
printf("The path must start with /int or /ext\r\n");
printf("Cmd list:\r\n");
printf("\tinfo\t - get FS info\r\n");
printf("\tformat\t - format filesystem\r\n");
printf("\tlist\t - list files and dirs\r\n");
printf("\ttree\t - list files and dirs, recursive\r\n");
printf("\tremove\t - delete the file or directory\r\n");
printf("\tread\t - read text from file and print file size and content to cli\r\n");
printf(
"\tread_chunks\t - read data from file and print file size and content to cli, <args> should contain how many bytes you want to read in block\r\n");
printf("\twrite\t - read text from cli and append it to file, stops by ctrl+c\r\n");
printf(
"\twrite_chunk\t - read data from cli and append it to file, <args> should contain how many bytes you want to write\r\n");
printf("\tcopy\t - copy file to new file, <args> must contain new path\r\n");
printf("\trename\t - move file to new file, <args> must contain new path\r\n");
printf("\tmkdir\t - creates a new directory\r\n");
printf("\tmd5\t - md5 hash of the file\r\n");
printf("\tstat\t - info about file or dir\r\n");
printf("\ttimestamp\t - last modification timestamp\r\n");
printf("\textract\t - extract tar archive to destination\r\n");
};
static void storage_cli_print_usage(void);
static void storage_cli_print_error(FS_Error error) {
printf("Storage error: %s\r\n", storage_error_get_desc(error));
}
static void storage_cli_info(Cli* cli, FuriString* path) {
static void storage_cli_info(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
if(furi_string_cmp_str(path, STORAGE_INT_PATH_PREFIX) == 0) {
@ -90,7 +68,8 @@ static void storage_cli_info(Cli* cli, FuriString* path) {
furi_record_close(RECORD_STORAGE);
};
static void storage_cli_format(Cli* cli, FuriString* path) {
static void storage_cli_format(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(args);
if(furi_string_cmp_str(path, STORAGE_INT_PATH_PREFIX) == 0) {
storage_cli_print_error(FSE_NOT_IMPLEMENTED);
} else if(furi_string_cmp_str(path, STORAGE_EXT_PATH_PREFIX) == 0) {
@ -116,8 +95,9 @@ static void storage_cli_format(Cli* cli, FuriString* path) {
}
};
static void storage_cli_list(Cli* cli, FuriString* path) {
static void storage_cli_list(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
if(furi_string_cmp_str(path, "/") == 0) {
printf("\t[D] int\r\n");
printf("\t[D] ext\r\n");
@ -153,12 +133,13 @@ static void storage_cli_list(Cli* cli, FuriString* path) {
}
}
static void storage_cli_tree(Cli* cli, FuriString* path) {
static void storage_cli_tree(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(args);
if(furi_string_cmp_str(path, "/") == 0) {
furi_string_set(path, STORAGE_INT_PATH_PREFIX);
storage_cli_tree(cli, path);
storage_cli_tree(cli, path, NULL);
furi_string_set(path, STORAGE_EXT_PATH_PREFIX);
storage_cli_tree(cli, path);
storage_cli_tree(cli, path, NULL);
} else {
Storage* api = furi_record_open(RECORD_STORAGE);
DirWalk* dir_walk = dir_walk_alloc(api);
@ -194,8 +175,9 @@ static void storage_cli_tree(Cli* cli, FuriString* path) {
}
}
static void storage_cli_read(Cli* cli, FuriString* path) {
static void storage_cli_read(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(api);
@ -225,7 +207,8 @@ static void storage_cli_read(Cli* cli, FuriString* path) {
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_write(Cli* cli, FuriString* path) {
static void storage_cli_write(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(api);
@ -355,8 +338,9 @@ static void storage_cli_write_chunk(Cli* cli, FuriString* path, FuriString* args
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_stat(Cli* cli, FuriString* path) {
static void storage_cli_stat(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
if(furi_string_cmp_str(path, "/") == 0) {
@ -396,8 +380,9 @@ static void storage_cli_stat(Cli* cli, FuriString* path) {
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_timestamp(Cli* cli, FuriString* path) {
static void storage_cli_timestamp(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
uint32_t timestamp = 0;
@ -433,8 +418,9 @@ static void storage_cli_copy(Cli* cli, FuriString* old_path, FuriString* args) {
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_remove(Cli* cli, FuriString* path) {
static void storage_cli_remove(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
FS_Error error = storage_common_remove(api, furi_string_get_cstr(path));
@ -466,8 +452,9 @@ static void storage_cli_rename(Cli* cli, FuriString* old_path, FuriString* args)
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_mkdir(Cli* cli, FuriString* path) {
static void storage_cli_mkdir(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
FS_Error error = storage_common_mkdir(api, furi_string_get_cstr(path));
@ -478,8 +465,9 @@ static void storage_cli_mkdir(Cli* cli, FuriString* path) {
furi_record_close(RECORD_STORAGE);
}
static void storage_cli_md5(Cli* cli, FuriString* path) {
static void storage_cli_md5(Cli* cli, FuriString* path, FuriString* args) {
UNUSED(cli);
UNUSED(args);
Storage* api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(api);
FuriString* md5 = furi_string_alloc();
@ -539,6 +527,111 @@ static void storage_cli_extract(Cli* cli, FuriString* old_path, FuriString* args
furi_record_close(RECORD_STORAGE);
}
typedef void (*StorageCliCommandCallback)(Cli* cli, FuriString* path, FuriString* args);
typedef struct {
const char* command;
const char* help;
const StorageCliCommandCallback impl;
} StorageCliCommand;
static const StorageCliCommand storage_cli_commands[] = {
{
"write_chunk",
"read data from cli and append it to file, <args> should contain how many bytes you want to write",
&storage_cli_write_chunk,
},
{
"read_chunks",
"read data from file and print file size and content to cli, <args> should contain how many bytes you want to read in block",
&storage_cli_read_chunks,
},
{
"list",
"list files and dirs",
&storage_cli_list,
},
{
"md5",
"md5 hash of the file",
&storage_cli_md5,
},
{
"stat",
"info about file or dir",
&storage_cli_stat,
},
{
"info",
"get FS info",
&storage_cli_info,
},
{
"tree",
"list files and dirs, recursive",
&storage_cli_tree,
},
{
"read",
"read text from file and print file size and content to cli",
&storage_cli_read,
},
{
"write",
"read text from cli and append it to file, stops by ctrl+c",
&storage_cli_write,
},
{
"copy",
"copy file to new file, <args> must contain new path",
&storage_cli_copy,
},
{
"remove",
"delete the file or directory",
&storage_cli_remove,
},
{
"rename",
"move file to new file, <args> must contain new path",
&storage_cli_rename,
},
{
"mkdir",
"creates a new directory",
&storage_cli_mkdir,
},
{
"timestamp",
"last modification timestamp",
&storage_cli_timestamp,
},
{
"extract",
"extract tar archive to destination",
&storage_cli_extract,
},
{
"format",
"format filesystem",
&storage_cli_format,
},
};
static void storage_cli_print_usage(void) {
printf("Usage:\r\n");
printf("storage <cmd> <path> <args>\r\n");
printf("The path must start with /int or /ext\r\n");
printf("Cmd list:\r\n");
for(size_t i = 0; i < COUNT_OF(storage_cli_commands); ++i) {
const StorageCliCommand* command_descr = &storage_cli_commands[i];
const char* cli_cmd = command_descr->command;
printf(
"\t%s%s - %s\r\n", cli_cmd, strlen(cli_cmd) > 8 ? "\t" : "\t\t", command_descr->help);
}
};
void storage_cli(Cli* cli, FuriString* args, void* context) {
UNUSED(context);
FuriString* cmd;
@ -557,87 +650,18 @@ void storage_cli(Cli* cli, FuriString* args, void* context) {
break;
}
if(furi_string_cmp_str(cmd, "info") == 0) {
storage_cli_info(cli, path);
break;
size_t i = 0;
for(; i < COUNT_OF(storage_cli_commands); ++i) {
const StorageCliCommand* command_descr = &storage_cli_commands[i];
if(furi_string_cmp_str(cmd, command_descr->command) == 0) {
command_descr->impl(cli, path, args);
break;
}
}
if(furi_string_cmp_str(cmd, "format") == 0) {
storage_cli_format(cli, path);
break;
if(i == COUNT_OF(storage_cli_commands)) {
storage_cli_print_usage();
}
if(furi_string_cmp_str(cmd, "list") == 0) {
storage_cli_list(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "tree") == 0) {
storage_cli_tree(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "read") == 0) {
storage_cli_read(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "read_chunks") == 0) {
storage_cli_read_chunks(cli, path, args);
break;
}
if(furi_string_cmp_str(cmd, "write") == 0) {
storage_cli_write(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "write_chunk") == 0) {
storage_cli_write_chunk(cli, path, args);
break;
}
if(furi_string_cmp_str(cmd, "copy") == 0) {
storage_cli_copy(cli, path, args);
break;
}
if(furi_string_cmp_str(cmd, "remove") == 0) {
storage_cli_remove(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "rename") == 0) {
storage_cli_rename(cli, path, args);
break;
}
if(furi_string_cmp_str(cmd, "mkdir") == 0) {
storage_cli_mkdir(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "md5") == 0) {
storage_cli_md5(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "stat") == 0) {
storage_cli_stat(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "timestamp") == 0) {
storage_cli_timestamp(cli, path);
break;
}
if(furi_string_cmp_str(cmd, "extract") == 0) {
storage_cli_extract(cli, path, args);
break;
}
storage_cli_print_usage();
} while(false);
furi_string_free(path);