storage-cli.c 10 KB

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