From cb08c1d0bf3a39181bef32a4a1f93f730ee18f4a Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Tue, 16 Apr 2013 17:33:06 +0300 Subject: [PATCH] Fix: sftp: password ask too often if hostname was bring from ~/.ssh/config file Signed-off-by: Slava Zanko --- lib/glibcompat.c | 32 ++++++++++++++++++++++++++++++-- lib/glibcompat.h | 4 ++++ src/vfs/sftpfs/connection.c | 5 +++-- src/vfs/sftpfs/internal.h | 1 + src/vfs/sftpfs/vfs_subclass.c | 31 +++++++++++-------------------- 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/lib/glibcompat.c b/lib/glibcompat.c index d80661e7e..9641806bd 100644 --- a/lib/glibcompat.c +++ b/lib/glibcompat.c @@ -1,11 +1,11 @@ /* GLIB - Library of useful routines for C programming - Copyright (C) 2009, 2011 + Copyright (C) 2009, 2011, 2013 The Free Software Foundation, Inc. Written by: - Slava Zanko , 2009. + Slava Zanko , 2009, 2013. This file is part of the Midnight Commander. @@ -31,6 +31,7 @@ */ #include +#include #include "global.h" #include "glibcompat.h" @@ -68,3 +69,30 @@ g_unichar_iszerowidth (gunichar c) #endif /* ! GLIB_CHECK_VERSION (2, 13, 0) */ /* --------------------------------------------------------------------------------------------- */ + +#if ! GLIB_CHECK_VERSION (2, 16, 0) +/** + * g_strcmp0: + * @str1: (allow-none): a C string or %NULL + * @str2: (allow-none): another C string or %NULL + * + * Compares @str1 and @str2 like strcmp(). Handles %NULL + * gracefully by sorting it before non-%NULL strings. + * Comparing two %NULL pointers returns 0. + * + * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2. + * + * Since: 2.16 + */ +int +g_strcmp0 (const char *str1, const char *str2) +{ + if (!str1) + return -(str1 != str2); + if (!str2) + return str1 != str2; + return strcmp (str1, str2); +} +#endif /* ! GLIB_CHECK_VERSION (2, 16, 0) */ + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/glibcompat.h b/lib/glibcompat.h index 418a81bd6..b4768e4be 100644 --- a/lib/glibcompat.h +++ b/lib/glibcompat.h @@ -15,6 +15,10 @@ gboolean g_unichar_iszerowidth (gunichar); #endif /* ! GLIB_CHECK_VERSION (2, 13, 0) */ +#if ! GLIB_CHECK_VERSION (2, 16, 0) +int g_strcmp0 (const char *str1, const char *str2); +#endif /* ! GLIB_CHECK_VERSION (2, 16, 0) */ + /*** inline functions ****************************************************************************/ #endif /* MC_GLIBCOMPAT_H */ diff --git a/src/vfs/sftpfs/connection.c b/src/vfs/sftpfs/connection.c index be4606f8f..83e33d005 100644 --- a/src/vfs/sftpfs/connection.c +++ b/src/vfs/sftpfs/connection.c @@ -1,12 +1,12 @@ /* Virtual File System: SFTP file system. The internal functions: connections - Copyright (C) 2011 + Copyright (C) 2011, 2013 The Free Software Foundation, Inc. Written by: Ilia Maslakov , 2011 - Slava Zanko , 2011, 2012 + Slava Zanko , 2011, 2012, 2013 This file is part of the Midnight Commander. @@ -437,6 +437,7 @@ sftpfs_close_connection (struct vfs_s_super *super, const char *shutdown_message if (super_data == NULL) return; + vfs_path_element_free (super_data->original_connection_info); if (super_data->agent != NULL) { libssh2_agent_disconnect (super_data->agent); diff --git a/src/vfs/sftpfs/internal.h b/src/vfs/sftpfs/internal.h index 7c0f457a0..4e4a2a193 100644 --- a/src/vfs/sftpfs/internal.h +++ b/src/vfs/sftpfs/internal.h @@ -43,6 +43,7 @@ typedef struct int socket_handle; const char *fingerprint; + vfs_path_element_t *original_connection_info; } sftpfs_super_data_t; /*** global variables defined in .c file *********************************************************/ diff --git a/src/vfs/sftpfs/vfs_subclass.c b/src/vfs/sftpfs/vfs_subclass.c index 49d9bbcda..5d83fb28e 100644 --- a/src/vfs/sftpfs/vfs_subclass.c +++ b/src/vfs/sftpfs/vfs_subclass.c @@ -1,12 +1,12 @@ /* Virtual File System: SFTP file system. The VFS subclass functions - Copyright (C) 2011 + Copyright (C) 2011, 2013 The Free Software Foundation, Inc. Written by: Ilia Maslakov , 2011 - Slava Zanko , 2011, 2012 + Slava Zanko , 2011, 2012, 2013 This file is part of the Midnight Commander. @@ -58,29 +58,17 @@ static gboolean sftpfs_cb_is_equal_connection (const vfs_path_element_t * vpath_element, struct vfs_s_super *super, const vfs_path_t * vpath, void *cookie) { - int port; - char *user_name; int result; + vfs_path_element_t *orig_connect_info; (void) vpath; (void) cookie; - if (vpath_element->user != NULL) - user_name = vpath_element->user; - else - user_name = vfs_get_local_username (); + orig_connect_info = ((sftpfs_super_data_t *) super->data)->original_connection_info; - if (vpath_element->port != 0) - port = vpath_element->port; - else - port = SFTP_DEFAULT_PORT; - - result = ((strcmp (vpath_element->host, super->path_element->host) == 0) - && (strcmp (user_name, super->path_element->user) == 0) - && (port == super->path_element->port)); - - if (user_name != vpath_element->user) - g_free (user_name); + result = ((g_strcmp0 (vpath_element->host, orig_connect_info->host) == 0) + && (g_strcmp0 (vpath_element->user, orig_connect_info->user) == 0) + && (vpath_element->port == orig_connect_info->port)); return result; } @@ -100,6 +88,7 @@ sftpfs_cb_open_connection (struct vfs_s_super *super, const vfs_path_t * vpath, const vfs_path_element_t * vpath_element) { GError *error = NULL; + sftpfs_super_data_t *sftpfs_super_data; int ret_value; (void) vpath; @@ -111,7 +100,9 @@ sftpfs_cb_open_connection (struct vfs_s_super *super, return -1; } - super->data = g_new0 (sftpfs_super_data_t, 1); + sftpfs_super_data = g_new0 (sftpfs_super_data_t, 1); + sftpfs_super_data->original_connection_info = vfs_path_element_clone (vpath_element); + super->data = sftpfs_super_data; super->path_element = vfs_path_element_clone (vpath_element); sftpfs_fill_connection_data_from_config (super, &error); -- 2.11.4.GIT