]> git.sesse.net Git - vlc/blobdiff - modules/access_filter/timeshift.c
Check asprintf return value.
[vlc] / modules / access_filter / timeshift.c
index 9693b89d38779dd149f6cac8737dfb6c34ce154d..924744a5aff2ebfb7497f7531f25de3a2e8c2363 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 
 #include <errno.h>
 
 
 #include <unistd.h>
 
+#ifdef WIN32
+#  include <direct.h>                                        /* _wgetcwd  */
+#endif
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -54,8 +62,8 @@ static void Close( vlc_object_t * );
   "access declares that it can control pace or pause." )
 
 vlc_module_begin();
-    set_shortname( _("Timeshift") );
-    set_description( _("Timeshift") );
+    set_shortname( N_("Timeshift") );
+    set_description( N_("Timeshift") );
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
     set_capability( "access_filter", 0 );
@@ -63,10 +71,11 @@ vlc_module_begin();
     set_callbacks( Open, Close );
 
     add_integer( "timeshift-granularity", 50, NULL, GRANULARITY_TEXT,
-                 GRANULARITY_LONGTEXT, VLC_TRUE );
-    add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, VLC_FALSE );
-    add_bool( "timeshift-force", VLC_FALSE, NULL, FORCE_TEXT, FORCE_LONGTEXT,
-              VLC_FALSE );
+                 GRANULARITY_LONGTEXT, true );
+    add_directory( "timeshift-dir", 0, 0, DIR_TEXT, DIR_LONGTEXT, false );
+        change_unsafe();
+    add_bool( "timeshift-force", false, NULL, FORCE_TEXT, FORCE_LONGTEXT,
+              false );
 vlc_module_end();
 
 /*****************************************************************************
@@ -76,7 +85,7 @@ vlc_module_end();
 static int      Seek( access_t *, int64_t );
 static block_t *Block  ( access_t *p_access );
 static int      Control( access_t *, int i_query, va_list args );
-static void     Thread ( access_t *p_access );
+static void*    Thread ( vlc_object_t *p_this );
 static int      WriteBlockToFile( access_t *p_access, block_t *p_block );
 static block_t *ReadBlockFromFile( access_t *p_access );
 static char    *GetTmpFilePath( access_t *p_access );
@@ -119,7 +128,7 @@ static int Open( vlc_object_t *p_this )
     access_t *p_access = (access_t*)p_this;
     access_t *p_src = p_access->p_source;
     access_sys_t *p_sys;
-    vlc_bool_t b_bool;
+    bool b_bool;
 
     var_Create( p_access, "timeshift-force", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
     if( var_GetBool( p_access, "timeshift-force" ) )
@@ -129,14 +138,14 @@ static int Open( vlc_object_t *p_this )
     else
     {
         /* Only work with not pace controled access */
-        if( access2_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
+        if( access_Control( p_src, ACCESS_CAN_CONTROL_PACE, &b_bool ) ||
             b_bool )
         {
             msg_Dbg( p_src, "ACCESS_CAN_CONTROL_PACE: timeshift useless" );
             return VLC_EGENERIC;
         }
         /* Refuse access that can be paused */
-        if( access2_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
+        if( access_Control( p_src, ACCESS_CAN_PAUSE, &b_bool ) || b_bool )
         {
             msg_Dbg( p_src, "ACCESS_CAN_PAUSE: timeshift useless" );
             return VLC_EGENERIC;
@@ -153,7 +162,7 @@ static int Open( vlc_object_t *p_this )
     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
 
     /* */
-    p_sys->p_fifo = block_FifoNew( p_access );
+    p_sys->p_fifo = block_FifoNew();
     p_sys->i_write_size = 0;
     p_sys->i_files = 0;
     p_sys->i_data = 0;
@@ -175,7 +184,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->psz_filename = malloc( strlen( p_sys->psz_filename_base ) + 1000 );
 
     if( vlc_thread_create( p_access, "timeshift thread", Thread,
-                           VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
+                           VLC_THREAD_PRIORITY_LOW, false ) )
     {
         msg_Err( p_access, "cannot spawn timeshift access thread" );
         return VLC_EGENERIC;
@@ -260,13 +269,14 @@ static block_t *Block( access_t *p_access )
 /*****************************************************************************
  *
  *****************************************************************************/
-static void Thread( access_t *p_access )
+static void* Thread( vlc_object_t* p_this )
 {
+    access_t *p_access = (access_t*)p_this;
     access_sys_t *p_sys = p_access->p_sys;
     access_t     *p_src = p_access->p_source;
     block_t      *p_block;
 
-    while( !p_access->b_die )
+    while( vlc_object_alive (p_access) )
     {
         /* Get a new block from the source */
         if( p_src->pf_block )
@@ -298,7 +308,7 @@ static void Thread( access_t *p_access )
 
         /* Write block */
         if( !p_sys->p_write_list && !p_sys->p_read_list &&
-            p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MAX )
+            block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MAX )
         {
             /* If there isn't too much timeshifted data,
              * write directly to FIFO */
@@ -310,8 +320,8 @@ static void Thread( access_t *p_access )
         block_Release( p_block );
 
         /* Read from file to fill up the fifo */
-        while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
-               !p_access->b_die )
+        while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
+               vlc_object_alive (p_access) )
         {
             p_block = ReadBlockFromFile( p_access );
             if( !p_block ) break;
@@ -322,12 +332,12 @@ static void Thread( access_t *p_access )
 
     msg_Dbg( p_access, "timeshift: no more input data" );
 
-    while( !p_access->b_die &&
-           (p_sys->p_read_list || p_sys->p_fifo->i_size) )
+    while( vlc_object_alive (p_access) &&
+           (p_sys->p_read_list || block_FifoSize( p_sys->p_fifo ) ) )
     {
         /* Read from file to fill up the fifo */
-        while( p_sys->p_fifo->i_size < TIMESHIFT_FIFO_MIN &&
-               !p_access->b_die && p_sys->p_read_list )
+        while( block_FifoSize( p_sys->p_fifo ) < TIMESHIFT_FIFO_MIN &&
+               vlc_object_alive (p_access) && p_sys->p_read_list )
         {
             p_block = ReadBlockFromFile( p_access );
             if( !p_block ) break;
@@ -339,10 +349,11 @@ static void Thread( access_t *p_access )
     }
 
     msg_Dbg( p_access, "timeshift: EOF" );
-    p_src->info.b_eof = VLC_TRUE;
+    p_src->info.b_eof = true;
 
     /* Send dummy packet to avoid deadlock in Block() */
     block_FifoPut( p_sys->p_fifo, block_New( p_access, 0 ) );
+    return NULL;
 }
 
 /*****************************************************************************
@@ -422,8 +433,8 @@ static int WriteBlockToFile( access_t *p_access, block_t *p_block )
         if( !file && p_sys->i_files < 2 )
         {
             /* We just can't work with less than 2 buffer files */
-            msg_Err( p_access, "cannot open temporary file '%s' (%s)",
-                     p_sys->psz_filename, strerror(errno) );
+            msg_Err( p_access, "cannot open temporary file '%s' (%m)",
+                     p_sys->psz_filename );
             return VLC_EGENERIC;
         }
         else if( !file ) return VLC_EGENERIC;
@@ -515,21 +526,21 @@ static int Seek( access_t *p_access, int64_t i_pos )
  *****************************************************************************/
 static int Control( access_t *p_access, int i_query, va_list args )
 {
-    vlc_bool_t   *pb_bool;
+    bool   *pb_bool;
     int          *pi_int;
 
     switch( i_query )
     {
     case ACCESS_CAN_SEEK:
     case ACCESS_CAN_FASTSEEK:
-        pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
-        *pb_bool = VLC_TRUE;
+        pb_bool = (bool*)va_arg( args, bool* );
+        *pb_bool = true;
         break;
 
     case ACCESS_CAN_CONTROL_PACE:   /* Not really true */
     case ACCESS_CAN_PAUSE:
-        pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
-        *pb_bool = VLC_TRUE;
+        pb_bool = (bool*)va_arg( args, bool* );
+        *pb_bool = true;
         break;
 
     case ACCESS_GET_MTU:
@@ -542,7 +553,7 @@ static int Control( access_t *p_access, int i_query, va_list args )
 
     /* Forward everything else to the source access */
     default:
-        return access2_vaControl( p_access->p_source, i_query, args );
+        return access_vaControl( p_access->p_source, i_query, args );
     }
     return VLC_SUCCESS;
 }
@@ -551,19 +562,13 @@ static int Control( access_t *p_access, int i_query, va_list args )
  * GetTmpFilePath:
  *****************************************************************************/
 #ifdef WIN32
-#define getpid() GetCurrentProcessId()
+#define getpid() (int)GetCurrentProcessId()
 #endif
 static char *GetTmpFilePath( access_t *p_access )
 {
-    char *psz_dir = var_GetString( p_access, "timeshift-dir" );
+    char *psz_dir = var_GetNonEmptyString( p_access, "timeshift-dir" );
     char *psz_filename_base;
 
-    if( ( psz_dir != NULL ) && ( psz_dir[0] == '\0' ) )
-    {
-        free( psz_dir );
-        psz_dir = NULL;
-    }
-
     if( psz_dir == NULL )
     {
 #ifdef WIN32
@@ -591,8 +596,9 @@ static char *GetTmpFilePath( access_t *p_access )
 #endif
     }
 
-    asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
-              psz_dir, getpid(), p_access->i_object_id );
+    if( asprintf( &psz_filename_base, "%s/vlc-timeshift-%d-%d-",
+              psz_dir, getpid(), p_access->i_object_id ) == -1 )
+        psz_filename_base = NULL;
     free( psz_dir );
 
     return psz_filename_base;