basesrc: Downgrade EOS warning
[platform/upstream/gstreamer.git] / plugins / elements / gstsparsefile.c
1 /* GStreamer
2  * Copyright (C) 2014 Wim Taymans <wtaymans@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include <glib/gstdio.h>
26
27 #include "gstsparsefile.h"
28
29 #ifdef G_OS_WIN32
30 #include <io.h>                 /* lseek, open, close, read */
31 #undef lseek
32 #define lseek _lseeki64
33 #undef off_t
34 #define off_t guint64
35 #else
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_FSEEKO
40 #define FSEEK_FILE(file,offset)  (fseeko (file, (off_t) offset, SEEK_SET) != 0)
41 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
42 #define FSEEK_FILE(file,offset)  (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
43 #else
44 #define FSEEK_FILE(file,offset)  (fseek (file, offset, SEEK_SET) != 0)
45 #endif
46
47 #define GST_SPARSE_FILE_IO_ERROR \
48     g_quark_from_static_string("gst-sparse-file-io-error-quark")
49
50 static GstSparseFileIOErrorEnum
51 gst_sparse_file_io_error_from_errno (gint err_no);
52
53 typedef struct _GstSparseRange GstSparseRange;
54
55 struct _GstSparseRange
56 {
57   GstSparseRange *next;
58
59   gsize start;
60   gsize stop;
61 };
62
63 #define RANGE_CONTAINS(r,o) ((r)->start <= (o) && (r)->stop > (o))
64
65 struct _GstSparseFile
66 {
67   gint fd;
68   FILE *file;
69   gsize current_pos;
70
71   GstSparseRange *ranges;
72   guint n_ranges;
73
74   GstSparseRange *write_range;
75   GstSparseRange *read_range;
76 };
77
78 static GstSparseRange *
79 get_write_range (GstSparseFile * file, gsize offset)
80 {
81   GstSparseRange *next, *prev, *result = NULL;
82
83   if (file->write_range && file->write_range->stop == offset)
84     return file->write_range;
85
86   prev = NULL;
87   next = file->ranges;
88   while (next) {
89     if (next->start > offset)
90       break;
91
92     if (next->stop >= offset) {
93       result = next;
94       break;
95     }
96     prev = next;
97     next = next->next;
98   }
99   if (result == NULL) {
100     result = g_slice_new0 (GstSparseRange);
101     result->start = offset;
102     result->stop = offset;
103
104     result->next = next;
105     if (prev)
106       prev->next = result;
107     else
108       file->ranges = result;
109
110     file->write_range = result;
111     file->read_range = NULL;
112
113     file->n_ranges++;
114   }
115   return result;
116 }
117
118 static GstSparseRange *
119 get_read_range (GstSparseFile * file, gsize offset, gsize count)
120 {
121   GstSparseRange *walk, *result = NULL;
122
123   if (file->read_range && RANGE_CONTAINS (file->read_range, offset))
124     return file->read_range;
125
126   for (walk = file->ranges; walk; walk = walk->next) {
127     if (walk->start > offset)
128       break;
129
130     if (walk->stop >= offset + count) {
131       result = walk;
132       break;
133     }
134   }
135   return result;
136 }
137
138 /**
139  * gst_sparse_file_new:
140  *
141  * Make a new #GstSparseFile
142  *
143  * Returns: a new #GstSparseFile, gst_sparse_file_free() after usage.
144  *
145  * Since: 1.4
146  */
147 GstSparseFile *
148 gst_sparse_file_new (void)
149 {
150   GstSparseFile *result;
151
152   result = g_slice_new0 (GstSparseFile);
153   result->current_pos = 0;
154   result->ranges = NULL;
155   result->n_ranges = 0;
156
157   return result;
158 }
159
160 /**
161  * gst_sparse_file_set_fd:
162  * @file: a #GstSparseFile
163  * @fd: a file descriptor
164  *
165  * Store the data for @file in the file represented with @fd.
166  *
167  * Returns: %TRUE when @fd could be set
168  *
169  * Since: 1.4
170  */
171 gboolean
172 gst_sparse_file_set_fd (GstSparseFile * file, gint fd)
173 {
174   g_return_val_if_fail (file != NULL, FALSE);
175   g_return_val_if_fail (fd != 0, FALSE);
176
177   file->file = fdopen (fd, "wb+");
178   file->fd = fd;
179
180   return file->file != NULL;
181 }
182
183 /**
184  * gst_sparse_file_clear:
185  * @file: a #GstSparseFile
186  *
187  * Clear all the ranges in @file.
188  */
189 void
190 gst_sparse_file_clear (GstSparseFile * file)
191 {
192   g_return_if_fail (file != NULL);
193
194   if (file->file) {
195     fclose (file->file);
196     file->file = fdopen (file->fd, "wb+");
197   }
198   g_slice_free_chain (GstSparseRange, file->ranges, next);
199   file->current_pos = 0;
200   file->ranges = NULL;
201   file->n_ranges = 0;
202 }
203
204 /**
205  * gst_sparse_file_free:
206  * @file: a #GstSparseFile
207  *
208  * Free the memory used by @file.
209  *
210  * Since: 1.4
211  */
212 void
213 gst_sparse_file_free (GstSparseFile * file)
214 {
215   g_return_if_fail (file != NULL);
216
217   if (file->file) {
218     fflush (file->file);
219     fclose (file->file);
220   }
221   g_slice_free_chain (GstSparseRange, file->ranges, next);
222   g_slice_free (GstSparseFile, file);
223 }
224
225 /**
226  * gst_sparse_file_write:
227  * @file: a #GstSparseFile
228  * @offset: the offset
229  * @data: the data
230  * @count: amount of bytes
231  * @available: amount of bytes already available
232  * @error: a #GError
233  *
234  * Write @count bytes from @data to @file at @offset.
235  *
236  * If @available is not %NULL, it will be updated with the amount of
237  * data already available after the last written byte.
238  *
239  * Returns: The number of bytes written or 0 on error.
240  *
241  * Since: 1.4
242  */
243 gsize
244 gst_sparse_file_write (GstSparseFile * file, gsize offset, gconstpointer data,
245     gsize count, gsize * available, GError ** error)
246 {
247   GstSparseRange *range, *next;
248   gsize stop;
249
250   g_return_val_if_fail (file != NULL, 0);
251   g_return_val_if_fail (count != 0, 0);
252
253   if (file->file) {
254     if (file->current_pos != offset) {
255       GST_DEBUG ("seeking to %" G_GSIZE_FORMAT, offset);
256       if (FSEEK_FILE (file->file, offset))
257         goto error;
258     }
259     if (fwrite (data, count, 1, file->file) != 1)
260       goto error;
261   }
262
263   file->current_pos = offset + count;
264
265   /* update the new stop position in the range */
266   range = get_write_range (file, offset);
267   stop = offset + count;
268   range->stop = MAX (range->stop, stop);
269
270   /* see if we can merge with next region */
271   while ((next = range->next)) {
272     if (next->start > range->stop)
273       break;
274
275     GST_DEBUG ("merging range %" G_GSIZE_FORMAT "-%" G_GSIZE_FORMAT ", next %"
276         G_GSIZE_FORMAT "-%" G_GSIZE_FORMAT, range->start, range->stop,
277         next->start, next->stop);
278
279     range->stop = MAX (next->stop, range->stop);
280     range->next = next->next;
281
282     if (file->write_range == next)
283       file->write_range = NULL;
284     if (file->read_range == next)
285       file->read_range = NULL;
286     g_slice_free (GstSparseRange, next);
287     file->n_ranges--;
288   }
289   if (available)
290     *available = range->stop - stop;
291
292   return count;
293
294   /* ERRORS */
295 error:
296   {
297     g_set_error (error, GST_SPARSE_FILE_IO_ERROR,
298         gst_sparse_file_io_error_from_errno (errno), "Error writing file: %s",
299         g_strerror (errno));
300     return 0;
301   }
302 }
303
304 /**
305  * gst_sparse_file_read:
306  * @file: a #GstSparseFile
307  * @offset: the offset
308  * @data: the data
309  * @count: amount of bytes
310  * @remaining: amount of bytes remaining
311  * @error: a #GError
312  *
313  * Read @count bytes from @file at @offset into @data.
314  *
315  * On error, @error will be set. If there are no @count bytes available
316  * at @offset, %GST_SPARSE_FILE_IO_ERROR_WOULD_BLOCK is returned.
317  *
318  * @remaining will be set to the amount of bytes remaining in the read
319  * range.
320  *
321  * Returns: The number of bytes read of 0 on error.
322  *
323  * Since: 1.4
324  */
325 gsize
326 gst_sparse_file_read (GstSparseFile * file, gsize offset, gpointer data,
327     gsize count, gsize * remaining, GError ** error)
328 {
329   GstSparseRange *range;
330   gsize res = 0;
331
332   g_return_val_if_fail (file != NULL, 0);
333   g_return_val_if_fail (count != 0, 0);
334
335   if ((range = get_read_range (file, offset, count)) == NULL)
336     goto no_range;
337
338   if (file->file) {
339     if (file->current_pos != offset) {
340       GST_DEBUG ("seeking from %" G_GSIZE_FORMAT " to %" G_GSIZE_FORMAT,
341           file->current_pos, offset);
342       if (FSEEK_FILE (file->file, offset))
343         goto error;
344     }
345     res = fread (data, 1, count, file->file);
346     if (G_UNLIKELY (res < count))
347       goto error;
348   }
349
350   file->current_pos = offset + res;
351
352   if (remaining)
353     *remaining = range->stop - file->current_pos;
354
355   return count;
356
357   /* ERRORS */
358 no_range:
359   {
360     g_set_error_literal (error, GST_SPARSE_FILE_IO_ERROR,
361         GST_SPARSE_FILE_IO_ERROR_WOULD_BLOCK, "Offset not written to file yet");
362     return 0;
363   }
364 error:
365   {
366     if (ferror (file->file)) {
367       g_set_error (error, GST_SPARSE_FILE_IO_ERROR,
368           gst_sparse_file_io_error_from_errno (errno), "Error reading file: %s",
369           g_strerror (errno));
370     } else if (feof (file->file)) {
371       return res;
372     }
373     return 0;
374   }
375 }
376
377 /**
378  * gst_sparse_file_n_ranges:
379  * @file: a #GstSparseFile
380  *
381  * Get the number of ranges that are written in @file.
382  *
383  * Returns: the number of written ranges.
384  *
385  * Since: 1.4
386  */
387 guint
388 gst_sparse_file_n_ranges (GstSparseFile * file)
389 {
390   g_return_val_if_fail (file != NULL, 0);
391
392   return file->n_ranges;
393 }
394
395 /**
396  * gst_sparse_file_get_range_before:
397  * @file: a #GstSparseFile
398  * @offset: the range offset
399  * @start: result start
400  * @stop: result stop
401  *
402  * Get the start and stop offset of the range containing data before or
403  * including @offset.
404  *
405  * Returns: %TRUE if the range with data before @offset exists.
406  *
407  * Since: 1.4
408  */
409 gboolean
410 gst_sparse_file_get_range_before (GstSparseFile * file, gsize offset,
411     gsize * start, gsize * stop)
412 {
413   GstSparseRange *walk, *result = NULL;
414
415   g_return_val_if_fail (file != NULL, FALSE);
416
417   for (walk = file->ranges; walk; walk = walk->next) {
418     GST_DEBUG ("start %" G_GSIZE_FORMAT " > %" G_GSIZE_FORMAT,
419         walk->stop, offset);
420     if (walk->start > offset)
421       break;
422
423     if (walk->start <= offset)
424       result = walk;
425   }
426
427   if (result) {
428     if (start)
429       *start = result->start;
430     if (stop)
431       *stop = result->stop;
432   }
433   return result != NULL;
434 }
435
436 /**
437  * gst_sparse_file_get_range_after:
438  * @file: a #GstSparseFile
439  * @offset: the range offset
440  * @start: result start
441  * @stop: result stop
442  *
443  * Get the start and stop offset of the range containing data after or
444  * including @offset.
445  *
446  * Returns: %TRUE if the range with data after @offset exists.
447  *
448  * Since: 1.4
449  */
450 gboolean
451 gst_sparse_file_get_range_after (GstSparseFile * file, gsize offset,
452     gsize * start, gsize * stop)
453 {
454   GstSparseRange *walk, *result = NULL;
455
456   g_return_val_if_fail (file != NULL, FALSE);
457
458   for (walk = file->ranges; walk; walk = walk->next) {
459     GST_DEBUG ("stop %" G_GSIZE_FORMAT " > %" G_GSIZE_FORMAT,
460         walk->stop, offset);
461     if (walk->stop > offset) {
462       result = walk;
463       break;
464     }
465   }
466   if (result) {
467     if (start)
468       *start = result->start;
469     if (stop)
470       *stop = result->stop;
471   }
472   return result != NULL;
473 }
474
475 /* we don't want to rely on libgio just for g_io_error_from_errno() */
476 static GstSparseFileIOErrorEnum
477 gst_sparse_file_io_error_from_errno (gint err_no)
478 {
479   switch (err_no) {
480 #ifdef EEXIST
481     case EEXIST:
482       return GST_SPARSE_FILE_IO_ERROR_EXISTS;
483       break;
484 #endif
485
486 #ifdef EISDIR
487     case EISDIR:
488       return GST_SPARSE_FILE_IO_ERROR_IS_DIRECTORY;
489       break;
490 #endif
491
492 #ifdef EACCES
493     case EACCES:
494       return GST_SPARSE_FILE_IO_ERROR_PERMISSION_DENIED;
495       break;
496 #endif
497
498 #ifdef ENAMETOOLONG
499     case ENAMETOOLONG:
500       return GST_SPARSE_FILE_IO_ERROR_FILENAME_TOO_LONG;
501       break;
502 #endif
503
504 #ifdef ENOENT
505     case ENOENT:
506       return GST_SPARSE_FILE_IO_ERROR_NOT_FOUND;
507       break;
508 #endif
509
510 #ifdef ENOTDIR
511     case ENOTDIR:
512       return GST_SPARSE_FILE_IO_ERROR_NOT_DIRECTORY;
513       break;
514 #endif
515
516 #ifdef EROFS
517     case EROFS:
518       return GST_SPARSE_FILE_IO_ERROR_READ_ONLY;
519       break;
520 #endif
521
522 #ifdef ELOOP
523     case ELOOP:
524       return GST_SPARSE_FILE_IO_ERROR_TOO_MANY_LINKS;
525       break;
526 #endif
527
528 #ifdef ENOSPC
529     case ENOSPC:
530       return GST_SPARSE_FILE_IO_ERROR_NO_SPACE;
531       break;
532 #endif
533
534 #ifdef ENOMEM
535     case ENOMEM:
536       return GST_SPARSE_FILE_IO_ERROR_NO_SPACE;
537       break;
538 #endif
539
540 #ifdef EINVAL
541     case EINVAL:
542       return GST_SPARSE_FILE_IO_ERROR_INVALID_ARGUMENT;
543       break;
544 #endif
545
546 #ifdef EPERM
547     case EPERM:
548       return GST_SPARSE_FILE_IO_ERROR_PERMISSION_DENIED;
549       break;
550 #endif
551
552 #ifdef ECANCELED
553     case ECANCELED:
554       return GST_SPARSE_FILE_IO_ERROR_CANCELLED;
555       break;
556 #endif
557
558       /* ENOTEMPTY == EEXIST on AIX for backward compatibility reasons */
559 #if defined (ENOTEMPTY) && (!defined (EEXIST) || (ENOTEMPTY != EEXIST))
560     case ENOTEMPTY:
561       return GST_SPARSE_FILE_IO_ERROR_NOT_EMPTY;
562       break;
563 #endif
564
565 #ifdef ENOTSUP
566     case ENOTSUP:
567       return GST_SPARSE_FILE_IO_ERROR_NOT_SUPPORTED;
568       break;
569 #endif
570
571       /* EOPNOTSUPP == ENOTSUP on Linux, but POSIX considers them distinct */
572 #if defined (EOPNOTSUPP) && (!defined (ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
573     case EOPNOTSUPP:
574       return GST_SPARSE_FILE_IO_ERROR_NOT_SUPPORTED;
575       break;
576 #endif
577
578 #ifdef EPROTONOSUPPORT
579     case EPROTONOSUPPORT:
580       return GST_SPARSE_FILE_IO_ERROR_NOT_SUPPORTED;
581       break;
582 #endif
583
584 #ifdef ESOCKTNOSUPPORT
585     case ESOCKTNOSUPPORT:
586       return GST_SPARSE_FILE_IO_ERROR_NOT_SUPPORTED;
587       break;
588 #endif
589
590 #ifdef EPFNOSUPPORT
591     case EPFNOSUPPORT:
592       return GST_SPARSE_FILE_IO_ERROR_NOT_SUPPORTED;
593       break;
594 #endif
595
596 #ifdef EAFNOSUPPORT
597     case EAFNOSUPPORT:
598       return GST_SPARSE_FILE_IO_ERROR_NOT_SUPPORTED;
599       break;
600 #endif
601
602 #ifdef ETIMEDOUT
603     case ETIMEDOUT:
604       return GST_SPARSE_FILE_IO_ERROR_TIMED_OUT;
605       break;
606 #endif
607
608 #ifdef EBUSY
609     case EBUSY:
610       return GST_SPARSE_FILE_IO_ERROR_BUSY;
611       break;
612 #endif
613
614 #ifdef EWOULDBLOCK
615     case EWOULDBLOCK:
616       return GST_SPARSE_FILE_IO_ERROR_WOULD_BLOCK;
617       break;
618 #endif
619
620       /* EWOULDBLOCK == EAGAIN on most systems, but POSIX considers them distinct */
621 #if defined (EAGAIN) && (!defined (EWOULDBLOCK) || (EWOULDBLOCK != EAGAIN))
622     case EAGAIN:
623       return GST_SPARSE_FILE_IO_ERROR_WOULD_BLOCK;
624       break;
625 #endif
626
627 #ifdef EMFILE
628     case EMFILE:
629       return GST_SPARSE_FILE_IO_ERROR_TOO_MANY_OPEN_FILES;
630       break;
631 #endif
632
633 #ifdef EADDRINUSE
634     case EADDRINUSE:
635       return GST_SPARSE_FILE_IO_ERROR_ADDRESS_IN_USE;
636       break;
637 #endif
638
639 #ifdef EHOSTUNREACH
640     case EHOSTUNREACH:
641       return GST_SPARSE_FILE_IO_ERROR_HOST_UNREACHABLE;
642       break;
643 #endif
644
645 #ifdef ENETUNREACH
646     case ENETUNREACH:
647       return GST_SPARSE_FILE_IO_ERROR_NETWORK_UNREACHABLE;
648       break;
649 #endif
650
651 #ifdef ECONNREFUSED
652     case ECONNREFUSED:
653       return GST_SPARSE_FILE_IO_ERROR_CONNECTION_REFUSED;
654       break;
655 #endif
656
657 #ifdef EPIPE
658     case EPIPE:
659       return GST_SPARSE_FILE_IO_ERROR_BROKEN_PIPE;
660       break;
661 #endif
662
663     default:
664       return GST_SPARSE_FILE_IO_ERROR_FAILED;
665       break;
666   }
667 }