| 1 | /* Copyright (C) 2010-2015 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (retro_stat.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 <errno.h> |
| 27 | |
| 28 | #if defined(_WIN32) |
| 29 | #ifdef _MSC_VER |
| 30 | #define setmode _setmode |
| 31 | #endif |
| 32 | #ifdef _XBOX |
| 33 | #include <xtl.h> |
| 34 | #define INVALID_FILE_ATTRIBUTES -1 |
| 35 | #else |
| 36 | #include <io.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <direct.h> |
| 39 | #include <windows.h> |
| 40 | #endif |
| 41 | #elif defined(VITA) |
| 42 | #define SCE_ERROR_ERRNO_EEXIST 0x80010011 |
| 43 | #include <psp2/io/fcntl.h> |
| 44 | #include <psp2/io/dirent.h> |
| 45 | #include <psp2/io/stat.h> |
| 46 | #else |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <unistd.h> |
| 50 | #endif |
| 51 | |
| 52 | #if defined(PSP) |
| 53 | #include <pspkernel.h> |
| 54 | #endif |
| 55 | |
| 56 | #ifdef __HAIKU__ |
| 57 | #include <kernel/image.h> |
| 58 | #endif |
| 59 | |
| 60 | #if defined(__CELLOS_LV2__) |
| 61 | #include <cell/cell_fs.h> |
| 62 | #endif |
| 63 | |
| 64 | #if defined(VITA) |
| 65 | #define FIO_S_ISDIR PSP2_S_ISDIR |
| 66 | #endif |
| 67 | |
| 68 | #if (defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)) || defined(__QNX__) || defined(PSP) |
| 69 | #include <unistd.h> /* stat() is defined here */ |
| 70 | #endif |
| 71 | |
| 72 | #include <retro_miscellaneous.h> |
| 73 | #include <boolean.h> |
| 74 | |
| 75 | enum stat_mode |
| 76 | { |
| 77 | IS_DIRECTORY = 0, |
| 78 | IS_CHARACTER_SPECIAL, |
| 79 | IS_VALID |
| 80 | }; |
| 81 | |
| 82 | static bool path_stat(const char *path, enum stat_mode mode, int32_t *size) |
| 83 | { |
| 84 | #if defined(VITA) || defined(PSP) |
| 85 | SceIoStat buf; |
| 86 | char *tmp = strdup(path); |
| 87 | size_t len = strlen(tmp); |
| 88 | if (tmp[len-1] == '/') |
| 89 | tmp[len-1]='\0'; |
| 90 | |
| 91 | if (sceIoGetstat(tmp, &buf) < 0) |
| 92 | { |
| 93 | free(tmp); |
| 94 | return false; |
| 95 | } |
| 96 | free(tmp); |
| 97 | |
| 98 | #elif defined(__CELLOS_LV2__) |
| 99 | CellFsStat buf; |
| 100 | if (cellFsStat(path, &buf) < 0) |
| 101 | return false; |
| 102 | #elif defined(_WIN32) |
| 103 | WIN32_FILE_ATTRIBUTE_DATA file_info; |
| 104 | GET_FILEEX_INFO_LEVELS fInfoLevelId = GetFileExInfoStandard; |
| 105 | DWORD ret = GetFileAttributesEx(path, fInfoLevelId, &file_info); |
| 106 | if (ret == 0) |
| 107 | return false; |
| 108 | #else |
| 109 | struct stat buf; |
| 110 | if (stat(path, &buf) < 0) |
| 111 | return false; |
| 112 | #endif |
| 113 | |
| 114 | #if defined(_WIN32) |
| 115 | if (size) |
| 116 | *size = file_info.nFileSizeLow; |
| 117 | #else |
| 118 | if (size) |
| 119 | *size = buf.st_size; |
| 120 | #endif |
| 121 | |
| 122 | switch (mode) |
| 123 | { |
| 124 | case IS_DIRECTORY: |
| 125 | #if defined(VITA) || defined(PSP) |
| 126 | return FIO_S_ISDIR(buf.st_mode); |
| 127 | #elif defined(__CELLOS_LV2__) |
| 128 | return ((buf.st_mode & S_IFMT) == S_IFDIR); |
| 129 | #elif defined(_WIN32) |
| 130 | return (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 131 | #else |
| 132 | return S_ISDIR(buf.st_mode); |
| 133 | #endif |
| 134 | case IS_CHARACTER_SPECIAL: |
| 135 | #if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__) || defined(_WIN32) |
| 136 | return false; |
| 137 | #else |
| 138 | return S_ISCHR(buf.st_mode); |
| 139 | #endif |
| 140 | case IS_VALID: |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * path_is_directory: |
| 149 | * @path : path |
| 150 | * |
| 151 | * Checks if path is a directory. |
| 152 | * |
| 153 | * Returns: true (1) if path is a directory, otherwise false (0). |
| 154 | */ |
| 155 | bool path_is_directory(const char *path) |
| 156 | { |
| 157 | return path_stat(path, IS_DIRECTORY, NULL); |
| 158 | } |
| 159 | |
| 160 | bool path_is_character_special(const char *path) |
| 161 | { |
| 162 | return path_stat(path, IS_CHARACTER_SPECIAL, NULL); |
| 163 | } |
| 164 | |
| 165 | bool path_is_valid(const char *path) |
| 166 | { |
| 167 | return path_stat(path, IS_VALID, NULL); |
| 168 | } |
| 169 | |
| 170 | int32_t path_get_size(const char *path) |
| 171 | { |
| 172 | int32_t filesize = 0; |
| 173 | if (path_stat(path, IS_VALID, &filesize)) |
| 174 | return filesize; |
| 175 | |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * path_mkdir_norecurse: |
| 181 | * @dir : directory |
| 182 | * |
| 183 | * Create directory on filesystem. |
| 184 | * |
| 185 | * Returns: true (1) if directory could be created, otherwise false (0). |
| 186 | **/ |
| 187 | bool mkdir_norecurse(const char *dir) |
| 188 | { |
| 189 | int ret; |
| 190 | #if defined(_WIN32) |
| 191 | ret = _mkdir(dir); |
| 192 | #elif defined(IOS) |
| 193 | ret = mkdir(dir, 0755); |
| 194 | #elif defined(VITA) || defined(PSP) |
| 195 | ret = sceIoMkdir(dir, 0777); |
| 196 | #else |
| 197 | ret = mkdir(dir, 0750); |
| 198 | #endif |
| 199 | /* Don't treat this as an error. */ |
| 200 | #if defined(VITA) |
| 201 | if ((ret == SCE_ERROR_ERRNO_EEXIST) && path_is_directory(dir)) |
| 202 | ret = 0; |
| 203 | #elif defined(PSP) |
| 204 | if ((ret == -1) && path_is_directory(dir)) |
| 205 | ret = 0; |
| 206 | #else |
| 207 | if (ret < 0 && errno == EEXIST && path_is_directory(dir)) |
| 208 | ret = 0; |
| 209 | #endif |
| 210 | if (ret < 0) |
| 211 | printf("mkdir(%s) error: %s.\n", dir, strerror(errno)); |
| 212 | return ret == 0; |
| 213 | } |