Cleanups in zip_support.c

This commit is contained in:
twinaphex 2015-01-11 19:21:03 +01:00
parent 1980fa065f
commit 59e6cce356

View File

@ -44,9 +44,12 @@
int read_zip_file(const char * archive_path,
const char *relative_path, void **buf, const char* optional_outfile)
{
uLong i;
unz_global_info global_info;
ssize_t bytes_read = -1;
bool finished_reading = false;
unzFile *zipfile = (unzFile*)unzOpen( archive_path );
if (!zipfile)
{
RARCH_ERR("Could not open ZIP file %s.\n",archive_path);
@ -54,24 +57,23 @@ int read_zip_file(const char * archive_path,
}
/* Get info about the zip file */
unz_global_info global_info;
if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK )
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
{
RARCH_ERR("Could not get global ZIP file info of %s."
"Could be only a GZIP file without the ZIP part.\n",
archive_path);
unzClose( zipfile );
unzClose(zipfile);
return -1;
}
/* Loop to extract all files */
uLong i;
for ( i = 0; i < global_info.number_entry; ++i )
{
/* Get info about current file. */
unz_file_info file_info;
char filename[PATH_MAX_LENGTH];
if ( unzGetCurrentFileInfo(
char last_char = ' ';
if (unzGetCurrentFileInfo(
zipfile,
&file_info,
filename,
@ -80,12 +82,11 @@ int read_zip_file(const char * archive_path,
{
RARCH_ERR("Could not read file info in ZIP %s.\n",
archive_path);
unzClose( zipfile );
unzClose(zipfile);
return -1;
}
/* Check if this entry is a directory or file. */
char last_char = ' ';
last_char = filename[strlen(filename)-1];
if ( last_char == '/' || last_char == '\\' )
{
@ -94,11 +95,11 @@ int read_zip_file(const char * archive_path,
else if (strcmp(filename,relative_path) == 0)
{
/* We found the correct file in the zip, now extract it to *buf */
if ( unzOpenCurrentFile( zipfile ) != UNZ_OK )
if (unzOpenCurrentFile(zipfile) != UNZ_OK )
{
RARCH_ERR("The file %s in %s could not be read.\n",
relative_path, archive_path);
unzClose( zipfile );
unzClose(zipfile);
return -1;
}
@ -109,26 +110,30 @@ int read_zip_file(const char * archive_path,
if (outsink == NULL)
{
RARCH_ERR("Could not open outfilepath %s.\n", optional_outfile);
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return -1;
}
bytes_read = 0;
do
{
bytes_read = unzReadCurrentFile( zipfile, read_buffer,
ssize_t fwrite_bytes;
bytes_read = unzReadCurrentFile(zipfile, read_buffer,
RARCH_ZIP_SUPPORT_BUFFER_SIZE_MAX );
ssize_t fwrite_bytes = fwrite(read_buffer,1,bytes_read,outsink);
if (fwrite_bytes != bytes_read)
{
/* couldn't write all bytes */
RARCH_ERR("Error writing to %s.\n",optional_outfile);
fclose(outsink);
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -1;
}
} while(bytes_read > 0) ;
fwrite_bytes = fwrite(read_buffer,1,bytes_read,outsink);
if (fwrite_bytes == bytes_read)
continue;
/* couldn't write all bytes */
RARCH_ERR("Error writing to %s.\n",optional_outfile);
fclose(outsink);
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return -1;
}while(bytes_read > 0);
fclose(outsink);
}
else
@ -136,7 +141,8 @@ int read_zip_file(const char * archive_path,
/* Allocate outbuffer */
*buf = malloc(file_info.uncompressed_size + 1 );
bytes_read = unzReadCurrentFile( zipfile, *buf, file_info.uncompressed_size );
bytes_read = unzReadCurrentFile(zipfile, *buf, file_info.uncompressed_size);
if (bytes_read != (ssize_t)file_info.uncompressed_size)
{
RARCH_ERR(
@ -144,42 +150,53 @@ int read_zip_file(const char * archive_path,
(unsigned int) file_info.uncompressed_size, (int)bytes_read,
relative_path, archive_path);
free(*buf);
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return -1;
}
((char*)(*buf))[file_info.uncompressed_size] = '\0';
}
finished_reading = true;
}
unzCloseCurrentFile( zipfile );
unzCloseCurrentFile(zipfile);
if (finished_reading)
break;
if ( ( i+1 ) < global_info.number_entry )
if ((i + 1) < global_info.number_entry)
{
if ( unzGoToNextFile( zipfile ) != UNZ_OK )
{
RARCH_ERR(
"Could not iterate to next file in %s. ZIP file might be corrupt.\n",
archive_path );
unzClose( zipfile );
return -1;
}
if (unzGoToNextFile(zipfile) == UNZ_OK)
continue;
RARCH_ERR(
"Could not iterate to next file in %s. ZIP file might be corrupt.\n",
archive_path );
unzClose(zipfile);
return -1;
}
}
unzClose( zipfile );
unzClose(zipfile);
if(!finished_reading)
{
RARCH_ERR("File %s not found in %s\n", relative_path, archive_path);
return -1;
}
return bytes_read;
}
struct string_list *compressed_zip_file_list_new(const char *path,
const char* ext)
{
uLong i;
unz_global_info global_info;
unzFile *zipfile = NULL;
ssize_t bytes_read = -1;
bool finished_reading = false;
struct string_list *ext_list = NULL;
struct string_list *list = string_list_new();
@ -189,10 +206,7 @@ struct string_list *compressed_zip_file_list_new(const char *path,
if (ext)
ext_list = string_split(ext, "|");
ssize_t bytes_read = -1;
bool finished_reading = false;
unzFile *zipfile = (unzFile*)unzOpen( path );
zipfile = (unzFile*)unzOpen( path );
(void)finished_reading;
(void)bytes_read;
@ -206,42 +220,41 @@ struct string_list *compressed_zip_file_list_new(const char *path,
}
/* Get info about the zip file */
unz_global_info global_info;
if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK )
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
{
RARCH_ERR("Could not get global zipfile info of %s."
"Could be only a GZIP file without the ZIP part.\n",
path);
unzClose( zipfile );
unzClose(zipfile);
string_list_free(list);
string_list_free(ext_list);
return NULL;
}
/* Loop to extract all files */
uLong i;
for ( i = 0; i < global_info.number_entry; ++i )
{
/* Get info about current file. */
unz_file_info file_info;
char filename[PATH_MAX_LENGTH];
if ( unzGetCurrentFileInfo(
char last_char = ' ';
if (unzGetCurrentFileInfo(
zipfile,
&file_info,
filename,
PATH_MAX_LENGTH,
NULL, 0, NULL, 0 ) != UNZ_OK )
NULL, 0, NULL, 0 ) != UNZ_OK)
{
RARCH_ERR("Could not read file info in ZIP %s.\n", path);
unzClose( zipfile );
unzClose(zipfile);
string_list_free(list);
string_list_free(ext_list);
return NULL;
}
/* Check if this entry is a directory or file. */
char last_char = ' ';
last_char = filename[strlen(filename)-1];
if ( last_char == '/' || last_char == '\\' )
{
/* We skip directories */
@ -251,6 +264,7 @@ struct string_list *compressed_zip_file_list_new(const char *path,
const char *file_ext = path_get_extension(filename);
bool supported_by_core = false;
union string_list_elem_attr attr;
if (string_list_find_elem_prefix(ext_list, ".", file_ext))
supported_by_core = true;
@ -260,26 +274,30 @@ struct string_list *compressed_zip_file_list_new(const char *path,
if (!string_list_append(list, filename, attr))
{
RARCH_ERR("Could not append item to string list.\n");
unzCloseCurrentFile( zipfile );
unzCloseCurrentFile(zipfile);
break;
}
}
}
unzCloseCurrentFile( zipfile );
if ( ( i+1 ) < global_info.number_entry )
unzCloseCurrentFile(zipfile);
if ((i + 1) < global_info.number_entry)
{
if ( unzGoToNextFile( zipfile ) != UNZ_OK )
if (unzGoToNextFile(zipfile) != UNZ_OK )
{
RARCH_ERR( "Could not iterate to next file in %s. ZIP file might be corrupt.\n",path );
unzClose( zipfile );
unzClose(zipfile);
string_list_free(list);
string_list_free(ext_list);
return NULL;
}
}
}
unzClose( zipfile );
unzClose(zipfile);
return list;
}