[vlc-commits] smb: reorder to avoid forward declarations

Rémi Denis-Courmont git at videolan.org
Sun Mar 18 18:40:08 CET 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Mar 18 19:16:16 2018 +0200| [a0278c29ba73a6ea817b11c1485752e589c8ff50] | committer: Rémi Denis-Courmont

smb: reorder to avoid forward declarations

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=a0278c29ba73a6ea817b11c1485752e589c8ff50
---

 modules/access/smb.c | 402 ++++++++++++++++++++++++---------------------------
 1 file changed, 189 insertions(+), 213 deletions(-)

diff --git a/modules/access/smb.c b/modules/access/smb.c
index 17dbe466d5..19dd3b3dac 100644
--- a/modules/access/smb.c
+++ b/modules/access/smb.c
@@ -56,38 +56,6 @@
 
 #include "smb_common.h"
 
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
-static int  Open ( vlc_object_t * );
-static void Close( vlc_object_t * );
-
-#define SMB_HELP N_("Samba (Windows network shares) input")
-vlc_module_begin ()
-    set_shortname( "SMB" )
-    set_description( N_("SMB input") )
-    set_help(SMB_HELP)
-    set_capability( "access", 0 )
-    set_category( CAT_INPUT )
-    set_subcategory( SUBCAT_INPUT_ACCESS )
-    add_string( "smb-user", NULL, SMB_USER_TEXT, SMB_USER_LONGTEXT,
-                false )
-    add_password( "smb-pwd", NULL, SMB_PASS_TEXT,
-                  SMB_PASS_LONGTEXT, false )
-    add_string( "smb-domain", NULL, SMB_DOMAIN_TEXT,
-                SMB_DOMAIN_LONGTEXT, false )
-    add_shortcut( "smb" )
-    set_callbacks( Open, Close )
-vlc_module_end ()
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static ssize_t Read( stream_t *, void *, size_t );
-static int Seek( stream_t *, uint64_t );
-static int Control( stream_t *, int, va_list );
-static int DirRead( stream_t *, input_item_node_t * );
-
 struct access_sys_t
 {
     int i_smb;
@@ -96,22 +64,44 @@ struct access_sys_t
 };
 
 #ifdef _WIN32
-static void Win32AddConnection( stream_t *, const char *, const char *, const char *, const char *, const char * );
-#else
-static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
-                      char *un, int unlen, char *pw, int pwlen )
+static void Win32AddConnection(stream_t *access, const char *server,
+                               const char *share, const char *user,
+                               const char *pwd, const char *domain)
 {
-    VLC_UNUSED(srv);
-    VLC_UNUSED(shr);
-    VLC_UNUSED(wg);
-    VLC_UNUSED(wglen);
-    VLC_UNUSED(un);
-    VLC_UNUSED(unlen);
-    VLC_UNUSED(pw);
-    VLC_UNUSED(pwlen);
-    //wglen = unlen = pwlen = 0;
+    NETRESOURCE net_resource;
+    char remote_name[MAX_PATH];
+
+    VLC_UNUSED(domain);
+
+    memset(&net_resource, 0, sizeof (net_resource));
+    net_resource.dwType = RESOURCETYPE_DISK;
+
+    snprintf(remote_name, sizeof (remote_name), "\\\\%s\\%s", server,
+             (share != NULL) ? share + 1 /* skip leading '/' */: "");
+
+    /* remove trailings '/' */
+    char *delim = strchr(remote_name, '/');
+    if (delim != NULL)
+        *delim = '\0';
+
+    const char *msg;
+    net_resource.lpRemoteName = remote_name;
+
+    switch (WNetAddConnection2(&net_resource, pwd, user, 0))
+    {
+        case NO_ERROR:
+            msg = "connected to %s";
+            break;
+        case ERROR_ALREADY_ASSIGNED:
+        case ERROR_DEVICE_ALREADY_REMEMBERED:
+            msg = "already connected to %s";
+            break;
+        default:
+            msg = "failed to connect to %s";
+    }
+    msg_Dbg(access, msg, remote_name);
 }
-#endif
+#endif // _WIN32
 
 /* Build an SMB URI
  * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
@@ -144,145 +134,6 @@ static int smb_get_uri( stream_t *p_access, char **ppsz_uri,
 #endif
 }
 
-/****************************************************************************
- * Open: connect to smb server and ask for file
- ****************************************************************************/
-static int Open( vlc_object_t *p_this )
-{
-    stream_t     *p_access = (stream_t*)p_this;
-    access_sys_t *p_sys;
-    struct stat  filestat;
-    vlc_url_t    url;
-    vlc_credential credential;
-    char         *psz_decoded_path = NULL, *psz_uri = NULL,
-                 *psz_var_domain = NULL;
-    int          i_ret;
-    int          i_smb;
-    uint64_t     i_size;
-    bool         b_is_dir = false;
-
-#ifndef _WIN32
-    if( smbc_init( smb_auth, 0 ) )
-        return VLC_EGENERIC;
-#endif
-
-    if( vlc_UrlParseFixup( &url, p_access->psz_url ) != 0 )
-    {
-        vlc_UrlClean( &url );
-        return VLC_EGENERIC;
-    }
-    if( url.psz_path )
-    {
-        psz_decoded_path = vlc_uri_decode_duplicate( url.psz_path );
-        if( !psz_decoded_path )
-        {
-            vlc_UrlClean( &url );
-            return VLC_EGENERIC;
-        }
-    }
-
-    vlc_credential_init( &credential, &url );
-    psz_var_domain = var_InheritString( p_access, "smb-domain" );
-    credential.psz_realm = psz_var_domain;
-    vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
-                        NULL, NULL );
-    for (;;)
-    {
-        if( smb_get_uri( p_access, &psz_uri, credential.psz_realm,
-                         credential.psz_username, credential.psz_password,
-                         url.psz_host, psz_decoded_path, NULL ) == -1 )
-        {
-            vlc_credential_clean( &credential );
-            free(psz_var_domain);
-            free( psz_decoded_path );
-            vlc_UrlClean( &url );
-            return VLC_ENOMEM;
-        }
-
-        if( ( i_ret = smbc_stat( psz_uri, &filestat ) ) && errno == EACCES )
-        {
-            errno = 0;
-            if( vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
-                                    SMB_LOGIN_DIALOG_TITLE,
-                                    SMB_LOGIN_DIALOG_TEXT, url.psz_host) )
-                continue;
-        }
-
-        /* smbc_stat fails with servers or shares. Assume they are directory */
-        if( i_ret || S_ISDIR( filestat.st_mode ) )
-            b_is_dir = true;
-        break;
-    }
-
-    vlc_credential_store( &credential, p_access );
-    vlc_credential_clean( &credential );
-    free(psz_var_domain);
-    free( psz_decoded_path );
-
-    /* Init p_access */
-    p_sys =
-    p_access->p_sys = vlc_obj_calloc( p_this, 1, sizeof( access_sys_t ) );
-    if( !p_sys )
-    {
-        free( psz_uri );
-        vlc_UrlClean( &url );
-        return VLC_ENOMEM;
-    }
-
-    if( b_is_dir )
-    {
-        p_sys->url = url;
-        p_access->pf_readdir = DirRead;
-        p_access->pf_control = access_vaDirectoryControlHelper;
-        i_size = 0;
-#ifndef _WIN32
-        i_smb = smbc_opendir( psz_uri );
-        if( i_smb < 0 )
-            vlc_UrlClean( &p_sys->url );
-#endif
-    }
-    else
-    {
-        ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek );
-        i_smb = smbc_open( psz_uri, O_RDONLY, 0 );
-        i_size = filestat.st_size;
-        vlc_UrlClean( &url );
-    }
-    free( psz_uri );
-
-#ifndef _WIN32
-    if( i_smb < 0 )
-    {
-        msg_Err( p_access, "open failed for '%s' (%s)",
-                 p_access->psz_location, vlc_strerror_c(errno) );
-        return VLC_EGENERIC;
-    }
-#endif
-
-    p_sys->size = i_size;
-    p_sys->i_smb = i_smb;
-
-    return VLC_SUCCESS;
-}
-
-/*****************************************************************************
- * Close: free unused data structures
- *****************************************************************************/
-static void Close( vlc_object_t *p_this )
-{
-    stream_t     *p_access = (stream_t*)p_this;
-    access_sys_t *p_sys = p_access->p_sys;
-
-    vlc_UrlClean( &p_sys->url );
-
-#ifndef _WIN32
-    if( p_access->pf_readdir )
-        smbc_closedir( p_sys->i_smb );
-    else
-#endif
-        smbc_close( p_sys->i_smb );
-}
-
 /*****************************************************************************
  * Seek: try to go at the right place
  *****************************************************************************/
@@ -499,46 +350,171 @@ static int Control( stream_t *p_access, int i_query, va_list args )
     return VLC_SUCCESS;
 }
 
-#ifdef _WIN32
-static void Win32AddConnection( stream_t *p_access, const char *psz_server,
-                                const char *psz_share, const char *psz_user,
-                                const char *psz_pwd, const char *psz_domain )
+#ifndef _WIN32
+static void smb_auth(const char *srv, const char *shr, char *wg, int wglen,
+                     char *un, int unlen, char *pw, int pwlen)
 {
-    char psz_remote[MAX_PATH];
-    NETRESOURCE net_resource;
-    DWORD i_result;
-    VLC_UNUSED( psz_domain );
+    VLC_UNUSED(srv);
+    VLC_UNUSED(shr);
+    VLC_UNUSED(wg);
+    VLC_UNUSED(wglen);
+    VLC_UNUSED(un);
+    VLC_UNUSED(unlen);
+    VLC_UNUSED(pw);
+    VLC_UNUSED(pwlen);
+    //wglen = unlen = pwlen = 0;
+}
+#endif
 
-    memset( &net_resource, 0, sizeof(net_resource) );
-    net_resource.dwType = RESOURCETYPE_DISK;
+static int Open(vlc_object_t *obj)
+{
+    stream_t *access = (stream_t *)obj;
+    vlc_url_t url;
+    vlc_credential credential;
+    char *psz_decoded_path = NULL, *psz_uri = NULL, *psz_var_domain = NULL;
+    int fd;
+    uint64_t size;
+    bool is_dir;
 
-    if (psz_share)
-        psz_share = psz_share + 1; /* skip first '/' */
-    else
-        psz_share = "";
+#ifndef _WIN32
+    if (smbc_init(smb_auth, 0))
+        return VLC_EGENERIC;
+#endif
 
-    snprintf( psz_remote, sizeof( psz_remote ), "\\\\%s\\%s", psz_server, psz_share );
-    /* remove trailings '/' */
-    char *psz_delim = strchr( psz_remote, '/' );
-    if( psz_delim )
-        *psz_delim = '\0';
+    if (vlc_UrlParseFixup(&url, access->psz_url) != 0)
+    {
+        vlc_UrlClean(&url);
+        return VLC_EGENERIC;
+    }
 
-    net_resource.lpRemoteName = psz_remote;
+    if (url.psz_path != NULL)
+    {
+        psz_decoded_path = vlc_uri_decode_duplicate(url.psz_path);
+        if (psz_decoded_path == NULL)
+        {
+            vlc_UrlClean(&url);
+            return VLC_EGENERIC;
+        }
+    }
 
-    i_result = WNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
+    vlc_credential_init(&credential, &url);
+    psz_var_domain = var_InheritString(access, "smb-domain");
+    credential.psz_realm = psz_var_domain;
+    vlc_credential_get(&credential, access, "smb-user", "smb-pwd", NULL, NULL);
 
-    if( i_result == NO_ERROR )
+    for (;;)
     {
-        msg_Dbg( p_access, "connected to %s", psz_remote );
+        struct stat st;
+
+        if (smb_get_uri(access, &psz_uri, credential.psz_realm,
+                        credential.psz_username, credential.psz_password,
+                        url.psz_host, psz_decoded_path, NULL ) == -1 )
+        {
+            vlc_credential_clean(&credential);
+            free(psz_var_domain);
+            free(psz_decoded_path);
+            vlc_UrlClean(&url);
+            return VLC_ENOMEM;
+        }
+
+        if (smbc_stat(psz_uri, &st) == 0)
+        {
+            is_dir = S_ISDIR(st.st_mode) != 0;
+            size = st.st_size;
+            break;
+        }
+
+        /* smbc_stat() fails with servers or shares. Assume directory. */
+        is_dir = true;
+        size = 0;
+
+        if (errno != EACCES)
+            break;
+
+        errno = 0;
+        if (!vlc_credential_get(&credential, access, "smb-user",
+                                "smb-pwd", SMB_LOGIN_DIALOG_TITLE,
+                                SMB_LOGIN_DIALOG_TEXT, url.psz_host))
+            break;
     }
-    else if( i_result != ERROR_ALREADY_ASSIGNED &&
-             i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
+
+    vlc_credential_store(&credential, access);
+    vlc_credential_clean(&credential);
+    free(psz_var_domain);
+    free(psz_decoded_path);
+
+    /* Init access */
+    access_sys_t *sys = vlc_obj_calloc(obj, 1, sizeof (*sys));
+    if (unlikely(sys == NULL))
     {
-        msg_Dbg( p_access, "already connected to %s", psz_remote );
+        free(psz_uri);
+        vlc_UrlClean(&url);
+        return VLC_ENOMEM;
+    }
+
+    access->p_sys = sys;
+
+    if (is_dir)
+    {
+        sys->url = url;
+        access->pf_readdir = DirRead;
+        access->pf_control = access_vaDirectoryControlHelper;
+#ifndef _WIN32
+        fd = smbc_opendir(psz_uri);
+        if (fd < 0)
+            vlc_UrlClean(&sys->url);
+#endif
     }
     else
     {
-        msg_Dbg( p_access, "failed to connect to %s", psz_remote );
+        access->pf_read = Read;
+        access->pf_control = Control;
+        access->pf_seek = Seek;
+        fd = smbc_open(psz_uri, O_RDONLY, 0);
+        vlc_UrlClean(&url);
     }
+    free(psz_uri);
+
+#ifndef _WIN32
+    if (fd < 0)
+    {
+        msg_Err(obj, "cannot open %s: %s",
+                access->psz_location, vlc_strerror_c(errno));
+        return VLC_EGENERIC;
+    }
+#endif
+
+    sys->size = size;
+    sys->i_smb = fd;
+
+    return VLC_SUCCESS;
 }
-#endif // _WIN32
+
+static void Close(vlc_object_t *obj)
+{
+    stream_t *access = (stream_t *)obj;
+    access_sys_t *sys = access->p_sys;
+
+    vlc_UrlClean(&sys->url);
+
+#ifndef _WIN32
+    if (access->pf_readdir != NULL)
+        smbc_closedir(sys->i_smb);
+    else
+#endif
+        smbc_close(sys->i_smb);
+}
+
+vlc_module_begin()
+    set_shortname("SMB")
+    set_description(N_("SMB input"))
+    set_help(N_("Samba (Windows network shares) input"))
+    set_capability("access", 0)
+    set_category(CAT_INPUT)
+    set_subcategory(SUBCAT_INPUT_ACCESS)
+    add_string("smb-user", NULL, SMB_USER_TEXT, SMB_USER_LONGTEXT, false)
+    add_password("smb-pwd", NULL, SMB_PASS_TEXT, SMB_PASS_LONGTEXT, false)
+    add_string("smb-domain", NULL, SMB_DOMAIN_TEXT, SMB_DOMAIN_LONGTEXT, false)
+    add_shortcut("smb")
+    set_callbacks(Open, Close)
+vlc_module_end()



More information about the vlc-commits mailing list