rpc_storage.c 25 KB

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