File: file_stream.c

package info (click to toggle)
retroarch 1.3.6%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 26,496 kB
  • ctags: 41,865
  • sloc: ansic: 250,395; cpp: 12,996; makefile: 3,500; objc: 3,266; xml: 2,141; python: 1,670; sh: 1,522; java: 798; asm: 542; perl: 393
file content (608 lines) | stat: -rw-r--r-- 13,849 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/* Copyright  (C) 2010-2016 The RetroArch team
 *
 * ---------------------------------------------------------------------------------------
 * The following license statement only applies to this file (file_stream.c).
 * ---------------------------------------------------------------------------------------
 *
 * Permission is hereby granted, free of charge,
 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#if defined(_WIN32)
#  ifdef _MSC_VER
#    define setmode _setmode
#  endif
#  ifdef _XBOX
#    include <xtl.h>
#    define INVALID_FILE_ATTRIBUTES -1
#  else
#    include <io.h>
#    include <fcntl.h>
#    include <direct.h>
#    include <windows.h>
#  endif
#elif defined(VITA)
#  include <psp2/io/fcntl.h>
#  include <psp2/io/dirent.h>

#define PSP_O_RDONLY SCE_O_RDONLY
#define PSP_O_RDWR   SCE_O_RDWR
#define PSP_O_CREAT  SCE_O_CREAT
#define PSP_O_WRONLY SCE_O_WRONLY
#define PSP_O_TRUNC  SCE_O_TRUNC
#else
#  if defined(PSP)
#    include <pspiofilemgr.h>
#  endif
#  include <sys/types.h>
#  include <sys/stat.h>
#  include <dirent.h>
#  include <unistd.h>
#endif

#ifdef __CELLOS_LV2__
#include <cell/cell_fs.h>
#define O_RDONLY CELL_FS_O_RDONLY
#define O_WRONLY CELL_FS_O_WRONLY
#define O_CREAT CELL_FS_O_CREAT
#define O_TRUNC CELL_FS_O_TRUNC
#define O_RDWR CELL_FS_O_RDWR
#else
#include <fcntl.h>
#endif

#include <streams/file_stream.h>
#include <memmap.h>

struct RFILE
{
   unsigned hints;
#if defined(PSP) || defined(VITA)
   SceUID fd;
#else

#define HAVE_BUFFERED_IO 1

#if defined(HAVE_BUFFERED_IO)
   FILE *fp;
#endif
#if defined(HAVE_MMAP)
   uint8_t *mapped;
   uint64_t mappos;
   uint64_t mapsize;
#endif
   int fd;
#endif
};

int filestream_get_fd(RFILE *stream)
{
   if (!stream)
      return -1;
#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
      return fileno(stream->fp);
#endif
   return stream->fd;
}

RFILE *filestream_open(const char *path, unsigned mode, ssize_t len)
{
   int            flags = 0;
   int         mode_int = 0;
#if defined(HAVE_BUFFERED_IO)
   const char *mode_str = NULL;
#endif
   RFILE        *stream = (RFILE*)calloc(1, sizeof(*stream));

   if (!stream)
      return NULL;

   (void)mode_int;
   (void)flags;

   stream->hints = mode;

#ifdef HAVE_MMAP
   if (stream->hints & RFILE_HINT_MMAP && (stream->hints & 0xff) == RFILE_MODE_READ)
      stream->hints |= RFILE_HINT_UNBUFFERED;
   else
#endif
      stream->hints &= ~RFILE_HINT_MMAP;

   switch (mode & 0xff)
   {
      case RFILE_MODE_READ_TEXT:
#if defined(VITA) || defined(PSP)
         mode_int = 0777;
         flags    = PSP_O_RDONLY;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = "r";
#endif
         /* No "else" here */
         flags    = O_RDONLY;
#endif
         break;
      case RFILE_MODE_READ:
#if defined(VITA) || defined(PSP)
         mode_int = 0777;
         flags    = PSP_O_RDONLY;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = "rb";
#endif
         /* No "else" here */
         flags    = O_RDONLY;
#endif
         break;
      case RFILE_MODE_WRITE:
#if defined(VITA) || defined(PSP)
         mode_int = 0777;
         flags    = PSP_O_CREAT | PSP_O_WRONLY | PSP_O_TRUNC;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = "wb";
#endif
         else
         {
            flags    = O_WRONLY | O_CREAT | O_TRUNC;
#ifndef _WIN32
            flags   |=  S_IRUSR | S_IWUSR;
#endif
         }
#endif
         break;
      case RFILE_MODE_READ_WRITE:
#if defined(VITA) || defined(PSP)
         mode_int = 0777;
         flags    = PSP_O_RDWR;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = "w+";
#endif
         else
         {
            flags    = O_RDWR;
#ifdef _WIN32
            flags   |= O_BINARY;
#endif
         }
#endif
         break;
   }

#if defined(VITA) || defined(PSP)
   stream->fd = sceIoOpen(path, flags, mode_int);
#else
#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
   {
      stream->fp = fopen(path, mode_str);
      if (!stream->fp)
         goto error;
   }
   else
#endif
   {
      stream->fd = open(path, flags);
      if (stream->fd == -1)
         goto error;
#ifdef HAVE_MMAP
      if (stream->hints & RFILE_HINT_MMAP)
      {
         stream->mappos  = 0;
         stream->mapped  = NULL;
         stream->mapsize = filestream_seek(stream, 0, SEEK_END);

         if (stream->mapsize == (uint64_t)-1)
            goto error;

         filestream_rewind(stream);

         stream->mapped = (uint8_t*)mmap((void*)0,
               stream->mapsize, PROT_READ,  MAP_SHARED, stream->fd, 0);

         if (stream->mapped == MAP_FAILED)
            stream->hints &= ~RFILE_HINT_MMAP;
      }
#endif
   }
#endif

#if defined(VITA) || defined(PSP)
   if (stream->fd == -1)
      goto error;
#endif

   return stream;

error:
   filestream_close(stream);
   return NULL;
}

char *filestream_getline(RFILE *stream)
{
   char* newline     = (char*)malloc(9);
   char* newline_tmp = NULL;
   size_t cur_size   = 8;
   size_t idx        = 0;
   int in            = filestream_getc(stream);

   if (!newline)
      return NULL;

   while (in != EOF && in != '\n')
   {
      if (idx == cur_size)
      {
         cur_size *= 2;
         newline_tmp = (char*)realloc(newline, cur_size + 1);

         if (!newline_tmp)
         {
            free(newline);
            return NULL;
         }

         newline = newline_tmp;
      }

      newline[idx++] = in;
      in             = filestream_getc(stream);
   }

   newline[idx] = '\0';
   return newline; 
}

char *filestream_gets(RFILE *stream, char *s, size_t len)
{
   if (!stream)
      return NULL;
#if defined(HAVE_BUFFERED_IO)
   return fgets(s, len, stream->fp);
#else
   return gets(s);
#endif
}

int filestream_getc(RFILE *stream)
{
   char c = 0;
   (void)c;
   if (!stream)
      return 0;
#if defined(HAVE_BUFFERED_IO)
    return fgetc(stream->fp);
#elif defined(VITA) || defined(PSP)
    if(filestream_read(stream, &c, 1) == 1)
       return (int)c;
    return EOF;
#else
   return getc(stream->fd);
#endif
}

ssize_t filestream_seek(RFILE *stream, ssize_t offset, int whence)
{
   if (!stream)
      goto error;

#if defined(VITA) || defined(PSP)
   if (sceIoLseek(stream->fd, (SceOff)offset, whence) == -1)
      goto error;
#else

#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
      return fseek(stream->fp, (long)offset, whence);
#endif

#ifdef HAVE_MMAP
   /* Need to check stream->mapped because this function is 
    * called in filestream_open() */
   if (stream->mapped && stream->hints & RFILE_HINT_MMAP)
   {
      /* fseek() returns error on under/overflow but allows cursor > EOF for
         read-only file descriptors. */
      switch (whence)
      {
         case SEEK_SET:
            if (offset < 0)
               goto error;

            stream->mappos = offset;
            break;

         case SEEK_CUR:
            if ((offset < 0 && stream->mappos + offset > stream->mappos) ||
                  (offset > 0 && stream->mappos + offset < stream->mappos))
               goto error;

            stream->mappos += offset;
            break;

         case SEEK_END:
            if (stream->mapsize + offset < stream->mapsize)
               goto error;

            stream->mappos = stream->mapsize + offset;
            break;
      }
      return stream->mappos;
   }
#endif

   if (lseek(stream->fd, offset, whence) < 0)
      goto error;

#endif

   return 0;

error:
   return -1;
}

int filestream_eof(RFILE *stream)
{
   size_t current_position = filestream_tell(stream);
   size_t end_position     = filestream_seek(stream, 0, SEEK_END);

   filestream_seek(stream, current_position, SEEK_SET);

   if (current_position >= end_position)
      return 1;
   return 0;
}

ssize_t filestream_tell(RFILE *stream)
{
   if (!stream)
      goto error;
#if defined(VITA) || defined(PSP)
  if (sceIoLseek(stream->fd, 0, SEEK_CUR) < 0)
     goto error;
#else
#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
      return ftell(stream->fp);
#endif
#ifdef HAVE_MMAP
   /* Need to check stream->mapped because this function 
    * is called in filestream_open() */
   if (stream->mapped && stream->hints & RFILE_HINT_MMAP)
      return stream->mappos;
#endif
   if (lseek(stream->fd, 0, SEEK_CUR) < 0)
      goto error;
#endif

   return 0;

error:
   return -1;
}

void filestream_rewind(RFILE *stream)
{
   filestream_seek(stream, 0L, SEEK_SET);
}

ssize_t filestream_read(RFILE *stream, void *s, size_t len)
{
   if (!stream || !s)
      goto error;
#if defined(VITA) || defined(PSP)
   return sceIoRead(stream->fd, s, len);
#else
#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
      return fread(s, 1, len, stream->fp);
#endif
#ifdef HAVE_MMAP
   if (stream->hints & RFILE_HINT_MMAP)
   {
      if (stream->mappos > stream->mapsize)
         goto error;

      if (stream->mappos + len > stream->mapsize)
         len = stream->mapsize - stream->mappos;

      memcpy(s, &stream->mapped[stream->mappos], len);
      stream->mappos += len;

      return len;
   }
#endif
   return read(stream->fd, s, len);
#endif

error:
   return -1;
}

ssize_t filestream_write(RFILE *stream, const void *s, size_t len)
{
   if (!stream)
      goto error;
#if defined(VITA) || defined(PSP)
   return sceIoWrite(stream->fd, s, len);
#else
#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
      return fwrite(s, 1, len, stream->fp);
#endif
#ifdef HAVE_MMAP
   if (stream->hints & RFILE_HINT_MMAP)
      goto error;
#endif
   return write(stream->fd, s, len);
#endif

error:
   return -1;
}

int filestream_putc(RFILE *stream, int c)
{
   if (!stream)
      return EOF;

#if defined(HAVE_BUFFERED_IO)
   return fputc(c, stream->fp);
#else
   /* unimplemented */
   return EOF;
#endif
}

int filestream_close(RFILE *stream)
{
   if (!stream)
      goto error;

#if defined(VITA) || defined(PSP)
   if (stream->fd > 0)
      sceIoClose(stream->fd);
#else
#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
   {
      if (stream->fp)
         fclose(stream->fp);
   }
   else
#endif
#ifdef HAVE_MMAP
      if (stream->hints & RFILE_HINT_MMAP)
         munmap(stream->mapped, stream->mapsize);
#endif

   if (stream->fd > 0)
      close(stream->fd);
#endif
   free(stream);

   return 0;

error:
   return -1;
}

/**
 * filestream_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.
 *
 * Returns: number of items read, -1 on error.
 */
int filestream_read_file(const char *path, void **buf, ssize_t *len)
{
   ssize_t ret              = 0;
   ssize_t content_buf_size = 0;
   void *content_buf        = NULL;
   RFILE *file              = filestream_open(path, RFILE_MODE_READ, -1);

   if (!file)
   {
      fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
      goto error;
   }

   if (filestream_seek(file, 0, SEEK_END) != 0)
      goto error;

   content_buf_size = filestream_tell(file);
   if (content_buf_size < 0)
      goto error;

   filestream_rewind(file);

   content_buf = malloc(content_buf_size + 1);

   if (!content_buf)
      goto error;

   ret = filestream_read(file, content_buf, content_buf_size);
   if (ret < 0)
   {
      fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
      goto error;
   }

   filestream_close(file);

   *buf    = content_buf;

   /* Allow for easy reading of strings to be safe.
    * Will only work with sane character formatting (Unix). */
   ((char*)content_buf)[content_buf_size] = '\0';

   if (len)
      *len = ret;

   return 1;

error:
   if (file)
      filestream_close(file);
   if (content_buf)
      free(content_buf);
   if (len)
      *len = -1;
   *buf = NULL;
   return 0;
}

/**
 * filestream_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.
 */
bool filestream_write_file(const char *path, const void *data, ssize_t size)
{
   ssize_t ret   = 0;
   RFILE *file   = filestream_open(path, RFILE_MODE_WRITE, -1);
   if (!file)
      return false;

   ret = filestream_write(file, data, size);
   filestream_close(file);

   if (ret != size)
      return false;

   return true;
}