rpc_storage.c 25 KB

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