Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC: Add TarExtract command, some small fixes #3685

Merged
merged 9 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions applications/services/rpc/rpc_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <lib/toolbox/md5_calc.h>
#include <lib/toolbox/path.h>
#include <update_util/lfs_backup.h>
#include <toolbox/tar/tar_archive.h>

#include <pb_decode.h>
#include <storage.pb.h>
Expand Down Expand Up @@ -678,6 +679,8 @@ static void rpc_system_storage_backup_create_process(const PB_Main* request, voi
RpcSession* session = rpc_storage->session;
furi_assert(session);

rpc_system_storage_reset_state(rpc_storage, session, true);

Storage* fs_api = furi_record_open(RECORD_STORAGE);

bool backup_ok =
Expand All @@ -700,6 +703,8 @@ static void rpc_system_storage_backup_restore_process(const PB_Main* request, vo
RpcSession* session = rpc_storage->session;
furi_assert(session);

rpc_system_storage_reset_state(rpc_storage, session, true);

Storage* fs_api = furi_record_open(RECORD_STORAGE);

bool backup_ok =
Expand All @@ -711,6 +716,51 @@ static void rpc_system_storage_backup_restore_process(const PB_Main* request, vo
session, request->command_id, backup_ok ? PB_CommandStatus_OK : PB_CommandStatus_ERROR);
}

static void rpc_system_storage_tar_extract_process(const PB_Main* request, void* context) {
furi_assert(request);
furi_assert(request->which_content == PB_Main_storage_tar_extract_request_tag);
furi_assert(context);

FURI_LOG_D(TAG, "TarExtract");

RpcStorageSystem* rpc_storage = context;
RpcSession* session = rpc_storage->session;
furi_assert(session);

PB_CommandStatus status;
rpc_system_storage_reset_state(rpc_storage, session, true);

Storage* fs_api = furi_record_open(RECORD_STORAGE);
TarArchive* archive = tar_archive_alloc(fs_api);

do {
if(!path_contains_only_ascii(request->content.storage_tar_extract_request.out_path)) {
status = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
break;
}

if(!tar_archive_open(
archive,
request->content.storage_tar_extract_request.tar_path,
TAR_OPEN_MODE_READ)) {
status = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
break;
}

if(!tar_archive_unpack_to(
archive, request->content.storage_tar_extract_request.out_path, NULL)) {
status = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
break;
}

status = PB_CommandStatus_OK;
} while(0);

tar_archive_free(archive);
furi_record_close(RECORD_STORAGE);
rpc_send_and_release_empty(session, request->command_id, status);
}

void* rpc_system_storage_alloc(RpcSession* session) {
furi_assert(session);

Expand Down Expand Up @@ -761,6 +811,9 @@ void* rpc_system_storage_alloc(RpcSession* session) {
rpc_handler.message_handler = rpc_system_storage_backup_restore_process;
rpc_add_handler(session, PB_Main_storage_backup_restore_request_tag, &rpc_handler);

rpc_handler.message_handler = rpc_system_storage_tar_extract_process;
rpc_add_handler(session, PB_Main_storage_tar_extract_request_tag, &rpc_handler);

return rpc_storage;
}

Expand Down
2 changes: 1 addition & 1 deletion assets/protobuf
5 changes: 5 additions & 0 deletions lib/toolbox/tar/tar_archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header,

FuriString* full_extracted_fname;
if(header->type == MTAR_TDIR) {
// Skip "/" entry since concat would leave it dangling, also want caller to mkdir destination
if(strcmp(header->name, "/") == 0) {
return 0;
}

full_extracted_fname = furi_string_alloc();
path_concat(op_params->work_dir, header->name, full_extracted_fname);

Expand Down
Loading