diff --git a/libretro-common/include/streams/file_stream.h b/libretro-common/include/streams/file_stream.h index d13290d4d3..4951d32a23 100644 --- a/libretro-common/include/streams/file_stream.h +++ b/libretro-common/include/streams/file_stream.h @@ -77,6 +77,8 @@ int filestream_putc(RFILE *stream, int c); int filestream_get_fd(RFILE *stream); +int filestream_flush(RFILE *stream); + RETRO_END_DECLS #endif diff --git a/libretro-common/streams/file_stream.c b/libretro-common/streams/file_stream.c index da871d5eaa..dd3e7ae549 100644 --- a/libretro-common/streams/file_stream.c +++ b/libretro-common/streams/file_stream.c @@ -449,6 +449,15 @@ error: return -1; } +int filestream_flush(RFILE *stream) +{ +#if defined(HAVE_BUFFERED_IO) + return fflush(stream->fp); +#else + return 0; +#endif +} + ssize_t filestream_write(RFILE *stream, const void *s, size_t len) { if (!stream) diff --git a/tasks/task_save.c b/tasks/task_save.c index faa48150a6..3ba08d678e 100644 --- a/tasks/task_save.c +++ b/tasks/task_save.c @@ -13,7 +13,6 @@ * You should have received a copy of the GNU General Public License along with RetroArch. * If not, see . */ -#include #include #include #include @@ -158,7 +157,7 @@ static void autosave_thread(void *data) if (differ) { /* Should probably deal with this more elegantly. */ - FILE *file = fopen(save->path, "wb"); + RFILE *file = filestream_open(save->path, RFILE_MODE_WRITE, -1); if (file) { @@ -174,10 +173,10 @@ static void autosave_thread(void *data) else RARCH_LOG("SRAM changed ... autosaving ...\n"); - failed |= fwrite(save->buffer, 1, save->bufsize, file) + failed |= filestream_write(file, save->buffer, save->bufsize) != save->bufsize; - failed |= fflush(file) != 0; - failed |= fclose(file) != 0; + failed |= filestream_flush(file) != 0; + failed |= filestream_close(file) != 0; if (failed) RARCH_WARN("Failed to autosave SRAM. Disk might be full.\n"); }