diff --git a/libretro-common/include/streams/interface_stream.h b/libretro-common/include/streams/interface_stream.h index ebcb8fda7d..98c0d61f99 100644 --- a/libretro-common/include/streams/interface_stream.h +++ b/libretro-common/include/streams/interface_stream.h @@ -92,6 +92,8 @@ int intfstream_close(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, unsigned mode, unsigned hints); diff --git a/libretro-common/streams/interface_stream.c b/libretro-common/streams/interface_stream.c index fb34bb3657..c1156ecb47 100644 --- a/libretro-common/streams/interface_stream.c +++ b/libretro-common/streams/interface_stream.c @@ -133,6 +133,24 @@ bool intfstream_open(intfstream_internal_t *intf, const char *path, 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) { if (!intf) diff --git a/tasks/task_save.c b/tasks/task_save.c index c3538aa905..eab39754b1 100644 --- a/tasks/task_save.c +++ b/tasks/task_save.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -78,7 +79,7 @@ struct sram_block typedef struct { - RFILE *file; + intfstream_t *file; char path[PATH_MAX_LENGTH]; void *data; void *undo_data; @@ -157,7 +158,7 @@ static void autosave_thread(void *data) if (differ) { /* 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); if (file) @@ -174,9 +175,9 @@ static void autosave_thread(void *data) else RARCH_LOG("SRAM changed ... autosaving ...\n"); - failed |= ((size_t)filestream_write(file, save->buffer, save->bufsize) != save->bufsize); - failed |= (filestream_flush(file) != 0); - failed |= (filestream_close(file) != 0); + failed |= ((size_t)intfstream_write(file, save->buffer, save->bufsize) != save->bufsize); + failed |= (intfstream_flush(file) != 0); + failed |= (intfstream_close(file) != 0); if (failed) 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); - filestream_close(state->file); + intfstream_close(state->file); if (!task_get_error(task) && task_get_cancelled(task)) task_set_error(task, strdup("Task canceled")); @@ -565,7 +566,7 @@ static void task_save_handler(retro_task_t *task) 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); 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); - written = (int)filestream_write(state->file, + written = (int)intfstream_write(state->file, (uint8_t*)state->data + state->written, remaining); state->written += written; @@ -712,7 +713,7 @@ static void task_load_handler_finished(retro_task_t *task, task_set_finished(task, true); if (state->file) - filestream_close(state->file); + intfstream_close(state->file); if (!task_get_error(task) && task_get_cancelled(task)) task_set_error(task, strdup("Task canceled")); @@ -738,22 +739,22 @@ static void task_load_handler(retro_task_t *task) 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_HINT_NONE); if (!state->file) goto error; - if (filestream_seek(state->file, 0, SEEK_END) != 0) + if (intfstream_seek(state->file, 0, SEEK_END) != 0) goto error; - state->size = filestream_tell(state->file); + state->size = intfstream_tell(state->file); if (state->size < 0) goto error; - filestream_rewind(state->file); + intfstream_rewind(state->file); 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); - bytes_read = filestream_read(state->file, + bytes_read = intfstream_read(state->file, (uint8_t*)state->data + state->bytes_read, remaining); state->bytes_read += bytes_read;