From 07f39198116e5832985e94de9d6cd4098f9d350a Mon Sep 17 00:00:00 2001 From: Brad Parker Date: Fri, 17 Nov 2017 02:28:45 -0500 Subject: [PATCH] only call setvbuf right after opening file, and use our own buffer for C89 compliance --- cheevos/cheevos.c | 2 +- libretro-common/file/config_file.c | 11 ++------- libretro-common/streams/file_stream.c | 32 ++++++++++++++++++++++----- libretro-db/rmsgpack_test.c | 2 +- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/cheevos/cheevos.c b/cheevos/cheevos.c index c5aca66d00..00dbf7685b 100644 --- a/cheevos/cheevos.c +++ b/cheevos/cheevos.c @@ -2605,7 +2605,7 @@ static int cheevos_iterate(coro_t* coro) /* Load the content into memory, or copy it over to our own buffer */ if (!CHEEVOS_VAR_DATA) { - CHEEVOS_VAR_STREAM = filestream_open(CHEEVOS_VAR_PATH, RFILE_MODE_READ, 0); + CHEEVOS_VAR_STREAM = filestream_open(CHEEVOS_VAR_PATH, RFILE_MODE_READ, -1); if (!CHEEVOS_VAR_STREAM) CORO_STOP(); diff --git a/libretro-common/file/config_file.c b/libretro-common/file/config_file.c index 302cebd3fc..87f72238fb 100644 --- a/libretro-common/file/config_file.c +++ b/libretro-common/file/config_file.c @@ -389,14 +389,13 @@ static config_file_t *config_file_new_internal( goto error; conf->include_depth = depth; - file = filestream_open(path, RFILE_MODE_READ_TEXT, -1); + file = filestream_open(path, RFILE_MODE_READ_TEXT, 0x4000); if (!file) { free(conf->path); goto error; } - setvbuf(filestream_get_fp(file), NULL, _IOFBF, 0x4000); while (!filestream_eof(file)) { @@ -917,15 +916,9 @@ bool config_file_write(config_file_t *conf, const char *path) if (!string_is_empty(path)) { - file = filestream_open(path, RFILE_MODE_WRITE, -1); + file = filestream_open(path, RFILE_MODE_WRITE, 0x4000); if (!file) return false; -#ifdef WIIU - /* TODO: use FBF everywhere once https://i.imgur.com/muVhNeF.jpg is fixed */ - setvbuf(filestream_get_fp(file), NULL, _IONBF, 0x4000); -#else - setvbuf(filestream_get_fp(file), NULL, _IOFBF, 0x4000); -#endif config_file_dump(conf, filestream_get_fp(file)); } else diff --git a/libretro-common/streams/file_stream.c b/libretro-common/streams/file_stream.c index 745bb251e2..21aecc0901 100644 --- a/libretro-common/streams/file_stream.c +++ b/libretro-common/streams/file_stream.c @@ -107,6 +107,7 @@ struct RFILE #endif int fd; #endif + char *buf; }; FILE* filestream_get_fp(RFILE *stream) @@ -154,6 +155,16 @@ void filestream_set_size(RFILE *stream) filestream_seek(stream, 0, SEEK_SET); } +static void filestream_set_buffer(RFILE *stream, ssize_t len) +{ + if (!stream || !stream->fp) + return; + + stream->buf = (char*)calloc(1, len); + + setvbuf(stream->fp, stream->buf, _IOFBF, len); +} + RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) { int flags = 0; @@ -254,6 +265,12 @@ RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) #if defined(PSP) stream->fd = sceIoOpen(path, flags, mode_int); + + if (stream->fd == -1) + goto error; + + if (len >= 0) + filestream_set_buffer(stream, len); #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0 && mode_str) @@ -275,8 +292,12 @@ RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) #else stream->fp = fopen(path, mode_str); #endif + if (!stream->fp) goto error; + + if (len >= 0) + filestream_set_buffer(stream, len); } else #endif @@ -299,8 +320,12 @@ RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) #else stream->fd = open(path, flags, mode_int); #endif + if (stream->fd == -1) goto error; + + if (len >= 0) + filestream_set_buffer(stream, len); #ifdef HAVE_MMAP if (stream->hints & RFILE_HINT_MMAP) { @@ -323,11 +348,6 @@ RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) } #endif -#if defined(PSP) - if (stream->fd == -1) - goto error; -#endif - { const char *ld = (const char*)strrchr(path, '.'); if (ld) @@ -666,6 +686,8 @@ int filestream_close(RFILE *stream) if (stream->fd > 0) close(stream->fd); #endif + if (stream->buf) + free(stream->buf); free(stream); return 0; diff --git a/libretro-db/rmsgpack_test.c b/libretro-db/rmsgpack_test.c index 2e7f7d0d3a..1c6f03cb57 100644 --- a/libretro-db/rmsgpack_test.c +++ b/libretro-db/rmsgpack_test.c @@ -186,7 +186,7 @@ static struct rmsgpack_read_callbacks stub_callbacks = { int main(void) { struct stub_state state; - RFILE *fd = filestream_open("test.msgpack", RFILE_MODE_READ, 0); + RFILE *fd = filestream_open("test.msgpack", RFILE_MODE_READ, -1); state.i = 0; state.stack[0] = 0;