RetroArch/tasks/task_decompress.c

327 lines
8.6 KiB
C
Raw Normal View History

2015-11-27 20:43:49 +00:00
/* RetroArch - A frontend for libretro.
2016-01-10 03:06:50 +00:00
* Copyright (C) 2011-2016 - Daniel De Matteis
2015-11-27 20:43:49 +00:00
*
* 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/>.
*/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <string/string_list.h>
2015-12-26 06:23:13 +00:00
#include <string/stdstring.h>
2015-11-27 20:43:49 +00:00
#include <file/file_path.h>
2016-01-24 06:29:11 +00:00
#include <file/file_archive.h>
#include <retro_miscellaneous.h>
#include <retro_stat.h>
2015-11-27 20:43:49 +00:00
2016-02-09 16:55:15 +00:00
#include "tasks_internal.h"
2015-11-27 20:43:49 +00:00
#include "../verbosity.h"
#include "../msg_hash.h"
2015-11-27 20:43:49 +00:00
typedef struct
{
2015-11-27 20:43:49 +00:00
char *source_file;
char *subdir;
2015-11-27 20:43:49 +00:00
char *target_dir;
char *target_file;
2015-11-27 20:43:49 +00:00
char *valid_ext;
char *callback_error;
2016-01-24 21:17:11 +00:00
file_archive_transfer_t zlib;
2015-11-27 20:43:49 +00:00
} decompress_state_t;
2016-02-03 16:41:04 +00:00
static int file_decompressed_target_file(const char *name,
const char *valid_exts,
const uint8_t *cdata,
unsigned cmode, uint32_t csize, uint32_t size,
uint32_t crc32, void *userdata)
{
/* TODO/FIXME */
return 0;
}
2016-02-03 16:41:04 +00:00
static int file_decompressed_subdir(const char *name,
const char *valid_exts,
const uint8_t *cdata,
unsigned cmode, uint32_t csize,uint32_t size,
uint32_t crc32, void *userdata)
{
char path[PATH_MAX_LENGTH];
char path_dir[PATH_MAX_LENGTH];
decompress_state_t *dec = (decompress_state_t*)userdata;
/* Ignore directories. */
2016-01-21 20:33:52 +00:00
if (name[strlen(name) - 1] == '/' || name[strlen(name) - 1] == '\\')
goto next_file;
if (strstr(name, dec->subdir) != name)
return 1;
name += strlen(dec->subdir) + 1;
fill_pathname_join(path, dec->target_dir, name, sizeof(path));
fill_pathname_basedir(path_dir, path, sizeof(path_dir));
/* Make directory */
if (!path_mkdir(path_dir))
goto error;
2016-01-24 06:42:46 +00:00
if (!file_archive_perform_mode(path, valid_exts,
cdata, cmode, csize, size, crc32, userdata))
goto error;
RARCH_LOG("[deflate subdir] Path: %s, CRC32: 0x%x\n", name, crc32);
next_file:
return 1;
error:
dec->callback_error = (char*)malloc(PATH_MAX_LENGTH);
2016-02-03 16:41:04 +00:00
snprintf(dec->callback_error,
PATH_MAX_LENGTH, "Failed to deflate %s.\n", path);
return 0;
}
2015-11-27 20:43:49 +00:00
static int file_decompressed(const char *name, const char *valid_exts,
const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
uint32_t crc32, void *userdata)
{
char path[PATH_MAX_LENGTH];
decompress_state_t *dec = (decompress_state_t*)userdata;
/* Ignore directories. */
2016-01-21 20:33:52 +00:00
if (name[strlen(name) - 1] == '/' || name[strlen(name) - 1] == '\\')
2015-11-27 20:43:49 +00:00
goto next_file;
/* Make directory */
fill_pathname_join(path, dec->target_dir, name, sizeof(path));
path_basedir(path);
if (!path_mkdir(path))
goto error;
fill_pathname_join(path, dec->target_dir, name, sizeof(path));
2016-01-24 06:42:46 +00:00
if (!file_archive_perform_mode(path, valid_exts,
2015-11-27 20:43:49 +00:00
cdata, cmode, csize, size, crc32, userdata))
goto error;
RARCH_LOG("[deflate] Path: %s, CRC32: 0x%x\n", name, crc32);
next_file:
return 1;
error:
dec->callback_error = (char*)malloc(PATH_MAX_LENGTH);
2016-02-03 16:41:04 +00:00
snprintf(dec->callback_error, PATH_MAX_LENGTH,
"Failed to deflate %s.\n", path);
2015-11-27 20:43:49 +00:00
return 0;
}
2016-02-09 16:47:04 +00:00
static void rarch_task_decompress_handler_finished(retro_task_t *task,
decompress_state_t *dec)
2015-11-27 20:43:49 +00:00
{
task->finished = true;
if (!task->error && task->cancelled)
2015-11-27 20:43:49 +00:00
task->error = strdup("Task canceled");
2016-01-23 23:06:50 +00:00
if (task->error)
free(dec->source_file);
else
2015-11-27 20:43:49 +00:00
{
decompress_task_data_t *data =
(decompress_task_data_t*)calloc(1, sizeof(*data));
2015-11-27 20:43:49 +00:00
data->source_file = dec->source_file;
2016-01-23 23:06:50 +00:00
task->task_data = data;
2015-11-27 20:43:49 +00:00
}
if (dec->subdir)
free(dec->subdir);
2015-11-27 20:43:49 +00:00
if (dec->valid_ext)
free(dec->valid_ext);
free(dec->target_dir);
free(dec);
}
2016-02-09 16:47:04 +00:00
static void rarch_task_decompress_handler(retro_task_t *task)
{
2016-02-03 16:41:04 +00:00
bool retdec = false;
decompress_state_t *dec = (decompress_state_t*)task->state;
2016-02-03 16:41:04 +00:00
int ret = file_archive_parse_file_iterate(&dec->zlib,
&retdec, dec->source_file,
dec->valid_ext, file_decompressed, dec);
2016-01-24 06:42:46 +00:00
task->progress = file_archive_parse_file_progress(&dec->zlib);
if (task->cancelled || ret != 0)
{
task->error = dec->callback_error;
2016-01-24 06:42:46 +00:00
file_archive_parse_file_iterate_stop(&dec->zlib);
rarch_task_decompress_handler_finished(task, dec);
}
}
2016-02-09 16:47:04 +00:00
static void rarch_task_decompress_handler_target_file(retro_task_t *task)
{
2016-02-03 16:41:04 +00:00
bool retdec;
decompress_state_t *dec = (decompress_state_t*)task->state;
2016-02-03 16:41:04 +00:00
int ret = file_archive_parse_file_iterate(&dec->zlib,
&retdec, dec->source_file,
dec->valid_ext, file_decompressed_target_file, dec);
2016-01-24 06:42:46 +00:00
task->progress = file_archive_parse_file_progress(&dec->zlib);
if (task->cancelled || ret != 0)
{
task->error = dec->callback_error;
2016-01-24 06:42:46 +00:00
file_archive_parse_file_iterate_stop(&dec->zlib);
rarch_task_decompress_handler_finished(task, dec);
}
}
2016-02-09 16:47:04 +00:00
static void rarch_task_decompress_handler_subdir(retro_task_t *task)
{
2016-02-03 16:41:04 +00:00
bool retdec;
decompress_state_t *dec = (decompress_state_t*)task->state;
2016-02-03 16:41:04 +00:00
int ret = file_archive_parse_file_iterate(&dec->zlib,
&retdec, dec->source_file,
dec->valid_ext, file_decompressed_subdir, dec);
2016-01-24 06:42:46 +00:00
task->progress = file_archive_parse_file_progress(&dec->zlib);
if (task->cancelled || ret != 0)
{
task->error = dec->callback_error;
2016-01-24 06:42:46 +00:00
file_archive_parse_file_iterate_stop(&dec->zlib);
rarch_task_decompress_handler_finished(task, dec);
}
}
2016-02-03 16:41:04 +00:00
static bool rarch_task_decompress_finder(
2016-02-09 16:47:04 +00:00
retro_task_t *task, void *user_data)
{
decompress_state_t *dec = (decompress_state_t*)task->state;
if (task->handler != rarch_task_decompress_handler)
return false;
2016-01-20 03:07:24 +00:00
return string_is_equal(dec->source_file, (const char*)user_data);
}
bool rarch_task_push_decompress(
const char *source_file,
const char *target_dir,
const char *target_file,
const char *subdir,
const char *valid_ext,
2016-02-09 16:47:04 +00:00
retro_task_callback_t cb,
void *user_data)
2015-11-27 20:43:49 +00:00
{
2016-01-28 08:57:55 +00:00
task_finder_data_t find_data;
char tmp[PATH_MAX_LENGTH];
2016-01-21 02:16:48 +00:00
decompress_state_t *s = NULL;
2016-02-09 16:47:04 +00:00
retro_task_t *t = NULL;
2016-01-21 02:16:48 +00:00
bool is_compressed = false;
2015-11-27 20:43:49 +00:00
2016-01-21 02:16:48 +00:00
if (string_is_empty(target_dir) || string_is_empty(source_file))
{
2016-02-03 16:41:04 +00:00
RARCH_WARN("[decompress] Empty or null source file or"
" target directory arguments.\n");
2015-11-27 20:43:49 +00:00
return false;
}
2015-11-27 20:43:49 +00:00
/* ZIP or APK only */
2016-01-21 02:16:48 +00:00
is_compressed = string_is_equal(path_get_extension(source_file), "zip");
2016-02-03 16:41:04 +00:00
is_compressed = is_compressed ||
string_is_equal(path_get_extension(source_file), "apk");
if (!path_file_exists(source_file) || !is_compressed)
{
RARCH_WARN("[decompress] File '%s' does not exist or is not a compressed file.\n",
source_file);
2015-11-27 20:43:49 +00:00
return false;
}
2015-11-27 20:43:49 +00:00
if (!valid_ext || !valid_ext[0])
2016-01-21 02:16:48 +00:00
valid_ext = NULL;
2015-11-27 20:43:49 +00:00
2016-01-28 08:57:55 +00:00
find_data.func = rarch_task_decompress_finder;
find_data.userdata = (void*)source_file;
2016-02-09 17:26:27 +00:00
if (task_queue_ctl(TASK_QUEUE_CTL_FIND, &find_data))
{
2016-02-03 16:41:04 +00:00
RARCH_LOG("[decompress] File '%s' already being decompressed.\n",
source_file);
return false;
}
2016-01-10 12:16:55 +00:00
RARCH_LOG("[decompress] File '%s.\n", source_file);
2016-01-21 02:16:48 +00:00
s = (decompress_state_t*)calloc(1, sizeof(*s));
2015-11-27 20:43:49 +00:00
if (!s)
goto error;
2015-11-27 20:43:49 +00:00
s->source_file = strdup(source_file);
s->target_dir = strdup(target_dir);
2016-01-21 02:16:48 +00:00
s->valid_ext = valid_ext ? strdup(valid_ext) : NULL;
s->zlib.type = ZLIB_TRANSFER_INIT;
2015-11-27 20:43:49 +00:00
2016-02-09 16:47:04 +00:00
t = (retro_task_t*)calloc(1, sizeof(*t));
if (!t)
goto error;
2016-01-21 02:16:48 +00:00
t->state = s;
t->handler = rarch_task_decompress_handler;
2015-12-26 06:23:13 +00:00
if (!string_is_empty(subdir))
{
s->subdir = strdup(subdir);
t->handler = rarch_task_decompress_handler_subdir;
}
else if (!string_is_empty(target_file))
{
2016-01-26 03:29:19 +00:00
s->target_file = strdup(target_file);
t->handler = rarch_task_decompress_handler_target_file;
}
2015-11-27 20:43:49 +00:00
2016-01-21 02:16:48 +00:00
t->callback = cb;
t->user_data = user_data;
2015-11-27 20:43:49 +00:00
2016-02-03 16:41:04 +00:00
snprintf(tmp, sizeof(tmp), "%s '%s'",
msg_hash_to_str(MSG_EXTRACTING), path_basename(source_file));
2016-01-21 02:16:48 +00:00
t->title = strdup(tmp);
2016-02-09 17:26:27 +00:00
task_queue_ctl(TASK_QUEUE_CTL_PUSH, t);
2015-11-27 20:43:49 +00:00
return true;
error:
if (s)
free(s);
if (t)
free(t);
return false;
2015-11-27 20:43:49 +00:00
}