Ticket #1535: SFTP support
[midnight-commander.git] / src / vfs / sftpfs / vfs_subclass.c
blob49d9bbcda91cc754ea9e3c2fc884746cdece6cdd
1 /* Virtual File System: SFTP file system.
2 The VFS subclass functions
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Ilia Maslakov <il.smind@gmail.com>, 2011
9 Slava Zanko <slavazanko@gmail.com>, 2011, 2012
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
28 #include <errno.h>
30 #include "lib/global.h"
31 #include "lib/vfs/utilvfs.h"
33 #include "internal.h"
35 /*** global variables ****************************************************************************/
37 struct vfs_s_subclass sftpfs_subclass;
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** file scope variables ************************************************************************/
45 /*** file scope functions ************************************************************************/
46 /* --------------------------------------------------------------------------------------------- */
47 /**
48 * Callback for checking if connection is equal to existing connection.
50 * @param vpath_element path element with connetion data
51 * @param super data with exists connection
52 * @param vpath unused
53 * @param cookie unused
54 * @return TRUE if connections is equal, FALSE otherwise
57 static gboolean
58 sftpfs_cb_is_equal_connection (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
59 const vfs_path_t * vpath, void *cookie)
61 int port;
62 char *user_name;
63 int result;
65 (void) vpath;
66 (void) cookie;
68 if (vpath_element->user != NULL)
69 user_name = vpath_element->user;
70 else
71 user_name = vfs_get_local_username ();
73 if (vpath_element->port != 0)
74 port = vpath_element->port;
75 else
76 port = SFTP_DEFAULT_PORT;
78 result = ((strcmp (vpath_element->host, super->path_element->host) == 0)
79 && (strcmp (user_name, super->path_element->user) == 0)
80 && (port == super->path_element->port));
82 if (user_name != vpath_element->user)
83 g_free (user_name);
85 return result;
88 /* --------------------------------------------------------------------------------------------- */
89 /**
90 * Callback for opening new connection.
92 * @param super connection data
93 * @param vpath unused
94 * @param vpath_element path element with connetion data
95 * @return 0 if success, -1 otherwise
98 static int
99 sftpfs_cb_open_connection (struct vfs_s_super *super,
100 const vfs_path_t * vpath, const vfs_path_element_t * vpath_element)
102 GError *error = NULL;
103 int ret_value;
105 (void) vpath;
107 if (vpath_element->host == NULL || *vpath_element->host == '\0')
109 vfs_print_message (_("sftp: Invalid host name."));
110 vpath_element->class->verrno = EPERM;
111 return -1;
114 super->data = g_new0 (sftpfs_super_data_t, 1);
115 super->path_element = vfs_path_element_clone (vpath_element);
117 sftpfs_fill_connection_data_from_config (super, &error);
118 if (error != NULL)
120 vpath_element->class->verrno = error->code;
121 sftpfs_show_error (&error);
122 return -1;
125 super->name = g_strdup (PATH_SEP_STR);
126 super->root =
127 vfs_s_new_inode (vpath_element->class, super,
128 vfs_s_default_stat (vpath_element->class, S_IFDIR | 0755));
130 ret_value = sftpfs_open_connection (super, &error);
131 sftpfs_show_error (&error);
132 return ret_value;
135 /* --------------------------------------------------------------------------------------------- */
137 * Callback for closing connection.
139 * @param me unused
140 * @param super connection data
143 static void
144 sftpfs_cb_close_connection (struct vfs_class *me, struct vfs_s_super *super)
146 GError *error = NULL;
148 (void) me;
149 sftpfs_close_connection (super, "Normal Shutdown", &error);
150 sftpfs_show_error (&error);
151 g_free (super->data);
154 /* --------------------------------------------------------------------------------------------- */
156 * Callback for getting directory content.
158 * @param me unused
159 * @param dir unused
160 * @param remote_path unused
161 * @return always 0
164 static int
165 sftpfs_cb_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
167 (void) me;
168 (void) dir;
169 (void) remote_path;
171 return 0;
174 /* --------------------------------------------------------------------------------------------- */
175 /*** public functions ****************************************************************************/
176 /* --------------------------------------------------------------------------------------------- */
178 * Initialization of VFS subclass structure.
180 * @return VFS subclass structure.
183 void
184 sftpfs_init_subclass (void)
186 memset (&sftpfs_subclass, 0, sizeof (struct vfs_s_subclass));
187 sftpfs_subclass.flags = VFS_S_REMOTE;
190 /* --------------------------------------------------------------------------------------------- */
192 * Initialization of VFS subclass callbacks.
195 void
196 sftpfs_init_subclass_callbacks (void)
198 sftpfs_subclass.archive_same = sftpfs_cb_is_equal_connection;
199 sftpfs_subclass.open_archive = sftpfs_cb_open_connection;
200 sftpfs_subclass.free_archive = sftpfs_cb_close_connection;
201 sftpfs_subclass.dir_load = sftpfs_cb_dir_load;
204 /* --------------------------------------------------------------------------------------------- */