rpc_storage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. 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_stat_process(const PB_Main* request, void* context) {
  88. furi_assert(request);
  89. furi_assert(context);
  90. furi_assert(request->which_content == PB_Main_storage_stat_request_tag);
  91. RpcStorageSystem* rpc_storage = context;
  92. rpc_system_storage_reset_state(rpc_storage, true);
  93. PB_Main* response = furi_alloc(sizeof(PB_Main));
  94. response->command_id = request->command_id;
  95. Storage* fs_api = furi_record_open("storage");
  96. const char* path = request->content.storage_stat_request.path;
  97. FileInfo fileinfo;
  98. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  99. response->command_status = rpc_system_storage_get_error(error);
  100. response->which_content = PB_Main_empty_tag;
  101. if(error == FSE_OK) {
  102. response->which_content = PB_Main_storage_stat_response_tag;
  103. response->content.storage_stat_response.has_file = true;
  104. response->content.storage_stat_response.file.type = (fileinfo.flags & FSF_DIRECTORY) ?
  105. PB_Storage_File_FileType_DIR :
  106. PB_Storage_File_FileType_FILE;
  107. response->content.storage_stat_response.file.size = fileinfo.size;
  108. }
  109. rpc_send_and_release(rpc_storage->rpc, response);
  110. free(response);
  111. furi_record_close("storage");
  112. }
  113. static void rpc_system_storage_list_root(const PB_Main* request, void* context) {
  114. RpcStorageSystem* rpc_storage = context;
  115. const char* hard_coded_dirs[] = {"any", "int", "ext"};
  116. PB_Main response = {
  117. .has_next = false,
  118. .command_id = request->command_id,
  119. .command_status = PB_CommandStatus_OK,
  120. .which_content = PB_Main_storage_list_response_tag,
  121. };
  122. furi_assert(COUNT_OF(hard_coded_dirs) < COUNT_OF(response.content.storage_list_response.file));
  123. for(int i = 0; i < COUNT_OF(hard_coded_dirs); ++i) {
  124. ++response.content.storage_list_response.file_count;
  125. response.content.storage_list_response.file[i].data = NULL;
  126. response.content.storage_list_response.file[i].size = 0;
  127. response.content.storage_list_response.file[i].type = PB_Storage_File_FileType_DIR;
  128. char* str = furi_alloc(strlen(hard_coded_dirs[i]) + 1);
  129. strcpy(str, hard_coded_dirs[i]);
  130. response.content.storage_list_response.file[i].name = str;
  131. }
  132. rpc_send_and_release(rpc_storage->rpc, &response);
  133. }
  134. static void rpc_system_storage_list_process(const PB_Main* request, void* context) {
  135. furi_assert(request);
  136. furi_assert(context);
  137. furi_assert(request->which_content == PB_Main_storage_list_request_tag);
  138. RpcStorageSystem* rpc_storage = context;
  139. rpc_system_storage_reset_state(rpc_storage, true);
  140. if(!strcmp(request->content.storage_list_request.path, "/")) {
  141. rpc_system_storage_list_root(request, context);
  142. return;
  143. }
  144. Storage* fs_api = furi_record_open("storage");
  145. File* dir = storage_file_alloc(fs_api);
  146. PB_Main response = {
  147. .command_id = request->command_id,
  148. .has_next = false,
  149. .which_content = PB_Main_storage_list_response_tag,
  150. .command_status = PB_CommandStatus_OK,
  151. };
  152. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  153. bool finish = false;
  154. int i = 0;
  155. if(!storage_dir_open(dir, request->content.storage_list_request.path)) {
  156. response.command_status = rpc_system_storage_get_file_error(dir);
  157. response.which_content = PB_Main_empty_tag;
  158. finish = true;
  159. }
  160. while(!finish) {
  161. FileInfo fileinfo;
  162. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  163. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  164. if(i == COUNT_OF(list->file)) {
  165. list->file_count = i;
  166. response.has_next = true;
  167. rpc_send_and_release(rpc_storage->rpc, &response);
  168. i = 0;
  169. }
  170. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
  171. PB_Storage_File_FileType_FILE;
  172. list->file[i].size = fileinfo.size;
  173. list->file[i].data = NULL;
  174. list->file[i].name = name;
  175. ++i;
  176. } else {
  177. list->file_count = i;
  178. finish = true;
  179. free(name);
  180. }
  181. }
  182. response.has_next = false;
  183. rpc_send_and_release(rpc_storage->rpc, &response);
  184. storage_dir_close(dir);
  185. storage_file_free(dir);
  186. furi_record_close("storage");
  187. }
  188. static void rpc_system_storage_read_process(const PB_Main* request, void* context) {
  189. furi_assert(request);
  190. furi_assert(request->which_content == PB_Main_storage_read_request_tag);
  191. RpcStorageSystem* rpc_storage = context;
  192. rpc_system_storage_reset_state(rpc_storage, true);
  193. /* use same message memory to send reponse */
  194. PB_Main* response = furi_alloc(sizeof(PB_Main));
  195. const char* path = request->content.storage_read_request.path;
  196. Storage* fs_api = furi_record_open("storage");
  197. File* file = storage_file_alloc(fs_api);
  198. bool result = false;
  199. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  200. size_t size_left = storage_file_size(file);
  201. do {
  202. response->command_id = request->command_id;
  203. response->which_content = PB_Main_storage_read_response_tag;
  204. response->command_status = PB_CommandStatus_OK;
  205. response->content.storage_read_response.has_file = true;
  206. response->content.storage_read_response.file.data =
  207. furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  208. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  209. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  210. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  211. *read_size_msg = storage_file_read(file, buffer, read_size);
  212. size_left -= read_size;
  213. result = (*read_size_msg == read_size);
  214. if(result) {
  215. response->has_next = (size_left > 0);
  216. rpc_send_and_release(rpc_storage->rpc, response);
  217. }
  218. } while((size_left != 0) && result);
  219. if(!result) {
  220. rpc_send_and_release_empty(
  221. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  222. }
  223. } else {
  224. rpc_send_and_release_empty(
  225. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  226. }
  227. free(response);
  228. storage_file_close(file);
  229. storage_file_free(file);
  230. furi_record_close("storage");
  231. }
  232. static void rpc_system_storage_write_process(const PB_Main* request, void* context) {
  233. furi_assert(request);
  234. furi_assert(request->which_content == PB_Main_storage_write_request_tag);
  235. RpcStorageSystem* rpc_storage = context;
  236. bool result = true;
  237. if((request->command_id != rpc_storage->current_command_id) &&
  238. (rpc_storage->state == RpcStorageStateWriting)) {
  239. rpc_system_storage_reset_state(rpc_storage, true);
  240. }
  241. if(rpc_storage->state != RpcStorageStateWriting) {
  242. rpc_storage->api = furi_record_open("storage");
  243. rpc_storage->file = storage_file_alloc(rpc_storage->api);
  244. rpc_storage->current_command_id = request->command_id;
  245. rpc_storage->state = RpcStorageStateWriting;
  246. const char* path = request->content.storage_write_request.path;
  247. result = storage_file_open(rpc_storage->file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  248. }
  249. File* file = rpc_storage->file;
  250. if(result) {
  251. uint8_t* buffer = request->content.storage_write_request.file.data->bytes;
  252. size_t buffer_size = request->content.storage_write_request.file.data->size;
  253. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  254. result = (written_size == buffer_size);
  255. if(result && !request->has_next) {
  256. rpc_send_and_release_empty(
  257. rpc_storage->rpc, rpc_storage->current_command_id, PB_CommandStatus_OK);
  258. rpc_system_storage_reset_state(rpc_storage, false);
  259. }
  260. }
  261. if(!result) {
  262. rpc_send_and_release_empty(
  263. rpc_storage->rpc,
  264. rpc_storage->current_command_id,
  265. rpc_system_storage_get_file_error(file));
  266. rpc_system_storage_reset_state(rpc_storage, false);
  267. }
  268. }
  269. static bool rpc_system_storage_is_dir_is_empty(Storage* fs_api, const char* path) {
  270. FileInfo fileinfo;
  271. bool is_dir_is_empty = false;
  272. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  273. if((error == FSE_OK) && (fileinfo.flags & FSF_DIRECTORY)) {
  274. File* dir = storage_file_alloc(fs_api);
  275. if(storage_dir_open(dir, path)) {
  276. char* name = furi_alloc(MAX_NAME_LENGTH);
  277. is_dir_is_empty = !storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH);
  278. free(name);
  279. }
  280. storage_dir_close(dir);
  281. storage_file_free(dir);
  282. }
  283. return is_dir_is_empty;
  284. }
  285. static void rpc_system_storage_delete_process(const PB_Main* request, void* context) {
  286. furi_assert(request);
  287. furi_assert(request->which_content == PB_Main_storage_delete_request_tag);
  288. furi_assert(context);
  289. RpcStorageSystem* rpc_storage = context;
  290. PB_CommandStatus status = PB_CommandStatus_ERROR;
  291. rpc_system_storage_reset_state(rpc_storage, true);
  292. Storage* fs_api = furi_record_open("storage");
  293. char* path = request->content.storage_delete_request.path;
  294. if(!path) {
  295. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  296. } else {
  297. FS_Error error_remove = storage_common_remove(fs_api, path);
  298. // FSE_DENIED is for empty directory, but not only for this
  299. // that's why we have to check it
  300. if((error_remove == FSE_DENIED) && !rpc_system_storage_is_dir_is_empty(fs_api, path)) {
  301. if(request->content.storage_delete_request.recursive) {
  302. bool deleted = storage_simply_remove_recursive(fs_api, path);
  303. status = deleted ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
  304. } else {
  305. status = PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY;
  306. }
  307. } else if(error_remove == FSE_NOT_EXIST) {
  308. status = PB_CommandStatus_OK;
  309. } else {
  310. status = rpc_system_storage_get_error(error_remove);
  311. }
  312. }
  313. furi_record_close("storage");
  314. rpc_send_and_release_empty(rpc_storage->rpc, request->command_id, status);
  315. }
  316. static void rpc_system_storage_mkdir_process(const PB_Main* request, void* context) {
  317. furi_assert(request);
  318. furi_assert(request->which_content == PB_Main_storage_mkdir_request_tag);
  319. furi_assert(context);
  320. RpcStorageSystem* rpc_storage = context;
  321. PB_CommandStatus status;
  322. rpc_system_storage_reset_state(rpc_storage, true);
  323. Storage* fs_api = furi_record_open("storage");
  324. char* path = request->content.storage_mkdir_request.path;
  325. if(path) {
  326. FS_Error error = storage_common_mkdir(fs_api, path);
  327. status = rpc_system_storage_get_error(error);
  328. } else {
  329. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  330. }
  331. rpc_send_and_release_empty(rpc_storage->rpc, request->command_id, status);
  332. }
  333. static void rpc_system_storage_md5sum_process(const PB_Main* request, void* context) {
  334. furi_assert(request);
  335. furi_assert(request->which_content == PB_Main_storage_md5sum_request_tag);
  336. furi_assert(context);
  337. RpcStorageSystem* rpc_storage = context;
  338. rpc_system_storage_reset_state(rpc_storage, true);
  339. const char* filename = request->content.storage_md5sum_request.path;
  340. if(!filename) {
  341. rpc_send_and_release_empty(
  342. rpc_storage->rpc, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  343. return;
  344. }
  345. Storage* fs_api = furi_record_open("storage");
  346. File* file = storage_file_alloc(fs_api);
  347. if(storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
  348. const uint16_t read_size = 512;
  349. const uint8_t hash_size = 16;
  350. uint8_t* data = malloc(read_size);
  351. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  352. md5_context* md5_ctx = malloc(sizeof(md5_context));
  353. md5_starts(md5_ctx);
  354. while(true) {
  355. uint16_t readed_size = storage_file_read(file, data, read_size);
  356. if(readed_size == 0) break;
  357. md5_update(md5_ctx, data, readed_size);
  358. }
  359. md5_finish(md5_ctx, hash);
  360. free(md5_ctx);
  361. PB_Main response = {
  362. .command_id = request->command_id,
  363. .command_status = PB_CommandStatus_OK,
  364. .which_content = PB_Main_storage_md5sum_response_tag,
  365. .has_next = false,
  366. };
  367. char* md5sum = response.content.storage_md5sum_response.md5sum;
  368. size_t md5sum_size = sizeof(response.content.storage_md5sum_response.md5sum);
  369. (void)md5sum_size;
  370. furi_assert(hash_size <= ((md5sum_size - 1) / 2));
  371. for(uint8_t i = 0; i < hash_size; i++) {
  372. md5sum += sprintf(md5sum, "%02x", hash[i]);
  373. }
  374. free(hash);
  375. free(data);
  376. storage_file_close(file);
  377. rpc_send_and_release(rpc_storage->rpc, &response);
  378. } else {
  379. rpc_send_and_release_empty(
  380. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  381. }
  382. storage_file_free(file);
  383. furi_record_close("storage");
  384. }
  385. void* rpc_system_storage_alloc(Rpc* rpc) {
  386. furi_assert(rpc);
  387. RpcStorageSystem* rpc_storage = furi_alloc(sizeof(RpcStorageSystem));
  388. rpc_storage->api = furi_record_open("storage");
  389. rpc_storage->rpc = rpc;
  390. rpc_storage->state = RpcStorageStateIdle;
  391. RpcHandler rpc_handler = {
  392. .message_handler = NULL,
  393. .decode_submessage = NULL,
  394. .context = rpc_storage,
  395. };
  396. rpc_handler.message_handler = rpc_system_storage_stat_process;
  397. rpc_add_handler(rpc, PB_Main_storage_stat_request_tag, &rpc_handler);
  398. rpc_handler.message_handler = rpc_system_storage_list_process;
  399. rpc_add_handler(rpc, PB_Main_storage_list_request_tag, &rpc_handler);
  400. rpc_handler.message_handler = rpc_system_storage_read_process;
  401. rpc_add_handler(rpc, PB_Main_storage_read_request_tag, &rpc_handler);
  402. rpc_handler.message_handler = rpc_system_storage_write_process;
  403. rpc_add_handler(rpc, PB_Main_storage_write_request_tag, &rpc_handler);
  404. rpc_handler.message_handler = rpc_system_storage_delete_process;
  405. rpc_add_handler(rpc, PB_Main_storage_delete_request_tag, &rpc_handler);
  406. rpc_handler.message_handler = rpc_system_storage_mkdir_process;
  407. rpc_add_handler(rpc, PB_Main_storage_mkdir_request_tag, &rpc_handler);
  408. rpc_handler.message_handler = rpc_system_storage_md5sum_process;
  409. rpc_add_handler(rpc, PB_Main_storage_md5sum_request_tag, &rpc_handler);
  410. return rpc_storage;
  411. }
  412. void rpc_system_storage_free(void* ctx) {
  413. RpcStorageSystem* rpc_storage = ctx;
  414. rpc_system_storage_reset_state(rpc_storage, false);
  415. free(rpc_storage);
  416. }