|
| 1 | +/* Copyright (C) 2010-2019 The RetroArch team |
| 2 | + * |
| 3 | + * --------------------------------------------------------------------------------------- |
| 4 | + * The following license statement only applies to this file (file_path_io.c). |
| 5 | + * --------------------------------------------------------------------------------------- |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, |
| 8 | + * to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 9 | + * to deal in the Software without restriction, including without limitation the rights to |
| 10 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 16 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdlib.h> |
| 25 | +#include <string.h> |
| 26 | +#include <time.h> |
| 27 | +#include <errno.h> |
| 28 | + |
| 29 | +#include <sys/stat.h> |
| 30 | + |
| 31 | +#include <boolean.h> |
| 32 | +#include <file/file_path.h> |
| 33 | +#include <retro_assert.h> |
| 34 | +#include <string/stdstring.h> |
| 35 | +#define VFS_FRONTEND |
| 36 | +#include <vfs/vfs_implementation.h> |
| 37 | + |
| 38 | +/* TODO: There are probably some unnecessary things on this huge include list now but I'm too afraid to touch it */ |
| 39 | +#ifdef __APPLE__ |
| 40 | +#include <CoreFoundation/CoreFoundation.h> |
| 41 | +#endif |
| 42 | +#ifdef __HAIKU__ |
| 43 | +#include <kernel/image.h> |
| 44 | +#endif |
| 45 | +#ifndef __MACH__ |
| 46 | +#include <compat/strl.h> |
| 47 | +#include <compat/posix_string.h> |
| 48 | +#endif |
| 49 | +#include <compat/strcasestr.h> |
| 50 | +#include <retro_miscellaneous.h> |
| 51 | +#include <encodings/utf.h> |
| 52 | + |
| 53 | +#if defined(_WIN32) |
| 54 | +#ifdef _MSC_VER |
| 55 | +#define setmode _setmode |
| 56 | +#endif |
| 57 | +#include <sys/stat.h> |
| 58 | +#ifdef _XBOX |
| 59 | +#include <xtl.h> |
| 60 | +#define INVALID_FILE_ATTRIBUTES -1 |
| 61 | +#else |
| 62 | +#include <io.h> |
| 63 | +#include <fcntl.h> |
| 64 | +#include <direct.h> |
| 65 | +#include <windows.h> |
| 66 | +#if defined(_MSC_VER) && _MSC_VER <= 1200 |
| 67 | +#define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| 68 | +#endif |
| 69 | +#endif |
| 70 | +#elif defined(VITA) |
| 71 | +#define SCE_ERROR_ERRNO_EEXIST 0x80010011 |
| 72 | +#include <psp2/io/fcntl.h> |
| 73 | +#include <psp2/io/dirent.h> |
| 74 | +#include <psp2/io/stat.h> |
| 75 | +#else |
| 76 | +#include <sys/types.h> |
| 77 | +#include <sys/stat.h> |
| 78 | +#include <unistd.h> |
| 79 | +#endif |
| 80 | + |
| 81 | +#if defined(PSP) |
| 82 | +#include <pspkernel.h> |
| 83 | +#endif |
| 84 | + |
| 85 | +#if defined(PS2) |
| 86 | +#include <fileXio_rpc.h> |
| 87 | +#include <fileXio.h> |
| 88 | +#endif |
| 89 | + |
| 90 | +#if defined(__CELLOS_LV2__) |
| 91 | +#include <cell/cell_fs.h> |
| 92 | +#endif |
| 93 | + |
| 94 | +#if defined(VITA) |
| 95 | +#define FIO_S_ISDIR SCE_S_ISDIR |
| 96 | +#endif |
| 97 | + |
| 98 | +#if (defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)) || defined(__QNX__) || defined(PSP) || defined(PS2) |
| 99 | +#include <unistd.h> /* stat() is defined here */ |
| 100 | +#endif |
| 101 | + |
| 102 | +#if !defined(RARCH_CONSOLE) && defined(RARCH_INTERNAL) |
| 103 | +#ifdef __WINRT__ |
| 104 | +#include <uwp/uwp_func.h> |
| 105 | +#endif |
| 106 | +#endif |
| 107 | + |
| 108 | +/* Assume W-functions do not work below Win2K and Xbox platforms */ |
| 109 | +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX) |
| 110 | + |
| 111 | +#ifndef LEGACY_WIN32 |
| 112 | +#define LEGACY_WIN32 |
| 113 | +#endif |
| 114 | + |
| 115 | +#endif |
| 116 | + |
| 117 | +static retro_vfs_stat_t path_stat_cb = retro_vfs_stat_impl; |
| 118 | +static retro_vfs_mkdir_t path_mkdir_cb = retro_vfs_mkdir_impl; |
| 119 | + |
| 120 | +void path_vfs_init(const struct retro_vfs_interface_info* vfs_info) |
| 121 | +{ |
| 122 | + const struct retro_vfs_interface* |
| 123 | + vfs_iface = vfs_info->iface; |
| 124 | + |
| 125 | + path_stat_cb = retro_vfs_stat_impl; |
| 126 | + path_mkdir_cb = retro_vfs_mkdir_impl; |
| 127 | + |
| 128 | + if (vfs_info->required_interface_version < PATH_REQUIRED_VFS_VERSION || !vfs_iface) |
| 129 | + return; |
| 130 | + |
| 131 | + path_stat_cb = vfs_iface->stat; |
| 132 | + path_mkdir_cb = vfs_iface->mkdir; |
| 133 | +} |
| 134 | + |
| 135 | +int path_stat(const char *path) |
| 136 | +{ |
| 137 | + return path_stat_cb(path, NULL); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * path_is_directory: |
| 142 | + * @path : path |
| 143 | + * |
| 144 | + * Checks if path is a directory. |
| 145 | + * |
| 146 | + * Returns: true (1) if path is a directory, otherwise false (0). |
| 147 | + */ |
| 148 | +bool path_is_directory(const char *path) |
| 149 | +{ |
| 150 | + return (path_stat_cb(path, NULL) & RETRO_VFS_STAT_IS_DIRECTORY) != 0; |
| 151 | +} |
| 152 | + |
| 153 | +bool path_is_character_special(const char *path) |
| 154 | +{ |
| 155 | + return (path_stat_cb(path, NULL) & RETRO_VFS_STAT_IS_CHARACTER_SPECIAL) != 0; |
| 156 | +} |
| 157 | + |
| 158 | +bool path_is_valid(const char *path) |
| 159 | +{ |
| 160 | + return (path_stat_cb(path, NULL) & RETRO_VFS_STAT_IS_VALID) != 0; |
| 161 | +} |
| 162 | + |
| 163 | +int32_t path_get_size(const char *path) |
| 164 | +{ |
| 165 | + int32_t filesize = 0; |
| 166 | + if (path_stat_cb(path, &filesize) != 0) |
| 167 | + return filesize; |
| 168 | + |
| 169 | + return -1; |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * path_mkdir: |
| 174 | + * @dir : directory |
| 175 | + * |
| 176 | + * Create directory on filesystem. |
| 177 | + * |
| 178 | + * Returns: true (1) if directory could be created, otherwise false (0). |
| 179 | + **/ |
| 180 | +bool path_mkdir(const char *dir) |
| 181 | +{ |
| 182 | + bool sret = false; |
| 183 | + bool norecurse = false; |
| 184 | + char *basedir = NULL; |
| 185 | + |
| 186 | + if (!(dir && *dir)) |
| 187 | + return false; |
| 188 | + |
| 189 | + /* Use heap. Real chance of stack |
| 190 | + * overflow if we recurse too hard. */ |
| 191 | + basedir = strdup(dir); |
| 192 | + |
| 193 | + if (!basedir) |
| 194 | + return false; |
| 195 | + |
| 196 | + path_parent_dir(basedir); |
| 197 | + |
| 198 | + if (!*basedir || !strcmp(basedir, dir)) |
| 199 | + { |
| 200 | + free(basedir); |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | +#if defined(GEKKO) |
| 205 | + { |
| 206 | + size_t len = strlen(basedir); |
| 207 | + |
| 208 | + /* path_parent_dir() keeps the trailing slash. |
| 209 | + * On Wii, mkdir() fails if the path has a |
| 210 | + * trailing slash... |
| 211 | + * We must therefore remove it. */ |
| 212 | + if (len > 0) |
| 213 | + if (basedir[len - 1] == '/') |
| 214 | + basedir[len - 1] = '\0'; |
| 215 | + } |
| 216 | +#endif |
| 217 | + |
| 218 | + if (path_is_directory(basedir)) |
| 219 | + norecurse = true; |
| 220 | + else |
| 221 | + { |
| 222 | + sret = path_mkdir(basedir); |
| 223 | + |
| 224 | + if (sret) |
| 225 | + norecurse = true; |
| 226 | + } |
| 227 | + |
| 228 | + free(basedir); |
| 229 | + |
| 230 | + if (norecurse) |
| 231 | + { |
| 232 | + int ret = path_mkdir_cb(dir); |
| 233 | + |
| 234 | + /* Don't treat this as an error. */ |
| 235 | + if (ret == -2 && path_is_directory(dir)) |
| 236 | + return true; |
| 237 | + |
| 238 | + return (ret == 0); |
| 239 | + } |
| 240 | + |
| 241 | + return sret; |
| 242 | +} |
0 commit comments