]> git.sesse.net Git - vlc/blobdiff - modules/access_filter/dump.c
During the compilation, print errors and warnings on stderr.
[vlc] / modules / access_filter / dump.c
index cd1664b40405474e9cee368c2f64dc429ad589b3..c2881496c9fdeafcfc40dc06e6fd0daaede3f6d4 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <assert.h>
 #include <time.h>
+#include <errno.h>
+
+#include <vlc_access.h>
 
-#include "vlc_access.h"
-#include "vlc_block.h"
-#include "charset.h"
+#include <vlc_charset.h>
 #include "vlc_keys.h"
 
 #define DEFAULT_MARGIN 32 // megabytes
@@ -61,7 +64,7 @@ vlc_module_begin ();
                  MARGIN_LONGTEXT, VLC_FALSE);
 vlc_module_end();
 
-static int Read (access_t *access, uint8_t *buffer, int len);
+static ssize_t Read (access_t *access, uint8_t *buffer, size_t len);
 static block_t *Block (access_t *access);
 static int Seek (access_t *access, int64_t offset);
 static int Control (access_t *access, int cmd, va_list ap);
@@ -74,6 +77,7 @@ struct access_sys_t
 {
     FILE *stream;
     int64_t tmp_max;
+    int64_t dumpsize;
 };
 
 /**
@@ -111,7 +115,7 @@ static int Open (vlc_object_t *obj)
 
     if ((p_sys->stream = tmpfile ()) == NULL)
     {
-        msg_Err (access, "cannot create temporary file: %s", strerror (errno));
+        msg_Err (access, "cannot create temporary file: %m");
         free (p_sys);
         return VLC_EGENERIC;
     }
@@ -144,21 +148,34 @@ static void Dump (access_t *access, const uint8_t *buffer, size_t len)
     access_sys_t *p_sys = access->p_sys;
     FILE *stream = p_sys->stream;
 
-    if ((stream == NULL) || (len == 0))
+    if ((stream == NULL) /* not dumping */
+     || (access->info.i_pos < p_sys->dumpsize) /* already known data */)
         return;
 
+    size_t needed = access->info.i_pos - p_sys->dumpsize;
+    if (len < needed)
+        return; /* gap between data and dump offset (seek too far ahead?) */
+
+    buffer += len - needed;
+    len = needed;
+
+    if (len == 0)
+        return; /* no useful data */
+
     if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max))
     {
         msg_Dbg (access, "too much data - dump will not work");
         goto error;
     }
 
+    assert (len > 0);
     if (fwrite (buffer, len, 1, stream) != 1)
     {
-        msg_Err (access, "cannot write to file: %s", strerror (errno));
+        msg_Err (access, "cannot write to file: %m");
         goto error;
     }
 
+    p_sys->dumpsize += len;
     return;
 
 error:
@@ -167,7 +184,7 @@ error:
 }
 
 
-static int Read (access_t *access, uint8_t *buffer, int len)
+static ssize_t Read (access_t *access, uint8_t *buffer, size_t len)
 {
     access_t *src = access->p_source;
 
@@ -176,7 +193,6 @@ static int Read (access_t *access, uint8_t *buffer, int len)
     access->info = src->info;
 
     Dump (access, buffer, len);
-    //Trigger (access);
 
     return len;
 }
@@ -220,11 +236,7 @@ static int Seek (access_t *access, int64_t offset)
     }
 
     if (p_sys->stream != NULL)
-    {
-        msg_Dbg (access, "seeking - dump will not work");
-        fclose (p_sys->stream);
-        p_sys->stream = NULL;
-    }
+        msg_Dbg (access, "seeking - dump might not work");
 
     src->info.i_update = access->info.i_update;
     int ret = src->pf_seek (src, offset);
@@ -241,7 +253,7 @@ static inline struct tm *localtime_r (const time_t *now, struct tm *res)
      * This is not thread-safe. Blame your C library.
      * On Win32 there SHOULD be _localtime_s instead, but of course
      * Cygwin and Mingw32 don't know about it. You're on your own if you
-     * this platform.
+     * use this platform.
      */
     if (unsafe == NULL)
         return NULL;
@@ -283,11 +295,10 @@ static void Trigger (access_t *access)
 
     msg_Info (access, "dumping media to \"%s\"...", filename);
 
-    FILE *newstream = fopen (filename, "wb");
+    FILE *newstream = utf8_fopen (filename, "wb");
     if (newstream == NULL)
     {
-        msg_Err (access, "cannot create dump file \"%s\": %s", filename,
-                 strerror (errno));
+        msg_Err (access, "cannot create dump file \"%s\": %m", filename);
         return;
     }
 
@@ -303,8 +314,7 @@ static void Trigger (access_t *access)
         {
             if (ferror (oldstream))
             {
-                msg_Err (access, "cannot read temporary file: %s",
-                         strerror (errno));
+                msg_Err (access, "cannot read temporary file: %m");
                 break;
             }
 
@@ -317,7 +327,7 @@ static void Trigger (access_t *access)
 
         if (fwrite (buf, len, 1, newstream) != 1)
         {
-            msg_Err (access, "cannot write dump file: %s", strerror (errno));
+            msg_Err (access, "cannot write dump file: %m");
             break;
         }
     }