| 1 | /* Copyright (C) 2010-2016 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (file_stream.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 | # include <psp2/io/fcntl.h> |
| 43 | # include <psp2/io/dirent.h> |
| 44 | |
| 45 | #define PSP_O_RDONLY PSP2_O_RDONLY |
| 46 | #define PSP_O_RDWR PSP2_O_RDWR |
| 47 | #define PSP_O_CREAT PSP2_O_CREAT |
| 48 | #define PSP_O_WRONLY PSP2_O_WRONLY |
| 49 | #define PSP_O_TRUNC PSP2_O_TRUNC |
| 50 | #else |
| 51 | # if defined(PSP) |
| 52 | # include <pspiofilemgr.h> |
| 53 | # endif |
| 54 | # include <sys/types.h> |
| 55 | # include <sys/stat.h> |
| 56 | # include <dirent.h> |
| 57 | # include <unistd.h> |
| 58 | #endif |
| 59 | |
| 60 | #ifdef __CELLOS_LV2__ |
| 61 | #include <cell/cell_fs.h> |
| 62 | #else |
| 63 | #include <fcntl.h> |
| 64 | #endif |
| 65 | |
| 66 | #include <streams/file_stream.h> |
| 67 | #include <memmap.h> |
| 68 | |
| 69 | #if 1 |
| 70 | #define HAVE_BUFFERED_IO 1 |
| 71 | #endif |
| 72 | |
| 73 | struct RFILE |
| 74 | { |
| 75 | unsigned hints; |
| 76 | #if defined(PSP) || defined(VITA) |
| 77 | SceUID fd; |
| 78 | #elif defined(__CELLOS_LV2__) |
| 79 | int fd; |
| 80 | #else |
| 81 | #if defined(HAVE_BUFFERED_IO) |
| 82 | FILE *fp; |
| 83 | #endif |
| 84 | #if defined(HAVE_MMAP) |
| 85 | uint8_t *mapped; |
| 86 | uint64_t mappos; |
| 87 | uint64_t mapsize; |
| 88 | #endif |
| 89 | int fd; |
| 90 | #endif |
| 91 | }; |
| 92 | |
| 93 | int filestream_get_fd(RFILE *stream) |
| 94 | { |
| 95 | if (!stream) |
| 96 | return -1; |
| 97 | #if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__) |
| 98 | return stream->fd; |
| 99 | #else |
| 100 | #if defined(HAVE_BUFFERED_IO) |
| 101 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 102 | return fileno(stream->fp); |
| 103 | #endif |
| 104 | return stream->fd; |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) |
| 109 | { |
| 110 | int flags = 0; |
| 111 | int mode_int = 0; |
| 112 | const char *mode_str = NULL; |
| 113 | RFILE *stream = (RFILE*)calloc(1, sizeof(*stream)); |
| 114 | |
| 115 | if (!stream) |
| 116 | return NULL; |
| 117 | |
| 118 | (void)mode_str; |
| 119 | (void)mode_int; |
| 120 | (void)flags; |
| 121 | |
| 122 | stream->hints = mode; |
| 123 | |
| 124 | #ifdef HAVE_MMAP |
| 125 | if (stream->hints & RFILE_HINT_MMAP && (stream->hints & 0xff) == RFILE_MODE_READ) |
| 126 | stream->hints |= RFILE_HINT_UNBUFFERED; |
| 127 | else |
| 128 | #endif |
| 129 | stream->hints &= ~RFILE_HINT_MMAP; |
| 130 | |
| 131 | switch (mode & 0xff) |
| 132 | { |
| 133 | case RFILE_MODE_READ: |
| 134 | #if defined(VITA) || defined(PSP) |
| 135 | mode_int = 0777; |
| 136 | flags = PSP_O_RDONLY; |
| 137 | #elif defined(__CELLOS_LV2__) |
| 138 | mode_int = 0777; |
| 139 | flags = CELL_FS_O_RDONLY; |
| 140 | #else |
| 141 | #if defined(HAVE_BUFFERED_IO) |
| 142 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 143 | mode_str = "rb"; |
| 144 | #endif |
| 145 | /* No "else" here */ |
| 146 | flags = O_RDONLY; |
| 147 | #endif |
| 148 | break; |
| 149 | case RFILE_MODE_WRITE: |
| 150 | #if defined(VITA) || defined(PSP) |
| 151 | mode_int = 0777; |
| 152 | flags = PSP_O_CREAT | PSP_O_WRONLY | PSP_O_TRUNC; |
| 153 | #elif defined(__CELLOS_LV2__) |
| 154 | mode_int = 0777; |
| 155 | flags = CELL_FS_O_CREAT | CELL_FS_O_WRONLY | CELL_FS_O_TRUNC; |
| 156 | #else |
| 157 | #if defined(HAVE_BUFFERED_IO) |
| 158 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 159 | mode_str = "wb"; |
| 160 | #endif |
| 161 | else |
| 162 | { |
| 163 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 164 | #ifndef _WIN32 |
| 165 | flags |= S_IRUSR | S_IWUSR; |
| 166 | #endif |
| 167 | } |
| 168 | #endif |
| 169 | break; |
| 170 | case RFILE_MODE_READ_WRITE: |
| 171 | #if defined(VITA) || defined(PSP) |
| 172 | mode_int = 0777; |
| 173 | flags = PSP_O_RDWR; |
| 174 | #elif defined(__CELLOS_LV2__) |
| 175 | mode_int = 0777; |
| 176 | flags = CELL_FS_O_RDWR; |
| 177 | #else |
| 178 | #if defined(HAVE_BUFFERED_IO) |
| 179 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 180 | mode_str = "w+"; |
| 181 | #endif |
| 182 | else |
| 183 | { |
| 184 | flags = O_RDWR; |
| 185 | #ifdef _WIN32 |
| 186 | flags |= O_BINARY; |
| 187 | #endif |
| 188 | } |
| 189 | #endif |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | #if defined(VITA) || defined(PSP) |
| 194 | stream->fd = sceIoOpen(path, flags, mode_int); |
| 195 | #elif defined(__CELLOS_LV2__) |
| 196 | cellFsOpen(path, flags, &stream->fd, NULL, 0); |
| 197 | #else |
| 198 | #if defined(HAVE_BUFFERED_IO) |
| 199 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 200 | { |
| 201 | stream->fp = fopen(path, mode_str); |
| 202 | if (!stream->fp) |
| 203 | goto error; |
| 204 | } |
| 205 | else |
| 206 | #endif |
| 207 | { |
| 208 | stream->fd = open(path, flags); |
| 209 | if (stream->fd == -1) |
| 210 | goto error; |
| 211 | #ifdef HAVE_MMAP |
| 212 | if (stream->hints & RFILE_HINT_MMAP) |
| 213 | { |
| 214 | stream->mappos = 0; |
| 215 | stream->mapped = NULL; |
| 216 | stream->mapsize = filestream_seek(stream, 0, SEEK_END); |
| 217 | |
| 218 | if (stream->mapsize == (uint64_t)-1) |
| 219 | goto error; |
| 220 | |
| 221 | filestream_rewind(stream); |
| 222 | |
| 223 | stream->mapped = (uint8_t*)mmap((void*)0, stream->mapsize, PROT_READ, MAP_SHARED, stream->fd, 0); |
| 224 | |
| 225 | if (stream->mapped == MAP_FAILED) |
| 226 | stream->hints &= ~RFILE_HINT_MMAP; |
| 227 | } |
| 228 | #endif |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | #if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__) |
| 233 | if (stream->fd == -1) |
| 234 | goto error; |
| 235 | #endif |
| 236 | |
| 237 | return stream; |
| 238 | |
| 239 | error: |
| 240 | filestream_close(stream); |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | ssize_t filestream_seek(RFILE *stream, ssize_t offset, int whence) |
| 245 | { |
| 246 | int ret = 0; |
| 247 | if (!stream) |
| 248 | return -1; |
| 249 | |
| 250 | (void)ret; |
| 251 | |
| 252 | #if defined(VITA) || defined(PSP) |
| 253 | ret = sceIoLseek(stream->fd, (SceOff)offset, whence); |
| 254 | if (ret == -1) |
| 255 | return -1; |
| 256 | return 0; |
| 257 | #elif defined(__CELLOS_LV2__) |
| 258 | uint64_t pos = 0; |
| 259 | if (cellFsLseek(stream->fd, offset, whence, &pos) != CELL_FS_SUCCEEDED) |
| 260 | return -1; |
| 261 | return 0; |
| 262 | #else |
| 263 | #if defined(HAVE_BUFFERED_IO) |
| 264 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 265 | return fseek(stream->fp, (long)offset, whence); |
| 266 | else |
| 267 | #endif |
| 268 | #ifdef HAVE_MMAP |
| 269 | /* Need to check stream->mapped because this function is |
| 270 | * called in filestream_open() */ |
| 271 | if (stream->mapped && stream->hints & RFILE_HINT_MMAP) |
| 272 | { |
| 273 | /* fseek() returns error on under/overflow but allows cursor > EOF for |
| 274 | read-only file descriptors. */ |
| 275 | switch (whence) |
| 276 | { |
| 277 | case SEEK_SET: |
| 278 | if (offset < 0) |
| 279 | return -1; |
| 280 | |
| 281 | stream->mappos = offset; |
| 282 | break; |
| 283 | |
| 284 | case SEEK_CUR: |
| 285 | if ((offset < 0 && stream->mappos + offset > stream->mappos) || |
| 286 | (offset > 0 && stream->mappos + offset < stream->mappos)) |
| 287 | return -1; |
| 288 | |
| 289 | stream->mappos += offset; |
| 290 | break; |
| 291 | |
| 292 | case SEEK_END: |
| 293 | if (stream->mapsize + offset < stream->mapsize) |
| 294 | return -1; |
| 295 | |
| 296 | stream->mappos = stream->mapsize + offset; |
| 297 | |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | return stream->mappos; |
| 302 | } |
| 303 | else |
| 304 | #endif |
| 305 | { |
| 306 | ret = lseek(stream->fd, offset, whence); |
| 307 | return ret < 0 ? -1 : ret; |
| 308 | } |
| 309 | #endif |
| 310 | } |
| 311 | |
| 312 | ssize_t filestream_tell(RFILE *stream) |
| 313 | { |
| 314 | if (!stream) |
| 315 | return -1; |
| 316 | #if defined(VITA) || defined(PSP) |
| 317 | return sceIoLseek(stream->fd, 0, SEEK_CUR); |
| 318 | #elif defined(__CELLOS_LV2__) |
| 319 | uint64_t pos = 0; |
| 320 | if (cellFsLseek(stream->fd, 0, CELL_FS_SEEK_CUR, &pos) != CELL_FS_SUCCEEDED) |
| 321 | return -1; |
| 322 | return 0; |
| 323 | #else |
| 324 | #if defined(HAVE_BUFFERED_IO) |
| 325 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 326 | return ftell(stream->fp); |
| 327 | else |
| 328 | #endif |
| 329 | #ifdef HAVE_MMAP |
| 330 | /* Need to check stream->mapped because this function |
| 331 | * is called in filestream_open() */ |
| 332 | if (stream->mapped && stream->hints & RFILE_HINT_MMAP) |
| 333 | return stream->mappos; |
| 334 | else |
| 335 | #endif |
| 336 | return lseek(stream->fd, 0, SEEK_CUR); |
| 337 | #endif |
| 338 | } |
| 339 | |
| 340 | void filestream_rewind(RFILE *stream) |
| 341 | { |
| 342 | filestream_seek(stream, 0L, SEEK_SET); |
| 343 | } |
| 344 | |
| 345 | ssize_t filestream_read(RFILE *stream, void *s, size_t len) |
| 346 | { |
| 347 | if (!stream || !s) |
| 348 | return -1; |
| 349 | #if defined(VITA) || defined(PSP) |
| 350 | return sceIoRead(stream->fd, s, len); |
| 351 | #elif defined(__CELLOS_LV2__) |
| 352 | uint64_t bytes_written; |
| 353 | if (cellFsRead(stream->fd, s, len, &bytes_written) != CELL_FS_SUCCEEDED) |
| 354 | return -1; |
| 355 | return bytes_written; |
| 356 | #else |
| 357 | #if defined(HAVE_BUFFERED_IO) |
| 358 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 359 | return fread(s, 1, len, stream->fp); |
| 360 | else |
| 361 | #endif |
| 362 | #ifdef HAVE_MMAP |
| 363 | if (stream->hints & RFILE_HINT_MMAP) |
| 364 | { |
| 365 | if (stream->mappos > stream->mapsize) |
| 366 | return -1; |
| 367 | |
| 368 | if (stream->mappos + len > stream->mapsize) |
| 369 | len = stream->mapsize - stream->mappos; |
| 370 | |
| 371 | memcpy(s, &stream->mapped[stream->mappos], len); |
| 372 | stream->mappos += len; |
| 373 | |
| 374 | return len; |
| 375 | } |
| 376 | else |
| 377 | #endif |
| 378 | return read(stream->fd, s, len); |
| 379 | #endif |
| 380 | } |
| 381 | |
| 382 | ssize_t filestream_write(RFILE *stream, const void *s, size_t len) |
| 383 | { |
| 384 | if (!stream) |
| 385 | return -1; |
| 386 | #if defined(VITA) || defined(PSP) |
| 387 | return sceIoWrite(stream->fd, s, len); |
| 388 | #elif defined(__CELLOS_LV2__) |
| 389 | uint64_t bytes_written; |
| 390 | if (cellFsWrite(stream->fd, s, len, &bytes_written) != CELL_FS_SUCCEEDED) |
| 391 | return -1; |
| 392 | return bytes_written; |
| 393 | #else |
| 394 | #if defined(HAVE_BUFFERED_IO) |
| 395 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 396 | return fwrite(s, 1, len, stream->fp); |
| 397 | else |
| 398 | #endif |
| 399 | #ifdef HAVE_MMAP |
| 400 | if (stream->hints & RFILE_HINT_MMAP) |
| 401 | return -1; |
| 402 | else |
| 403 | #endif |
| 404 | return write(stream->fd, s, len); |
| 405 | #endif |
| 406 | } |
| 407 | |
| 408 | int filestream_close(RFILE *stream) |
| 409 | { |
| 410 | if (!stream) |
| 411 | return -1; |
| 412 | |
| 413 | #if defined(VITA) || defined(PSP) |
| 414 | if (stream->fd > 0) |
| 415 | sceIoClose(stream->fd); |
| 416 | #elif defined(__CELLOS_LV2__) |
| 417 | if (stream->fd > 0) |
| 418 | cellFsClose(stream->fd); |
| 419 | #else |
| 420 | #if defined(HAVE_BUFFERED_IO) |
| 421 | if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) |
| 422 | { |
| 423 | if (stream->fp) |
| 424 | fclose(stream->fp); |
| 425 | } |
| 426 | else |
| 427 | #endif |
| 428 | #ifdef HAVE_MMAP |
| 429 | if (stream->hints & RFILE_HINT_MMAP) |
| 430 | munmap(stream->mapped, stream->mapsize); |
| 431 | #endif |
| 432 | |
| 433 | if (stream->fd > 0) |
| 434 | close(stream->fd); |
| 435 | #endif |
| 436 | free(stream); |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * filestream_read_file: |
| 443 | * @path : path to file. |
| 444 | * @buf : buffer to allocate and read the contents of the |
| 445 | * file into. Needs to be freed manually. |
| 446 | * |
| 447 | * Read the contents of a file into @buf. |
| 448 | * |
| 449 | * Returns: number of items read, -1 on error. |
| 450 | */ |
| 451 | int filestream_read_file(const char *path, void **buf, ssize_t *len) |
| 452 | { |
| 453 | ssize_t ret = 0; |
| 454 | ssize_t content_buf_size = 0; |
| 455 | void *content_buf = NULL; |
| 456 | RFILE *file = filestream_open(path, RFILE_MODE_READ, -1); |
| 457 | |
| 458 | if (!file) |
| 459 | { |
| 460 | #if __STDC_VERSION__ >= 199901L |
| 461 | fprintf(stderr, "%s: Failed to open %s: %s\n", __FUNCTION__, path, strerror(errno)); |
| 462 | #else |
| 463 | fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); |
| 464 | #endif |
| 465 | goto error; |
| 466 | } |
| 467 | |
| 468 | if (filestream_seek(file, 0, SEEK_END) != 0) |
| 469 | goto error; |
| 470 | |
| 471 | content_buf_size = filestream_tell(file); |
| 472 | if (content_buf_size < 0) |
| 473 | goto error; |
| 474 | |
| 475 | filestream_rewind(file); |
| 476 | |
| 477 | content_buf = malloc(content_buf_size + 1); |
| 478 | |
| 479 | if (!content_buf) |
| 480 | goto error; |
| 481 | |
| 482 | ret = filestream_read(file, content_buf, content_buf_size); |
| 483 | if (ret < 0) |
| 484 | { |
| 485 | #if __STDC_VERSION__ >= 199901L |
| 486 | fprintf(stderr, "%s: Failed to read %s: %s\n", __FUNCTION__, path, strerror(errno)); |
| 487 | #else |
| 488 | fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno)); |
| 489 | #endif |
| 490 | goto error; |
| 491 | } |
| 492 | |
| 493 | filestream_close(file); |
| 494 | |
| 495 | *buf = content_buf; |
| 496 | |
| 497 | /* Allow for easy reading of strings to be safe. |
| 498 | * Will only work with sane character formatting (Unix). */ |
| 499 | ((char*)content_buf)[content_buf_size] = '\0'; |
| 500 | |
| 501 | if (len) |
| 502 | *len = ret; |
| 503 | |
| 504 | return 1; |
| 505 | |
| 506 | error: |
| 507 | if (file) |
| 508 | filestream_close(file); |
| 509 | if (content_buf) |
| 510 | free(content_buf); |
| 511 | if (len) |
| 512 | *len = -1; |
| 513 | *buf = NULL; |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * filestream_write_file: |
| 519 | * @path : path to file. |
| 520 | * @data : contents to write to the file. |
| 521 | * @size : size of the contents. |
| 522 | * |
| 523 | * Writes data to a file. |
| 524 | * |
| 525 | * Returns: true (1) on success, false (0) otherwise. |
| 526 | */ |
| 527 | bool filestream_write_file(const char *path, const void *data, ssize_t size) |
| 528 | { |
| 529 | ssize_t ret = 0; |
| 530 | RFILE *file = filestream_open(path, RFILE_MODE_WRITE, -1); |
| 531 | if (!file) |
| 532 | return false; |
| 533 | |
| 534 | ret = filestream_write(file, data, size); |
| 535 | filestream_close(file); |
| 536 | |
| 537 | return (ret == size); |
| 538 | } |