rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "ff.h" |
| 5 | #include <ffs.h> |
| 6 | |
| 7 | #if defined(WITH_LIB_CONSOLE) |
| 8 | #include <lib/console.h> |
| 9 | |
| 10 | static int ffs_cmd(int argc, const cmd_args *argv) |
| 11 | { |
| 12 | FRESULT res; |
| 13 | |
| 14 | if (argc < 2) { |
| 15 | printf("ffs commands:\n"); |
| 16 | usage: |
| 17 | // printf("%s mount <volume>\n", argv[0].str); |
| 18 | printf("%s mkfs <volume>\n", argv[0].str); |
| 19 | printf("%s mkdir <path>\n", argv[0].str); |
| 20 | printf("%s touch <path>\n", argv[0].str); |
| 21 | |
| 22 | goto out; |
| 23 | } |
| 24 | |
| 25 | #if 0 |
| 26 | if (!strcmp(argv[1].str, "mount")) { |
| 27 | if (argc < 3) |
| 28 | goto usage; |
| 29 | |
| 30 | if (argv[2].u >= countof(ffs_work)) { |
| 31 | printf("Error: unsupported volume %u\n", argv[2].u); |
| 32 | goto out; |
| 33 | } |
| 34 | |
| 35 | res = f_mount(argv[2].u, &ffs_work[argv[2].u]); |
| 36 | if (res != FR_OK) |
| 37 | printf("Error: %d\n", res); |
| 38 | } else |
| 39 | #endif |
| 40 | if (!strcmp(argv[1].str, "mkfs")) { |
| 41 | if (argc < 3) |
| 42 | goto usage; |
| 43 | |
| 44 | res = f_mkfs(argv[2].u, 1, 0); |
| 45 | if (res != FR_OK) |
| 46 | printf("Error: %d\n", res); |
| 47 | } else if (!strcmp(argv[1].str, "mkdir")) { |
| 48 | if (argc < 3) |
| 49 | goto usage; |
| 50 | |
| 51 | res = f_mkdir(argv[2].str); |
| 52 | if (res != FR_OK) |
| 53 | printf("Error: %d\n", res); |
| 54 | } else if (!strcmp(argv[1].str, "touch")) { |
| 55 | FIL fil; |
| 56 | |
| 57 | if (argc < 3) |
| 58 | goto usage; |
| 59 | |
| 60 | res = f_open(&fil, argv[2].str, FA_OPEN_ALWAYS); |
| 61 | if (res != FR_OK) |
| 62 | printf("Error: %d\n", res); |
| 63 | else |
| 64 | f_close(&fil); |
| 65 | } else if (!strcmp(argv[1].str, "ls")) { |
| 66 | FILINFO fno; |
| 67 | DIR dir; |
| 68 | char lfn[_MAX_LFN + 1]; |
| 69 | fno.lfname = lfn; |
| 70 | fno.lfsize = sizeof(lfn); |
| 71 | const char *path; |
| 72 | char *fn; |
| 73 | char wattr; |
| 74 | |
| 75 | if (argc < 3) |
| 76 | path = "/"; |
| 77 | else |
| 78 | path = argv[2].str; |
| 79 | |
| 80 | res = f_opendir(&dir, path); |
| 81 | if (res != FR_OK) { |
| 82 | printf("Error: %d\n", res); |
| 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | while (1) { |
| 87 | res = f_readdir(&dir, &fno); |
| 88 | if (res != FR_OK || fno.fname[0] == 0) |
| 89 | break; |
| 90 | |
| 91 | fn = *fno.lfname ? fno.lfname : fno.fname; |
| 92 | wattr = fno.fattrib & AM_RDO ? '-' : 'w'; |
| 93 | |
| 94 | if (fno.fattrib & AM_DIR) |
| 95 | printf("dr%cxr%cxr%cx %10lu %s\n", wattr, wattr, wattr, fno.fsize, fn); |
| 96 | else |
| 97 | printf(" r%c-r%c-r%c- %10lu %s\n", wattr, wattr, wattr, fno.fsize, fn); |
| 98 | } |
| 99 | |
| 100 | if (res != FR_OK) |
| 101 | printf("Error: %d\n", res); |
| 102 | } |
| 103 | |
| 104 | out: |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | STATIC_COMMAND_START |
| 109 | STATIC_COMMAND("ffs", "ffs toolbox", &ffs_cmd) |
| 110 | STATIC_COMMAND_END(ffscommands); |
| 111 | |
| 112 | |
| 113 | #endif |
| 114 | |