2015-05-05 18:05:59 +02:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-05-24 07:36:30 +02:00
|
|
|
|
|
|
|
#include "../dir_list_special.h"
|
2015-05-05 18:49:14 +02:00
|
|
|
#include "../file_ops.h"
|
2015-05-05 18:05:59 +02:00
|
|
|
|
2015-05-05 18:43:51 +02:00
|
|
|
#include "../general.h"
|
2015-05-05 18:05:59 +02:00
|
|
|
#include "../runloop_data.h"
|
|
|
|
#include "tasks.h"
|
|
|
|
|
2015-05-05 18:43:51 +02:00
|
|
|
|
2015-05-05 18:05:59 +02:00
|
|
|
#ifdef HAVE_LIBRETRODB
|
2015-05-05 18:43:51 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
static int zlib_compare_crc32(const char *name, const char *valid_exts,
|
|
|
|
const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
|
|
|
|
uint32_t crc32, void *userdata)
|
|
|
|
{
|
|
|
|
RARCH_LOG("CRC32: 0x%x\n", crc32);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2015-05-23 21:56:17 +02:00
|
|
|
|
|
|
|
static int database_info_iterate_start
|
|
|
|
(database_info_handle_t *db, const char *name)
|
|
|
|
{
|
|
|
|
char msg[PATH_MAX_LENGTH];
|
|
|
|
snprintf(msg, sizeof(msg), "%zu/%zu: Scanning %s...\n",
|
|
|
|
db->list_ptr, db->list->size, name);
|
|
|
|
|
|
|
|
if (msg[0] != '\0')
|
|
|
|
rarch_main_msg_queue_push(msg, 1, 180, true);
|
|
|
|
|
|
|
|
RARCH_LOG("msg: %s\n", msg);
|
|
|
|
|
|
|
|
db->status = DATABASE_STATUS_ITERATE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-23 16:41:33 +02:00
|
|
|
static int database_info_iterate_playlist(
|
2015-05-23 21:56:17 +02:00
|
|
|
database_state_handle_t *db_state,
|
2015-05-05 18:43:51 +02:00
|
|
|
database_info_handle_t *db, const char *name)
|
|
|
|
{
|
|
|
|
char parent_dir[PATH_MAX_LENGTH];
|
|
|
|
|
|
|
|
path_parent_dir(parent_dir);
|
|
|
|
|
|
|
|
if (!strcmp(path_get_extension(name), "zip"))
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (!zlib_parse_file(name, NULL, zlib_compare_crc32,
|
|
|
|
(void*)parent_dir))
|
|
|
|
RARCH_LOG("Could not process ZIP file.\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
uint32_t crc, target_crc = 0;
|
2015-05-23 21:56:17 +02:00
|
|
|
int read_from = read_file(name, (void**)&db_state->buf, &ret);
|
2015-05-05 18:43:51 +02:00
|
|
|
|
|
|
|
(void)target_crc;
|
|
|
|
|
2015-05-05 19:13:21 +02:00
|
|
|
if (read_from != 1 || ret <= 0)
|
2015-05-05 18:43:51 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
2015-05-23 21:56:17 +02:00
|
|
|
crc = zlib_crc32_calculate(db_state->buf, ret);
|
2015-05-05 18:43:51 +02:00
|
|
|
|
|
|
|
RARCH_LOG("CRC32: 0x%x .\n", (unsigned)crc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:53:43 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int database_info_iterate_next(
|
|
|
|
database_info_handle_t *db)
|
|
|
|
{
|
2015-05-05 18:43:51 +02:00
|
|
|
db->list_ptr++;
|
|
|
|
|
2015-05-23 19:18:56 +02:00
|
|
|
if (db->list_ptr < db->list->size)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
2015-05-05 18:43:51 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 21:56:17 +02:00
|
|
|
static int database_info_iterate(database_state_handle_t *state, database_info_handle_t *db)
|
2015-05-05 18:43:51 +02:00
|
|
|
{
|
2015-05-23 21:56:17 +02:00
|
|
|
const char *name = db ? db->list->elems[db->list_ptr].data : NULL;
|
2015-05-05 18:43:51 +02:00
|
|
|
|
|
|
|
if (!db || !db->list)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (db->type)
|
|
|
|
{
|
|
|
|
case DATABASE_TYPE_NONE:
|
|
|
|
break;
|
2015-05-23 16:41:33 +02:00
|
|
|
case DATABASE_TYPE_ITERATE:
|
2015-05-23 21:56:17 +02:00
|
|
|
return database_info_iterate_playlist(state, db, name);
|
2015-05-05 18:43:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-23 18:45:58 +02:00
|
|
|
static int database_info_poll(db_handle_t *db)
|
|
|
|
{
|
2015-05-23 22:07:59 +02:00
|
|
|
char elem0[PATH_MAX_LENGTH], elem1[PATH_MAX_LENGTH];
|
|
|
|
struct string_list *str_list = NULL;
|
2015-05-23 18:45:58 +02:00
|
|
|
const char *path = msg_queue_pull(db->msg_queue);
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return -1;
|
|
|
|
|
2015-05-23 22:07:59 +02:00
|
|
|
str_list = string_split(path, "|");
|
|
|
|
|
|
|
|
if (!str_list)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (str_list->size > 0)
|
|
|
|
strlcpy(elem0, str_list->elems[0].data, sizeof(elem0));
|
|
|
|
if (str_list->size > 1)
|
|
|
|
strlcpy(elem1, str_list->elems[1].data, sizeof(elem1));
|
|
|
|
|
|
|
|
db->handle = database_info_init(elem0, DATABASE_TYPE_ITERATE);
|
|
|
|
|
|
|
|
string_list_free(str_list);
|
2015-05-23 18:45:58 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-05-23 22:07:59 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (str_list)
|
|
|
|
string_list_free(str_list);
|
|
|
|
|
|
|
|
return -1;
|
2015-05-23 18:45:58 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:52:22 +02:00
|
|
|
static void rarch_main_data_db_cleanup_state(void *data)
|
|
|
|
{
|
|
|
|
data_runloop_t *runloop = (data_runloop_t*)data;
|
|
|
|
|
|
|
|
if (!runloop)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (runloop->db.state.buf)
|
|
|
|
free(runloop->db.state.buf);
|
|
|
|
runloop->db.state.buf = NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-05 18:05:59 +02:00
|
|
|
void rarch_main_data_db_iterate(bool is_thread, void *data)
|
|
|
|
{
|
|
|
|
data_runloop_t *runloop = (data_runloop_t*)data;
|
2015-05-23 18:13:23 +02:00
|
|
|
database_info_handle_t *db = runloop ? runloop->db.handle : NULL;
|
2015-05-23 21:56:17 +02:00
|
|
|
const char *name = db ? db->list->elems[db->list_ptr].data : NULL;
|
2015-05-05 18:05:59 +02:00
|
|
|
|
2015-05-23 18:13:23 +02:00
|
|
|
if (!db || !runloop)
|
2015-05-23 19:18:56 +02:00
|
|
|
goto do_poll;
|
2015-05-05 18:05:59 +02:00
|
|
|
|
|
|
|
switch (db->status)
|
|
|
|
{
|
2015-05-24 06:14:44 +02:00
|
|
|
case DATABASE_STATUS_ITERATE_BEGIN:
|
2015-05-24 07:36:30 +02:00
|
|
|
if (!runloop->db.list)
|
|
|
|
{
|
|
|
|
runloop->db.list = dir_list_new_special(NULL, DIR_LIST_DATABASES);
|
|
|
|
runloop->db.list_index = 0;
|
|
|
|
runloop->db.entry_index = 0;
|
|
|
|
}
|
2015-05-24 06:14:44 +02:00
|
|
|
db->status = DATABASE_STATUS_ITERATE_START;
|
|
|
|
break;
|
2015-05-23 21:56:17 +02:00
|
|
|
case DATABASE_STATUS_ITERATE_START:
|
2015-05-23 22:52:22 +02:00
|
|
|
rarch_main_data_db_cleanup_state(data);
|
2015-05-23 21:56:17 +02:00
|
|
|
database_info_iterate_start(db, name);
|
|
|
|
break;
|
2015-05-05 18:05:59 +02:00
|
|
|
case DATABASE_STATUS_ITERATE:
|
2015-05-23 22:46:23 +02:00
|
|
|
if (database_info_iterate(&runloop->db.state, db) == 0)
|
|
|
|
db->status = DATABASE_STATUS_ITERATE_NEXT;
|
2015-05-23 20:53:43 +02:00
|
|
|
break;
|
|
|
|
case DATABASE_STATUS_ITERATE_NEXT:
|
|
|
|
if (database_info_iterate_next(db) == 0)
|
2015-05-23 22:52:22 +02:00
|
|
|
{
|
2015-05-23 21:56:17 +02:00
|
|
|
db->status = DATABASE_STATUS_ITERATE_START;
|
2015-05-23 22:52:22 +02:00
|
|
|
}
|
2015-05-23 20:53:43 +02:00
|
|
|
else
|
2015-05-23 19:18:56 +02:00
|
|
|
{
|
|
|
|
rarch_main_msg_queue_push("Scanning of directory finished.\n", 0, 180, true);
|
|
|
|
db->status = DATABASE_STATUS_FREE;
|
|
|
|
}
|
2015-05-05 18:05:59 +02:00
|
|
|
break;
|
|
|
|
case DATABASE_STATUS_FREE:
|
2015-05-24 07:36:30 +02:00
|
|
|
if (runloop->db.list)
|
|
|
|
dir_list_free(runloop->db.list);
|
|
|
|
runloop->db.list = NULL;
|
2015-05-23 22:52:22 +02:00
|
|
|
rarch_main_data_db_cleanup_state(data);
|
2015-05-05 18:05:59 +02:00
|
|
|
database_info_free(db);
|
2015-05-23 19:18:56 +02:00
|
|
|
if (runloop->db.handle)
|
|
|
|
free(runloop->db.handle);
|
|
|
|
runloop->db.handle = NULL;
|
2015-05-05 18:05:59 +02:00
|
|
|
break;
|
2015-05-23 18:08:42 +02:00
|
|
|
default:
|
|
|
|
case DATABASE_STATUS_NONE:
|
2015-05-23 19:18:56 +02:00
|
|
|
goto do_poll;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
do_poll:
|
|
|
|
if (database_info_poll(&runloop->db) != -1)
|
|
|
|
{
|
|
|
|
if (runloop->db.handle)
|
2015-05-24 06:14:44 +02:00
|
|
|
runloop->db.handle->status = DATABASE_STATUS_ITERATE_BEGIN;
|
2015-05-05 18:05:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|