rpc_storage.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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_timestamp_process(const PB_Main* request, void* context) {
  120. furi_assert(request);
  121. furi_assert(context);
  122. furi_assert(request->which_content == PB_Main_storage_timestamp_request_tag);
  123. FURI_LOG_D(TAG, "Timestamp");
  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_timestamp_request.path;
  132. uint32_t timestamp = 0;
  133. FS_Error error = storage_common_timestamp(fs_api, path, &timestamp);
  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_timestamp_response_tag;
  138. response->content.storage_timestamp_response.timestamp = timestamp;
  139. }
  140. rpc_send_and_release(session, response);
  141. free(response);
  142. furi_record_close(RECORD_STORAGE);
  143. }
  144. static void rpc_system_storage_stat_process(const PB_Main* request, void* context) {
  145. furi_assert(request);
  146. furi_assert(context);
  147. furi_assert(request->which_content == PB_Main_storage_stat_request_tag);
  148. FURI_LOG_D(TAG, "Stat");
  149. RpcStorageSystem* rpc_storage = context;
  150. RpcSession* session = rpc_storage->session;
  151. furi_assert(session);
  152. rpc_system_storage_reset_state(rpc_storage, session, true);
  153. PB_Main* response = malloc(sizeof(PB_Main));
  154. response->command_id = request->command_id;
  155. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  156. const char* path = request->content.storage_stat_request.path;
  157. FileInfo fileinfo;
  158. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  159. response->command_status = rpc_system_storage_get_error(error);
  160. response->which_content = PB_Main_empty_tag;
  161. if(error == FSE_OK) {
  162. response->which_content = PB_Main_storage_stat_response_tag;
  163. response->content.storage_stat_response.has_file = true;
  164. response->content.storage_stat_response.file.type = (fileinfo.flags & FSF_DIRECTORY) ?
  165. PB_Storage_File_FileType_DIR :
  166. PB_Storage_File_FileType_FILE;
  167. response->content.storage_stat_response.file.size = fileinfo.size;
  168. }
  169. rpc_send_and_release(session, response);
  170. free(response);
  171. furi_record_close(RECORD_STORAGE);
  172. }
  173. static void rpc_system_storage_list_root(const PB_Main* request, void* context) {
  174. furi_assert(request);
  175. furi_assert(context);
  176. RpcStorageSystem* rpc_storage = context;
  177. RpcSession* session = rpc_storage->session;
  178. furi_assert(session);
  179. const char* hard_coded_dirs[] = {"any", "int", "ext"};
  180. PB_Main response = {
  181. .has_next = false,
  182. .command_id = request->command_id,
  183. .command_status = PB_CommandStatus_OK,
  184. .which_content = PB_Main_storage_list_response_tag,
  185. };
  186. furi_assert(COUNT_OF(hard_coded_dirs) < COUNT_OF(response.content.storage_list_response.file));
  187. for(uint32_t i = 0; i < COUNT_OF(hard_coded_dirs); ++i) {
  188. ++response.content.storage_list_response.file_count;
  189. response.content.storage_list_response.file[i].data = NULL;
  190. response.content.storage_list_response.file[i].size = 0;
  191. response.content.storage_list_response.file[i].type = PB_Storage_File_FileType_DIR;
  192. char* str = malloc(strlen(hard_coded_dirs[i]) + 1);
  193. strcpy(str, hard_coded_dirs[i]);
  194. response.content.storage_list_response.file[i].name = str;
  195. }
  196. rpc_send_and_release(session, &response);
  197. }
  198. static void rpc_system_storage_list_process(const PB_Main* request, void* context) {
  199. furi_assert(request);
  200. furi_assert(context);
  201. furi_assert(request->which_content == PB_Main_storage_list_request_tag);
  202. FURI_LOG_D(TAG, "List");
  203. RpcStorageSystem* rpc_storage = context;
  204. RpcSession* session = rpc_storage->session;
  205. furi_assert(session);
  206. rpc_system_storage_reset_state(rpc_storage, session, true);
  207. if(!strcmp(request->content.storage_list_request.path, "/")) {
  208. rpc_system_storage_list_root(request, context);
  209. return;
  210. }
  211. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  212. File* dir = storage_file_alloc(fs_api);
  213. PB_Main response = {
  214. .command_id = request->command_id,
  215. .has_next = false,
  216. .which_content = PB_Main_storage_list_response_tag,
  217. .command_status = PB_CommandStatus_OK,
  218. };
  219. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  220. bool finish = false;
  221. int i = 0;
  222. if(!storage_dir_open(dir, request->content.storage_list_request.path)) {
  223. response.command_status = rpc_system_storage_get_file_error(dir);
  224. response.which_content = PB_Main_empty_tag;
  225. finish = true;
  226. }
  227. while(!finish) {
  228. FileInfo fileinfo;
  229. char* name = malloc(MAX_NAME_LENGTH + 1);
  230. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  231. if(path_contains_only_ascii(name)) {
  232. if(i == COUNT_OF(list->file)) {
  233. list->file_count = i;
  234. response.has_next = true;
  235. rpc_send_and_release(session, &response);
  236. i = 0;
  237. }
  238. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ?
  239. PB_Storage_File_FileType_DIR :
  240. PB_Storage_File_FileType_FILE;
  241. list->file[i].size = fileinfo.size;
  242. list->file[i].data = NULL;
  243. list->file[i].name = name;
  244. ++i;
  245. } else {
  246. free(name);
  247. }
  248. } else {
  249. list->file_count = i;
  250. finish = true;
  251. free(name);
  252. }
  253. }
  254. response.has_next = false;
  255. rpc_send_and_release(session, &response);
  256. storage_dir_close(dir);
  257. storage_file_free(dir);
  258. furi_record_close(RECORD_STORAGE);
  259. }
  260. static void rpc_system_storage_read_process(const PB_Main* request, void* context) {
  261. furi_assert(request);
  262. furi_assert(context);
  263. furi_assert(request->which_content == PB_Main_storage_read_request_tag);
  264. FURI_LOG_D(TAG, "Read");
  265. RpcStorageSystem* rpc_storage = context;
  266. RpcSession* session = rpc_storage->session;
  267. furi_assert(session);
  268. rpc_system_storage_reset_state(rpc_storage, session, true);
  269. /* use same message memory to send reponse */
  270. PB_Main* response = malloc(sizeof(PB_Main));
  271. const char* path = request->content.storage_read_request.path;
  272. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  273. File* file = storage_file_alloc(fs_api);
  274. bool fs_operation_success = storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING);
  275. if(fs_operation_success) {
  276. size_t size_left = storage_file_size(file);
  277. do {
  278. response->command_id = request->command_id;
  279. response->which_content = PB_Main_storage_read_response_tag;
  280. response->command_status = PB_CommandStatus_OK;
  281. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  282. if(read_size) {
  283. response->content.storage_read_response.has_file = true;
  284. response->content.storage_read_response.file.data =
  285. malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(read_size));
  286. uint8_t* buffer = &response->content.storage_read_response.file.data->bytes[0];
  287. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  288. *read_size_msg = storage_file_read(file, buffer, read_size);
  289. size_left -= *read_size_msg;
  290. fs_operation_success = (*read_size_msg == read_size);
  291. response->has_next = fs_operation_success && (size_left > 0);
  292. } else {
  293. response->content.storage_read_response.file.data =
  294. malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(0));
  295. response->content.storage_read_response.file.data->size = 0;
  296. response->content.storage_read_response.has_file = true;
  297. response->has_next = false;
  298. fs_operation_success = true;
  299. }
  300. if(fs_operation_success) {
  301. rpc_send_and_release(session, response);
  302. }
  303. } while((size_left != 0) && fs_operation_success);
  304. }
  305. if(!fs_operation_success) {
  306. rpc_send_and_release_empty(
  307. session, request->command_id, rpc_system_storage_get_file_error(file));
  308. }
  309. free(response);
  310. storage_file_close(file);
  311. storage_file_free(file);
  312. furi_record_close(RECORD_STORAGE);
  313. }
  314. static void rpc_system_storage_write_process(const PB_Main* request, void* context) {
  315. furi_assert(request);
  316. furi_assert(context);
  317. furi_assert(request->which_content == PB_Main_storage_write_request_tag);
  318. FURI_LOG_D(TAG, "Write");
  319. RpcStorageSystem* rpc_storage = context;
  320. RpcSession* session = rpc_storage->session;
  321. furi_assert(session);
  322. bool fs_operation_success = true;
  323. if(!path_contains_only_ascii(request->content.storage_write_request.path)) {
  324. rpc_storage->current_command_id = request->command_id;
  325. rpc_send_and_release_empty(
  326. session, rpc_storage->current_command_id, PB_CommandStatus_ERROR_STORAGE_INVALID_NAME);
  327. rpc_system_storage_reset_state(rpc_storage, session, false);
  328. return;
  329. }
  330. if((request->command_id != rpc_storage->current_command_id) &&
  331. (rpc_storage->state == RpcStorageStateWriting)) {
  332. rpc_system_storage_reset_state(rpc_storage, session, true);
  333. }
  334. if(rpc_storage->state != RpcStorageStateWriting) {
  335. rpc_storage->api = furi_record_open(RECORD_STORAGE);
  336. rpc_storage->file = storage_file_alloc(rpc_storage->api);
  337. rpc_storage->current_command_id = request->command_id;
  338. rpc_storage->state = RpcStorageStateWriting;
  339. const char* path = request->content.storage_write_request.path;
  340. fs_operation_success =
  341. storage_file_open(rpc_storage->file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  342. }
  343. File* file = rpc_storage->file;
  344. bool send_response = false;
  345. if(fs_operation_success) {
  346. if(request->content.storage_write_request.has_file &&
  347. request->content.storage_write_request.file.data &&
  348. request->content.storage_write_request.file.data->size) {
  349. uint8_t* buffer = request->content.storage_write_request.file.data->bytes;
  350. size_t buffer_size = request->content.storage_write_request.file.data->size;
  351. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  352. fs_operation_success = (written_size == buffer_size);
  353. }
  354. send_response = !request->has_next;
  355. }
  356. PB_CommandStatus command_status = PB_CommandStatus_OK;
  357. if(!fs_operation_success) {
  358. send_response = true;
  359. command_status = rpc_system_storage_get_file_error(file);
  360. if(command_status == PB_CommandStatus_OK) {
  361. // Report errors not handled by underlying APIs
  362. command_status = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
  363. }
  364. }
  365. if(send_response) {
  366. rpc_send_and_release_empty(session, rpc_storage->current_command_id, command_status);
  367. rpc_system_storage_reset_state(rpc_storage, session, false);
  368. }
  369. }
  370. static bool rpc_system_storage_is_dir_is_empty(Storage* fs_api, const char* path) {
  371. furi_assert(fs_api);
  372. furi_assert(path);
  373. FileInfo fileinfo;
  374. bool is_dir_is_empty = true;
  375. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  376. if((error == FSE_OK) && (fileinfo.flags & FSF_DIRECTORY)) {
  377. File* dir = storage_file_alloc(fs_api);
  378. if(storage_dir_open(dir, path)) {
  379. char* name = malloc(MAX_NAME_LENGTH);
  380. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  381. if(path_contains_only_ascii(name)) {
  382. is_dir_is_empty = false;
  383. break;
  384. }
  385. }
  386. free(name);
  387. }
  388. storage_dir_close(dir);
  389. storage_file_free(dir);
  390. }
  391. return is_dir_is_empty;
  392. }
  393. static void rpc_system_storage_delete_process(const PB_Main* request, void* context) {
  394. furi_assert(request);
  395. furi_assert(request->which_content == PB_Main_storage_delete_request_tag);
  396. furi_assert(context);
  397. FURI_LOG_D(TAG, "Delete");
  398. RpcStorageSystem* rpc_storage = context;
  399. RpcSession* session = rpc_storage->session;
  400. furi_assert(session);
  401. PB_CommandStatus status = PB_CommandStatus_ERROR;
  402. rpc_system_storage_reset_state(rpc_storage, session, true);
  403. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  404. char* path = request->content.storage_delete_request.path;
  405. if(!path) {
  406. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  407. } else {
  408. FS_Error error_remove = storage_common_remove(fs_api, path);
  409. // FSE_DENIED is for empty directory, but not only for this
  410. // that's why we have to check it
  411. if((error_remove == FSE_DENIED) && !rpc_system_storage_is_dir_is_empty(fs_api, path)) {
  412. if(request->content.storage_delete_request.recursive) {
  413. bool deleted = storage_simply_remove_recursive(fs_api, path);
  414. status = deleted ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
  415. } else {
  416. status = PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY;
  417. }
  418. } else if(error_remove == FSE_NOT_EXIST) {
  419. status = PB_CommandStatus_OK;
  420. } else {
  421. status = rpc_system_storage_get_error(error_remove);
  422. }
  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_mkdir_process(const PB_Main* request, void* context) {
  428. furi_assert(request);
  429. furi_assert(request->which_content == PB_Main_storage_mkdir_request_tag);
  430. furi_assert(context);
  431. FURI_LOG_D(TAG, "Mkdir");
  432. RpcStorageSystem* rpc_storage = context;
  433. RpcSession* session = rpc_storage->session;
  434. furi_assert(session);
  435. PB_CommandStatus status;
  436. rpc_system_storage_reset_state(rpc_storage, session, true);
  437. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  438. char* path = request->content.storage_mkdir_request.path;
  439. if(path) {
  440. if(path_contains_only_ascii(path)) {
  441. FS_Error error = storage_common_mkdir(fs_api, path);
  442. status = rpc_system_storage_get_error(error);
  443. } else {
  444. status = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  445. }
  446. } else {
  447. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  448. }
  449. furi_record_close(RECORD_STORAGE);
  450. rpc_send_and_release_empty(session, request->command_id, status);
  451. }
  452. static void rpc_system_storage_md5sum_process(const PB_Main* request, void* context) {
  453. furi_assert(request);
  454. furi_assert(request->which_content == PB_Main_storage_md5sum_request_tag);
  455. furi_assert(context);
  456. FURI_LOG_D(TAG, "Md5sum");
  457. RpcStorageSystem* rpc_storage = context;
  458. RpcSession* session = rpc_storage->session;
  459. furi_assert(session);
  460. rpc_system_storage_reset_state(rpc_storage, session, true);
  461. const char* filename = request->content.storage_md5sum_request.path;
  462. if(!filename) {
  463. rpc_send_and_release_empty(
  464. session, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  465. return;
  466. }
  467. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  468. File* file = storage_file_alloc(fs_api);
  469. if(storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
  470. const uint16_t size_to_read = 512;
  471. const uint8_t hash_size = 16;
  472. uint8_t* data = malloc(size_to_read);
  473. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  474. md5_context* md5_ctx = malloc(sizeof(md5_context));
  475. md5_starts(md5_ctx);
  476. while(true) {
  477. uint16_t read_size = storage_file_read(file, data, size_to_read);
  478. if(read_size == 0) break;
  479. md5_update(md5_ctx, data, read_size);
  480. }
  481. md5_finish(md5_ctx, hash);
  482. free(md5_ctx);
  483. PB_Main response = {
  484. .command_id = request->command_id,
  485. .command_status = PB_CommandStatus_OK,
  486. .which_content = PB_Main_storage_md5sum_response_tag,
  487. .has_next = false,
  488. };
  489. char* md5sum = response.content.storage_md5sum_response.md5sum;
  490. size_t md5sum_size = sizeof(response.content.storage_md5sum_response.md5sum);
  491. (void)md5sum_size;
  492. furi_assert(hash_size <= ((md5sum_size - 1) / 2));
  493. for(uint8_t i = 0; i < hash_size; i++) {
  494. md5sum += snprintf(md5sum, md5sum_size, "%02x", hash[i]);
  495. }
  496. free(hash);
  497. free(data);
  498. storage_file_close(file);
  499. rpc_send_and_release(session, &response);
  500. } else {
  501. rpc_send_and_release_empty(
  502. session, request->command_id, rpc_system_storage_get_file_error(file));
  503. }
  504. storage_file_free(file);
  505. furi_record_close(RECORD_STORAGE);
  506. }
  507. static void rpc_system_storage_rename_process(const PB_Main* request, void* context) {
  508. furi_assert(request);
  509. furi_assert(request->which_content == PB_Main_storage_rename_request_tag);
  510. furi_assert(context);
  511. FURI_LOG_D(TAG, "Rename");
  512. RpcStorageSystem* rpc_storage = context;
  513. RpcSession* session = rpc_storage->session;
  514. furi_assert(session);
  515. PB_CommandStatus status;
  516. rpc_system_storage_reset_state(rpc_storage, session, true);
  517. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  518. if(path_contains_only_ascii(request->content.storage_rename_request.new_path)) {
  519. FS_Error error = storage_common_rename(
  520. fs_api,
  521. request->content.storage_rename_request.old_path,
  522. request->content.storage_rename_request.new_path);
  523. status = rpc_system_storage_get_error(error);
  524. } else {
  525. status = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  526. }
  527. furi_record_close(RECORD_STORAGE);
  528. rpc_send_and_release_empty(session, request->command_id, status);
  529. }
  530. static void rpc_system_storage_backup_create_process(const PB_Main* request, void* context) {
  531. furi_assert(request);
  532. furi_assert(request->which_content == PB_Main_storage_backup_create_request_tag);
  533. furi_assert(context);
  534. FURI_LOG_D(TAG, "BackupCreate");
  535. RpcStorageSystem* rpc_storage = context;
  536. RpcSession* session = rpc_storage->session;
  537. furi_assert(session);
  538. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  539. bool backup_ok =
  540. lfs_backup_create(fs_api, request->content.storage_backup_create_request.archive_path);
  541. furi_record_close(RECORD_STORAGE);
  542. rpc_send_and_release_empty(
  543. session, request->command_id, backup_ok ? PB_CommandStatus_OK : PB_CommandStatus_ERROR);
  544. }
  545. static void rpc_system_storage_backup_restore_process(const PB_Main* request, void* context) {
  546. furi_assert(request);
  547. furi_assert(request->which_content == PB_Main_storage_backup_restore_request_tag);
  548. furi_assert(context);
  549. FURI_LOG_D(TAG, "BackupRestore");
  550. RpcStorageSystem* rpc_storage = context;
  551. RpcSession* session = rpc_storage->session;
  552. furi_assert(session);
  553. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  554. bool backup_ok =
  555. lfs_backup_unpack(fs_api, request->content.storage_backup_restore_request.archive_path);
  556. furi_record_close(RECORD_STORAGE);
  557. rpc_send_and_release_empty(
  558. session, request->command_id, backup_ok ? PB_CommandStatus_OK : PB_CommandStatus_ERROR);
  559. }
  560. void* rpc_system_storage_alloc(RpcSession* session) {
  561. furi_assert(session);
  562. RpcStorageSystem* rpc_storage = malloc(sizeof(RpcStorageSystem));
  563. rpc_storage->api = furi_record_open(RECORD_STORAGE);
  564. rpc_storage->session = session;
  565. rpc_storage->state = RpcStorageStateIdle;
  566. RpcHandler rpc_handler = {
  567. .message_handler = NULL,
  568. .decode_submessage = NULL,
  569. .context = rpc_storage,
  570. };
  571. rpc_handler.message_handler = rpc_system_storage_info_process;
  572. rpc_add_handler(session, PB_Main_storage_info_request_tag, &rpc_handler);
  573. rpc_handler.message_handler = rpc_system_storage_timestamp_process;
  574. rpc_add_handler(session, PB_Main_storage_timestamp_request_tag, &rpc_handler);
  575. rpc_handler.message_handler = rpc_system_storage_stat_process;
  576. rpc_add_handler(session, PB_Main_storage_stat_request_tag, &rpc_handler);
  577. rpc_handler.message_handler = rpc_system_storage_list_process;
  578. rpc_add_handler(session, PB_Main_storage_list_request_tag, &rpc_handler);
  579. rpc_handler.message_handler = rpc_system_storage_read_process;
  580. rpc_add_handler(session, PB_Main_storage_read_request_tag, &rpc_handler);
  581. rpc_handler.message_handler = rpc_system_storage_write_process;
  582. rpc_add_handler(session, PB_Main_storage_write_request_tag, &rpc_handler);
  583. rpc_handler.message_handler = rpc_system_storage_delete_process;
  584. rpc_add_handler(session, PB_Main_storage_delete_request_tag, &rpc_handler);
  585. rpc_handler.message_handler = rpc_system_storage_mkdir_process;
  586. rpc_add_handler(session, PB_Main_storage_mkdir_request_tag, &rpc_handler);
  587. rpc_handler.message_handler = rpc_system_storage_md5sum_process;
  588. rpc_add_handler(session, PB_Main_storage_md5sum_request_tag, &rpc_handler);
  589. rpc_handler.message_handler = rpc_system_storage_rename_process;
  590. rpc_add_handler(session, PB_Main_storage_rename_request_tag, &rpc_handler);
  591. rpc_handler.message_handler = rpc_system_storage_backup_create_process;
  592. rpc_add_handler(session, PB_Main_storage_backup_create_request_tag, &rpc_handler);
  593. rpc_handler.message_handler = rpc_system_storage_backup_restore_process;
  594. rpc_add_handler(session, PB_Main_storage_backup_restore_request_tag, &rpc_handler);
  595. return rpc_storage;
  596. }
  597. void rpc_system_storage_free(void* context) {
  598. furi_assert(context);
  599. RpcStorageSystem* rpc_storage = context;
  600. RpcSession* session = rpc_storage->session;
  601. furi_assert(session);
  602. rpc_system_storage_reset_state(rpc_storage, session, false);
  603. free(rpc_storage);
  604. }