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
|
2014-09-15 16:46:07 +00:00
|
|
|
* Copyright (C) 2011-2014 - 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/>.
|
|
|
|
*/
|
|
|
|
|
2013-11-06 13:21:12 +00:00
|
|
|
#include "file_path.h"
|
2012-04-13 19:29:11 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "boolean.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2013-10-01 09:57:07 +00:00
|
|
|
#include <errno.h>
|
2012-04-13 19:29:11 +00:00
|
|
|
#include "compat/strl.h"
|
2012-06-18 18:37:35 +00:00
|
|
|
#include "compat/posix_string.h"
|
2013-11-06 13:21:12 +00:00
|
|
|
#include "miscellaneous.h"
|
2012-04-13 19:29:11 +00:00
|
|
|
|
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
|
2013-12-26 21:51:17 +00:00
|
|
|
#include "msvc/msvc_compat.h"
|
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
|
|
|
|
|
2014-09-02 15:05:15 +00:00
|
|
|
#ifdef HAVE_7ZIP
|
|
|
|
#include "decompress/7zip_support.h"
|
|
|
|
#endif
|
2014-09-09 14:00:38 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
#include "decompress/zip_support.h"
|
|
|
|
#endif
|
2014-09-02 15:05:15 +00:00
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
/* Dump to file. */
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:05:15 +00:00
|
|
|
/* Generic compressed file loader. */
|
|
|
|
#ifdef HAVE_COMPRESSION
|
2014-09-12 17:06:36 +00:00
|
|
|
long read_compressed_file(const char * path, void **buf)
|
2014-09-02 15:05:15 +00:00
|
|
|
{
|
2014-09-12 17:06:36 +00:00
|
|
|
//We split carchive path and relative path:
|
|
|
|
char archive_path[PATH_MAX];
|
|
|
|
strlcpy(archive_path,path,sizeof(archive_path));
|
|
|
|
char* archive_found = 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;
|
|
|
|
|
|
|
|
|
2014-09-09 14:00:38 +00:00
|
|
|
const char* file_ext = path_get_extension(archive_path);
|
2014-09-02 15:05:15 +00:00
|
|
|
#ifdef HAVE_7ZIP
|
2014-09-12 15:05:05 +00:00
|
|
|
if (strcasecmp(file_ext,"7z") == 0)
|
2014-09-12 17:06:36 +00:00
|
|
|
return read_7zip_file(archive_path,archive_found,buf);
|
2014-09-09 14:00:38 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ZLIB
|
2014-09-12 15:05:05 +00:00
|
|
|
if (strcasecmp(file_ext,"zip") == 0)
|
2014-09-12 17:06:36 +00:00
|
|
|
return read_zip_file(archive_path,archive_found,buf);
|
2014-09-02 15:05:15 +00:00
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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);
|
|
|
|
free(rom_buf);
|
|
|
|
*buf = NULL;
|
|
|
|
return -1;
|
2014-09-12 17:06:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generic file loader. */
|
|
|
|
long read_file(const char *path, void **buf)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
* */
|
|
|
|
#ifdef HAVE_COMPRESSION
|
|
|
|
if (path_contains_compressed_file(path))
|
|
|
|
return read_compressed_file(path,buf);
|
|
|
|
#endif
|
|
|
|
return read_generic_file(path,buf);
|
2013-10-14 19:08:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
/* Reads file content as one string. */
|
2013-10-14 19:08:41 +00:00
|
|
|
bool read_file_string(const char *path, char **buf)
|
|
|
|
{
|
|
|
|
*buf = NULL;
|
|
|
|
FILE *file = fopen(path, "r");
|
2013-12-31 21:54:11 +00:00
|
|
|
long len = 0;
|
2013-10-14 19:08:41 +00:00
|
|
|
char *ptr = NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-27 12:31:16 +00:00
|
|
|
const char *path_get_extension(const char *path)
|
2012-06-17 22:34:47 +00:00
|
|
|
{
|
2013-10-06 10:16:44 +00:00
|
|
|
const char *ext = strrchr(path_basename(path), '.');
|
2012-06-17 22:34:47 +00:00
|
|
|
if (ext)
|
|
|
|
return ext + 1;
|
2014-08-27 00:06:39 +00:00
|
|
|
return "";
|
2012-06-17 22:34:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 18:34:36 +00:00
|
|
|
char *path_remove_extension(char *path)
|
|
|
|
{
|
2013-11-01 16:16:02 +00:00
|
|
|
char *last = (char*)strrchr(path_basename(path), '.');
|
2013-09-15 18:34:36 +00:00
|
|
|
if (*last)
|
|
|
|
*last = '\0';
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2014-09-06 18:49:15 +00:00
|
|
|
struct string_list *compressed_file_list_new(const char *path,
|
|
|
|
const char* ext)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_COMPRESSION
|
|
|
|
const char* file_ext = path_get_extension(path);
|
|
|
|
#ifdef HAVE_7ZIP
|
2014-09-12 15:05:05 +00:00
|
|
|
if (strcasecmp(file_ext,"7z") == 0)
|
2014-09-06 18:49:15 +00:00
|
|
|
return compressed_7zip_file_list_new(path,ext);
|
|
|
|
#endif
|
2014-09-09 14:00:38 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2014-09-12 15:05:05 +00:00
|
|
|
if (strcasecmp(file_ext,"zip") == 0)
|
2014-09-09 14:00:38 +00:00
|
|
|
return compressed_zip_file_list_new(path,ext);
|
|
|
|
#endif
|
2014-09-06 18:49:15 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-17 11:59:52 +00:00
|
|
|
static bool path_char_is_slash(char c)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return (c == '/') || (c == '\\');
|
|
|
|
#else
|
2014-08-27 00:06:39 +00:00
|
|
|
return (c == '/');
|
2013-03-17 11:59:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *path_default_slash(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return "\\";
|
|
|
|
#else
|
|
|
|
return "/";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-12 15:05:05 +00:00
|
|
|
bool path_contains_compressed_file(const char *path)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Currently we only check for hash symbol inside the pathname.
|
|
|
|
* If path is ever expanded to a general URI, we should check for that here.
|
|
|
|
*/
|
|
|
|
return (strchr(path,'#') != NULL);
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:42:49 +00:00
|
|
|
bool path_is_compressed_file(const char* path)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_COMPRESSION
|
|
|
|
const char* file_ext = path_get_extension(path);
|
|
|
|
#ifdef HAVE_7ZIP
|
|
|
|
if (strcmp(file_ext,"7z") == 0)
|
|
|
|
return true;
|
|
|
|
#endif
|
2014-09-09 14:00:38 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (strcmp(file_ext,"zip") == 0)
|
|
|
|
return true;
|
|
|
|
#endif
|
2014-09-04 19:42:49 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-17 11:59:52 +00:00
|
|
|
|
2012-04-13 19:29:11 +00:00
|
|
|
bool path_is_directory(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD ret = GetFileAttributes(path);
|
|
|
|
return (ret & FILE_ATTRIBUTE_DIRECTORY) && (ret != INVALID_FILE_ATTRIBUTES);
|
|
|
|
#else
|
|
|
|
struct stat buf;
|
|
|
|
if (stat(path, &buf) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return S_ISDIR(buf.st_mode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool path_file_exists(const char *path)
|
|
|
|
{
|
|
|
|
FILE *dummy = fopen(path, "rb");
|
2014-08-02 10:01:11 +00:00
|
|
|
|
2012-04-13 19:29:11 +00:00
|
|
|
if (dummy)
|
|
|
|
{
|
|
|
|
fclose(dummy);
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-02 10:01:11 +00:00
|
|
|
|
2012-04-13 19:29:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname(char *out_path, const char *in_path,
|
|
|
|
const char *replace, size_t size)
|
2012-04-13 19:29:11 +00:00
|
|
|
{
|
|
|
|
char tmp_path[PATH_MAX];
|
2014-08-02 10:01:11 +00:00
|
|
|
char *tok;
|
2012-04-13 19:29:11 +00:00
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
rarch_assert(strlcpy(tmp_path, in_path,
|
|
|
|
sizeof(tmp_path)) < sizeof(tmp_path));
|
2014-08-02 10:01:11 +00:00
|
|
|
if ((tok = (char*)strrchr(path_basename(tmp_path), '.')))
|
2012-04-13 19:29:11 +00:00
|
|
|
*tok = '\0';
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
rarch_assert(strlcpy(out_path, tmp_path, size) < size);
|
|
|
|
rarch_assert(strlcat(out_path, replace, size) < size);
|
2012-04-13 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_noext(char *out_path, const char *in_path,
|
|
|
|
const char *replace, size_t size)
|
2012-04-13 19:29:11 +00:00
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
rarch_assert(strlcpy(out_path, in_path, size) < size);
|
|
|
|
rarch_assert(strlcat(out_path, replace, size) < size);
|
2012-04-13 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-12 00:55:25 +00:00
|
|
|
static char *find_last_slash(const char *str)
|
|
|
|
{
|
|
|
|
const char *slash = strrchr(str, '/');
|
|
|
|
#ifdef _WIN32
|
|
|
|
const char *backslash = strrchr(str, '\\');
|
2014-08-02 10:01:11 +00:00
|
|
|
|
2013-01-12 00:55:25 +00:00
|
|
|
if (backslash && ((slash && backslash > slash) || !slash))
|
|
|
|
slash = backslash;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (char*)slash;
|
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
/* Assumes path is a directory. Appends a slash
|
|
|
|
* if not already there. */
|
2014-05-10 03:25:50 +00:00
|
|
|
void fill_pathname_slash(char *path, size_t size)
|
2012-04-13 19:29:11 +00:00
|
|
|
{
|
2013-01-12 22:34:24 +00:00
|
|
|
size_t path_len = strlen(path);
|
|
|
|
const char *last_slash = find_last_slash(path);
|
2012-04-13 19:29:11 +00:00
|
|
|
|
2013-01-12 22:34:24 +00:00
|
|
|
// Try to preserve slash type.
|
|
|
|
if (last_slash && (last_slash != (path + path_len - 1)))
|
|
|
|
{
|
|
|
|
char join_str[2] = {*last_slash};
|
|
|
|
rarch_assert(strlcat(path, join_str, size) < size);
|
|
|
|
}
|
|
|
|
else if (!last_slash)
|
2013-03-17 11:59:52 +00:00
|
|
|
rarch_assert(strlcat(path, path_default_slash(), size) < size);
|
2013-01-12 22:34:24 +00:00
|
|
|
}
|
2012-04-13 19:29:11 +00:00
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_dir(char *in_dir, const char *in_basename,
|
|
|
|
const char *replace, size_t size)
|
2013-01-12 22:34:24 +00:00
|
|
|
{
|
|
|
|
fill_pathname_slash(in_dir, size);
|
|
|
|
const char *base = path_basename(in_basename);
|
2012-04-21 21:25:32 +00:00
|
|
|
rarch_assert(strlcat(in_dir, base, size) < size);
|
|
|
|
rarch_assert(strlcat(in_dir, replace, size) < size);
|
2012-04-13 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 20:54:07 +00:00
|
|
|
void fill_pathname_base(char *out, const char *in_path, size_t size)
|
2012-04-13 19:29:11 +00:00
|
|
|
{
|
2013-01-12 00:55:25 +00:00
|
|
|
const char *ptr = find_last_slash(in_path);
|
2012-04-13 19:29:11 +00:00
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
ptr++;
|
|
|
|
else
|
|
|
|
ptr = in_path;
|
|
|
|
|
2012-10-11 20:54:07 +00:00
|
|
|
rarch_assert(strlcpy(out, ptr, size) < size);
|
2012-04-13 19:29:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_basedir(char *out_dir,
|
|
|
|
const char *in_path, size_t size)
|
2012-07-28 18:43:34 +00:00
|
|
|
{
|
2012-07-29 07:23:04 +00:00
|
|
|
rarch_assert(strlcpy(out_dir, in_path, size) < size);
|
2012-11-18 16:08:28 +00:00
|
|
|
path_basedir(out_dir);
|
|
|
|
}
|
2012-07-28 18:43:34 +00:00
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_parent_dir(char *out_dir,
|
|
|
|
const char *in_dir, size_t size)
|
2012-11-25 15:22:53 +00:00
|
|
|
{
|
|
|
|
rarch_assert(strlcpy(out_dir, in_dir, size) < size);
|
|
|
|
path_parent_dir(out_dir);
|
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_dated_filename(char *out_filename,
|
|
|
|
const char *ext, size_t size)
|
2013-02-17 02:49:59 +00:00
|
|
|
{
|
|
|
|
time_t cur_time;
|
|
|
|
time(&cur_time);
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
strftime(out_filename, size,
|
|
|
|
"RetroArch-%m%d-%H%M%S.", localtime(&cur_time));
|
2013-02-17 09:20:10 +00:00
|
|
|
strlcat(out_filename, ext, size);
|
2013-02-17 02:49:59 +00:00
|
|
|
}
|
|
|
|
|
2012-11-18 16:08:28 +00:00
|
|
|
void path_basedir(char *path)
|
|
|
|
{
|
|
|
|
if (strlen(path) < 2)
|
|
|
|
return;
|
2012-07-28 18:43:34 +00:00
|
|
|
|
2013-01-12 00:55:25 +00:00
|
|
|
char *last = find_last_slash(path);
|
2012-11-18 16:08:28 +00:00
|
|
|
|
|
|
|
if (last)
|
|
|
|
last[1] = '\0';
|
|
|
|
else
|
2013-03-17 11:59:52 +00:00
|
|
|
snprintf(path, 3, ".%s", path_default_slash());
|
2012-11-18 16:08:28 +00:00
|
|
|
}
|
|
|
|
|
2012-11-25 15:22:53 +00:00
|
|
|
void path_parent_dir(char *path)
|
|
|
|
{
|
|
|
|
size_t len = strlen(path);
|
2013-03-17 11:59:52 +00:00
|
|
|
if (len && path_char_is_slash(path[len - 1]))
|
2012-11-25 15:22:53 +00:00
|
|
|
path[len - 1] = '\0';
|
|
|
|
path_basedir(path);
|
|
|
|
}
|
|
|
|
|
2012-11-18 16:08:28 +00:00
|
|
|
const char *path_basename(const char *path)
|
|
|
|
{
|
2013-01-12 00:55:25 +00:00
|
|
|
const char *last = find_last_slash(path);
|
2012-11-18 16:08:28 +00:00
|
|
|
|
|
|
|
if (last)
|
|
|
|
return last + 1;
|
2014-08-27 00:06:39 +00:00
|
|
|
return path;
|
2012-11-18 16:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool path_is_absolute(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-09-13 00:36:13 +00:00
|
|
|
/* Many roads lead to Rome ... */
|
2014-09-02 03:57:53 +00:00
|
|
|
return path[0] == '/' || (strstr(path, "\\\\") == path)
|
|
|
|
|| strstr(path, ":/") || strstr(path, ":\\") || strstr(path, ":\\\\");
|
2012-11-18 16:08:28 +00:00
|
|
|
#else
|
|
|
|
return path[0] == '/';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-13 21:25:37 +00:00
|
|
|
void path_resolve_realpath(char *buf, size_t size)
|
|
|
|
{
|
|
|
|
#ifndef RARCH_CONSOLE
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
strlcpy(tmp, buf, sizeof(tmp));
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (!_fullpath(buf, tmp, size))
|
|
|
|
strlcpy(buf, tmp, size);
|
|
|
|
#else
|
|
|
|
rarch_assert(size >= PATH_MAX);
|
2014-09-02 03:57:53 +00:00
|
|
|
|
|
|
|
/* NOTE: realpath() expects at least PATH_MAX bytes in buf.
|
|
|
|
* Technically, PATH_MAX needn't be defined, but we rely on it anyways.
|
|
|
|
* POSIX 2008 can automatically allocate for you,
|
|
|
|
* but don't rely on that. */
|
2013-05-13 21:25:37 +00:00
|
|
|
if (!realpath(tmp, buf))
|
|
|
|
strlcpy(buf, tmp, size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
(void)buf;
|
|
|
|
(void)size;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-10-01 09:57:07 +00:00
|
|
|
static bool path_mkdir_norecurse(const char *dir)
|
|
|
|
{
|
2014-08-02 10:01:11 +00:00
|
|
|
int ret;
|
2013-11-22 00:56:54 +00:00
|
|
|
#if defined(_WIN32)
|
2014-08-02 10:01:11 +00:00
|
|
|
ret = _mkdir(dir);
|
2013-11-22 00:56:54 +00:00
|
|
|
#elif defined(IOS)
|
2014-08-02 10:01:11 +00:00
|
|
|
ret = mkdir(dir, 0755);
|
2013-10-01 09:57:07 +00:00
|
|
|
#else
|
2014-08-02 10:01:11 +00:00
|
|
|
ret = mkdir(dir, 0750);
|
2013-10-01 09:57:07 +00:00
|
|
|
#endif
|
2014-09-02 03:57:53 +00:00
|
|
|
/* Don't treat this as an error. */
|
|
|
|
if (ret < 0 && errno == EEXIST && path_is_directory(dir))
|
2013-10-01 09:57:07 +00:00
|
|
|
ret = 0;
|
|
|
|
if (ret < 0)
|
|
|
|
RARCH_ERR("mkdir(%s) error: %s.\n", dir, strerror(errno));
|
|
|
|
return ret == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool path_mkdir(const char *dir)
|
|
|
|
{
|
|
|
|
const char *target = NULL;
|
2014-09-02 03:57:53 +00:00
|
|
|
/* Use heap. Real chance of stack overflow if we recurse too hard. */
|
|
|
|
char *basedir = strdup(dir);
|
2013-10-01 09:57:07 +00:00
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (!basedir)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
path_parent_dir(basedir);
|
|
|
|
if (!*basedir || !strcmp(basedir, dir))
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path_is_directory(basedir))
|
|
|
|
{
|
|
|
|
target = dir;
|
|
|
|
ret = path_mkdir_norecurse(dir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target = basedir;
|
|
|
|
ret = path_mkdir(basedir);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
target = dir;
|
|
|
|
ret = path_mkdir_norecurse(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (target && !ret)
|
|
|
|
RARCH_ERR("Failed to create directory: \"%s\".\n", target);
|
|
|
|
free(basedir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_resolve_relative(char *out_path,
|
|
|
|
const char *in_refpath, const char *in_path, size_t size)
|
2012-11-18 16:08:28 +00:00
|
|
|
{
|
|
|
|
if (path_is_absolute(in_path))
|
|
|
|
rarch_assert(strlcpy(out_path, in_path, size) < size);
|
|
|
|
else
|
2012-07-29 07:23:04 +00:00
|
|
|
{
|
2012-11-18 16:08:28 +00:00
|
|
|
rarch_assert(strlcpy(out_path, in_refpath, size) < size);
|
|
|
|
path_basedir(out_path);
|
|
|
|
rarch_assert(strlcat(out_path, in_path, size) < size);
|
2012-07-29 07:23:04 +00:00
|
|
|
}
|
2012-07-28 18:43:34 +00:00
|
|
|
}
|
2012-07-28 20:36:49 +00:00
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_join(char *out_path,
|
|
|
|
const char *dir, const char *path, size_t size)
|
2012-11-25 15:03:16 +00:00
|
|
|
{
|
2013-01-12 22:34:24 +00:00
|
|
|
rarch_assert(strlcpy(out_path, dir, size) < size);
|
2012-11-25 15:03:16 +00:00
|
|
|
|
2013-01-12 22:34:24 +00:00
|
|
|
if (*out_path)
|
|
|
|
fill_pathname_slash(out_path, size);
|
2012-11-25 15:03:16 +00:00
|
|
|
|
|
|
|
rarch_assert(strlcat(out_path, path, size) < size);
|
|
|
|
}
|
|
|
|
|
2014-09-12 15:46:40 +00:00
|
|
|
void fill_pathname_join_delim(char *out_path, const char *dir,
|
|
|
|
const char *path, const char delim, size_t size)
|
|
|
|
{
|
|
|
|
size_t copied = strlcpy(out_path, dir, size);
|
|
|
|
rarch_assert(copied < size+1);
|
|
|
|
|
|
|
|
out_path[copied]=delim;
|
|
|
|
out_path[copied+1] = '\0';
|
|
|
|
|
|
|
|
rarch_assert(strlcat(out_path, path, size) < size);
|
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_expand_special(char *out_path,
|
|
|
|
const char *in_path, size_t size)
|
2013-12-24 17:23:21 +00:00
|
|
|
{
|
|
|
|
#if !defined(RARCH_CONSOLE)
|
|
|
|
if (*in_path == '~')
|
|
|
|
{
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
if (home)
|
|
|
|
{
|
|
|
|
size_t src_size = strlcpy(out_path, home, size);
|
|
|
|
rarch_assert(src_size < size);
|
|
|
|
|
|
|
|
out_path += src_size;
|
|
|
|
size -= src_size;
|
|
|
|
in_path++;
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 22:02:45 +00:00
|
|
|
else if ((in_path[0] == ':') &&
|
2013-12-24 17:23:21 +00:00
|
|
|
#ifdef _WIN32
|
2014-09-13 00:36:13 +00:00
|
|
|
((in_path[1] == '/') || (in_path[1] == '\\'))
|
2013-12-24 17:23:21 +00:00
|
|
|
#else
|
2014-09-13 00:36:13 +00:00
|
|
|
(in_path[1] == '/')
|
2013-12-24 17:23:21 +00:00
|
|
|
#endif
|
2014-09-13 00:36:13 +00:00
|
|
|
)
|
2013-12-24 17:23:21 +00:00
|
|
|
{
|
|
|
|
char application_dir[PATH_MAX];
|
|
|
|
fill_pathname_application_path(application_dir, sizeof(application_dir));
|
|
|
|
path_basedir(application_dir);
|
|
|
|
|
|
|
|
size_t src_size = strlcpy(out_path, application_dir, size);
|
|
|
|
rarch_assert(src_size < size);
|
|
|
|
|
|
|
|
out_path += src_size;
|
|
|
|
size -= src_size;
|
|
|
|
in_path += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rarch_assert(strlcpy(out_path, in_path, size) < size);
|
|
|
|
}
|
|
|
|
|
2014-09-12 15:05:05 +00:00
|
|
|
void fill_short_pathname_representation(char* out_rep,
|
|
|
|
const char *in_path, size_t size)
|
|
|
|
{
|
|
|
|
char path_short[PATH_MAX];
|
|
|
|
fill_pathname(path_short, path_basename(in_path), "",
|
|
|
|
sizeof(path_short));
|
|
|
|
|
|
|
|
char* last_hash = strchr(path_short,'#');
|
|
|
|
/* We handle paths like:
|
|
|
|
* /path/to/file.7z#mygame.img
|
|
|
|
* short_name: mygame.img:
|
|
|
|
*/
|
|
|
|
if(last_hash != NULL)
|
|
|
|
{
|
2014-09-13 00:36:13 +00:00
|
|
|
/* We check whether something is actually
|
|
|
|
* after the hash to avoid going over the buffer.
|
2014-09-12 15:05:05 +00:00
|
|
|
*/
|
|
|
|
rarch_assert(strlen(last_hash) > 1);
|
|
|
|
strlcpy(out_rep,last_hash + 1, size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strlcpy(out_rep,path_short, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
void fill_pathname_abbreviate_special(char *out_path,
|
|
|
|
const char *in_path, size_t size)
|
2013-12-24 17:23:21 +00:00
|
|
|
{
|
|
|
|
#if !defined(RARCH_CONSOLE)
|
2013-12-24 22:02:45 +00:00
|
|
|
unsigned i;
|
2013-12-24 17:23:21 +00:00
|
|
|
|
2013-12-24 22:02:45 +00:00
|
|
|
const char *home = getenv("HOME");
|
2013-12-24 17:23:21 +00:00
|
|
|
char application_dir[PATH_MAX];
|
|
|
|
fill_pathname_application_path(application_dir, sizeof(application_dir));
|
|
|
|
path_basedir(application_dir);
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
/* application_dir could be zero-string. Safeguard against this.
|
|
|
|
*
|
|
|
|
* Keep application dir in front of home, moving app dir to a
|
|
|
|
* new location inside home would break otherwise. */
|
2013-12-24 22:36:47 +00:00
|
|
|
|
2013-12-24 22:02:45 +00:00
|
|
|
const char *candidates[3] = { application_dir, home, NULL };
|
|
|
|
const char *notations[3] = { ":", "~", NULL };
|
2013-12-24 17:23:21 +00:00
|
|
|
|
2013-12-24 22:02:45 +00:00
|
|
|
for (i = 0; candidates[i]; i++)
|
2013-12-24 17:23:21 +00:00
|
|
|
{
|
2013-12-24 22:36:47 +00:00
|
|
|
if (*candidates[i] && strstr(in_path, candidates[i]) == in_path)
|
2013-12-24 17:23:21 +00:00
|
|
|
{
|
|
|
|
size_t src_size = strlcpy(out_path, notations[i], size);
|
|
|
|
rarch_assert(src_size < size);
|
|
|
|
|
|
|
|
out_path += src_size;
|
|
|
|
size -= src_size;
|
|
|
|
in_path += strlen(candidates[i]);
|
|
|
|
|
|
|
|
if (!path_char_is_slash(*in_path))
|
|
|
|
{
|
|
|
|
rarch_assert(strlcpy(out_path, path_default_slash(), size) < size);
|
2013-12-24 22:02:45 +00:00
|
|
|
out_path++;
|
|
|
|
size--;
|
2013-12-24 22:40:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 03:57:53 +00:00
|
|
|
break; /* Don't allow more abbrevs to take place. */
|
2013-12-24 17:23:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rarch_assert(strlcpy(out_path, in_path, size) < size);
|
|
|
|
}
|
|
|
|
|
2013-03-17 11:27:33 +00:00
|
|
|
#ifndef RARCH_CONSOLE
|
|
|
|
void fill_pathname_application_path(char *buf, size_t size)
|
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2013-12-25 00:28:55 +00:00
|
|
|
(void)i;
|
2013-03-17 11:27:33 +00:00
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD ret = GetModuleFileName(GetModuleHandle(NULL), buf, size - 1);
|
|
|
|
buf[ret] = '\0';
|
2013-12-24 17:23:21 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
CFBundleRef bundle = CFBundleGetMainBundle();
|
|
|
|
if (bundle)
|
|
|
|
{
|
|
|
|
CFURLRef bundle_url = CFBundleCopyBundleURL(bundle);
|
|
|
|
CFStringRef bundle_path = CFURLCopyPath(bundle_url);
|
|
|
|
CFStringGetCString(bundle_path, buf, size, kCFStringEncodingUTF8);
|
|
|
|
CFRelease(bundle_path);
|
|
|
|
CFRelease(bundle_url);
|
|
|
|
|
|
|
|
rarch_assert(strlcat(buf, "nobin", size) < size);
|
|
|
|
return;
|
|
|
|
}
|
2014-08-28 16:45:12 +00:00
|
|
|
#elif defined(__HAIKU__)
|
|
|
|
image_info info;
|
|
|
|
int32 cookie = 0;
|
|
|
|
|
|
|
|
while (get_next_image_info(0, &cookie, &info) == B_OK)
|
|
|
|
{
|
|
|
|
if (info.type == B_APP_IMAGE)
|
|
|
|
{
|
|
|
|
strlcpy(buf, info.name, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-03-17 11:27:33 +00:00
|
|
|
#else
|
|
|
|
*buf = '\0';
|
|
|
|
pid_t pid = getpid();
|
|
|
|
char link_path[PATH_MAX];
|
2014-09-02 03:57:53 +00:00
|
|
|
/* Linux, BSD and Solaris paths. Not standardized. */
|
|
|
|
static const char *exts[] = { "exe", "file", "path/a.out" };
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(exts); i++)
|
2013-03-17 11:27:33 +00:00
|
|
|
{
|
2014-09-02 03:57:53 +00:00
|
|
|
snprintf(link_path, sizeof(link_path), "/proc/%u/%s",
|
|
|
|
(unsigned)pid, exts[i]);
|
2013-03-17 11:27:33 +00:00
|
|
|
ssize_t ret = readlink(link_path, buf, size - 1);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
buf[ret] = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RARCH_ERR("Cannot resolve application path! This should not happen.\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|