rpc_storage.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. #include <lib/toolbox/path.h>
  14. #include <update_util/lfs_backup.h>
  15. #define TAG "RpcStorage"
  16. #define MAX_NAME_LENGTH 255
  17. static const size_t MAX_DATA_SIZE = 512;
  18. typedef enum {
  19. RpcStorageStateIdle = 0,
  20. RpcStorageStateWriting,
  21. } RpcStorageState;
  22. typedef struct {
  23. RpcSession* session;
  24. Storage* api;
  25. File* file;
  26. RpcStorageState state;
  27. uint32_t current_command_id;
  28. } RpcStorageSystem;
  29. static void rpc_system_storage_reset_state(
  30. RpcStorageSystem* rpc_storage,
  31. RpcSession* session,
  32. bool send_error) {
  33. furi_assert(rpc_storage);
  34. if(rpc_storage->state != RpcStorageStateIdle) {
  35. if(send_error) {
  36. rpc_send_and_release_empty(
  37. session,
  38. rpc_storage->current_command_id,
  39. PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED);
  40. }
  41. if(rpc_storage->state == RpcStorageStateWriting) {
  42. storage_file_close(rpc_storage->file);
  43. storage_file_free(rpc_storage->file);
  44. furi_record_close("storage");
  45. }
  46. rpc_storage->state = RpcStorageStateIdle;
  47. }
  48. }
  49. PB_CommandStatus rpc_system_storage_get_error(FS_Error fs_error) {
  50. PB_CommandStatus pb_error;
  51. switch(fs_error) {
  52. case FSE_OK:
  53. pb_error = PB_CommandStatus_OK;
  54. break;
  55. case FSE_INVALID_NAME:
  56. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  57. break;
  58. case FSE_INVALID_PARAMETER:
  59. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
  60. break;
  61. case FSE_INTERNAL:
  62. pb_error = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
  63. break;
  64. case FSE_ALREADY_OPEN:
  65. pb_error = PB_CommandStatus_ERROR_STORAGE_ALREADY_OPEN;
  66. break;
  67. case FSE_DENIED:
  68. pb_error = PB_CommandStatus_ERROR_STORAGE_DENIED;
  69. break;
  70. case FSE_EXIST:
  71. pb_error = PB_CommandStatus_ERROR_STORAGE_EXIST;
  72. break;
  73. case FSE_NOT_EXIST:
  74. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_EXIST;
  75. break;
  76. case FSE_NOT_READY:
  77. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_READY;
  78. break;
  79. case FSE_NOT_IMPLEMENTED:
  80. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_IMPLEMENTED;
  81. break;
  82. default:
  83. pb_error = PB_CommandStatus_ERROR;
  84. break;
  85. }
  86. return pb_error;
  87. }
  88. static PB_CommandStatus rpc_system_storage_get_file_error(File* file) {
  89. return rpc_system_storage_get_error(storage_file_get_error(file));
  90. }
  91. static void rpc_system_storage_info_process(const PB_Main* request, void* context) {
  92. furi_assert(request);
  93. furi_assert(context);
  94. furi_assert(request->which_content == PB_Main_storage_info_request_tag);
  95. FURI_LOG_D(TAG, "Info");
  96. RpcStorageSystem* rpc_storage = context;
  97. RpcSession* session = rpc_storage->session;
  98. furi_assert(session);
  99. rpc_system_storage_reset_state(rpc_storage, session, true);
  100. PB_Main* response = malloc(sizeof(PB_Main));
  101. response->command_id = request->command_id;
  102. Storage* fs_api = furi_record_open("storage");
  103. FS_Error error = storage_common_fs_info(
  104. fs_api,
  105. request->content.storage_info_request.path,
  106. &response->content.storage_info_response.total_space,
  107. &response->content.storage_info_response.free_space);
  108. response->command_status = rpc_system_storage_get_error(error);
  109. if(error == FSE_OK) {
  110. response->which_content = PB_Main_storage_info_response_tag;
  111. } else {
  112. response->which_content = PB_Main_empty_tag;
  113. }
  114. rpc_send_and_release(session, response);
  115. free(response);
  116. furi_record_close("storage");
  117. }
  118. static void rpc_system_storage_stat_process(const PB_Main* request, void* context) {
  119. furi_assert(request);
  120. furi_assert(context);
  121. furi_assert(request->which_content == PB_Main_storage_stat_request_tag);
  122. FURI_LOG_D(TAG, "Stat");
  123. RpcStorageSystem* rpc_storage = context;
  124. RpcSession* session = rpc_storage->session;
  125. furi_assert(session);
  126. rpc_system_storage_reset_state(rpc_storage, session, true);
  127. PB_Main* response = malloc(sizeof(PB_Main));
  128. response->command_id = request->command_id;
  129. Storage* fs_api = furi_record_open("storage");
  130. const char* path = request->content.storage_stat_request.path;
  131. FileInfo fileinfo;
  132. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  133. response->command_status = rpc_system_storage_get_error(error);
  134. response->which_content = PB_Main_empty_tag;
  135. if(error == FSE_OK) {
  136. response->which_content = PB_Main_storage_stat_response_tag;
  137. response->content.storage_stat_response.has_file = true;
  138. response->content.storage_stat_response.file.type = (fileinfo.flags & FSF_DIRECTORY) ?
  139. PB_Storage_File_FileType_DIR :
  140. PB_Storage_File_FileType_FILE;
  141. response->content.storage_stat_response.file.size = fileinfo.size;
  142. }
  143. rpc_send_and_release(session, response);
  144. free(response);
  145. furi_record_close("storage");
  146. }
  147. static void rpc_system_storage_list_root(const PB_Main* request, void* context) {
  148. RpcStorageSystem* rpc_storage = context;
  149. RpcSession* session = rpc_storage->session;
  150. furi_assert(session);
  151. const char* hard_coded_dirs[] = {"any", "int", "ext"};
  152. PB_Main response = {
  153. .has_next = false,
  154. .command_id = request->command_id,
  155. .command_status = PB_CommandStatus_OK,
  156. .which_content = PB_Main_storage_list_response_tag,
  157. };
  158. furi_assert(COUNT_OF(hard_coded_dirs) < COUNT_OF(response.content.storage_list_response.file));
  159. for(uint32_t i = 0; i < COUNT_OF(hard_coded_dirs); ++i) {
  160. ++response.content.storage_list_response.file_count;
  161. response.content.storage_list_response.file[i].data = NULL;
  162. response.content.storage_list_response.file[i].size = 0;
  163. response.content.storage_list_response.file[i].type = PB_Storage_File_FileType_DIR;
  164. char* str = malloc(strlen(hard_coded_dirs[i]) + 1);
  165. strcpy(str, hard_coded_dirs[i]);
  166. response.content.storage_list_response.file[i].name = str;
  167. }
  168. rpc_send_and_release(session, &response);
  169. }
  170. static void rpc_system_storage_list_process(const PB_Main* request, void* context) {
  171. furi_assert(request);
  172. furi_assert(context);
  173. furi_assert(request->which_content == PB_Main_storage_list_request_tag);
  174. FURI_LOG_D(TAG, "List");
  175. RpcStorageSystem* rpc_storage = context;
  176. RpcSession* session = rpc_storage->session;
  177. furi_assert(session);
  178. rpc_system_storage_reset_state(rpc_storage, session, true);
  179. if(!strcmp(request->content.storage_list_request.path, "/")) {
  180. rpc_system_storage_list_root(request, context);
  181. return;
  182. }
  183. Storage* fs_api = furi_record_open("storage");
  184. File* dir = storage_file_alloc(fs_api);
  185. PB_Main response = {
  186. .command_id = request->command_id,
  187. .has_next = false,
  188. .which_content = PB_Main_storage_list_response_tag,
  189. .command_status = PB_CommandStatus_OK,
  190. };
  191. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  192. bool finish = false;
  193. int i = 0;
  194. if(!storage_dir_open(dir, request->content.storage_list_request.path)) {
  195. response.command_status = rpc_system_storage_get_file_error(dir);
  196. response.which_content = PB_Main_empty_tag;
  197. finish = true;
  198. }
  199. while(!finish) {
  200. FileInfo fileinfo;
  201. char* name = malloc(MAX_NAME_LENGTH + 1);
  202. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  203. if(path_contains_only_ascii(name)) {
  204. if(i == COUNT_OF(list->file)) {
  205. list->file_count = i;
  206. response.has_next = true;
  207. rpc_send_and_release(session, &response);
  208. i = 0;
  209. }
  210. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ?
  211. PB_Storage_File_FileType_DIR :
  212. PB_Storage_File_FileType_FILE;
  213. list->file[i].size = fileinfo.size;
  214. list->file[i].data = NULL;
  215. list->file[i].name = name;
  216. ++i;
  217. } else {
  218. free(name);
  219. }
  220. } else {
  221. list->file_count = i;
  222. finish = true;
  223. free(name);
  224. }
  225. }
  226. response.has_next = false;
  227. rpc_send_and_release(session, &response);
  228. storage_dir_close(dir);
  229. storage_file_free(dir);
  230. furi_record_close("storage");
  231. }
  232. static void rpc_system_storage_read_process(const PB_Main* request, void* context) {
  233. furi_assert(request);
  234. furi_assert(context);
  235. furi_assert(request->which_content == PB_Main_storage_read_request_tag);
  236. FURI_LOG_D(TAG, "Read");
  237. RpcStorageSystem* rpc_storage = context;
  238. RpcSession* session = rpc_storage->session;
  239. furi_assert(session);
  240. rpc_system_storage_reset_state(rpc_storage, session, true);
  241. /* use same message memory to send reponse */
  242. PB_Main* response = malloc(sizeof(PB_Main));
  243. const char* path = request->content.storage_read_request.path;
  244. Storage* fs_api = furi_record_open("storage");
  245. File* file = storage_file_alloc(fs_api);
  246. bool result = false;
  247. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  248. size_t size_left = storage_file_size(file);
  249. do {
  250. response->command_id = request->command_id;
  251. response->which_content = PB_Main_storage_read_response_tag;
  252. response->command_status = PB_CommandStatus_OK;
  253. response->content.storage_read_response.has_file = true;
  254. response->content.storage_read_response.file.data =
  255. malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  256. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  257. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  258. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  259. *read_size_msg = storage_file_read(file, buffer, read_size);
  260. size_left -= read_size;
  261. result = (*read_size_msg == read_size);
  262. if(result) {
  263. response->has_next = (size_left > 0);
  264. rpc_send_and_release(session, response);
  265. }
  266. } while((size_left != 0) && result);
  267. if(!result) {
  268. rpc_send_and_release_empty(
  269. session, request->command_id, rpc_system_storage_get_file_error(file));
  270. }
  271. } else {
  272. rpc_send_and_release_empty(
  273. session, request->command_id, rpc_system_storage_get_file_error(file));
  274. }
  275. free(response);
  276. storage_file_close(file);
  277. storage_file_free(file);
  278. furi_record_close("storage");
  279. }
  280. static void rpc_system_storage_write_process(const PB_Main* request, void* context) {
  281. furi_assert(request);
  282. furi_assert(context);
  283. furi_assert(request->which_content == PB_Main_storage_write_request_tag);
  284. FURI_LOG_D(TAG, "Write");
  285. RpcStorageSystem* rpc_storage = context;
  286. RpcSession* session = rpc_storage->session;
  287. furi_assert(session);
  288. bool result = true;
  289. if(!path_contains_only_ascii(request->content.storage_write_request.path)) {
  290. rpc_storage->current_command_id = request->command_id;
  291. rpc_send_and_release_empty(
  292. session, rpc_storage->current_command_id, PB_CommandStatus_ERROR_STORAGE_INVALID_NAME);
  293. rpc_system_storage_reset_state(rpc_storage, session, false);
  294. return;
  295. }
  296. if((request->command_id != rpc_storage->current_command_id) &&
  297. (rpc_storage->state == RpcStorageStateWriting)) {
  298. rpc_system_storage_reset_state(rpc_storage, session, true);
  299. }
  300. if(rpc_storage->state != RpcStorageStateWriting) {
  301. rpc_storage->api = furi_record_open("storage");
  302. rpc_storage->file = storage_file_alloc(rpc_storage->api);
  303. rpc_storage->current_command_id = request->command_id;
  304. rpc_storage->state = RpcStorageStateWriting;
  305. const char* path = request->content.storage_write_request.path;
  306. result = storage_file_open(rpc_storage->file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  307. }
  308. File* file = rpc_storage->file;
  309. if(result) {
  310. uint8_t* buffer = request->content.storage_write_request.file.data->bytes;
  311. size_t buffer_size = request->content.storage_write_request.file.data->size;
  312. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  313. result = (written_size == buffer_size);
  314. if(result && !request->has_next) {
  315. rpc_send_and_release_empty(
  316. session, rpc_storage->current_command_id, PB_CommandStatus_OK);
  317. rpc_system_storage_reset_state(rpc_storage, session, false);
  318. }
  319. }
  320. if(!result) {
  321. rpc_send_and_release_empty(
  322. session, rpc_storage->current_command_id, rpc_system_storage_get_file_error(file));
  323. rpc_system_storage_reset_state(rpc_storage, session, false);
  324. }
  325. }
  326. static bool rpc_system_storage_is_dir_is_empty(Storage* fs_api, const char* path) {
  327. FileInfo fileinfo;
  328. bool is_dir_is_empty = true;
  329. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  330. if((error == FSE_OK) && (fileinfo.flags & FSF_DIRECTORY)) {
  331. File* dir = storage_file_alloc(fs_api);
  332. if(storage_dir_open(dir, path)) {
  333. char* name = malloc(MAX_NAME_LENGTH);
  334. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  335. if(path_contains_only_ascii(name)) {
  336. is_dir_is_empty = false;
  337. break;
  338. }
  339. }
  340. free(name);
  341. }
  342. storage_dir_close(dir);
  343. storage_file_free(dir);
  344. }
  345. return is_dir_is_empty;
  346. }
  347. static void rpc_system_storage_delete_process(const PB_Main* request, void* context) {
  348. furi_assert(request);
  349. furi_assert(request->which_content == PB_Main_storage_delete_request_tag);
  350. furi_assert(context);
  351. FURI_LOG_D(TAG, "Delete");
  352. RpcStorageSystem* rpc_storage = context;
  353. RpcSession* session = rpc_storage->session;
  354. furi_assert(session);
  355. PB_CommandStatus status = PB_CommandStatus_ERROR;
  356. rpc_system_storage_reset_state(rpc_storage, session, true);
  357. Storage* fs_api = furi_record_open("storage");
  358. char* path = request->content.storage_delete_request.path;
  359. if(!path) {
  360. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  361. } else {
  362. FS_Error error_remove = storage_common_remove(fs_api, path);
  363. // FSE_DENIED is for empty directory, but not only for this
  364. // that's why we have to check it
  365. if((error_remove == FSE_DENIED) && !rpc_system_storage_is_dir_is_empty(fs_api, path)) {
  366. if(request->content.storage_delete_request.recursive) {
  367. bool deleted = storage_simply_remove_recursive(fs_api, path);
  368. status = deleted ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
  369. } else {
  370. status = PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY;
  371. }
  372. } else if(error_remove == FSE_NOT_EXIST) {
  373. status = PB_CommandStatus_OK;
  374. } else {
  375. status = rpc_system_storage_get_error(error_remove);
  376. }
  377. }
  378. furi_record_close("storage");
  379. rpc_send_and_release_empty(session, request->command_id, status);
  380. }
  381. static void rpc_system_storage_mkdir_process(const PB_Main* request, void* context) {
  382. furi_assert(request);
  383. furi_assert(request->which_content == PB_Main_storage_mkdir_request_tag);
  384. furi_assert(context);
  385. FURI_LOG_D(TAG, "Mkdir");
  386. RpcStorageSystem* rpc_storage = context;
  387. RpcSession* session = rpc_storage->session;
  388. furi_assert(session);
  389. PB_CommandStatus status;
  390. rpc_system_storage_reset_state(rpc_storage, session, true);
  391. Storage* fs_api = furi_record_open("storage");
  392. char* path = request->content.storage_mkdir_request.path;
  393. if(path) {
  394. if(path_contains_only_ascii(path)) {
  395. FS_Error error = storage_common_mkdir(fs_api, path);
  396. status = rpc_system_storage_get_error(error);
  397. } else {
  398. status = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  399. }
  400. } else {
  401. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  402. }
  403. furi_record_close("storage");
  404. rpc_send_and_release_empty(session, request->command_id, status);
  405. }
  406. static void rpc_system_storage_md5sum_process(const PB_Main* request, void* context) {
  407. furi_assert(request);
  408. furi_assert(request->which_content == PB_Main_storage_md5sum_request_tag);
  409. furi_assert(context);
  410. FURI_LOG_D(TAG, "Md5sum");
  411. RpcStorageSystem* rpc_storage = context;
  412. RpcSession* session = rpc_storage->session;
  413. furi_assert(session);
  414. rpc_system_storage_reset_state(rpc_storage, session, true);
  415. const char* filename = request->content.storage_md5sum_request.path;
  416. if(!filename) {
  417. rpc_send_and_release_empty(
  418. session, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  419. return;
  420. }
  421. Storage* fs_api = furi_record_open("storage");
  422. File* file = storage_file_alloc(fs_api);
  423. if(storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
  424. const uint16_t size_to_read = 512;
  425. const uint8_t hash_size = 16;
  426. uint8_t* data = malloc(size_to_read);
  427. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  428. md5_context* md5_ctx = malloc(sizeof(md5_context));
  429. md5_starts(md5_ctx);
  430. while(true) {
  431. uint16_t read_size = storage_file_read(file, data, size_to_read);
  432. if(read_size == 0) break;
  433. md5_update(md5_ctx, data, read_size);
  434. }
  435. md5_finish(md5_ctx, hash);
  436. free(md5_ctx);
  437. PB_Main response = {
  438. .command_id = request->command_id,
  439. .command_status = PB_CommandStatus_OK,
  440. .which_content = PB_Main_storage_md5sum_response_tag,
  441. .has_next = false,
  442. };
  443. char* md5sum = response.content.storage_md5sum_response.md5sum;
  444. size_t md5sum_size = sizeof(response.content.storage_md5sum_response.md5sum);
  445. (void)md5sum_size;
  446. furi_assert(hash_size <= ((md5sum_size - 1) / 2));
  447. for(uint8_t i = 0; i < hash_size; i++) {
  448. md5sum += sprintf(md5sum, "%02x", hash[i]);
  449. }
  450. free(hash);
  451. free(data);
  452. storage_file_close(file);
  453. rpc_send_and_release(session, &response);
  454. } else {
  455. rpc_send_and_release_empty(
  456. session, request->command_id, rpc_system_storage_get_file_error(file));
  457. }
  458. storage_file_free(file);
  459. furi_record_close("storage");
  460. }
  461. static void rpc_system_storage_rename_process(const PB_Main* request, void* context) {
  462. furi_assert(request);
  463. furi_assert(request->which_content == PB_Main_storage_rename_request_tag);
  464. furi_assert(context);
  465. FURI_LOG_D(TAG, "Rename");
  466. RpcStorageSystem* rpc_storage = context;
  467. RpcSession* session = rpc_storage->session;
  468. furi_assert(session);
  469. PB_CommandStatus status;
  470. rpc_system_storage_reset_state(rpc_storage, session, true);
  471. Storage* fs_api = furi_record_open("storage");
  472. if(path_contains_only_ascii(request->content.storage_rename_request.new_path)) {
  473. FS_Error error = storage_common_rename(
  474. fs_api,
  475. request->content.storage_rename_request.old_path,
  476. request->content.storage_rename_request.new_path);
  477. status = rpc_system_storage_get_error(error);
  478. } else {
  479. status = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  480. }
  481. furi_record_close("storage");
  482. rpc_send_and_release_empty(session, request->command_id, status);
  483. }
  484. static void rpc_system_storage_backup_create_process(const PB_Main* request, void* context) {
  485. furi_assert(request);
  486. furi_assert(request->which_content == PB_Main_storage_backup_create_request_tag);
  487. FURI_LOG_D(TAG, "BackupCreate");
  488. RpcSession* session = (RpcSession*)context;
  489. furi_assert(session);
  490. PB_Main* response = malloc(sizeof(PB_Main));
  491. response->command_id = request->command_id;
  492. response->has_next = false;
  493. Storage* fs_api = furi_record_open("storage");
  494. bool backup_ok =
  495. lfs_backup_create(fs_api, request->content.storage_backup_create_request.archive_path);
  496. response->command_status = backup_ok ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
  497. furi_record_close("storage");
  498. rpc_send_and_release(session, response);
  499. free(response);
  500. }
  501. static void rpc_system_storage_backup_restore_process(const PB_Main* request, void* context) {
  502. furi_assert(request);
  503. furi_assert(request->which_content == PB_Main_storage_backup_restore_request_tag);
  504. FURI_LOG_D(TAG, "BackupRestore");
  505. RpcSession* session = (RpcSession*)context;
  506. furi_assert(session);
  507. PB_Main* response = malloc(sizeof(PB_Main));
  508. response->command_id = request->command_id;
  509. response->has_next = false;
  510. response->command_status = PB_CommandStatus_OK;
  511. Storage* fs_api = furi_record_open("storage");
  512. bool backup_ok =
  513. lfs_backup_unpack(fs_api, request->content.storage_backup_restore_request.archive_path);
  514. response->command_status = backup_ok ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
  515. furi_record_close("storage");
  516. rpc_send_and_release(session, response);
  517. free(response);
  518. }
  519. void* rpc_system_storage_alloc(RpcSession* session) {
  520. furi_assert(session);
  521. RpcStorageSystem* rpc_storage = malloc(sizeof(RpcStorageSystem));
  522. rpc_storage->api = furi_record_open("storage");
  523. rpc_storage->session = session;
  524. rpc_storage->state = RpcStorageStateIdle;
  525. RpcHandler rpc_handler = {
  526. .message_handler = NULL,
  527. .decode_submessage = NULL,
  528. .context = rpc_storage,
  529. };
  530. rpc_handler.message_handler = rpc_system_storage_info_process;
  531. rpc_add_handler(session, PB_Main_storage_info_request_tag, &rpc_handler);
  532. rpc_handler.message_handler = rpc_system_storage_stat_process;
  533. rpc_add_handler(session, PB_Main_storage_stat_request_tag, &rpc_handler);
  534. rpc_handler.message_handler = rpc_system_storage_list_process;
  535. rpc_add_handler(session, PB_Main_storage_list_request_tag, &rpc_handler);
  536. rpc_handler.message_handler = rpc_system_storage_read_process;
  537. rpc_add_handler(session, PB_Main_storage_read_request_tag, &rpc_handler);
  538. rpc_handler.message_handler = rpc_system_storage_write_process;
  539. rpc_add_handler(session, PB_Main_storage_write_request_tag, &rpc_handler);
  540. rpc_handler.message_handler = rpc_system_storage_delete_process;
  541. rpc_add_handler(session, PB_Main_storage_delete_request_tag, &rpc_handler);
  542. rpc_handler.message_handler = rpc_system_storage_mkdir_process;
  543. rpc_add_handler(session, PB_Main_storage_mkdir_request_tag, &rpc_handler);
  544. rpc_handler.message_handler = rpc_system_storage_md5sum_process;
  545. rpc_add_handler(session, PB_Main_storage_md5sum_request_tag, &rpc_handler);
  546. rpc_handler.message_handler = rpc_system_storage_rename_process;
  547. rpc_add_handler(session, PB_Main_storage_rename_request_tag, &rpc_handler);
  548. rpc_handler.message_handler = rpc_system_storage_backup_create_process;
  549. rpc_add_handler(session, PB_Main_storage_backup_create_request_tag, &rpc_handler);
  550. rpc_handler.message_handler = rpc_system_storage_backup_restore_process;
  551. rpc_add_handler(session, PB_Main_storage_backup_restore_request_tag, &rpc_handler);
  552. return rpc_storage;
  553. }
  554. void rpc_system_storage_free(void* context) {
  555. RpcStorageSystem* rpc_storage = context;
  556. RpcSession* session = rpc_storage->session;
  557. furi_assert(session);
  558. rpc_system_storage_reset_state(rpc_storage, session, false);
  559. free(rpc_storage);
  560. }