rpc_storage.c 20 KB

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