storage-cli.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include <furi.h>
  2. #include <cli/cli.h>
  3. #include <lib/toolbox/args.h>
  4. #include <storage/storage.h>
  5. #include <storage/storage-sd-api.h>
  6. #include <api-hal-version.h>
  7. #define MAX_NAME_LENGTH 255
  8. void storage_cli(Cli* cli, string_t args, void* context);
  9. // app cli function
  10. void storage_cli_init() {
  11. Cli* cli = furi_record_open("cli");
  12. cli_add_command(cli, "storage", CliCommandFlagDefault, storage_cli, NULL);
  13. furi_record_close("cli");
  14. }
  15. void storage_cli_print_usage() {
  16. printf("Usage:\r\n");
  17. printf("storage <cmd> <path> <args>\r\n");
  18. printf("The path must start with /int or /ext\r\n");
  19. printf("Cmd list:\r\n");
  20. printf("\tinfo\t - get FS info\r\n");
  21. printf("\tformat\t - format filesystem\r\n");
  22. printf("\tlist\t - list files and dirs\r\n");
  23. printf("\tremove\t - delete the file or directory\r\n");
  24. printf("\tread\t - read data from file and print file size and content to cli\r\n");
  25. printf(
  26. "\twrite\t - read data from cli and append it to file, <args> should contain how many bytes you want to write\r\n");
  27. printf("\tcopy\t - copy file to new file, <args> must contain new path\r\n");
  28. printf("\trename\t - move file to new file, <args> must contain new path\r\n");
  29. printf("\tmkdir\t - creates a new directory\r\n");
  30. };
  31. void storage_cli_print_error(FS_Error error) {
  32. printf("Storage error: %s\r\n", storage_error_get_desc(error));
  33. }
  34. void storage_cli_print_path_error(string_t path, FS_Error error) {
  35. printf(
  36. "Storage error for path \"%s\": %s\r\n",
  37. string_get_cstr(path),
  38. storage_error_get_desc(error));
  39. }
  40. void storage_cli_print_file_error(string_t path, File* file) {
  41. printf(
  42. "Storage error for path \"%s\": %s\r\n",
  43. string_get_cstr(path),
  44. storage_file_get_error_desc(file));
  45. }
  46. void storage_cli_info(Cli* cli, string_t path) {
  47. Storage* api = furi_record_open("storage");
  48. if(string_cmp_str(path, "/int") == 0) {
  49. uint64_t total_space;
  50. uint64_t free_space;
  51. FS_Error error = storage_common_fs_info(api, "/int", &total_space, &free_space);
  52. if(error != FSE_OK) {
  53. storage_cli_print_path_error(path, error);
  54. } else {
  55. printf(
  56. "Label: %s\r\nType: LittleFS\r\n%lu KB total\r\n%lu KB free\r\n",
  57. api_hal_version_get_name_ptr(),
  58. (uint32_t)(total_space / 1024),
  59. (uint32_t)(free_space / 1024));
  60. }
  61. } else if(string_cmp_str(path, "/ext") == 0) {
  62. SDInfo sd_info;
  63. FS_Error error = storage_sd_info(api, &sd_info);
  64. if(error != FSE_OK) {
  65. storage_cli_print_path_error(path, error);
  66. } else {
  67. printf(
  68. "Label: %s\r\nType: %s\r\n%lu KB total\r\n%lu KB free\r\n",
  69. sd_info.label,
  70. sd_api_get_fs_type_text(sd_info.fs_type),
  71. sd_info.kb_total,
  72. sd_info.kb_free);
  73. }
  74. } else {
  75. storage_cli_print_usage();
  76. }
  77. furi_record_close("storage");
  78. };
  79. void storage_cli_format(Cli* cli, string_t path) {
  80. if(string_cmp_str(path, "/int") == 0) {
  81. storage_cli_print_path_error(path, FSE_NOT_IMPLEMENTED);
  82. } else if(string_cmp_str(path, "/ext") == 0) {
  83. printf("Formatting SD card, all data will be lost. Are you sure (y/n)?\r\n");
  84. char answer = cli_getc(cli);
  85. if(answer == 'y' || answer == 'Y') {
  86. Storage* api = furi_record_open("storage");
  87. printf("Formatting, please wait...\r\n");
  88. FS_Error error = storage_sd_format(api);
  89. if(error != FSE_OK) {
  90. storage_cli_print_path_error(path, error);
  91. } else {
  92. printf("SD card was successfully formatted.\r\n");
  93. }
  94. furi_record_close("storage");
  95. } else {
  96. printf("Cancelled.\r\n");
  97. }
  98. } else {
  99. storage_cli_print_usage();
  100. }
  101. };
  102. void storage_cli_list(Cli* cli, string_t path) {
  103. if(string_cmp_str(path, "/") == 0) {
  104. printf("\t[D] int\r\n");
  105. printf("\t[D] ext\r\n");
  106. printf("\t[D] any\r\n");
  107. } else {
  108. Storage* api = furi_record_open("storage");
  109. File* file = storage_file_alloc(api);
  110. if(storage_dir_open(file, string_get_cstr(path))) {
  111. FileInfo fileinfo;
  112. char name[MAX_NAME_LENGTH];
  113. bool readed = false;
  114. while(storage_dir_read(file, &fileinfo, name, MAX_NAME_LENGTH)) {
  115. readed = true;
  116. if(fileinfo.flags & FSF_DIRECTORY) {
  117. printf("\t[D] %s\r\n", name);
  118. } else {
  119. printf("\t[F] %s %lub\r\n", name, (uint32_t)(fileinfo.size));
  120. }
  121. }
  122. if(!readed) {
  123. printf("\tEmpty\r\n");
  124. }
  125. } else {
  126. storage_cli_print_file_error(path, file);
  127. }
  128. storage_dir_close(file);
  129. storage_file_free(file);
  130. furi_record_close("storage");
  131. }
  132. }
  133. void storage_cli_read(Cli* cli, string_t path) {
  134. Storage* api = furi_record_open("storage");
  135. File* file = storage_file_alloc(api);
  136. if(storage_file_open(file, string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  137. const uint16_t read_size = 128;
  138. uint16_t readed_size = 0;
  139. uint8_t* data = furi_alloc(read_size);
  140. printf("Size: %lu\r\n", (uint32_t)storage_file_size(file));
  141. do {
  142. readed_size = storage_file_read(file, data, read_size);
  143. for(uint16_t i = 0; i < readed_size; i++) {
  144. printf("%c", data[i]);
  145. }
  146. } while(readed_size > 0);
  147. printf("\r\n");
  148. free(data);
  149. } else {
  150. storage_cli_print_file_error(path, file);
  151. }
  152. storage_file_close(file);
  153. storage_file_free(file);
  154. furi_record_close("storage");
  155. }
  156. void storage_cli_write(Cli* cli, string_t path, string_t args) {
  157. Storage* api = furi_record_open("storage");
  158. File* file = storage_file_alloc(api);
  159. uint32_t size;
  160. int parsed_count = sscanf(string_get_cstr(args), "%lu", &size);
  161. if(parsed_count == EOF || parsed_count != 1) {
  162. storage_cli_print_usage();
  163. } else {
  164. if(storage_file_open(file, string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
  165. const uint16_t write_size = 8;
  166. uint32_t readed_index = 0;
  167. uint8_t* data = furi_alloc(write_size);
  168. while(true) {
  169. data[readed_index % write_size] = cli_getc(cli);
  170. printf("%c", data[readed_index % write_size]);
  171. fflush(stdout);
  172. readed_index++;
  173. if(((readed_index % write_size) == 0)) {
  174. uint16_t writed_size = storage_file_write(file, data, write_size);
  175. if(writed_size != write_size) {
  176. storage_cli_print_file_error(path, file);
  177. break;
  178. }
  179. } else if(readed_index == size) {
  180. uint16_t writed_size = storage_file_write(file, data, size % write_size);
  181. if(writed_size != (size % write_size)) {
  182. storage_cli_print_file_error(path, file);
  183. break;
  184. }
  185. }
  186. if(readed_index == size) {
  187. break;
  188. }
  189. }
  190. printf("\r\n");
  191. free(data);
  192. } else {
  193. storage_cli_print_file_error(path, file);
  194. }
  195. storage_file_close(file);
  196. }
  197. storage_file_free(file);
  198. furi_record_close("storage");
  199. }
  200. void storage_cli_copy(Cli* cli, string_t old_path, string_t args) {
  201. Storage* api = furi_record_open("storage");
  202. string_t new_path;
  203. string_init(new_path);
  204. if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
  205. storage_cli_print_usage();
  206. } else {
  207. FS_Error error =
  208. storage_common_copy(api, string_get_cstr(old_path), string_get_cstr(new_path));
  209. if(error != FSE_OK) {
  210. storage_cli_print_error(error);
  211. }
  212. }
  213. string_clear(new_path);
  214. furi_record_close("storage");
  215. }
  216. void storage_cli_remove(Cli* cli, string_t path) {
  217. Storage* api = furi_record_open("storage");
  218. FS_Error error = storage_common_remove(api, string_get_cstr(path));
  219. if(error != FSE_OK) {
  220. storage_cli_print_error(error);
  221. }
  222. furi_record_close("storage");
  223. }
  224. void storage_cli_rename(Cli* cli, string_t old_path, string_t args) {
  225. Storage* api = furi_record_open("storage");
  226. string_t new_path;
  227. string_init(new_path);
  228. if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
  229. storage_cli_print_usage();
  230. } else {
  231. FS_Error error =
  232. storage_common_rename(api, string_get_cstr(old_path), string_get_cstr(new_path));
  233. if(error != FSE_OK) {
  234. storage_cli_print_error(error);
  235. }
  236. }
  237. string_clear(new_path);
  238. furi_record_close("storage");
  239. }
  240. void storage_cli_mkdir(Cli* cli, string_t path) {
  241. Storage* api = furi_record_open("storage");
  242. FS_Error error = storage_common_mkdir(api, string_get_cstr(path));
  243. if(error != FSE_OK) {
  244. storage_cli_print_error(error);
  245. }
  246. furi_record_close("storage");
  247. }
  248. void storage_cli(Cli* cli, string_t args, void* context) {
  249. string_t cmd;
  250. string_t path;
  251. string_init(cmd);
  252. string_init(path);
  253. do {
  254. if(!args_read_string_and_trim(args, cmd)) {
  255. storage_cli_print_usage();
  256. break;
  257. }
  258. if(!args_read_probably_quoted_string_and_trim(args, path)) {
  259. storage_cli_print_usage();
  260. break;
  261. }
  262. if(string_cmp_str(cmd, "info") == 0) {
  263. storage_cli_info(cli, path);
  264. break;
  265. }
  266. if(string_cmp_str(cmd, "format") == 0) {
  267. storage_cli_format(cli, path);
  268. break;
  269. }
  270. if(string_cmp_str(cmd, "list") == 0) {
  271. storage_cli_list(cli, path);
  272. break;
  273. }
  274. if(string_cmp_str(cmd, "read") == 0) {
  275. storage_cli_read(cli, path);
  276. break;
  277. }
  278. if(string_cmp_str(cmd, "write") == 0) {
  279. storage_cli_write(cli, path, args);
  280. break;
  281. }
  282. if(string_cmp_str(cmd, "copy") == 0) {
  283. storage_cli_copy(cli, path, args);
  284. break;
  285. }
  286. if(string_cmp_str(cmd, "remove") == 0) {
  287. storage_cli_remove(cli, path);
  288. break;
  289. }
  290. if(string_cmp_str(cmd, "rename") == 0) {
  291. storage_cli_rename(cli, path, args);
  292. break;
  293. }
  294. if(string_cmp_str(cmd, "mkdir") == 0) {
  295. storage_cli_mkdir(cli, path);
  296. break;
  297. }
  298. storage_cli_print_usage();
  299. } while(false);
  300. string_clear(path);
  301. string_clear(cmd);
  302. }