You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
5.8 KiB
316 lines
5.8 KiB
#include <ctype.h>
|
|
#include <inttypes.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <flash.h>
|
|
#include <ftl.h>
|
|
#include <macros.h>
|
|
#include <shell.h>
|
|
|
|
#include <fs/mufs.h>
|
|
|
|
extern struct flash_dev *flash;
|
|
struct mufs *mufs = NULL;
|
|
|
|
extern struct usart_console *admin_con;
|
|
|
|
static void do_mufs_mount(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
(void)argc;
|
|
(void)argv;
|
|
|
|
if (!flash) {
|
|
fprintf(out, "error: no flash device probed.\n");
|
|
return;
|
|
}
|
|
|
|
if (mufs) {
|
|
mufs_unmount(mufs);
|
|
mufs = NULL;
|
|
}
|
|
|
|
if (!(mufs = mufs_mount(flash))) {
|
|
fprintf(out, "error: unable to mount the filesystem.\n");
|
|
return;
|
|
}
|
|
|
|
flash = NULL;
|
|
}
|
|
|
|
static void do_mufs_unmount(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
(void)argc;
|
|
(void)argv;
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "no mufs filesystem currently active\n");
|
|
return;
|
|
}
|
|
|
|
if (flash)
|
|
flash_release(flash);
|
|
|
|
flash = mufs->dev;
|
|
mufs->dev = NULL;
|
|
|
|
mufs_unmount(mufs);
|
|
mufs = NULL;
|
|
}
|
|
|
|
static void do_mufs_format(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
(void)argc;
|
|
(void)argv;
|
|
|
|
if (!flash) {
|
|
fprintf(out, "error: no flash device probed.\n");
|
|
return;
|
|
}
|
|
|
|
if (mufs_format(flash) < 0) {
|
|
fprintf(out, "error: unable to format the flash device.\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void do_mufs_mkdir(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
if (argc < 1) {
|
|
fprintf(out, "usage: mufs mkdir <path>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (mufs_mkdir(mufs, argv[0]) < 0) {
|
|
fprintf(out, "error: unable to create the directory\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void do_mufs_rmdir(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
if (argc < 1) {
|
|
fprintf(out, "usage: mufs rmdir <path>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (mufs_rmdir(mufs, argv[0]) < 0) {
|
|
fprintf(out, "error: unable to remove the directory\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void do_mufs_stat(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
struct mufs_stat stat;
|
|
|
|
if (argc < 1) {
|
|
fprintf(out, "usage: mufs stat <path>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (mufs_stat(mufs, argv[0], &stat) < 0) {
|
|
fprintf(out, "error: unable to stat the file\n");
|
|
return;
|
|
}
|
|
|
|
switch (stat.type) {
|
|
case MUFS_DIR: printf(" type: directory\n"); break;
|
|
case MUFS_FILE: printf(" type: file\n"); break;
|
|
default: return;
|
|
}
|
|
|
|
printf(" file size: %" PRIu32 " bytes\n", stat.file_size);
|
|
}
|
|
|
|
static void do_mufs_cat(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
char data[256];
|
|
struct mufs_file *file;
|
|
|
|
if (argc < 1) {
|
|
fprintf(out, "usage: mufs cat <path>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (!(file = mufs_open(mufs, argv[0], MUFS_READ))) {
|
|
fprintf(out, "error: unable to open the file\n");
|
|
return;
|
|
}
|
|
|
|
while (mufs_read(file, data, sizeof data) != 0) {
|
|
fwrite(data, sizeof *data, sizeof data, out);
|
|
}
|
|
|
|
mufs_close(file);
|
|
}
|
|
|
|
static void do_mufs_write(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
char data[256];
|
|
struct mufs_file *file;
|
|
ssize_t ret;
|
|
|
|
if (argc < 1) {
|
|
fprintf(out, "usage: mufs write <path>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE))) {
|
|
fprintf(out, "error: unable to open the file\n");
|
|
return;
|
|
}
|
|
|
|
fprintf(out, "> ");
|
|
|
|
while ((ret = console_read(admin_con, data, sizeof data)) > 0) {
|
|
mufs_write(file, data, ret);
|
|
fprintf(out, "> ");
|
|
}
|
|
|
|
mufs_close(file);
|
|
}
|
|
|
|
static void do_mufs_append(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
struct mufs_file *file;
|
|
char data[256];
|
|
size_t n;
|
|
|
|
if (argc < 2) {
|
|
fprintf(out, "usage: mufs append <path> <line>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE | MUFS_APPEND))) {
|
|
fprintf(out, "error: unable to open the file\n");
|
|
return;
|
|
}
|
|
|
|
n = strlen(argv[1]);
|
|
|
|
memcpy(data, argv[1], n);
|
|
data[n] = '\n';
|
|
data[n + 1] = '\0';
|
|
|
|
mufs_write(file, data, n + 1);
|
|
mufs_close(file);
|
|
}
|
|
|
|
static void do_mufs_mv(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
if (argc < 2) {
|
|
fprintf(out, "usage: mufs mv <old> <new>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (mufs_rename(mufs, argv[0], argv[1]) < 0) {
|
|
fprintf(out, "error: unable to move the file\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void do_mufs_rm(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
if (argc < 1) {
|
|
fprintf(out, "usage: mufs rm <path>\n");
|
|
return;
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (mufs_unlink(mufs, argv[0]) < 0) {
|
|
fprintf(out, "error: unable to remove the file\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void do_mufs_ls(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
struct mufs_dirent ent;
|
|
struct mufs_dir *dir;
|
|
const char *path = "";
|
|
|
|
if (argc >= 1) {
|
|
path = argv[0];
|
|
}
|
|
|
|
if (!mufs) {
|
|
fprintf(out, "error: no file system mounted.\n");
|
|
return;
|
|
}
|
|
|
|
if (!(dir = mufs_opendir(mufs, path))) {
|
|
fprintf(out, "error: unable to open the directory\n");
|
|
return;
|
|
}
|
|
|
|
while (mufs_readdir(dir, &ent) == 0) {
|
|
fprintf(out, "%s\n", ent.path);
|
|
}
|
|
|
|
mufs_closedir(dir);
|
|
}
|
|
|
|
static struct cmd mufs_cmds[] = {
|
|
{ "mount", NULL, do_mufs_mount },
|
|
{ "umount", NULL, do_mufs_unmount },
|
|
{ "format", NULL, do_mufs_format },
|
|
{ "mkdir", NULL, do_mufs_mkdir },
|
|
{ "rmdir", NULL, do_mufs_rmdir },
|
|
{ "ls", NULL, do_mufs_ls },
|
|
{ "stat", NULL, do_mufs_stat },
|
|
{ "cat", NULL, do_mufs_cat },
|
|
{ "write", NULL, do_mufs_write },
|
|
{ "append", NULL, do_mufs_append },
|
|
{ "mv", NULL, do_mufs_mv, },
|
|
{ "rm", NULL, do_mufs_rm, },
|
|
{ NULL, NULL, NULL },
|
|
};
|
|
|
|
void do_mufs_cmd(FILE *out, const char **argv, size_t argc)
|
|
{
|
|
cmd_exec(mufs_cmds, out, argv, argc);
|
|
}
|
|
|