792 lines
17 KiB
C
Raw Normal View History

/* Copyright (C) 2010-2017 The RetroArch team
2015-09-22 12:52:15 +02:00
*
* ---------------------------------------------------------------------------------------
2016-03-20 16:29:14 +01:00
* The following license statement only applies to this file (file_stream.c).
2015-09-22 12:52:15 +02:00
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2015-09-17 20:24:49 +02:00
#include <errno.h>
#if defined(_WIN32)
# ifdef _MSC_VER
# define setmode _setmode
# endif
# ifdef _XBOX
# include <xtl.h>
# define INVALID_FILE_ATTRIBUTES -1
# else
# include <io.h>
# include <fcntl.h>
# include <direct.h>
# include <windows.h>
# endif
#else
# if defined(PSP)
# include <pspiofilemgr.h>
# endif
# include <sys/types.h>
# include <sys/stat.h>
2016-08-05 01:47:05 +02:00
# if !defined(VITA)
# include <dirent.h>
2016-08-05 01:47:05 +02:00
# endif
# include <unistd.h>
#endif
#ifdef __CELLOS_LV2__
#include <cell/cell_fs.h>
#define O_RDONLY CELL_FS_O_RDONLY
#define O_WRONLY CELL_FS_O_WRONLY
#define O_CREAT CELL_FS_O_CREAT
#define O_TRUNC CELL_FS_O_TRUNC
#define O_RDWR CELL_FS_O_RDWR
#else
#include <fcntl.h>
#endif
/* Assume W-functions do not work below VC2005 and Xbox platforms */
#if defined(_MSC_VER) && _MSC_VER < 1400 || defined(_XBOX)
#ifndef LEGACY_WIN32
#define LEGACY_WIN32
#endif
#endif
2016-03-20 16:29:14 +01:00
#include <streams/file_stream.h>
2017-09-29 21:29:34 +02:00
#include <string/stdstring.h>
2015-11-14 14:56:00 -03:00
#include <memmap.h>
2016-11-29 13:47:07 -05:00
#include <retro_miscellaneous.h>
#include <encodings/utf.h>
2015-09-22 01:45:16 +02:00
2015-09-17 20:24:49 +02:00
struct RFILE
{
2015-11-14 20:47:20 -03:00
unsigned hints;
2017-02-18 02:16:40 +01:00
char *ext;
2017-09-05 16:39:12 -04:00
int64_t size;
2017-10-30 23:31:51 -04:00
FILE *fp;
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2015-09-18 02:38:48 +02:00
SceUID fd;
2015-09-17 20:48:06 +02:00
#else
#define HAVE_BUFFERED_IO 1
#if !defined(_WIN32) || defined(LEGACY_WIN32)
2016-11-29 13:47:07 -05:00
#define MODE_STR_READ "r"
#define MODE_STR_READ_UNBUF "rb"
#define MODE_STR_WRITE_UNBUF "wb"
#define MODE_STR_WRITE_PLUS "w+"
#else
#define MODE_STR_READ L"r"
#define MODE_STR_READ_UNBUF L"rb"
#define MODE_STR_WRITE_UNBUF L"wb"
#define MODE_STR_WRITE_PLUS L"w+"
#endif
2016-11-29 13:47:07 -05:00
2015-11-14 14:56:00 -03:00
#if defined(HAVE_MMAP)
uint8_t *mapped;
uint64_t mappos;
uint64_t mapsize;
#endif
int fd;
2015-09-17 20:48:06 +02:00
#endif
char *buf;
2015-09-17 20:24:49 +02:00
};
FILE* filestream_get_fp(RFILE *stream)
{
if (!stream)
return NULL;
return stream->fp;
}
2016-03-24 04:09:25 +01:00
int filestream_get_fd(RFILE *stream)
{
if (!stream)
return -1;
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fileno(stream->fp);
#endif
return stream->fd;
}
2017-02-18 02:16:40 +01:00
const char *filestream_get_ext(RFILE *stream)
{
if (!stream)
return NULL;
return stream->ext;
}
2017-09-05 16:39:12 -04:00
int64_t filestream_get_size(RFILE *stream)
{
if (!stream)
return 0;
return stream->size;
}
void filestream_set_size(RFILE *stream)
{
if (!stream)
return;
filestream_seek(stream, 0, SEEK_SET);
filestream_seek(stream, 0, SEEK_END);
stream->size = filestream_tell(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);
}
2016-03-24 04:23:17 +01:00
RFILE *filestream_open(const char *path, unsigned mode, ssize_t len)
{
2015-09-18 05:42:54 +02:00
int flags = 0;
int mode_int = 0;
2016-04-06 16:55:17 +02:00
#if defined(HAVE_BUFFERED_IO)
#if !defined(_WIN32) || defined(LEGACY_WIN32)
2016-12-02 23:00:09 -05:00
const char *mode_str = NULL;
#else
const wchar_t *mode_str = NULL;
#endif
2016-04-06 16:55:17 +02:00
#endif
2015-09-18 05:42:54 +02:00
RFILE *stream = (RFILE*)calloc(1, sizeof(*stream));
#if defined(_WIN32) && !defined(_XBOX)
char *path_local = NULL;
wchar_t *path_wide = NULL;
#endif
if (!stream)
return NULL;
2015-09-18 05:42:54 +02:00
(void)mode_int;
2015-09-18 06:41:05 +02:00
(void)flags;
2015-09-18 05:42:54 +02:00
stream->hints = mode;
2015-11-14 14:56:00 -03:00
#ifdef HAVE_MMAP
if (stream->hints & RFILE_HINT_MMAP && (stream->hints & 0xff) == RFILE_MODE_READ)
stream->hints |= RFILE_HINT_UNBUFFERED;
else
#endif
stream->hints &= ~RFILE_HINT_MMAP;
2015-11-14 18:12:09 -03:00
switch (mode & 0xff)
{
2016-06-03 03:14:42 +02:00
case RFILE_MODE_READ_TEXT:
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2016-08-04 02:05:55 +02:00
mode_int = 0666;
2016-06-03 03:14:42 +02:00
flags = PSP_O_RDONLY;
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
2016-11-29 13:47:07 -05:00
mode_str = MODE_STR_READ;
2016-06-03 03:14:42 +02:00
#endif
/* No "else" here */
flags = O_RDONLY;
#endif
break;
case RFILE_MODE_READ:
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2016-08-04 02:05:55 +02:00
mode_int = 0666;
2015-09-20 10:19:08 +02:00
flags = PSP_O_RDONLY;
2015-09-17 20:48:06 +02:00
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
2016-11-29 13:47:07 -05:00
mode_str = MODE_STR_READ_UNBUF;
#endif
2015-11-14 14:56:00 -03:00
/* No "else" here */
flags = O_RDONLY;
2015-09-17 20:48:06 +02:00
#endif
break;
case RFILE_MODE_WRITE:
2016-08-05 01:47:05 +02:00
#if defined(PSP)
mode_int = 0666;
2015-09-20 10:19:08 +02:00
flags = PSP_O_CREAT | PSP_O_WRONLY | PSP_O_TRUNC;
2015-09-17 20:48:06 +02:00
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
2016-11-29 13:47:07 -05:00
mode_str = MODE_STR_WRITE_UNBUF;
#endif
2015-11-14 14:56:00 -03:00
else
{
flags = O_WRONLY | O_CREAT | O_TRUNC;
#ifndef _WIN32
flags |= S_IRUSR | S_IWUSR;
#endif
}
2015-09-17 20:48:06 +02:00
#endif
break;
case RFILE_MODE_READ_WRITE:
2016-08-05 01:47:05 +02:00
#if defined(PSP)
mode_int = 0666;
2015-09-20 10:19:08 +02:00
flags = PSP_O_RDWR;
2015-09-17 20:48:06 +02:00
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
2016-11-29 13:47:07 -05:00
mode_str = MODE_STR_WRITE_PLUS;
#endif
2015-11-14 14:56:00 -03:00
else
{
flags = O_RDWR;
#ifdef _WIN32
flags |= O_BINARY;
2015-09-17 20:48:06 +02:00
#endif
}
#endif
break;
}
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2015-09-18 05:42:54 +02:00
stream->fd = sceIoOpen(path, flags, mode_int);
if (stream->fd == -1)
goto error;
if (len >= 0)
filestream_set_buffer(stream, len);
2015-09-18 05:42:54 +02:00
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0 && mode_str)
{
#if defined(_WIN32) && !defined(_XBOX)
(void)path_local;
(void)path_wide;
#if defined(LEGACY_WIN32)
path_local = utf8_to_local_string_alloc(path);
stream->fp = fopen(path_local, mode_str);
if (path_local)
free(path_local);
#else
path_wide = utf8_to_utf16_string_alloc(path);
stream->fp = _wfopen(path_wide, mode_str);
if (path_wide)
free(path_wide);
#endif
#else
stream->fp = fopen(path, mode_str);
#endif
if (!stream->fp)
goto error;
if (len >= 0)
filestream_set_buffer(stream, len);
}
else
#endif
{
2016-11-29 13:47:07 -05:00
/* FIXME: HAVE_BUFFERED_IO is always 1, but if it is ever changed, open() needs to be changed to _wopen() for WIndows. */
#if defined(_WIN32) && !defined(_XBOX)
#if defined(LEGACY_WIN32)
(void)path_wide;
path_local = utf8_to_local_string_alloc(path);
stream->fd = open(path_local, flags, mode_int);
if (path_local)
free(path_local);
#else
(void)path_local;
path_wide = utf8_to_utf16_string_alloc(path);
stream->fd = _wopen(path_wide, flags, mode_int);
if (path_wide)
free(path_wide);
#endif
#else
2017-05-25 13:36:11 +02:00
stream->fd = open(path, flags, mode_int);
#endif
if (stream->fd == -1)
goto error;
if (len >= 0)
filestream_set_buffer(stream, len);
2015-11-14 14:56:00 -03:00
#ifdef HAVE_MMAP
if (stream->hints & RFILE_HINT_MMAP)
{
stream->mappos = 0;
stream->mapped = NULL;
2016-03-24 04:23:17 +01:00
stream->mapsize = filestream_seek(stream, 0, SEEK_END);
2015-11-14 14:56:00 -03:00
if (stream->mapsize == (uint64_t)-1)
goto error;
2016-03-24 04:23:17 +01:00
filestream_rewind(stream);
2015-11-14 14:56:00 -03:00
2016-04-06 16:55:17 +02:00
stream->mapped = (uint8_t*)mmap((void*)0,
stream->mapsize, PROT_READ, MAP_SHARED, stream->fd, 0);
2015-11-14 14:56:00 -03:00
if (stream->mapped == MAP_FAILED)
stream->hints &= ~RFILE_HINT_MMAP;
}
#endif
}
2015-09-18 05:42:54 +02:00
#endif
2017-02-18 02:16:40 +01:00
{
const char *ld = (const char*)strrchr(path, '.');
2017-11-08 17:37:16 +01:00
if (ld)
stream->ext = strdup(ld + 1);
2017-02-18 02:16:40 +01:00
}
filestream_set_size(stream);
return stream;
2015-09-17 21:31:00 +02:00
error:
2016-03-24 04:23:17 +01:00
filestream_close(stream);
2015-09-17 21:31:00 +02:00
return NULL;
}
2016-06-03 07:09:50 +02:00
char *filestream_getline(RFILE *stream)
{
char* newline = (char*)malloc(9);
char* newline_tmp = NULL;
size_t cur_size = 8;
size_t idx = 0;
int in = filestream_getc(stream);
if (!newline)
return NULL;
while (in != EOF && in != '\n')
{
if (idx == cur_size)
{
cur_size *= 2;
newline_tmp = (char*)realloc(newline, cur_size + 1);
if (!newline_tmp)
{
free(newline);
return NULL;
}
newline = newline_tmp;
}
newline[idx++] = in;
in = filestream_getc(stream);
}
newline[idx] = '\0';
return newline;
2016-06-03 07:09:50 +02:00
}
char *filestream_gets(RFILE *stream, char *s, size_t len)
{
if (!stream)
return NULL;
#if defined(HAVE_BUFFERED_IO)
return fgets(s, (int)len, stream->fp);
2016-08-05 01:47:05 +02:00
#elif defined(PSP)
2016-07-31 09:58:35 +02:00
if(filestream_read(stream,s,len)==len)
return s;
2016-08-02 16:19:27 +02:00
return NULL;
#else
return gets(s);
#endif
}
2016-06-03 00:07:00 +02:00
int filestream_getc(RFILE *stream)
{
2016-06-04 07:37:54 +02:00
char c = 0;
(void)c;
2016-06-03 00:07:00 +02:00
if (!stream)
return 0;
#if defined(HAVE_BUFFERED_IO)
return fgetc(stream->fp);
2016-08-05 01:47:05 +02:00
#elif defined(PSP)
if(filestream_read(stream, &c, 1) == 1)
return (int)c;
return EOF;
2016-06-03 00:07:00 +02:00
#else
return getc(stream->fd);
2016-06-03 00:07:00 +02:00
#endif
}
2016-03-24 04:23:17 +01:00
ssize_t filestream_seek(RFILE *stream, ssize_t offset, int whence)
{
if (!stream)
2016-04-06 16:55:17 +02:00
goto error;
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2016-04-07 04:16:46 +02:00
if (sceIoLseek(stream->fd, (SceOff)offset, whence) == -1)
2016-04-06 16:55:17 +02:00
goto error;
2015-09-17 20:48:06 +02:00
#else
2016-04-07 04:16:46 +02:00
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fseek(stream->fp, (long)offset, whence);
#endif
2016-04-07 04:16:46 +02:00
2015-11-14 14:56:00 -03:00
#ifdef HAVE_MMAP
/* Need to check stream->mapped because this function is
2016-04-06 16:48:51 +02:00
* called in filestream_open() */
if (stream->mapped && stream->hints & RFILE_HINT_MMAP)
{
/* fseek() returns error on under/overflow but allows cursor > EOF for
read-only file descriptors. */
switch (whence)
2015-11-14 14:56:00 -03:00
{
2016-04-06 16:48:51 +02:00
case SEEK_SET:
if (offset < 0)
2016-04-06 16:55:17 +02:00
goto error;
2015-11-14 14:56:00 -03:00
2016-04-06 16:48:51 +02:00
stream->mappos = offset;
break;
2015-11-14 14:56:00 -03:00
2016-04-06 16:48:51 +02:00
case SEEK_CUR:
if ((offset < 0 && stream->mappos + offset > stream->mappos) ||
(offset > 0 && stream->mappos + offset < stream->mappos))
2016-04-06 16:55:17 +02:00
goto error;
2016-04-06 16:48:51 +02:00
stream->mappos += offset;
break;
2015-11-14 14:56:00 -03:00
2016-04-06 16:48:51 +02:00
case SEEK_END:
if (stream->mapsize + offset < stream->mapsize)
2016-04-06 16:55:17 +02:00
goto error;
2015-11-14 14:56:00 -03:00
2016-04-06 16:48:51 +02:00
stream->mappos = stream->mapsize + offset;
break;
2015-11-14 14:56:00 -03:00
}
2016-04-06 16:48:51 +02:00
return stream->mappos;
}
2015-11-14 14:56:00 -03:00
#endif
2016-04-07 04:16:46 +02:00
if (lseek(stream->fd, offset, whence) < 0)
2016-04-06 16:55:17 +02:00
goto error;
2016-04-07 04:16:46 +02:00
2015-09-17 20:48:06 +02:00
#endif
2016-04-06 16:55:17 +02:00
2016-04-07 04:16:46 +02:00
return 0;
2016-04-06 16:55:17 +02:00
error:
return -1;
}
2016-06-03 07:29:27 +02:00
int filestream_eof(RFILE *stream)
{
return feof(stream->fp);
/* TODO: FIXME: I can't figure out why this breaks on Windows.
The while loop in config_file_new_internal just never exits.
The current position seems to jump backwards a few lines,
but it doesn't start until somewhere in the middle of the file.
*/
/*
size_t current_position = filestream_tell(stream);
size_t end_position;
filestream_seek(stream, 0, SEEK_END);
end_position = filestream_tell(stream);
2016-06-03 07:29:27 +02:00
filestream_seek(stream, current_position, SEEK_SET);
if (current_position >= end_position)
return 1;
return 0;
*/
2016-06-03 07:29:27 +02:00
}
2016-03-24 04:23:17 +01:00
ssize_t filestream_tell(RFILE *stream)
{
if (!stream)
2016-04-06 16:55:17 +02:00
goto error;
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2016-04-07 04:20:09 +02:00
if (sceIoLseek(stream->fd, 0, SEEK_CUR) < 0)
goto error;
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return ftell(stream->fp);
#endif
2015-11-14 14:56:00 -03:00
#ifdef HAVE_MMAP
/* Need to check stream->mapped because this function
2016-04-06 16:48:51 +02:00
* is called in filestream_open() */
if (stream->mapped && stream->hints & RFILE_HINT_MMAP)
return stream->mappos;
2015-11-14 14:56:00 -03:00
#endif
2016-04-07 04:20:09 +02:00
if (lseek(stream->fd, 0, SEEK_CUR) < 0)
goto error;
#endif
2016-04-06 16:55:17 +02:00
2016-04-07 04:20:09 +02:00
return 0;
2016-04-06 16:55:17 +02:00
error:
return -1;
}
2016-03-24 04:23:17 +01:00
void filestream_rewind(RFILE *stream)
{
2016-03-24 04:23:17 +01:00
filestream_seek(stream, 0L, SEEK_SET);
}
2016-03-24 04:23:17 +01:00
ssize_t filestream_read(RFILE *stream, void *s, size_t len)
{
if (!stream || !s)
2016-04-06 16:55:17 +02:00
goto error;
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2015-09-18 02:38:48 +02:00
return sceIoRead(stream->fd, s, len);
2015-09-17 20:48:06 +02:00
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fread(s, 1, len, stream->fp);
#endif
2015-11-14 14:56:00 -03:00
#ifdef HAVE_MMAP
2016-04-06 16:48:51 +02:00
if (stream->hints & RFILE_HINT_MMAP)
{
if (stream->mappos > stream->mapsize)
2016-04-06 16:55:17 +02:00
goto error;
2016-04-06 16:48:51 +02:00
if (stream->mappos + len > stream->mapsize)
len = stream->mapsize - stream->mappos;
2015-11-14 14:56:00 -03:00
2016-04-06 16:48:51 +02:00
memcpy(s, &stream->mapped[stream->mappos], len);
stream->mappos += len;
2015-11-14 14:56:00 -03:00
2016-04-06 16:48:51 +02:00
return len;
}
2015-11-14 14:56:00 -03:00
#endif
2016-04-06 16:48:51 +02:00
return read(stream->fd, s, len);
2015-09-17 20:48:06 +02:00
#endif
2016-04-06 16:55:17 +02:00
error:
return -1;
}
int filestream_flush(RFILE *stream)
{
#if defined(HAVE_BUFFERED_IO)
return fflush(stream->fp);
#else
return 0;
#endif
}
2016-03-24 04:23:17 +01:00
ssize_t filestream_write(RFILE *stream, const void *s, size_t len)
{
if (!stream)
2016-04-06 16:55:17 +02:00
goto error;
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2015-09-18 02:38:48 +02:00
return sceIoWrite(stream->fd, s, len);
2015-09-17 20:48:06 +02:00
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fwrite(s, 1, len, stream->fp);
#endif
2015-11-14 14:56:00 -03:00
#ifdef HAVE_MMAP
2016-04-06 16:48:51 +02:00
if (stream->hints & RFILE_HINT_MMAP)
2016-04-06 16:55:17 +02:00
goto error;
2015-11-14 14:56:00 -03:00
#endif
2016-04-06 16:48:51 +02:00
return write(stream->fd, s, len);
2015-09-17 20:48:06 +02:00
#endif
2016-04-06 16:55:17 +02:00
error:
return -1;
}
2016-04-07 03:23:01 +02:00
int filestream_putc(RFILE *stream, int c)
{
if (!stream)
return EOF;
#if defined(HAVE_BUFFERED_IO)
return fputc(c, stream->fp);
#else
2016-04-07 03:30:02 +02:00
/* unimplemented */
2016-04-07 03:23:01 +02:00
return EOF;
#endif
}
2017-09-06 23:17:00 +02:00
int filestream_vprintf(RFILE *stream, const char* format, va_list args)
{
static char buffer[8 * 1024];
int numChars = vsprintf(buffer, format, args);
if (numChars < 0)
return -1;
else if (numChars == 0)
return 0;
return filestream_write(stream, buffer, numChars);
}
int filestream_printf(RFILE *stream, const char* format, ...)
{
va_list vl;
int result;
va_start(vl, format);
result = filestream_vprintf(stream, format, vl);
va_end(vl);
return result;
}
int filestream_error(RFILE *stream)
{
2017-09-06 23:40:38 +02:00
#if defined(HAVE_BUFFERED_IO)
2017-09-06 23:17:00 +02:00
return ferror(stream->fp);
2017-09-06 23:40:38 +02:00
#else
/* stub */
return 0;
#endif
2017-09-06 23:17:00 +02:00
}
2016-03-24 04:23:17 +01:00
int filestream_close(RFILE *stream)
{
if (!stream)
2016-04-06 16:55:17 +02:00
goto error;
2017-09-30 07:52:00 +02:00
if (!string_is_empty(stream->ext))
2017-04-28 21:27:28 +02:00
free(stream->ext);
2016-08-05 01:47:05 +02:00
#if defined(PSP)
2015-09-18 02:38:48 +02:00
if (stream->fd > 0)
sceIoClose(stream->fd);
2015-09-17 20:48:06 +02:00
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
{
if (stream->fp)
fclose(stream->fp);
}
else
#endif
2015-11-14 14:56:00 -03:00
#ifdef HAVE_MMAP
if (stream->hints & RFILE_HINT_MMAP)
munmap(stream->mapped, stream->mapsize);
#endif
2016-04-06 17:03:17 +02:00
if (stream->fd > 0)
close(stream->fd);
2015-09-17 20:48:06 +02:00
#endif
if (stream->buf)
free(stream->buf);
free(stream);
return 0;
2016-04-06 16:55:17 +02:00
error:
return -1;
}
2015-09-17 20:24:49 +02:00
/**
2016-03-24 04:09:25 +01:00
* filestream_read_file:
* @path : path to file.
* @buf : buffer to allocate and read the contents of the
* file into. Needs to be freed manually.
*
* Read the contents of a file into @buf.
*
* Returns: number of items read, -1 on error.
*/
2016-03-24 04:09:25 +01:00
int filestream_read_file(const char *path, void **buf, ssize_t *len)
{
ssize_t ret = 0;
ssize_t content_buf_size = 0;
void *content_buf = NULL;
2016-03-24 04:23:17 +01:00
RFILE *file = filestream_open(path, RFILE_MODE_READ, -1);
if (!file)
{
2015-12-26 08:13:33 +01:00
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
goto error;
}
2016-03-24 04:23:17 +01:00
if (filestream_seek(file, 0, SEEK_END) != 0)
goto error;
2016-03-24 04:23:17 +01:00
content_buf_size = filestream_tell(file);
if (content_buf_size < 0)
goto error;
2016-03-24 04:23:17 +01:00
filestream_rewind(file);
content_buf = malloc(content_buf_size + 1);
if (!content_buf)
goto error;
2016-03-24 04:23:17 +01:00
ret = filestream_read(file, content_buf, content_buf_size);
if (ret < 0)
{
2015-12-26 08:13:33 +01:00
fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
goto error;
}
2016-03-24 04:23:17 +01:00
filestream_close(file);
*buf = content_buf;
/* Allow for easy reading of strings to be safe.
* Will only work with sane character formatting (Unix). */
2017-04-23 20:26:31 +02:00
((char*)content_buf)[ret] = '\0';
if (len)
*len = ret;
return 1;
error:
if (file)
2016-03-24 04:23:17 +01:00
filestream_close(file);
if (content_buf)
free(content_buf);
if (len)
*len = -1;
*buf = NULL;
return 0;
}
2015-09-19 00:34:24 +02:00
/**
2016-03-24 04:09:25 +01:00
* filestream_write_file:
2015-09-19 00:34:24 +02:00
* @path : path to file.
* @data : contents to write to the file.
* @size : size of the contents.
*
* Writes data to a file.
*
* Returns: true (1) on success, false (0) otherwise.
*/
2016-03-24 04:09:25 +01:00
bool filestream_write_file(const char *path, const void *data, ssize_t size)
2015-09-19 00:34:24 +02:00
{
ssize_t ret = 0;
2016-03-24 04:23:17 +01:00
RFILE *file = filestream_open(path, RFILE_MODE_WRITE, -1);
2015-09-19 00:34:24 +02:00
if (!file)
return false;
2016-03-24 04:23:17 +01:00
ret = filestream_write(file, data, size);
filestream_close(file);
2015-09-19 00:34:24 +02:00
2016-04-06 16:55:17 +02:00
if (ret != size)
return false;
return true;
2015-09-19 00:34:24 +02:00
}