rpc_storage.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #include "flipper.pb.h"
  2. #include "furi/common_defines.h"
  3. #include "furi/memmgr.h"
  4. #include "furi/record.h"
  5. #include "pb_decode.h"
  6. #include "rpc/rpc.h"
  7. #include "rpc_i.h"
  8. #include "storage.pb.h"
  9. #include "storage/filesystem-api-defines.h"
  10. #include "storage/storage.h"
  11. #include <stdint.h>
  12. #include <lib/toolbox/md5.h>
  13. #define RPC_TAG "RPC_STORAGE"
  14. #define MAX_NAME_LENGTH 255
  15. #define MAX_DATA_SIZE 512
  16. typedef enum {
  17. RpcStorageStateIdle = 0,
  18. RpcStorageStateWriting,
  19. } RpcStorageState;
  20. typedef struct {
  21. Rpc* rpc;
  22. Storage* api;
  23. File* file;
  24. RpcStorageState state;
  25. uint32_t current_command_id;
  26. } RpcStorageSystem;
  27. void rpc_print_message(const PB_Main* message);
  28. static void rpc_system_storage_reset_state(RpcStorageSystem* rpc_storage, bool send_error) {
  29. furi_assert(rpc_storage);
  30. if(rpc_storage->state != RpcStorageStateIdle) {
  31. if(send_error) {
  32. rpc_send_and_release_empty(
  33. rpc_storage->rpc,
  34. rpc_storage->current_command_id,
  35. PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED);
  36. }
  37. if(rpc_storage->state == RpcStorageStateWriting) {
  38. storage_file_close(rpc_storage->file);
  39. storage_file_free(rpc_storage->file);
  40. furi_record_close("storage");
  41. }
  42. rpc_storage->state = RpcStorageStateIdle;
  43. }
  44. }
  45. static PB_CommandStatus rpc_system_storage_get_error(FS_Error fs_error) {
  46. PB_CommandStatus pb_error;
  47. switch(fs_error) {
  48. case FSE_OK:
  49. pb_error = PB_CommandStatus_OK;
  50. break;
  51. case FSE_INVALID_NAME:
  52. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  53. break;
  54. case FSE_INVALID_PARAMETER:
  55. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
  56. break;
  57. case FSE_INTERNAL:
  58. pb_error = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
  59. break;
  60. case FSE_ALREADY_OPEN:
  61. pb_error = PB_CommandStatus_ERROR_STORAGE_ALREADY_OPEN;
  62. break;
  63. case FSE_DENIED:
  64. pb_error = PB_CommandStatus_ERROR_STORAGE_DENIED;
  65. break;
  66. case FSE_EXIST:
  67. pb_error = PB_CommandStatus_ERROR_STORAGE_EXIST;
  68. break;
  69. case FSE_NOT_EXIST:
  70. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_EXIST;
  71. break;
  72. case FSE_NOT_READY:
  73. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_READY;
  74. break;
  75. case FSE_NOT_IMPLEMENTED:
  76. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_IMPLEMENTED;
  77. break;
  78. default:
  79. pb_error = PB_CommandStatus_ERROR;
  80. break;
  81. }
  82. return pb_error;
  83. }
  84. static PB_CommandStatus rpc_system_storage_get_file_error(File* file) {
  85. return rpc_system_storage_get_error(storage_file_get_error(file));
  86. }
  87. static void rpc_system_storage_list_root(const PB_Main* request, void* context) {
  88. RpcStorageSystem* rpc_storage = context;
  89. const char* hard_coded_dirs[] = {"any", "int", "ext"};
  90. PB_Main response = {
  91. .has_next = false,
  92. .command_id = request->command_id,
  93. .command_status = PB_CommandStatus_OK,
  94. .which_content = PB_Main_storage_list_response_tag,
  95. };
  96. furi_assert(COUNT_OF(hard_coded_dirs) < COUNT_OF(response.content.storage_list_response.file));
  97. for(int i = 0; i < COUNT_OF(hard_coded_dirs); ++i) {
  98. ++response.content.storage_list_response.file_count;
  99. response.content.storage_list_response.file[i].data = NULL;
  100. response.content.storage_list_response.file[i].size = 0;
  101. response.content.storage_list_response.file[i].type = PB_Storage_File_FileType_DIR;
  102. char* str = furi_alloc(strlen(hard_coded_dirs[i]) + 1);
  103. strcpy(str, hard_coded_dirs[i]);
  104. response.content.storage_list_response.file[i].name = str;
  105. }
  106. rpc_send_and_release(rpc_storage->rpc, &response);
  107. }
  108. static void rpc_system_storage_list_process(const PB_Main* request, void* context) {
  109. furi_assert(request);
  110. furi_assert(context);
  111. furi_assert(request->which_content == PB_Main_storage_list_request_tag);
  112. RpcStorageSystem* rpc_storage = context;
  113. rpc_system_storage_reset_state(rpc_storage, true);
  114. if(!strcmp(request->content.storage_list_request.path, "/")) {
  115. rpc_system_storage_list_root(request, context);
  116. return;
  117. }
  118. Storage* fs_api = furi_record_open("storage");
  119. File* dir = storage_file_alloc(fs_api);
  120. PB_Main response = {
  121. .command_id = request->command_id,
  122. .has_next = false,
  123. .which_content = PB_Main_storage_list_request_tag,
  124. .command_status = PB_CommandStatus_OK,
  125. };
  126. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  127. response.which_content = PB_Main_storage_list_response_tag;
  128. bool finish = false;
  129. int i = 0;
  130. if(!storage_dir_open(dir, request->content.storage_list_request.path)) {
  131. response.command_status = rpc_system_storage_get_file_error(dir);
  132. response.which_content = PB_Main_empty_tag;
  133. finish = true;
  134. }
  135. while(!finish) {
  136. FileInfo fileinfo;
  137. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  138. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  139. if(i == COUNT_OF(list->file)) {
  140. list->file_count = i;
  141. response.has_next = true;
  142. rpc_send_and_release(rpc_storage->rpc, &response);
  143. i = 0;
  144. }
  145. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
  146. PB_Storage_File_FileType_FILE;
  147. list->file[i].size = fileinfo.size;
  148. list->file[i].data = NULL;
  149. list->file[i].name = name;
  150. ++i;
  151. } else {
  152. list->file_count = i;
  153. finish = true;
  154. free(name);
  155. }
  156. }
  157. response.has_next = false;
  158. rpc_send_and_release(rpc_storage->rpc, &response);
  159. storage_dir_close(dir);
  160. storage_file_free(dir);
  161. furi_record_close("storage");
  162. }
  163. static void rpc_system_storage_read_process(const PB_Main* request, void* context) {
  164. furi_assert(request);
  165. furi_assert(request->which_content == PB_Main_storage_read_request_tag);
  166. RpcStorageSystem* rpc_storage = context;
  167. rpc_system_storage_reset_state(rpc_storage, true);
  168. /* use same message memory to send reponse */
  169. PB_Main* response = furi_alloc(sizeof(PB_Main));
  170. const char* path = request->content.storage_read_request.path;
  171. Storage* fs_api = furi_record_open("storage");
  172. File* file = storage_file_alloc(fs_api);
  173. bool result = false;
  174. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  175. size_t size_left = storage_file_size(file);
  176. do {
  177. response->command_id = request->command_id;
  178. response->which_content = PB_Main_storage_read_response_tag;
  179. response->command_status = PB_CommandStatus_OK;
  180. response->content.storage_read_response.has_file = true;
  181. response->content.storage_read_response.file.data =
  182. furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  183. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  184. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  185. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  186. *read_size_msg = storage_file_read(file, buffer, read_size);
  187. size_left -= read_size;
  188. result = (*read_size_msg == read_size);
  189. if(result) {
  190. response->has_next = (size_left > 0);
  191. rpc_send_and_release(rpc_storage->rpc, response);
  192. }
  193. } while((size_left != 0) && result);
  194. if(!result) {
  195. rpc_send_and_release_empty(
  196. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  197. }
  198. } else {
  199. rpc_send_and_release_empty(
  200. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  201. }
  202. free(response);
  203. storage_file_close(file);
  204. storage_file_free(file);
  205. furi_record_close("storage");
  206. }
  207. static void rpc_system_storage_write_process(const PB_Main* request, void* context) {
  208. furi_assert(request);
  209. furi_assert(request->which_content == PB_Main_storage_write_request_tag);
  210. RpcStorageSystem* rpc_storage = context;
  211. bool result = true;
  212. if((request->command_id != rpc_storage->current_command_id) &&
  213. (rpc_storage->state == RpcStorageStateWriting)) {
  214. rpc_system_storage_reset_state(rpc_storage, true);
  215. }
  216. if(rpc_storage->state != RpcStorageStateWriting) {
  217. rpc_storage->api = furi_record_open("storage");
  218. rpc_storage->file = storage_file_alloc(rpc_storage->api);
  219. rpc_storage->current_command_id = request->command_id;
  220. rpc_storage->state = RpcStorageStateWriting;
  221. const char* path = request->content.storage_write_request.path;
  222. result = storage_file_open(rpc_storage->file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  223. }
  224. File* file = rpc_storage->file;
  225. if(result) {
  226. uint8_t* buffer = request->content.storage_write_request.file.data->bytes;
  227. size_t buffer_size = request->content.storage_write_request.file.data->size;
  228. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  229. result = (written_size == buffer_size);
  230. if(result && !request->has_next) {
  231. rpc_send_and_release_empty(
  232. rpc_storage->rpc, rpc_storage->current_command_id, PB_CommandStatus_OK);
  233. rpc_system_storage_reset_state(rpc_storage, false);
  234. }
  235. }
  236. if(!result) {
  237. rpc_send_and_release_empty(
  238. rpc_storage->rpc,
  239. rpc_storage->current_command_id,
  240. rpc_system_storage_get_file_error(file));
  241. rpc_system_storage_reset_state(rpc_storage, false);
  242. }
  243. }
  244. static bool rpc_system_storage_is_dir_is_empty(Storage* fs_api, const char* path) {
  245. FileInfo fileinfo;
  246. bool is_dir_is_empty = false;
  247. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  248. if((error == FSE_OK) && (fileinfo.flags & FSF_DIRECTORY)) {
  249. File* dir = storage_file_alloc(fs_api);
  250. if(storage_dir_open(dir, path)) {
  251. char* name = furi_alloc(MAX_NAME_LENGTH);
  252. is_dir_is_empty = !storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH);
  253. free(name);
  254. }
  255. storage_dir_close(dir);
  256. storage_file_free(dir);
  257. }
  258. return is_dir_is_empty;
  259. }
  260. static void rpc_system_storage_delete_process(const PB_Main* request, void* context) {
  261. furi_assert(request);
  262. furi_assert(request->which_content == PB_Main_storage_delete_request_tag);
  263. furi_assert(context);
  264. RpcStorageSystem* rpc_storage = context;
  265. PB_CommandStatus status = PB_CommandStatus_ERROR;
  266. rpc_system_storage_reset_state(rpc_storage, true);
  267. Storage* fs_api = furi_record_open("storage");
  268. char* path = request->content.storage_delete_request.path;
  269. if(!path) {
  270. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  271. } else {
  272. FS_Error error_remove = storage_common_remove(fs_api, path);
  273. // FSE_DENIED is for empty directory, but not only for this
  274. // that's why we have to check it
  275. if((error_remove == FSE_DENIED) && !rpc_system_storage_is_dir_is_empty(fs_api, path)) {
  276. if(request->content.storage_delete_request.recursive) {
  277. bool deleted = storage_simply_remove_recursive(fs_api, path);
  278. status = deleted ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
  279. } else {
  280. status = PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY;
  281. }
  282. } else if(error_remove == FSE_NOT_EXIST) {
  283. status = PB_CommandStatus_OK;
  284. } else {
  285. status = rpc_system_storage_get_error(error_remove);
  286. }
  287. }
  288. furi_record_close("storage");
  289. rpc_send_and_release_empty(rpc_storage->rpc, request->command_id, status);
  290. }
  291. static void rpc_system_storage_mkdir_process(const PB_Main* request, void* context) {
  292. furi_assert(request);
  293. furi_assert(request->which_content == PB_Main_storage_mkdir_request_tag);
  294. furi_assert(context);
  295. RpcStorageSystem* rpc_storage = context;
  296. PB_CommandStatus status;
  297. rpc_system_storage_reset_state(rpc_storage, true);
  298. Storage* fs_api = furi_record_open("storage");
  299. char* path = request->content.storage_mkdir_request.path;
  300. if(path) {
  301. FS_Error error = storage_common_mkdir(fs_api, path);
  302. status = rpc_system_storage_get_error(error);
  303. } else {
  304. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  305. }
  306. rpc_send_and_release_empty(rpc_storage->rpc, request->command_id, status);
  307. }
  308. static void rpc_system_storage_md5sum_process(const PB_Main* request, void* context) {
  309. furi_assert(request);
  310. furi_assert(request->which_content == PB_Main_storage_md5sum_request_tag);
  311. furi_assert(context);
  312. RpcStorageSystem* rpc_storage = context;
  313. rpc_system_storage_reset_state(rpc_storage, true);
  314. const char* filename = request->content.storage_md5sum_request.path;
  315. if(!filename) {
  316. rpc_send_and_release_empty(
  317. rpc_storage->rpc, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  318. return;
  319. }
  320. Storage* fs_api = furi_record_open("storage");
  321. File* file = storage_file_alloc(fs_api);
  322. if(storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
  323. const uint16_t read_size = 512;
  324. const uint8_t hash_size = 16;
  325. uint8_t* data = malloc(read_size);
  326. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  327. md5_context* md5_ctx = malloc(sizeof(md5_context));
  328. md5_starts(md5_ctx);
  329. while(true) {
  330. uint16_t readed_size = storage_file_read(file, data, read_size);
  331. if(readed_size == 0) break;
  332. md5_update(md5_ctx, data, readed_size);
  333. }
  334. md5_finish(md5_ctx, hash);
  335. free(md5_ctx);
  336. PB_Main response = {
  337. .command_id = request->command_id,
  338. .command_status = PB_CommandStatus_OK,
  339. .which_content = PB_Main_storage_md5sum_response_tag,
  340. .has_next = false,
  341. };
  342. char* md5sum = response.content.storage_md5sum_response.md5sum;
  343. size_t md5sum_size = sizeof(response.content.storage_md5sum_response.md5sum);
  344. (void)md5sum_size;
  345. furi_assert(hash_size <= ((md5sum_size - 1) / 2));
  346. for(uint8_t i = 0; i < hash_size; i++) {
  347. md5sum += sprintf(md5sum, "%02x", hash[i]);
  348. }
  349. free(hash);
  350. free(data);
  351. storage_file_close(file);
  352. rpc_send_and_release(rpc_storage->rpc, &response);
  353. } else {
  354. rpc_send_and_release_empty(
  355. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  356. }
  357. storage_file_free(file);
  358. furi_record_close("storage");
  359. }
  360. void* rpc_system_storage_alloc(Rpc* rpc) {
  361. furi_assert(rpc);
  362. RpcStorageSystem* rpc_storage = furi_alloc(sizeof(RpcStorageSystem));
  363. rpc_storage->api = furi_record_open("storage");
  364. rpc_storage->rpc = rpc;
  365. rpc_storage->state = RpcStorageStateIdle;
  366. RpcHandler rpc_handler = {
  367. .message_handler = NULL,
  368. .decode_submessage = NULL,
  369. .context = rpc_storage,
  370. };
  371. rpc_handler.message_handler = rpc_system_storage_list_process;
  372. rpc_add_handler(rpc, PB_Main_storage_list_request_tag, &rpc_handler);
  373. rpc_handler.message_handler = rpc_system_storage_read_process;
  374. rpc_add_handler(rpc, PB_Main_storage_read_request_tag, &rpc_handler);
  375. rpc_handler.message_handler = rpc_system_storage_write_process;
  376. rpc_add_handler(rpc, PB_Main_storage_write_request_tag, &rpc_handler);
  377. rpc_handler.message_handler = rpc_system_storage_delete_process;
  378. rpc_add_handler(rpc, PB_Main_storage_delete_request_tag, &rpc_handler);
  379. rpc_handler.message_handler = rpc_system_storage_mkdir_process;
  380. rpc_add_handler(rpc, PB_Main_storage_mkdir_request_tag, &rpc_handler);
  381. rpc_handler.message_handler = rpc_system_storage_md5sum_process;
  382. rpc_add_handler(rpc, PB_Main_storage_md5sum_request_tag, &rpc_handler);
  383. return rpc_storage;
  384. }
  385. void rpc_system_storage_free(void* ctx) {
  386. RpcStorageSystem* rpc_storage = ctx;
  387. rpc_system_storage_reset_state(rpc_storage, false);
  388. free(rpc_storage);
  389. }