Refactor read_file

This commit is contained in:
twinaphex 2015-02-16 03:31:37 +01:00
parent 63cf5a07c3
commit 935b112a11
7 changed files with 108 additions and 58 deletions

View File

@ -42,7 +42,7 @@
#endif
static bool apply_patch_content(uint8_t **buf,
ssize_t *size, const char *patch_desc, const char *patch_path,
size_t *size, const char *patch_desc, const char *patch_path,
patch_func_t func)
{
void *patch_data = NULL;
@ -52,7 +52,10 @@ static bool apply_patch_content(uint8_t **buf,
uint8_t *patched_content = NULL;
ssize_t ret_size = *size;
uint8_t *ret_buf = *buf;
ssize_t patch_size = read_file(patch_desc, &patch_data);
size_t patch_size;
if (!read_file(patch_desc, &patch_data, &patch_size))
return false;
if (patch_size < 0)
return false;
@ -102,7 +105,7 @@ error:
return false;
}
static bool try_bps_patch(uint8_t **buf, ssize_t *size)
static bool try_bps_patch(uint8_t **buf, size_t *size)
{
bool allow_bps = !g_extern.ups_pref && !g_extern.ips_pref;
@ -115,7 +118,7 @@ static bool try_bps_patch(uint8_t **buf, ssize_t *size)
bps_apply_patch);
}
static bool try_ups_patch(uint8_t **buf, ssize_t *size)
static bool try_ups_patch(uint8_t **buf, size_t *size)
{
bool allow_ups = !g_extern.bps_pref && !g_extern.ips_pref;
@ -128,7 +131,7 @@ static bool try_ups_patch(uint8_t **buf, ssize_t *size)
ups_apply_patch);
}
static bool try_ips_patch(uint8_t **buf, ssize_t *size)
static bool try_ips_patch(uint8_t **buf, size_t *size)
{
bool allow_ips = !g_extern.ups_pref && !g_extern.bps_pref;
@ -149,7 +152,7 @@ static bool try_ips_patch(uint8_t **buf, ssize_t *size)
* Apply patch to the content file in-memory.
*
**/
static void patch_content(uint8_t **buf, ssize_t *size)
static void patch_content(uint8_t **buf, size_t *size)
{
if (g_extern.ups_pref + g_extern.bps_pref + g_extern.ips_pref > 1)
{
@ -167,33 +170,36 @@ static void patch_content(uint8_t **buf, ssize_t *size)
* read_content_file:
* @path : buffer of the content file.
* @buf : size of the content file.
* @length : size of the content file that has been read from.
*
* Read the content file into memory. Also performs soft patching
* (see patch_content function) in case soft patching has not been
* blocked by the enduser.
*
* Returns: size of the content file that has been read from.
* Returns: true if successful, false on error.
**/
static ssize_t read_content_file(const char *path, void **buf)
static bool read_content_file(const char *path, void **buf,
size_t *length)
{
uint8_t *ret_buf = NULL;
ssize_t ret = -1;
RARCH_LOG("Loading content file: %s.\n", path);
ret = read_file(path, (void**) &ret_buf);
if (!read_file(path, (void**) &ret_buf, length))
return false;
if (ret <= 0)
return ret;
if (*length <= 0)
return false;
/* Attempt to apply a patch. */
if (!g_extern.block_patch)
patch_content(&ret_buf, &ret);
patch_content(&ret_buf, length);
g_extern.content_crc = crc32_calculate(ret_buf, ret);
g_extern.content_crc = crc32_calculate(ret_buf, *length);
RARCH_LOG("CRC32: 0x%x .\n", (unsigned)g_extern.content_crc);
*buf = ret_buf;
return ret;
return true;
}
/**
@ -301,12 +307,13 @@ bool load_state(const char *path)
bool ret = true;
void *buf = NULL;
struct sram_block *blocks = NULL;
size_t size;
ssize_t size = read_file(path, &buf);
ret = read_file(path, &buf, &size);
RARCH_LOG("Loading state: \"%s\".\n", path);
if (size < 0)
if (!ret || size < 0)
{
RARCH_ERR("Failed to load state from \"%s\".\n", path);
return false;
@ -376,7 +383,8 @@ bool load_state(const char *path)
*/
void load_ram_file(const char *path, int type)
{
ssize_t rc;
size_t rc;
bool ret = false;
void *buf = NULL;
size_t size = pretro_get_memory_size(type);
void *data = pretro_get_memory_data(type);
@ -384,7 +392,10 @@ void load_ram_file(const char *path, int type)
if (size == 0 || !data)
return;
rc = read_file(path, &buf);
ret = read_file(path, &buf, &rc);
if (!ret)
return;
if (rc > 0)
{
@ -475,22 +486,26 @@ static bool load_content(const struct retro_subsystem_info *special,
if (!need_fullpath && *path)
{
size_t len;
/* Load the content into memory. */
/* First content file is significant, attempt to do patching,
* CRC checking, etc. */
long size = (i == 0) ?
read_content_file(path, (void**)&info[i].data) :
read_file(path, (void**)&info[i].data);
bool ret = false;
if (i == 0)
ret = read_content_file(path, (void**)&info[i].data, &len);
else
ret = read_file(path, (void**)&info[i].data, &len);
if (size < 0)
if (!ret || len < 0)
{
RARCH_ERR("Could not read content file \"%s\".\n", path);
ret = false;
goto end;
}
info[i].size = size;
info[i].size = len;
}
else
{
@ -502,6 +517,8 @@ static bool load_content(const struct retro_subsystem_info *special,
{
if (need_fullpath && path_contains_compressed_file(path))
{
bool ret = false;
size_t len;
char new_path[PATH_MAX_LENGTH], new_basedir[PATH_MAX_LENGTH];
union string_list_elem_attr attributes;
@ -525,7 +542,16 @@ static bool load_content(const struct retro_subsystem_info *special,
attributes.i = 0;
fill_pathname_join(new_path, new_basedir,
path_basename(path), sizeof(new_path));
read_compressed_file(path,NULL,new_path);
ret = read_compressed_file(path,NULL,new_path, &len);
if (!ret || len < 0)
{
RARCH_ERR("Could not read content file \"%s\".\n", path);
ret = false;
goto end;
}
string_list_append(additional_path_allocs,new_path, attributes);
info[i].path =
additional_path_allocs->elems

View File

@ -92,16 +92,16 @@ int database_info_write_rdl(const char *dir)
else
#endif
{
ssize_t ret;
size_t ret;
uint32_t crc, target_crc = 0;
uint8_t *ret_buf = NULL;
bool read_from = false;
(void)target_crc;
RARCH_LOG("name: %s\n", name);
ret = read_file(name, (void**)&ret_buf);
read_from = read_file(name, (void**)&ret_buf, &ret);
if (ret <= 0)
if (!read_from || ret <= 0)
continue;
crc = crc32_calculate(ret_buf, ret);

View File

@ -150,19 +150,26 @@ error:
* Extracts to buf, unless optional_filename != 0
* Then extracts to optional_filename and leaves buf alone.
*/
long read_compressed_file(const char * path, void **buf,
const char* optional_filename)
bool read_compressed_file(const char * path, void **buf,
const char* optional_filename, size_t *length)
{
const char* file_ext;
char archive_path[PATH_MAX_LENGTH], *archive_found = NULL;
/* Safety check.
* If optional_filename and optional_filename exists, we simply return 0,
* hoping that optional_filename is the same as requested.
*/
if (optional_filename)
{
/* Safety check.
* If optional_filename and optional_filename
* exists, we simply return 0,
* hoping that optional_filename is the
* same as requested.
*/
if(path_file_exists(optional_filename))
return 0;
{
*length = 0;
return true;
}
}
//We split carchive path and relative path:
strlcpy(archive_path,path,sizeof(archive_path));
@ -179,23 +186,32 @@ long read_compressed_file(const char * path, void **buf,
*/
RARCH_ERR("Could not extract image path and carchive path from "
"path: %s.\n", path);
return -1;
*length = 0;
return false;
}
/* We split the string in two, by putting a \0, where the hash was: */
*archive_found = '\0';
archive_found+=1;
*archive_found = '\0';
archive_found += 1;
file_ext = path_get_extension(archive_path);
file_ext = path_get_extension(archive_path);
#ifdef HAVE_7ZIP
if (strcasecmp(file_ext,"7z") == 0)
return read_7zip_file(archive_path,archive_found,buf,optional_filename);
{
*length = read_7zip_file(archive_path,archive_found,buf,optional_filename);
if (*length != -1)
return true;
}
#endif
#ifdef HAVE_ZLIB
if (strcasecmp(file_ext,"zip") == 0)
return read_zip_file(archive_path,archive_found,buf,optional_filename);
{
*length = read_zip_file(archive_path,archive_found,buf,optional_filename);
if (*length != -1)
return true;
}
#endif
return -1;
return false;
}
#endif
@ -204,13 +220,14 @@ long read_compressed_file(const char * path, void **buf,
* @path : path to file.
* @buf : buffer to allocate and read the contents of the
* file into. Needs to be freed manually.
* @length : Number of items read, -1 on error.
*
* Read the contents of a file into @buf. Will call read_compressed_file
* if path contains a compressed file, otherwise will call read_generic_file.
*
* Returns: number of items read, -1 on error.
* Returns: true if file read, false on error.
*/
long read_file(const char *path, void **buf)
bool read_file(const char *path, void **buf, size_t *length)
{
#ifdef HAVE_COMPRESSION
/* Here we check, whether the file, we are about to read is
@ -223,10 +240,11 @@ long read_file(const char *path, void **buf)
* carchive_path: /home/user/game.7z
* */
if (path_contains_compressed_file(path))
return read_compressed_file(path,buf,0);
if (read_compressed_file(path, buf, NULL, length))
return true;
#endif
size_t length = 0;
if (!read_generic_file(path, buf, &length))
return 0;
return length;
if (read_generic_file(path, buf, length))
return true;
*length = -1;
return false;
}

View File

@ -33,8 +33,8 @@ extern "C" {
* Extracts to buf, unless optional_filename != 0
* Then extracts to optional_filename and leaves buf alone.
*/
long read_compressed_file(const char * path, void **buf,
const char* optional_filename);
bool read_compressed_file(const char * path, void **buf,
const char* optional_filename, size_t *length);
#endif
/**
@ -42,13 +42,14 @@ long read_compressed_file(const char * path, void **buf,
* @path : path to file.
* @buf : buffer to allocate and read the contents of the
* file into. Needs to be freed manually.
* @length : Number of items read, -1 on error.
*
* Read the contents of a file into @buf. Will call read_compressed_file
* if path contains a compressed file, otherwise will call read_generic_file.
*
* Returns: number of items read, -1 on error.
* Returns: true if file read, false on error.
*/
long read_file(const char *path, void **buf);
bool read_file(const char *path, void **buf, size_t *length);
/**
* write_file:

View File

@ -433,7 +433,9 @@ static GLuint compile_program(glsl_shader_data_t *glsl,
static bool load_source_path(struct video_shader_pass *pass,
const char *path)
{
if (read_file(path, (void**)&pass->source.string.vertex) <= 0)
size_t len;
bool ret = read_file(path, (void**)&pass->source.string.vertex, &len);
if (!ret || len <= 0)
return false;
pass->source.string.fragment = strdup(pass->source.string.vertex);

View File

@ -214,9 +214,10 @@ bool texture_image_load(struct texture_image *out_img, const char *path)
{
void *raw_buf = NULL;
uint8_t *buf = NULL;
ssize_t len = read_file(path, &raw_buf);
size_t len;
bool ret = read_file(path, &raw_buf, &len);
if (len < 0)
if (!ret || len < 0)
{
RARCH_ERR("Failed to read image: %s.\n", path);
return false;

View File

@ -297,7 +297,9 @@ py_state_t *py_state_new(const char *script,
* compiled with MSVC. */
char *script_ = NULL;
if (read_file(script, (void**)&script_) < 0)
size_t len;
bool ret = read_file(script, (void**)&script_, &len);
if (!ret || len < 0)
{
RARCH_ERR("Python: Failed to read script\n");
goto error;