[FL-3461] RPC: md5 in storage list (#2929)
* Protobuf: update * Toolbox: md5 for file. Unit-Tests: test md5_calc. * Storage RPC, CLI, unit tests: use new md5_calc * Protobuf: update * RPC, StorageList: append md5 info to file * fbt: attempt to fix shallow submodule checkouts * pvs: make happy * Protobuf: update to latest release Co-authored-by: hedger <hedger@nanode.su> Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
co-authored by
hedger
hedger
Aleksandr Kutuzov
parent
e9f1af44f2
commit
00cdc3d1cb
@@ -9,7 +9,7 @@
|
||||
#include "storage/filesystem_api_defines.h"
|
||||
#include "storage/storage.h"
|
||||
#include <stdint.h>
|
||||
#include <lib/toolbox/md5.h>
|
||||
#include <lib/toolbox/md5_calc.h>
|
||||
#include <lib/toolbox/path.h>
|
||||
#include <update_util/lfs_backup.h>
|
||||
|
||||
@@ -271,6 +271,11 @@ static void rpc_system_storage_list_process(const PB_Main* request, void* contex
|
||||
};
|
||||
PB_Storage_ListResponse* list = &response.content.storage_list_response;
|
||||
|
||||
bool include_md5 = request->content.storage_list_request.include_md5;
|
||||
FuriString* md5 = furi_string_alloc();
|
||||
FuriString* md5_path = furi_string_alloc();
|
||||
File* file = storage_file_alloc(fs_api);
|
||||
|
||||
bool finish = false;
|
||||
int i = 0;
|
||||
|
||||
@@ -296,6 +301,21 @@ static void rpc_system_storage_list_process(const PB_Main* request, void* contex
|
||||
list->file[i].size = fileinfo.size;
|
||||
list->file[i].data = NULL;
|
||||
list->file[i].name = name;
|
||||
|
||||
if(include_md5 && !file_info_is_dir(&fileinfo)) {
|
||||
furi_string_printf( //-V576
|
||||
md5_path,
|
||||
"%s/%s",
|
||||
request->content.storage_list_request.path,
|
||||
name);
|
||||
|
||||
if(md5_string_calc_file(file, furi_string_get_cstr(md5_path), md5, NULL)) {
|
||||
char* md5sum = list->file[i].md5sum;
|
||||
size_t md5sum_size = sizeof(list->file[i].md5sum);
|
||||
snprintf(md5sum, md5sum_size, "%s", furi_string_get_cstr(md5));
|
||||
}
|
||||
}
|
||||
|
||||
++i;
|
||||
} else {
|
||||
free(name);
|
||||
@@ -310,8 +330,11 @@ static void rpc_system_storage_list_process(const PB_Main* request, void* contex
|
||||
response.has_next = false;
|
||||
rpc_send_and_release(session, &response);
|
||||
|
||||
furi_string_free(md5);
|
||||
furi_string_free(md5_path);
|
||||
storage_dir_close(dir);
|
||||
storage_file_free(dir);
|
||||
storage_file_free(file);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
@@ -569,23 +592,10 @@ static void rpc_system_storage_md5sum_process(const PB_Main* request, void* cont
|
||||
|
||||
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(fs_api);
|
||||
FuriString* md5 = furi_string_alloc();
|
||||
FS_Error file_error;
|
||||
|
||||
if(storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
const uint16_t size_to_read = 512;
|
||||
const uint8_t hash_size = 16;
|
||||
uint8_t* data = malloc(size_to_read);
|
||||
uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
|
||||
md5_context* md5_ctx = malloc(sizeof(md5_context));
|
||||
|
||||
md5_starts(md5_ctx);
|
||||
while(true) {
|
||||
uint16_t read_size = storage_file_read(file, data, size_to_read);
|
||||
if(read_size == 0) break;
|
||||
md5_update(md5_ctx, data, read_size);
|
||||
}
|
||||
md5_finish(md5_ctx, hash);
|
||||
free(md5_ctx);
|
||||
|
||||
if(md5_string_calc_file(file, filename, md5, &file_error)) {
|
||||
PB_Main response = {
|
||||
.command_id = request->command_id,
|
||||
.command_status = PB_CommandStatus_OK,
|
||||
@@ -595,21 +605,15 @@ static void rpc_system_storage_md5sum_process(const PB_Main* request, void* cont
|
||||
|
||||
char* md5sum = response.content.storage_md5sum_response.md5sum;
|
||||
size_t md5sum_size = sizeof(response.content.storage_md5sum_response.md5sum);
|
||||
(void)md5sum_size;
|
||||
furi_assert(hash_size <= ((md5sum_size - 1) / 2)); //-V547
|
||||
for(uint8_t i = 0; i < hash_size; i++) {
|
||||
md5sum += snprintf(md5sum, md5sum_size, "%02x", hash[i]);
|
||||
}
|
||||
snprintf(md5sum, md5sum_size, "%s", furi_string_get_cstr(md5));
|
||||
|
||||
free(hash);
|
||||
free(data);
|
||||
storage_file_close(file);
|
||||
rpc_send_and_release(session, &response);
|
||||
} else {
|
||||
rpc_send_and_release_empty(
|
||||
session, request->command_id, rpc_system_storage_get_file_error(file));
|
||||
session, request->command_id, rpc_system_storage_get_error(file_error));
|
||||
}
|
||||
|
||||
furi_string_free(md5);
|
||||
storage_file_free(file);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
Reference in New Issue
Block a user