1 /* Virtual File System: SFTP file system.
2 The VFS subclass functions
5 The Free Software Foundation, Inc.
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/>.
30 #include "lib/global.h"
31 #include "lib/vfs/utilvfs.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 /* --------------------------------------------------------------------------------------------- */
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
53 * @param cookie unused
54 * @return TRUE if connections is equal, FALSE otherwise
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
)
68 if (vpath_element
->user
!= NULL
)
69 user_name
= vpath_element
->user
;
71 user_name
= vfs_get_local_username ();
73 if (vpath_element
->port
!= 0)
74 port
= vpath_element
->port
;
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
)
88 /* --------------------------------------------------------------------------------------------- */
90 * Callback for opening new connection.
92 * @param super connection data
94 * @param vpath_element path element with connetion data
95 * @return 0 if success, -1 otherwise
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
;
107 if (vpath_element
->host
== NULL
|| *vpath_element
->host
== '\0')
109 vfs_print_message (_("sftp: Invalid host name."));
110 vpath_element
->class->verrno
= EPERM
;
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
);
120 vpath_element
->class->verrno
= error
->code
;
121 sftpfs_show_error (&error
);
125 super
->name
= g_strdup (PATH_SEP_STR
);
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
);
135 /* --------------------------------------------------------------------------------------------- */
137 * Callback for closing connection.
140 * @param super connection data
144 sftpfs_cb_close_connection (struct vfs_class
*me
, struct vfs_s_super
*super
)
146 GError
*error
= NULL
;
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.
160 * @param remote_path unused
165 sftpfs_cb_dir_load (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
174 /* --------------------------------------------------------------------------------------------- */
175 /*** public functions ****************************************************************************/
176 /* --------------------------------------------------------------------------------------------- */
178 * Initialization of VFS subclass structure.
180 * @return VFS subclass structure.
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.
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 /* --------------------------------------------------------------------------------------------- */