rpc_storage.c 26 KB

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