(task_save.c) Move to intfstream

This commit is contained in:
twinaphex 2017-12-11 14:56:58 +01:00
parent 1cd7568f1f
commit 32511090ca
3 changed files with 35 additions and 14 deletions

View File

@ -92,6 +92,8 @@ int intfstream_close(intfstream_internal_t *intf);
int64_t intfstream_get_size(intfstream_internal_t *intf); int64_t intfstream_get_size(intfstream_internal_t *intf);
int intfstream_flush(intfstream_internal_t *intf);
intfstream_t* intfstream_open_file(const char *path, intfstream_t* intfstream_open_file(const char *path,
unsigned mode, unsigned hints); unsigned mode, unsigned hints);

View File

@ -133,6 +133,24 @@ bool intfstream_open(intfstream_internal_t *intf, const char *path,
return true; return true;
} }
int intfstream_flush(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_flush(intf->file.fp);
case INTFSTREAM_MEMORY:
case INTFSTREAM_CHD:
/* Should we stub this for these interfaces? */
break;
}
return 0;
}
int intfstream_close(intfstream_internal_t *intf) int intfstream_close(intfstream_internal_t *intf)
{ {
if (!intf) if (!intf)

View File

@ -29,6 +29,7 @@
#include <compat/strl.h> #include <compat/strl.h>
#include <retro_assert.h> #include <retro_assert.h>
#include <lists/string_list.h> #include <lists/string_list.h>
#include <streams/interface_stream.h>
#include <streams/file_stream.h> #include <streams/file_stream.h>
#include <rthreads/rthreads.h> #include <rthreads/rthreads.h>
#include <file/file_path.h> #include <file/file_path.h>
@ -78,7 +79,7 @@ struct sram_block
typedef struct typedef struct
{ {
RFILE *file; intfstream_t *file;
char path[PATH_MAX_LENGTH]; char path[PATH_MAX_LENGTH];
void *data; void *data;
void *undo_data; void *undo_data;
@ -157,7 +158,7 @@ static void autosave_thread(void *data)
if (differ) if (differ)
{ {
/* Should probably deal with this more elegantly. */ /* Should probably deal with this more elegantly. */
RFILE *file = filestream_open(save->path, intfstream_t *file = intfstream_open_file(save->path,
RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE); RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (file) if (file)
@ -174,9 +175,9 @@ static void autosave_thread(void *data)
else else
RARCH_LOG("SRAM changed ... autosaving ...\n"); RARCH_LOG("SRAM changed ... autosaving ...\n");
failed |= ((size_t)filestream_write(file, save->buffer, save->bufsize) != save->bufsize); failed |= ((size_t)intfstream_write(file, save->buffer, save->bufsize) != save->bufsize);
failed |= (filestream_flush(file) != 0); failed |= (intfstream_flush(file) != 0);
failed |= (filestream_close(file) != 0); failed |= (intfstream_close(file) != 0);
if (failed) if (failed)
RARCH_WARN("Failed to autosave SRAM. Disk might be full.\n"); RARCH_WARN("Failed to autosave SRAM. Disk might be full.\n");
} }
@ -530,7 +531,7 @@ static void task_save_handler_finished(retro_task_t *task,
task_set_finished(task, true); task_set_finished(task, true);
filestream_close(state->file); intfstream_close(state->file);
if (!task_get_error(task) && task_get_cancelled(task)) if (!task_get_error(task) && task_get_cancelled(task))
task_set_error(task, strdup("Task canceled")); task_set_error(task, strdup("Task canceled"));
@ -565,7 +566,7 @@ static void task_save_handler(retro_task_t *task)
if (!state->file) if (!state->file)
{ {
state->file = filestream_open(state->path, RETRO_VFS_FILE_ACCESS_WRITE, state->file = intfstream_open_file(state->path, RETRO_VFS_FILE_ACCESS_WRITE,
RETRO_VFS_FILE_ACCESS_HINT_NONE); RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!state->file) if (!state->file)
@ -573,7 +574,7 @@ static void task_save_handler(retro_task_t *task)
} }
remaining = MIN(state->size - state->written, SAVE_STATE_CHUNK); remaining = MIN(state->size - state->written, SAVE_STATE_CHUNK);
written = (int)filestream_write(state->file, written = (int)intfstream_write(state->file,
(uint8_t*)state->data + state->written, remaining); (uint8_t*)state->data + state->written, remaining);
state->written += written; state->written += written;
@ -712,7 +713,7 @@ static void task_load_handler_finished(retro_task_t *task,
task_set_finished(task, true); task_set_finished(task, true);
if (state->file) if (state->file)
filestream_close(state->file); intfstream_close(state->file);
if (!task_get_error(task) && task_get_cancelled(task)) if (!task_get_error(task) && task_get_cancelled(task))
task_set_error(task, strdup("Task canceled")); task_set_error(task, strdup("Task canceled"));
@ -738,22 +739,22 @@ static void task_load_handler(retro_task_t *task)
if (!state->file) if (!state->file)
{ {
state->file = filestream_open(state->path, state->file = intfstream_open_file(state->path,
RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE); RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!state->file) if (!state->file)
goto error; goto error;
if (filestream_seek(state->file, 0, SEEK_END) != 0) if (intfstream_seek(state->file, 0, SEEK_END) != 0)
goto error; goto error;
state->size = filestream_tell(state->file); state->size = intfstream_tell(state->file);
if (state->size < 0) if (state->size < 0)
goto error; goto error;
filestream_rewind(state->file); intfstream_rewind(state->file);
state->data = malloc(state->size + 1); state->data = malloc(state->size + 1);
@ -762,7 +763,7 @@ static void task_load_handler(retro_task_t *task)
} }
remaining = MIN(state->size - state->bytes_read, SAVE_STATE_CHUNK); remaining = MIN(state->size - state->bytes_read, SAVE_STATE_CHUNK);
bytes_read = filestream_read(state->file, bytes_read = intfstream_read(state->file,
(uint8_t*)state->data + state->bytes_read, remaining); (uint8_t*)state->data + state->bytes_read, remaining);
state->bytes_read += bytes_read; state->bytes_read += bytes_read;