2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2015-01-07 16:46:50 +00:00
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
2012-04-13 19:29:11 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2012-04-13 19:29:11 +00:00
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2012-04-13 19:29:11 +00:00
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2012-04-21 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2012-04-13 19:29:11 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-10-21 21:53:09 +00:00
|
|
|
#include "file_ops.h"
|
2014-10-21 22:23:06 +00:00
|
|
|
#include <file/file_path.h>
|
2012-04-13 19:29:11 +00:00
|
|
|
#include <stdlib.h>
|
2014-10-21 03:05:52 +00:00
|
|
|
#include <boolean.h>
|
2012-04-13 19:29:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2013-10-01 09:57:07 +00:00
|
|
|
#include <errno.h>
|
2014-10-21 05:58:58 +00:00
|
|
|
#include <compat/strl.h>
|
|
|
|
#include <compat/posix_string.h>
|
2014-10-21 06:39:37 +00:00
|
|
|
#include <retro_miscellaneous.h>
|
2012-04-13 19:29:11 +00:00
|
|
|
|
2015-01-11 15:38:20 +00:00
|
|
|
#ifdef HAVE_COMPRESSION
|
|
|
|
#include "file_extract.h"
|
|
|
|
#endif
|
|
|
|
|
2014-08-28 16:45:12 +00:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <kernel/image.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-20 00:48:11 +00:00
|
|
|
#if (defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)) || defined(__QNX__) || defined(PSP)
|
2014-09-02 03:57:53 +00:00
|
|
|
#include <unistd.h> /* stat() is defined here */
|
2012-04-13 19:29:11 +00:00
|
|
|
#endif
|
2013-05-01 02:03:40 +00:00
|
|
|
|
|
|
|
#if defined(__CELLOS_LV2__)
|
|
|
|
|
|
|
|
#ifndef S_ISDIR
|
|
|
|
#define S_ISDIR(x) (x & 0040000)
|
|
|
|
#endif
|
|
|
|
|
2013-04-11 17:05:28 +00:00
|
|
|
#endif
|
2012-04-13 19:29:11 +00:00
|
|
|
|
2013-04-11 20:35:15 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define setmode _setmode
|
|
|
|
#endif
|
2012-06-24 21:14:49 +00:00
|
|
|
#ifdef _XBOX
|
2012-06-24 20:54:43 +00:00
|
|
|
#include <xtl.h>
|
2013-04-11 20:35:15 +00:00
|
|
|
#define INVALID_FILE_ATTRIBUTES -1
|
|
|
|
#else
|
2012-04-13 19:29:11 +00:00
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
2013-10-01 09:57:07 +00:00
|
|
|
#include <direct.h>
|
2012-04-13 19:29:11 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
2013-03-17 11:27:33 +00:00
|
|
|
#include <unistd.h>
|
2012-04-13 19:29:11 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-08 14:08:44 +00:00
|
|
|
/**
|
|
|
|
* write_file:
|
|
|
|
* @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.
|
|
|
|
*/
|
2013-10-14 19:08:41 +00:00
|
|
|
bool write_file(const char *path, const void *data, size_t size)
|
|
|
|
{
|
2014-08-27 00:06:39 +00:00
|
|
|
bool ret = false;
|
2013-10-14 19:08:41 +00:00
|
|
|
FILE *file = fopen(path, "wb");
|
|
|
|
if (!file)
|
|
|
|
return false;
|
2014-08-27 00:06:39 +00:00
|
|
|
|
|
|
|
ret = fwrite(data, 1, size, file) == size;
|
|
|
|
fclose(file);
|
|
|
|
return ret;
|
2013-10-14 19:08:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 14:08:44 +00:00
|
|
|
/**
|
|
|
|
* write_empty_file:
|
|
|
|
* @path : path to file.
|
|
|
|
*
|
|
|
|
* Creates an empty file.
|
|
|
|
*
|
|
|
|
* Returns: true (1) on success, false (0) otherwise.
|
|
|
|
*/
|
2014-09-15 03:49:48 +00:00
|
|
|
bool write_empty_file(const char *path)
|
|
|
|
{
|
|
|
|
FILE *file = fopen(path, "w");
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-08 14:08:44 +00:00
|
|
|
/**
|
|
|
|
* read_generic_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.
|
|
|
|
*/
|
2014-09-12 17:06:36 +00:00
|
|
|
static long read_generic_file(const char *path, void **buf)
|
2013-10-14 19:08:41 +00:00
|
|
|
{
|
2013-12-31 21:54:11 +00:00
|
|
|
long rc = 0, len = 0;
|
2013-10-14 19:08:41 +00:00
|
|
|
void *rom_buf = NULL;
|
2014-09-12 17:06:36 +00:00
|
|
|
|
2013-10-14 19:08:41 +00:00
|
|
|
FILE *file = fopen(path, "rb");
|
2013-12-31 21:54:11 +00:00
|
|
|
|
2013-10-14 19:08:41 +00:00
|
|
|
if (!file)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
len = ftell(file);
|
|
|
|
rewind(file);
|
|
|
|
rom_buf = malloc(len + 1);
|
|
|
|
if (!rom_buf)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Couldn't allocate memory.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-12-31 21:54:11 +00:00
|
|
|
if ((rc = fread(rom_buf, 1, len, file)) < len)
|
2013-10-14 19:08:41 +00:00
|
|
|
RARCH_WARN("Didn't read whole file.\n");
|
|
|
|
|
|
|
|
*buf = rom_buf;
|
2014-09-02 03:57:53 +00:00
|
|
|
|
|
|
|
/* Allow for easy reading of strings to be safe.
|
|
|
|
* Will only work with sane character formatting (Unix). */
|
2013-12-31 21:54:11 +00:00
|
|
|
((char*)rom_buf)[len] = '\0';
|
2013-10-14 19:08:41 +00:00
|
|
|
fclose(file);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
2015-01-08 14:08:44 +00:00
|
|
|
if (rom_buf)
|
|
|
|
free(rom_buf);
|
2013-10-14 19:08:41 +00:00
|
|
|
*buf = NULL;
|
|
|
|
return -1;
|
2014-09-12 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 15:38:20 +00:00
|
|
|
#ifdef HAVE_COMPRESSION
|
|
|
|
/* Generic compressed file loader.
|
|
|
|
* Extracts to buf, unless optional_filename != 0
|
|
|
|
* Then extracts to optional_filename and leaves buf alone.
|
|
|
|
*/
|
|
|
|
long read_compressed_file(const char * path, void **buf,
|
|
|
|
const char* optional_filename)
|
|
|
|
{
|
|
|
|
const char* file_ext;
|
|
|
|
char archive_path[PATH_MAX_LENGTH], *archive_found = NULL;
|
|
|
|
|
|
|
|
/* Safety check.
|
|
|
|
* If optional_filename and optional_filename exists, we simply return 0,
|
|
|
|
* hoping that optional_filename is the same as requested.
|
|
|
|
*/
|
|
|
|
if (optional_filename)
|
|
|
|
if(path_file_exists(optional_filename))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
//We split carchive path and relative path:
|
|
|
|
strlcpy(archive_path,path,sizeof(archive_path));
|
|
|
|
archive_found = (char*)strchr(archive_path,'#');
|
|
|
|
rarch_assert(archive_found != NULL);
|
|
|
|
|
|
|
|
//We assure that there is something after the '#' symbol
|
|
|
|
if (strlen(archive_found) <= 1)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This error condition happens for example, when
|
|
|
|
* path = /path/to/file.7z, or
|
|
|
|
* path = /path/to/file.7z#
|
|
|
|
*/
|
|
|
|
RARCH_ERR("Could not extract image path and carchive path from "
|
|
|
|
"path: %s.\n", path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We split the string in two, by putting a \0, where the hash was: */
|
|
|
|
*archive_found = '\0';
|
|
|
|
archive_found+=1;
|
|
|
|
|
|
|
|
file_ext = path_get_extension(archive_path);
|
|
|
|
#ifdef HAVE_7ZIP
|
|
|
|
if (strcasecmp(file_ext,"7z") == 0)
|
|
|
|
return read_7zip_file(archive_path,archive_found,buf,optional_filename);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (strcasecmp(file_ext,"zip") == 0)
|
|
|
|
return read_zip_file(archive_path,archive_found,buf,optional_filename);
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-08 14:08:44 +00:00
|
|
|
/**
|
|
|
|
* 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. Will call read_compressed_file
|
|
|
|
* if path contains a compressed file, otherwise will call read_generic_file.
|
|
|
|
*
|
|
|
|
* Returns: number of items read, -1 on error.
|
|
|
|
*/
|
2014-09-12 17:06:36 +00:00
|
|
|
long read_file(const char *path, void **buf)
|
|
|
|
{
|
2015-01-08 14:08:44 +00:00
|
|
|
#ifdef HAVE_COMPRESSION
|
2014-09-12 17:06:36 +00:00
|
|
|
/* Here we check, whether the file, we are about to read is
|
|
|
|
* inside an archive, or not.
|
|
|
|
*
|
|
|
|
* We determine, whether a file is inside a compressed archive,
|
|
|
|
* by checking for the # inside the URL.
|
|
|
|
*
|
|
|
|
* For example: fullpath: /home/user/game.7z/mygame.rom
|
|
|
|
* carchive_path: /home/user/game.7z
|
|
|
|
* */
|
|
|
|
if (path_contains_compressed_file(path))
|
2014-09-21 08:48:12 +00:00
|
|
|
return read_compressed_file(path,buf,0);
|
2014-09-12 17:06:36 +00:00
|
|
|
#endif
|
|
|
|
return read_generic_file(path,buf);
|
2013-10-14 19:08:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 14:08:44 +00:00
|
|
|
/**
|
|
|
|
* read_file_string:
|
|
|
|
* @path : path to file to be read from.
|
|
|
|
* @buf : buffer to allocate and read the contents of the
|
|
|
|
* file into. Needs to be freed manually.
|
|
|
|
*
|
|
|
|
* Reads file content as one string.
|
|
|
|
*
|
|
|
|
* Returns: true (1) on success, false (0) otherwise.
|
|
|
|
*/
|
2013-10-14 19:08:41 +00:00
|
|
|
bool read_file_string(const char *path, char **buf)
|
|
|
|
{
|
2013-12-31 21:54:11 +00:00
|
|
|
long len = 0;
|
2013-10-14 19:08:41 +00:00
|
|
|
char *ptr = NULL;
|
2015-01-08 14:08:44 +00:00
|
|
|
FILE *file = fopen(path, "r");
|
|
|
|
|
|
|
|
*buf = NULL;
|
2013-10-14 19:08:41 +00:00
|
|
|
|
|
|
|
if (!file)
|
|
|
|
goto error;
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
/* ftell with "r" can be troublesome ...
|
|
|
|
* Haven't run into issues yet though. */
|
2013-10-14 19:08:41 +00:00
|
|
|
fseek(file, 0, SEEK_END);
|
2014-09-02 03:57:53 +00:00
|
|
|
|
|
|
|
/* Takes account of being able to read
|
|
|
|
* in EOF and '\0' at end. */
|
|
|
|
len = ftell(file) + 2;
|
2013-10-14 19:08:41 +00:00
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
*buf = (char*)calloc(len, sizeof(char));
|
|
|
|
if (!*buf)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ptr = *buf;
|
|
|
|
|
|
|
|
while (ptr && !feof(file))
|
|
|
|
{
|
2014-09-02 03:57:53 +00:00
|
|
|
size_t bufsize = (size_t)(((ptrdiff_t)*buf +
|
|
|
|
(ptrdiff_t)len) - (ptrdiff_t)ptr);
|
2013-10-14 19:08:41 +00:00
|
|
|
fgets(ptr, bufsize, file);
|
|
|
|
|
|
|
|
ptr += strlen(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = strchr(ptr, EOF);
|
|
|
|
if (ptr)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
|
|
|
if (*buf)
|
|
|
|
free(*buf);
|
|
|
|
return false;
|
|
|
|
}
|