|
@@ -168,7 +168,44 @@ typedef struct {
|
|
|
Storage_name_converter converter;
|
|
Storage_name_converter converter;
|
|
|
} TarArchiveDirectoryOpParams;
|
|
} TarArchiveDirectoryOpParams;
|
|
|
|
|
|
|
|
|
|
+static bool archive_extract_current_file(TarArchive* archive, const char* dst_path) {
|
|
|
|
|
+ mtar_t* tar = &archive->tar;
|
|
|
|
|
+ File* out_file = storage_file_alloc(archive->storage);
|
|
|
|
|
+ uint8_t* readbuf = malloc(FILE_BLOCK_SIZE);
|
|
|
|
|
+
|
|
|
|
|
+ bool success = true;
|
|
|
|
|
+ uint8_t n_tries = FILE_OPEN_NTRIES;
|
|
|
|
|
+ do {
|
|
|
|
|
+ while(n_tries-- > 0) {
|
|
|
|
|
+ if(storage_file_open(out_file, dst_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ FURI_LOG_W(TAG, "Failed to open '%s', reties: %d", dst_path, n_tries);
|
|
|
|
|
+ storage_file_close(out_file);
|
|
|
|
|
+ furi_delay_ms(FILE_OPEN_RETRY_DELAY);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(!storage_file_is_open(out_file)) {
|
|
|
|
|
+ success = false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ while(!mtar_eof_data(tar)) {
|
|
|
|
|
+ int32_t readcnt = mtar_read_data(tar, readbuf, FILE_BLOCK_SIZE);
|
|
|
|
|
+ if(!readcnt || !storage_file_write(out_file, readbuf, readcnt)) {
|
|
|
|
|
+ success = false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } while(false);
|
|
|
|
|
+ storage_file_free(out_file);
|
|
|
|
|
+ free(readbuf);
|
|
|
|
|
+
|
|
|
|
|
+ return success;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header, void* param) {
|
|
static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header, void* param) {
|
|
|
|
|
+ UNUSED(tar);
|
|
|
TarArchiveDirectoryOpParams* op_params = param;
|
|
TarArchiveDirectoryOpParams* op_params = param;
|
|
|
TarArchive* archive = op_params->archive;
|
|
TarArchive* archive = op_params->archive;
|
|
|
|
|
|
|
@@ -199,58 +236,22 @@ static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header,
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- string_init(full_extracted_fname);
|
|
|
|
|
|
|
+ FURI_LOG_D(TAG, "Extracting %d bytes to '%s'", header->size, header->name);
|
|
|
|
|
|
|
|
string_t converted_fname;
|
|
string_t converted_fname;
|
|
|
string_init_set(converted_fname, header->name);
|
|
string_init_set(converted_fname, header->name);
|
|
|
if(op_params->converter) {
|
|
if(op_params->converter) {
|
|
|
op_params->converter(converted_fname);
|
|
op_params->converter(converted_fname);
|
|
|
}
|
|
}
|
|
|
- path_concat(op_params->work_dir, string_get_cstr(converted_fname), full_extracted_fname);
|
|
|
|
|
- string_clear(converted_fname);
|
|
|
|
|
|
|
|
|
|
- FURI_LOG_D(TAG, "Extracting %d bytes to '%s'", header->size, header->name);
|
|
|
|
|
- File* out_file = storage_file_alloc(archive->storage);
|
|
|
|
|
- uint8_t* readbuf = malloc(FILE_BLOCK_SIZE);
|
|
|
|
|
-
|
|
|
|
|
- bool failed = false;
|
|
|
|
|
- uint8_t n_tries = FILE_OPEN_NTRIES;
|
|
|
|
|
- do {
|
|
|
|
|
- while(n_tries-- > 0) {
|
|
|
|
|
- if(storage_file_open(
|
|
|
|
|
- out_file,
|
|
|
|
|
- string_get_cstr(full_extracted_fname),
|
|
|
|
|
- FSAM_WRITE,
|
|
|
|
|
- FSOM_CREATE_ALWAYS)) {
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- FURI_LOG_W(
|
|
|
|
|
- TAG,
|
|
|
|
|
- "Failed to open '%s', reties: %d",
|
|
|
|
|
- string_get_cstr(full_extracted_fname),
|
|
|
|
|
- n_tries);
|
|
|
|
|
- storage_file_close(out_file);
|
|
|
|
|
- furi_delay_ms(FILE_OPEN_RETRY_DELAY);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(!storage_file_is_open(out_file)) {
|
|
|
|
|
- failed = true;
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ string_init(full_extracted_fname);
|
|
|
|
|
+ path_concat(op_params->work_dir, string_get_cstr(converted_fname), full_extracted_fname);
|
|
|
|
|
|
|
|
- while(!mtar_eof_data(tar)) {
|
|
|
|
|
- int32_t readcnt = mtar_read_data(tar, readbuf, FILE_BLOCK_SIZE);
|
|
|
|
|
- if(!readcnt || !storage_file_write(out_file, readbuf, readcnt)) {
|
|
|
|
|
- failed = true;
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } while(false);
|
|
|
|
|
|
|
+ bool success = archive_extract_current_file(archive, string_get_cstr(full_extracted_fname));
|
|
|
|
|
|
|
|
- storage_file_free(out_file);
|
|
|
|
|
- free(readbuf);
|
|
|
|
|
|
|
+ string_clear(converted_fname);
|
|
|
string_clear(full_extracted_fname);
|
|
string_clear(full_extracted_fname);
|
|
|
- return failed ? -1 : 0;
|
|
|
|
|
|
|
+ return success ? 0 : -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool tar_archive_unpack_to(
|
|
bool tar_archive_unpack_to(
|
|
@@ -369,3 +370,16 @@ bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const ch
|
|
|
storage_file_free(directory);
|
|
storage_file_free(directory);
|
|
|
return success;
|
|
return success;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+bool tar_archive_unpack_file(
|
|
|
|
|
+ TarArchive* archive,
|
|
|
|
|
+ const char* archive_fname,
|
|
|
|
|
+ const char* destination) {
|
|
|
|
|
+ furi_assert(archive);
|
|
|
|
|
+ furi_assert(archive_fname);
|
|
|
|
|
+ furi_assert(destination);
|
|
|
|
|
+ if(mtar_find(&archive->tar, archive_fname) != MTAR_ESUCCESS) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return archive_extract_current_file(archive, destination);
|
|
|
|
|
+}
|