Refactor zlib_inflate_data_to_file so that it is nonblocking

This commit is contained in:
twinaphex 2015-03-28 19:18:42 +01:00
parent db2677de99
commit a532742b96
4 changed files with 72 additions and 35 deletions

View File

@ -58,11 +58,19 @@ static int zlib_cb(const char *name, const char *valid_exts,
break; break;
case 8: /* Deflate */ case 8: /* Deflate */
if (!zlib_inflate_data_to_file(path, valid_exts, cdata,
csize, size, crc32))
{ {
RARCH_ERR("Failed to deflate to: %s.\n", path); int ret = 0;
return 0; zlib_file_handle_t handle = {0};
if (!zlib_inflate_data_to_file_init(&handle, cdata, csize, size))
goto error;
do{
ret = zlib_inflate_data_to_file_iterate(handle.stream);
}while(ret == 0);
if (!zlib_inflate_data_to_file(&handle, ret, path, valid_exts,
cdata, csize, size, crc32))
goto error;
} }
break; break;
@ -71,6 +79,10 @@ static int zlib_cb(const char *name, const char *valid_exts,
} }
return 1; return 1;
error:
RARCH_ERR("Failed to deflate to: %s.\n", path);
return 0;
} }
JNIEXPORT jboolean JNICALL Java_com_retroarch_browser_NativeInterface_extractArchiveTo( JNIEXPORT jboolean JNICALL Java_com_retroarch_browser_NativeInterface_extractArchiveTo(

View File

@ -314,40 +314,30 @@ int zlib_inflate_data_to_file_iterate(void *data)
* *
* Returns: true (1) on success, otherwise false (0). * Returns: true (1) on success, otherwise false (0).
**/ **/
int zlib_inflate_data_to_file(const char *path, const char *valid_exts, int zlib_inflate_data_to_file(zlib_file_handle_t *handle,
int ret, const char *path, const char *valid_exts,
const uint8_t *cdata, uint32_t csize, uint32_t size, uint32_t checksum) const uint8_t *cdata, uint32_t csize, uint32_t size, uint32_t checksum)
{ {
int ret = 1; if (handle)
zlib_file_handle_t handle = {0}; z_stream_free(handle->stream);
(void)valid_exts; if (!handle || ret == -1)
{
if (!zlib_inflate_data_to_file_init(&handle, cdata, csize, size))
GOTO_END_ERROR();
do{
ret = zlib_inflate_data_to_file_iterate(handle.stream);
}while(ret == 0);
if (ret == -1)
ret = 0; ret = 0;
z_stream_free(handle.stream);
if (ret == 0)
goto end; goto end;
}
handle.real_checksum = crc32_calculate(handle.data, size); handle->real_checksum = crc32_calculate(handle->data, size);
if (handle.real_checksum != checksum) if (handle->real_checksum != checksum)
RARCH_WARN("File CRC differs from ZIP CRC. File: 0x%x, ZIP: 0x%x.\n", RARCH_WARN("File CRC differs from ZIP CRC. File: 0x%x, ZIP: 0x%x.\n",
(unsigned)handle.real_checksum, (unsigned)checksum); (unsigned)handle->real_checksum, (unsigned)checksum);
if (!write_file(path, handle.data, size)) if (!write_file(path, handle->data, size))
GOTO_END_ERROR(); GOTO_END_ERROR();
end: end:
if (handle.data) if (handle->data)
free(handle.data); free(handle->data);
return ret; return ret;
} }
@ -497,14 +487,25 @@ static int zip_extract_cb(const char *name, const char *valid_exts,
data->found_content = write_file(new_path, cdata, size); data->found_content = write_file(new_path, cdata, size);
return false; return false;
case ZLIB_MODE_DEFLATE: case ZLIB_MODE_DEFLATE:
if (zlib_inflate_data_to_file(new_path, valid_exts,
cdata, csize, size, checksum))
{ {
strlcpy(data->zip_path, new_path, data->zip_path_size); int ret = 0;
data->found_content = true; zlib_file_handle_t handle = {0};
if (!zlib_inflate_data_to_file_init(&handle, cdata, csize, size))
return 0;
do{
ret = zlib_inflate_data_to_file_iterate(handle.stream);
}while(ret == 0);
if (zlib_inflate_data_to_file(&handle, ret, new_path, valid_exts,
cdata, csize, size, checksum))
{
strlcpy(data->zip_path, new_path, data->zip_path_size);
data->found_content = true;
return 0;
}
return 0; return 0;
} }
return 0;
default: default:
return 0; return 0;

View File

@ -74,6 +74,12 @@ bool zlib_extract_first_content_file(char *zip_path, size_t zip_path_size,
**/ **/
struct string_list *zlib_get_file_list(const char *path, const char *valid_exts); struct string_list *zlib_get_file_list(const char *path, const char *valid_exts);
bool zlib_inflate_data_to_file_init(
zlib_file_handle_t *handle,
const uint8_t *cdata, uint32_t csize, uint32_t size);
int zlib_inflate_data_to_file_iterate(void *data);
/** /**
* zlib_inflate_data_to_file: * zlib_inflate_data_to_file:
* @path : filename path of archive. * @path : filename path of archive.
@ -86,8 +92,9 @@ struct string_list *zlib_get_file_list(const char *path, const char *valid_exts)
* *
* Returns: true (1) on success, otherwise false (0). * Returns: true (1) on success, otherwise false (0).
**/ **/
int zlib_inflate_data_to_file(const char *path, const char *valid_exts, int zlib_inflate_data_to_file(zlib_file_handle_t *handle,
const uint8_t *data, uint32_t csize, uint32_t size, uint32_t crc32); int ret, const char *path, const char *valid_exts,
const uint8_t *cdata, uint32_t csize, uint32_t size, uint32_t checksum);
struct string_list *compressed_file_list_new(const char *filename, struct string_list *compressed_file_list_new(const char *filename,
const char* ext); const char* ext);

View File

@ -81,11 +81,28 @@ static int zlib_extract_core_callback(const char *name, const char *valid_exts,
write_file(path, cdata, size); write_file(path, cdata, size);
break; break;
case 8: /* Deflate */ case 8: /* Deflate */
zlib_inflate_data_to_file(path, valid_exts, cdata, csize, size, crc32); {
int ret = 0;
zlib_file_handle_t handle = {0};
if (!zlib_inflate_data_to_file_init(&handle, cdata, csize, size))
goto error;
do{
ret = zlib_inflate_data_to_file_iterate(handle.stream);
}while(ret == 0);
if (!zlib_inflate_data_to_file(&handle, ret, path, valid_exts,
cdata, csize, size, crc32))
goto error;
}
break; break;
} }
return 1; return 1;
error:
RARCH_ERR("Failed to deflate to: %s.\n", path);
return 0;
} }
int cb_core_updater_download(void *data_, size_t len) int cb_core_updater_download(void *data_, size_t len)