filesrc: do not mistake short reads for EOS
authorVincent Penquerc'h <vincent.penquerch@collabora.co.uk>
Mon, 12 Dec 2011 13:05:36 +0000 (13:05 +0000)
committerVincent Penquerc'h <vincent.penquerch@collabora.co.uk>
Mon, 12 Dec 2011 13:35:06 +0000 (13:35 +0000)
While local filesystems will usually not cause short reads,
this may happen on seekable files on some remote filesystems.
Instead, loop till we get the requested amount of data, or
an actual EOS (ie, 0 bytes).

https://bugzilla.gnome.org/show_bug.cgi?id=665921

plugins/elements/gstfilesrc.c

index f8dbfe2..d2b88c6 100644 (file)
@@ -800,6 +800,7 @@ gst_file_src_create_read (GstFileSrc * src, guint64 offset, guint length,
 {
   int ret;
   GstBuffer *buf;
+  guint64 woffset;
 
   if (G_UNLIKELY (src->read_position != offset)) {
     off_t res;
@@ -818,27 +819,31 @@ gst_file_src_create_read (GstFileSrc * src, guint64 offset, guint length,
   }
 
   /* No need to read anything if length is 0 */
-  if (length > 0) {
+  GST_BUFFER_SIZE (buf) = 0;
+  GST_BUFFER_OFFSET (buf) = offset;
+  GST_BUFFER_OFFSET_END (buf) = offset;
+  woffset = 0;
+  while (length > 0) {
     GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
-        length, offset);
-    ret = read (src->fd, GST_BUFFER_DATA (buf), length);
-    if (G_UNLIKELY (ret < 0))
+        length, offset + woffset);
+    errno = 0;
+    ret = read (src->fd, GST_BUFFER_DATA (buf) + woffset, length);
+    if (G_UNLIKELY (ret < 0)) {
+      if (errno == EAGAIN || errno == EINTR)
+        continue;
       goto could_not_read;
+    }
 
-    /* seekable regular files should have given us what we expected */
-    if (G_UNLIKELY ((guint) ret < length && src->seekable))
-      goto unexpected_eos;
-
-    /* other files should eos if they read 0 and more was requested */
-    if (G_UNLIKELY (ret == 0 && length > 0))
+    /* files should eos if they read 0 and more was requested */
+    if (G_UNLIKELY (ret == 0))
       goto eos;
 
-    length = ret;
-    GST_BUFFER_SIZE (buf) = length;
-    GST_BUFFER_OFFSET (buf) = offset;
-    GST_BUFFER_OFFSET_END (buf) = offset + length;
+    length -= ret;
+    woffset += ret;
+    GST_BUFFER_SIZE (buf) += ret;
+    GST_BUFFER_OFFSET_END (buf) += ret;
 
-    src->read_position += length;
+    src->read_position += ret;
   }
 
   *buffer = buf;
@@ -857,16 +862,9 @@ could_not_read:
     gst_buffer_unref (buf);
     return GST_FLOW_ERROR;
   }
-unexpected_eos:
-  {
-    GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
-        ("unexpected end of file."));
-    gst_buffer_unref (buf);
-    return GST_FLOW_ERROR;
-  }
 eos:
   {
-    GST_DEBUG ("non-regular file hits EOS");
+    GST_DEBUG ("EOS");
     gst_buffer_unref (buf);
     return GST_FLOW_UNEXPECTED;
   }