]> git.sesse.net Git - vlc/blobdiff - modules/access/ftp.c
logger: don't reinvent stdio, kill a few relocations
[vlc] / modules / access / ftp.c
index d1512d9853eece8e734d25886e0b1b51660621a7..79cf667b14c24daa7c782e3cd710f4ba19e3dd27 100644 (file)
@@ -30,7 +30,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_plugin.h>
 
 #include <assert.h>
@@ -111,6 +111,7 @@ struct access_sys_t
     int        fd_data;
 
     char       sz_epsv_ip[NI_MAXNUMERICHOST];
+    bool       out;
 };
 #define GET_OUT_SYS( p_this ) \
     ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
@@ -152,6 +153,8 @@ static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
         psz = strdup( p_sys->url.psz_username );
     else
         psz = var_CreateGetString( p_access, "ftp-user" );
+    if( !psz )
+        return -1;
 
     if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
         ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
@@ -172,6 +175,8 @@ static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
                 psz = strdup( p_sys->url.psz_password );
             else
                 psz = var_CreateGetString( p_access, "ftp-pwd" );
+            if( !psz )
+                return -1;
 
             if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
                 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
@@ -282,7 +287,7 @@ static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
 static int parseURL( vlc_url_t *url, const char *path )
 {
     if( path == NULL )
-        return -1;
+        return VLC_EGENERIC;
 
     /* *** Parse URL and get server addr/port and path *** */
     while( *path == '/' )
@@ -291,7 +296,7 @@ static int parseURL( vlc_url_t *url, const char *path )
     vlc_UrlParse( url, path, 0 );
 
     if( url->psz_host == NULL || *url->psz_host == '\0' )
-        return -1;
+        return VLC_EGENERIC;
 
     if( url->i_port <= 0 )
         url->i_port = IPPORT_FTP; /* default port */
@@ -299,10 +304,10 @@ static int parseURL( vlc_url_t *url, const char *path )
     /* FTP URLs are relative to user's default directory (RFC1738)
     For absolute path use ftp://foo.bar//usr/local/etc/filename */
 
-    if( *url->psz_path == '/' )
+    if( url->psz_path && *url->psz_path == '/' )
         url->psz_path++;
 
-    return 0;
+    return VLC_SUCCESS;
 }
 
 
@@ -318,6 +323,7 @@ static int InOpen( vlc_object_t *p_this )
     /* Init p_access */
     STANDARD_READ_ACCESS_INIT
     p_sys->fd_data = -1;
+    p_sys->out = false;
 
     if( parseURL( &p_sys->url, p_access->psz_path ) )
         goto exit_error;
@@ -326,7 +332,7 @@ static int InOpen( vlc_object_t *p_this )
         goto exit_error;
 
     /* get size */
-    if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 ||
+    if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ? : "" ) < 0 ||
         ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
     {
         msg_Err( p_access, "cannot get file size" );
@@ -368,6 +374,7 @@ static int OutOpen( vlc_object_t *p_this )
 
     /* Init p_access */
     p_sys->fd_data = -1;
+    p_sys->out = true;
 
     if( parseURL( &p_sys->url, p_access->psz_path ) )
         goto exit_error;
@@ -472,7 +479,7 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
     int i_read;
 
     assert( p_sys->fd_data != -1 );
-    assert( p_access->i_object_type == VLC_OBJECT_ACCESS );
+    assert( !p_sys->out );
 
     if( p_access->info.b_eof )
         return 0;
@@ -586,10 +593,13 @@ static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,
     char         *psz_cmd;
 
     va_start( args, psz_fmt );
-    vasprintf( &psz_cmd, psz_fmt, args );
+    if( vasprintf( &psz_cmd, psz_fmt, args ) == -1 )
+        return VLC_EGENERIC;
+
     va_end( args );
 
     msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
+
     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
                     psz_cmd ) < 0 )
     {
@@ -755,17 +765,15 @@ static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
 
     /* "1xx" message */
     if( ftp_SendCommand( p_access, p_sys, "%s %s",
-                         (p_access->i_object_type == VLC_OBJECT_ACCESS)
-                                 ? "RETR" : "STOR",
-                         p_sys->url.psz_path ) < 0 ||
+                         p_sys->out ? "STOR" : "RETR",
+                         p_sys->url.psz_path ?: "" ) < 0 ||
         ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
     {
         msg_Err( p_access, "cannot retrieve file" );
         return VLC_EGENERIC;
     }
 
-    shutdown( p_sys->fd_data,
-              ( p_access->i_object_type == VLC_OBJECT_ACCESS ) );
+    shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );
 
     return VLC_SUCCESS;
 }
@@ -783,16 +791,13 @@ static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
 
     if( p_sys->fd_data != -1 )
     {
-        int i_answer;
-        ftp_ReadCommand( p_access, p_sys, &i_answer, NULL );
-        if ( i_answer != 227 )
-            /* If answer is from the previous command,
-             * rathen that succesful ABOR - read next command */
-            ftp_ReadCommand( p_access, p_sys, NULL, NULL );
-
         net_Close( p_sys->fd_data );
         p_sys->fd_data = -1;
+        /* Read the final response from RETR/STOR, i.e. 426 or 226 */
+        ftp_ReadCommand( p_access, p_sys, NULL, NULL );
     }
+    /* Read the response from ABOR, i.e. 226 or 225 */
+    ftp_ReadCommand( p_access, p_sys, NULL, NULL );
 
     return VLC_SUCCESS;
 }