(task_core_updater) small cleanups -

* Get rid of early returns where not necessary
* Reduce return paths to one where possible
This commit is contained in:
libretroadmin 2022-07-09 16:52:43 +02:00
parent 177691e6fa
commit f78f64f6be

View file

@ -205,31 +205,24 @@ typedef struct play_feature_delivery_switch_cores_handle
/* Returns CRC32 of specified core file */ /* Returns CRC32 of specified core file */
static uint32_t task_core_updater_get_core_crc(const char *core_path) static uint32_t task_core_updater_get_core_crc(const char *core_path)
{ {
if (string_is_empty(core_path)) /* Open core file */
return 0; intfstream_t *core_file = intfstream_open_file(
core_path, RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (path_is_valid(core_path)) if (core_file)
{ {
/* Open core file */ uint32_t crc = 0;
intfstream_t *core_file = intfstream_open_file( /* Get CRC value */
core_path, RETRO_VFS_FILE_ACCESS_READ, bool success = intfstream_get_crc(core_file, &crc);
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (core_file) /* Close core file */
{ intfstream_close(core_file);
uint32_t crc = 0; free(core_file);
core_file = NULL;
/* Get CRC value */ if (success)
bool success = intfstream_get_crc(core_file, &crc); return crc;
/* Close core file */
intfstream_close(core_file);
free(core_file);
core_file = NULL;
if (success)
return crc;
}
} }
return 0; return 0;
@ -251,9 +244,7 @@ static void cb_http_task_core_updater_get_list(
if (!transf) if (!transf)
goto finish; goto finish;
list_handle = (core_updater_list_handle_t*)transf->user_data; if (!(list_handle = (core_updater_list_handle_t*)transf->user_data))
if (!list_handle)
goto finish; goto finish;
task_set_data(task, NULL); /* going to pass ownership to list_handle */ task_set_data(task, NULL); /* going to pass ownership to list_handle */
@ -264,7 +255,6 @@ static void cb_http_task_core_updater_get_list(
finish: finish:
/* Log any error messages */ /* Log any error messages */
if (!success) if (!success)
RARCH_ERR("[core updater] Download of core list '%s' failed: %s\n", RARCH_ERR("[core updater] Download of core list '%s' failed: %s\n",
@ -278,9 +268,6 @@ finish:
static void free_core_updater_list_handle( static void free_core_updater_list_handle(
core_updater_list_handle_t *list_handle) core_updater_list_handle_t *list_handle)
{ {
if (!list_handle)
return;
if (list_handle->http_data) if (list_handle->http_data)
{ {
/* since we took onwership, we have to destroy it ourself */ /* since we took onwership, we have to destroy it ourself */
@ -301,9 +288,7 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
if (!task) if (!task)
goto task_finished; goto task_finished;
list_handle = (core_updater_list_handle_t*)task->state; if (!(list_handle = (core_updater_list_handle_t*)task->state))
if (!list_handle)
goto task_finished; goto task_finished;
if (task_get_cancelled(task)) if (task_get_cancelled(task))
@ -313,14 +298,14 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
{ {
case CORE_UPDATER_LIST_BEGIN: case CORE_UPDATER_LIST_BEGIN:
{ {
settings_t *settings = config_get_ptr();
file_transfer_t *transf = NULL;
char *tmp_url = NULL;
char buildbot_url[PATH_MAX_LENGTH]; char buildbot_url[PATH_MAX_LENGTH];
settings_t *settings = config_get_ptr();
file_transfer_t *transf = NULL;
char *tmp_url = NULL;
const char *net_buildbot_url = const char *net_buildbot_url =
settings->paths.network_buildbot_url; settings->paths.network_buildbot_url;
buildbot_url[0] = '\0'; buildbot_url[0] = '\0';
/* Reset core updater list */ /* Reset core updater list */
core_updater_list_reset(list_handle->core_list); core_updater_list_reset(list_handle->core_list);
@ -349,9 +334,8 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
goto task_finished; goto task_finished;
/* Configure file transfer object */ /* Configure file transfer object */
transf = (file_transfer_t*)calloc(1, sizeof(file_transfer_t)); if (!(transf = (file_transfer_t*)calloc(1,
sizeof(file_transfer_t))))
if (!transf)
goto task_finished; goto task_finished;
/* > Seems to be required - not sure why the /* > Seems to be required - not sure why the
@ -361,7 +345,8 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
transf->user_data = (void*)list_handle; transf->user_data = (void*)list_handle;
/* Push HTTP transfer task */ /* Push HTTP transfer task */
list_handle->http_task = (retro_task_t*)task_push_http_transfer_file( list_handle->http_task = (retro_task_t*)
task_push_http_transfer_file(
buildbot_url, true, NULL, buildbot_url, true, NULL,
cb_http_task_core_updater_get_list, transf); cb_http_task_core_updater_get_list, transf);
@ -379,12 +364,10 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
/* Otherwise, check if HTTP task is still running */ /* Otherwise, check if HTTP task is still running */
else if (!list_handle->http_task_finished) else if (!list_handle->http_task_finished)
{ {
list_handle->http_task_finished =
task_get_finished(list_handle->http_task);
/* If HTTP task is running, copy current /* If HTTP task is running, copy current
* progress value to *this* task */ * progress value to *this* task */
if (!list_handle->http_task_finished) if (!(list_handle->http_task_finished =
task_get_finished(list_handle->http_task)))
task_set_progress( task_set_progress(
task, task_get_progress(list_handle->http_task)); task, task_get_progress(list_handle->http_task));
} }
@ -436,11 +419,11 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
return; return;
task_finished: task_finished:
if (task) if (task)
task_set_finished(task, true); task_set_finished(task, true);
free_core_updater_list_handle(list_handle); if (list_handle)
free_core_updater_list_handle(list_handle);
} }
static bool task_core_updater_get_list_finder(retro_task_t *task, void *user_data) static bool task_core_updater_get_list_finder(retro_task_t *task, void *user_data)
@ -453,8 +436,7 @@ static bool task_core_updater_get_list_finder(retro_task_t *task, void *user_dat
if (task->handler != task_core_updater_get_list_handler) if (task->handler != task_core_updater_get_list_handler)
return false; return false;
list_handle = (core_updater_list_handle_t*)task->state; if (!(list_handle = (core_updater_list_handle_t*)task->state))
if (!list_handle)
return false; return false;
return ((uintptr_t)user_data == (uintptr_t)list_handle->core_list); return ((uintptr_t)user_data == (uintptr_t)list_handle->core_list);
@ -499,9 +481,7 @@ void *task_push_get_core_updater_list(
goto error; goto error;
/* Create task */ /* Create task */
task = task_init(); if (!(task = task_init()))
if (!task)
goto error; goto error;
/* Configure task */ /* Configure task */
@ -527,7 +507,8 @@ error:
} }
/* Clean up handle */ /* Clean up handle */
free_core_updater_list_handle(list_handle); if (list_handle)
free_core_updater_list_handle(list_handle);
return NULL; return NULL;
} }
@ -598,9 +579,7 @@ void cb_http_task_core_updater_download(
if (!data->data || string_is_empty(transf->path)) if (!data->data || string_is_empty(transf->path))
goto finish; goto finish;
download_handle = (core_updater_download_handle_t*)transf->user_data; if (!(download_handle = (core_updater_download_handle_t*)transf->user_data))
if (!download_handle)
goto finish; goto finish;
/* Update download_handle task status /* Update download_handle task status
@ -651,14 +630,12 @@ void cb_http_task_core_updater_download(
* in such a way that this cannot happen... */ * in such a way that this cannot happen... */
if (path_is_compressed_file(transf->path)) if (path_is_compressed_file(transf->path))
{ {
download_handle->decompress_task = (retro_task_t*)task_push_decompress( if (!(download_handle->decompress_task = (retro_task_t*)task_push_decompress(
transf->path, output_dir, transf->path, output_dir,
NULL, NULL, NULL, NULL, NULL, NULL,
cb_decompress_task_core_updater_download, cb_decompress_task_core_updater_download,
(void*)download_handle, (void*)download_handle,
NULL, true); NULL, true)))
if (!download_handle->decompress_task)
{ {
err = msg_hash_to_str(MSG_DECOMPRESSION_FAILED); err = msg_hash_to_str(MSG_DECOMPRESSION_FAILED);
goto finish; goto finish;
@ -669,7 +646,6 @@ void cb_http_task_core_updater_download(
#endif #endif
finish: finish:
/* Log any error messages */ /* Log any error messages */
if (!string_is_empty(err)) if (!string_is_empty(err))
RARCH_ERR("[core updater] Download of '%s' failed: %s\n", RARCH_ERR("[core updater] Download of '%s' failed: %s\n",
@ -681,9 +657,6 @@ finish:
static void free_core_updater_download_handle(core_updater_download_handle_t *download_handle) static void free_core_updater_download_handle(core_updater_download_handle_t *download_handle)
{ {
if (!download_handle)
return;
if (download_handle->path_dir_libretro) if (download_handle->path_dir_libretro)
free(download_handle->path_dir_libretro); free(download_handle->path_dir_libretro);
@ -716,9 +689,7 @@ static void task_core_updater_download_handler(retro_task_t *task)
if (!task) if (!task)
goto task_finished; goto task_finished;
download_handle = (core_updater_download_handle_t*)task->state; if (!(download_handle = (core_updater_download_handle_t*)task->state))
if (!download_handle)
goto task_finished; goto task_finished;
if (task_get_cancelled(task)) if (task_get_cancelled(task))
@ -730,8 +701,16 @@ static void task_core_updater_download_handler(retro_task_t *task)
{ {
/* Get CRC of existing core, if required */ /* Get CRC of existing core, if required */
if (download_handle->local_crc == 0) if (download_handle->local_crc == 0)
download_handle->local_crc = task_core_updater_get_core_crc( {
download_handle->local_core_path); const char *local_core_path =
download_handle->local_core_path;
if (
!string_is_empty(local_core_path)
&& path_is_valid (local_core_path)
)
download_handle->local_crc =
task_core_updater_get_core_crc(local_core_path);
}
/* Check whether existing core and remote core /* Check whether existing core and remote core
* have the same CRC */ * have the same CRC */
@ -839,9 +818,8 @@ static void task_core_updater_download_handler(retro_task_t *task)
task_title[0] = '\0'; task_title[0] = '\0';
/* Configure file transfer object */ /* Configure file transfer object */
transf = (file_transfer_t*)calloc(1, sizeof(file_transfer_t)); if (!(transf = (file_transfer_t*)calloc(1,
sizeof(file_transfer_t))))
if (!transf)
goto task_finished; goto task_finished;
strlcpy( strlcpy(
@ -994,24 +972,19 @@ task_finished:
if (task) if (task)
task_set_finished(task, true); task_set_finished(task, true);
free_core_updater_download_handle(download_handle); if (download_handle)
free_core_updater_download_handle(download_handle);
} }
static bool task_core_updater_download_finder(retro_task_t *task, void *user_data) static bool task_core_updater_download_finder(retro_task_t *task, void *user_data)
{ {
core_updater_download_handle_t *download_handle = NULL; if (task && user_data && task->handler == task_core_updater_download_handler)
{
if (!task || !user_data) core_updater_download_handle_t *download_handle = NULL;
return false; if ((download_handle = (core_updater_download_handle_t*)task->state))
return string_is_equal((const char*)user_data, download_handle->remote_filename);
if (task->handler != task_core_updater_download_handler) }
return false; return false;
download_handle = (core_updater_download_handle_t*)task->state;
if (!download_handle)
return false;
return string_is_equal((const char*)user_data, download_handle->remote_filename);
} }
void *task_push_core_updater_download( void *task_push_core_updater_download(
@ -1123,9 +1096,7 @@ void *task_push_core_updater_download(
goto error; goto error;
/* Create task */ /* Create task */
task = task_init(); if (!(task = task_init()))
if (!task)
goto error; goto error;
/* Configure task */ /* Configure task */
@ -1157,7 +1128,8 @@ error:
} }
/* Clean up handle */ /* Clean up handle */
free_core_updater_download_handle(download_handle); if (download_handle)
free_core_updater_download_handle(download_handle);
return NULL; return NULL;
} }
@ -1169,9 +1141,6 @@ error:
static void free_update_installed_cores_handle( static void free_update_installed_cores_handle(
update_installed_cores_handle_t *update_installed_handle) update_installed_cores_handle_t *update_installed_handle)
{ {
if (!update_installed_handle)
return;
if (update_installed_handle->path_dir_libretro) if (update_installed_handle->path_dir_libretro)
free(update_installed_handle->path_dir_libretro); free(update_installed_handle->path_dir_libretro);
@ -1303,7 +1272,7 @@ static void task_update_installed_cores_handler(retro_task_t *task)
case UPDATE_INSTALLED_CORES_UPDATE_CORE: case UPDATE_INSTALLED_CORES_UPDATE_CORE:
{ {
const core_updater_list_entry_t *list_entry = NULL; const core_updater_list_entry_t *list_entry = NULL;
uint32_t local_crc; uint32_t local_crc = 0;
/* Get list entry /* Get list entry
* > In the event of an error, just return * > In the event of an error, just return
@ -1336,8 +1305,15 @@ static void task_update_installed_cores_handler(retro_task_t *task)
} }
/* Get CRC of existing core */ /* Get CRC of existing core */
local_crc = task_core_updater_get_core_crc( {
list_entry->local_core_path); const char *local_core_path = list_entry->local_core_path;
if (
!string_is_empty(local_core_path)
&& path_is_valid (local_core_path)
)
local_crc = task_core_updater_get_core_crc(
local_core_path);
}
/* Check whether existing core and remote core /* Check whether existing core and remote core
* have the same CRC * have the same CRC
@ -1468,11 +1444,11 @@ static void task_update_installed_cores_handler(retro_task_t *task)
return; return;
task_finished: task_finished:
if (task) if (task)
task_set_finished(task, true); task_set_finished(task, true);
free_update_installed_cores_handle(update_installed_handle); if (update_installed_handle)
free_update_installed_cores_handle(update_installed_handle);
} }
static void task_update_single_core_handler(retro_task_t *task) static void task_update_single_core_handler(retro_task_t *task)
@ -1503,7 +1479,7 @@ static void task_update_single_core_handler(retro_task_t *task)
break; break;
case UPDATE_SINGLE_CORE_UPDATE_CORE: case UPDATE_SINGLE_CORE_UPDATE_CORE:
{ {
uint32_t crc; uint32_t crc = 0;
const core_updater_list_entry_t *entry = NULL; const core_updater_list_entry_t *entry = NULL;
if (!core_updater_list_get_core(handle->core_list, if (!core_updater_list_get_core(handle->core_list,
@ -1519,7 +1495,15 @@ static void task_update_single_core_handler(retro_task_t *task)
break; break;
} }
crc = task_core_updater_get_core_crc(entry->local_core_path); {
const char *local_core_path = entry->local_core_path;
if (
!string_is_empty(local_core_path)
&& path_is_valid (local_core_path)
)
crc = task_core_updater_get_core_crc(local_core_path);
}
if (!crc || crc == entry->crc) if (!crc || crc == entry->crc)
{ {
handle->status = UPDATE_SINGLE_CORE_END; handle->status = UPDATE_SINGLE_CORE_END;
@ -1572,13 +1556,10 @@ static void task_update_single_core_cleanup(retro_task_t *task)
static bool task_update_installed_cores_finder(retro_task_t *task, void *user_data) static bool task_update_installed_cores_finder(retro_task_t *task, void *user_data)
{ {
if (!task) if (task)
return false; if ( task->handler == task_update_installed_cores_handler
|| task->handler == task_update_single_core_handler)
if (task->handler == task_update_installed_cores_handler || return true;
task->handler == task_update_single_core_handler)
return true;
return false; return false;
} }
@ -1632,9 +1613,7 @@ void task_push_update_installed_cores(
goto error; goto error;
/* Create task */ /* Create task */
task = task_init(); if (!(task = task_init()))
if (!task)
goto error; goto error;
/* Configure task */ /* Configure task */
@ -1659,7 +1638,8 @@ error:
} }
/* Clean up handle */ /* Clean up handle */
free_update_installed_cores_handle(update_installed_handle); if (update_installed_handle)
free_update_installed_cores_handle(update_installed_handle);
} }
bool task_push_update_single_core( bool task_push_update_single_core(
@ -1731,9 +1711,6 @@ bool task_push_update_single_core(
static void free_play_feature_delivery_install_handle( static void free_play_feature_delivery_install_handle(
play_feature_delivery_install_handle_t *pfd_install_handle) play_feature_delivery_install_handle_t *pfd_install_handle)
{ {
if (!pfd_install_handle)
return;
if (pfd_install_handle->core_filename) if (pfd_install_handle->core_filename)
free(pfd_install_handle->core_filename); free(pfd_install_handle->core_filename);
@ -1750,16 +1727,16 @@ static void free_play_feature_delivery_install_handle(
pfd_install_handle = NULL; pfd_install_handle = NULL;
} }
static void task_play_feature_delivery_core_install_handler(retro_task_t *task) static void task_play_feature_delivery_core_install_handler(
retro_task_t *task)
{ {
play_feature_delivery_install_handle_t *pfd_install_handle = NULL; play_feature_delivery_install_handle_t *pfd_install_handle = NULL;
if (!task) if (!task)
goto task_finished; goto task_finished;
pfd_install_handle = (play_feature_delivery_install_handle_t*)task->state; if (!(pfd_install_handle =
(play_feature_delivery_install_handle_t*)task->state))
if (!pfd_install_handle)
goto task_finished; goto task_finished;
if (task_get_cancelled(task)) if (task_get_cancelled(task))
@ -1953,19 +1930,15 @@ task_finished:
if (task) if (task)
task_set_finished(task, true); task_set_finished(task, true);
free_play_feature_delivery_install_handle(pfd_install_handle); if (pfd_install_handle)
free_play_feature_delivery_install_handle(pfd_install_handle);
} }
static bool task_play_feature_delivery_core_install_finder( static bool task_play_feature_delivery_core_install_finder(
retro_task_t *task, void *user_data) retro_task_t *task, void *user_data)
{ {
if (!task) return (task && task->handler ==
return false; task_play_feature_delivery_core_install_handler);
if (task->handler == task_play_feature_delivery_core_install_handler)
return true;
return false;
} }
void *task_push_play_feature_delivery_core_install( void *task_push_play_feature_delivery_core_install(
@ -1983,10 +1956,10 @@ void *task_push_play_feature_delivery_core_install(
task_title[0] = '\0'; task_title[0] = '\0';
/* Sanity check */ /* Sanity check */
if (!core_list || if ( !core_list
string_is_empty(filename) || || string_is_empty(filename)
!pfd_install_handle || || !pfd_install_handle
!play_feature_delivery_enabled()) || !play_feature_delivery_enabled())
goto error; goto error;
/* Get core updater list entry */ /* Get core updater list entry */
@ -1994,8 +1967,8 @@ void *task_push_play_feature_delivery_core_install(
core_list, filename, &list_entry)) core_list, filename, &list_entry))
goto error; goto error;
if (string_is_empty(list_entry->local_core_path) || if ( string_is_empty(list_entry->local_core_path)
string_is_empty(list_entry->display_name)) || string_is_empty(list_entry->display_name))
goto error; goto error;
/* Only one core may be downloaded at a time */ /* Only one core may be downloaded at a time */
@ -2015,9 +1988,7 @@ void *task_push_play_feature_delivery_core_install(
pfd_install_handle->status = PLAY_FEATURE_DELIVERY_INSTALL_BEGIN; pfd_install_handle->status = PLAY_FEATURE_DELIVERY_INSTALL_BEGIN;
/* Create task */ /* Create task */
task = task_init(); if (!(task = task_init()))
if (!task)
goto error; goto error;
/* Configure task */ /* Configure task */
@ -2056,7 +2027,8 @@ error:
} }
/* Clean up handle */ /* Clean up handle */
free_play_feature_delivery_install_handle(pfd_install_handle); if (pfd_install_handle)
free_play_feature_delivery_install_handle(pfd_install_handle);
return NULL; return NULL;
} }
@ -2068,9 +2040,6 @@ error:
static void free_play_feature_delivery_switch_cores_handle( static void free_play_feature_delivery_switch_cores_handle(
play_feature_delivery_switch_cores_handle_t *pfd_switch_cores_handle) play_feature_delivery_switch_cores_handle_t *pfd_switch_cores_handle)
{ {
if (!pfd_switch_cores_handle)
return;
if (pfd_switch_cores_handle->path_dir_libretro) if (pfd_switch_cores_handle->path_dir_libretro)
free(pfd_switch_cores_handle->path_dir_libretro); free(pfd_switch_cores_handle->path_dir_libretro);
@ -2086,16 +2055,16 @@ static void free_play_feature_delivery_switch_cores_handle(
pfd_switch_cores_handle = NULL; pfd_switch_cores_handle = NULL;
} }
static void task_play_feature_delivery_switch_cores_handler(retro_task_t *task) static void task_play_feature_delivery_switch_cores_handler(
retro_task_t *task)
{ {
play_feature_delivery_switch_cores_handle_t *pfd_switch_cores_handle = NULL; play_feature_delivery_switch_cores_handle_t *pfd_switch_cores_handle = NULL;
if (!task) if (!task)
goto task_finished; goto task_finished;
pfd_switch_cores_handle = (play_feature_delivery_switch_cores_handle_t*)task->state; if (!(pfd_switch_cores_handle =
(play_feature_delivery_switch_cores_handle_t*)task->state))
if (!pfd_switch_cores_handle)
goto task_finished; goto task_finished;
if (task_get_cancelled(task)) if (task_get_cancelled(task))
@ -2337,23 +2306,18 @@ static void task_play_feature_delivery_switch_cores_handler(retro_task_t *task)
return; return;
task_finished: task_finished:
if (task) if (task)
task_set_finished(task, true); task_set_finished(task, true);
free_play_feature_delivery_switch_cores_handle(pfd_switch_cores_handle); if (pfd_switch_cores_handle)
free_play_feature_delivery_switch_cores_handle(pfd_switch_cores_handle);
} }
static bool task_play_feature_delivery_switch_cores_finder( static bool task_play_feature_delivery_switch_cores_finder(
retro_task_t *task, void *user_data) retro_task_t *task, void *user_data)
{ {
if (!task) return (task && task->handler ==
return false; task_play_feature_delivery_switch_cores_handler);
if (task->handler == task_play_feature_delivery_switch_cores_handler)
return true;
return false;
} }
void task_push_play_feature_delivery_switch_installed_cores( void task_push_play_feature_delivery_switch_installed_cores(
@ -2367,10 +2331,10 @@ void task_push_play_feature_delivery_switch_installed_cores(
calloc(1, sizeof(play_feature_delivery_switch_cores_handle_t)); calloc(1, sizeof(play_feature_delivery_switch_cores_handle_t));
/* Sanity check */ /* Sanity check */
if (string_is_empty(path_dir_libretro) || if ( string_is_empty(path_dir_libretro)
string_is_empty(path_libretro_info) || || string_is_empty(path_libretro_info)
!pfd_switch_cores_handle || || !pfd_switch_cores_handle
!play_feature_delivery_enabled()) || !play_feature_delivery_enabled())
goto error; goto error;
/* Only one instance of this task my run at a time */ /* Only one instance of this task my run at a time */
@ -2395,9 +2359,7 @@ void task_push_play_feature_delivery_switch_installed_cores(
goto error; goto error;
/* Create task */ /* Create task */
task = task_init(); if (!(task = task_init()))
if (!task)
goto error; goto error;
/* Configure task */ /* Configure task */
@ -2413,7 +2375,6 @@ void task_push_play_feature_delivery_switch_installed_cores(
return; return;
error: error:
/* Clean up task */ /* Clean up task */
if (task) if (task)
{ {
@ -2422,7 +2383,7 @@ error:
} }
/* Clean up handle */ /* Clean up handle */
free_play_feature_delivery_switch_cores_handle(pfd_switch_cores_handle); if (pfd_switch_cores_handle)
free_play_feature_delivery_switch_cores_handle(pfd_switch_cores_handle);
} }
#endif #endif