1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/* Virtual File System: SFTP file system.
The internal functions: dirs
Copyright (C) 2011-2022
Free Software Foundation, Inc.
Written by:
Ilia Maslakov <il.smind@gmail.com>, 2011
Slava Zanko <slavazanko@gmail.com>, 2011, 2012
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <libssh2.h>
#include <libssh2_sftp.h>
#include "lib/global.h"
#include "lib/util.h"
#include "internal.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
typedef struct
{
LIBSSH2_SFTP_HANDLE *handle;
sftpfs_super_t *super;
} sftpfs_dir_data_t;
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/**
* Open a directory stream corresponding to the directory name.
*
* @param vpath path to directory
* @param mcerror pointer to the error handler
* @return directory data handler if success, NULL otherwise
*/
void *
sftpfs_opendir (const vfs_path_t * vpath, GError ** mcerror)
{
sftpfs_dir_data_t *sftpfs_dir;
struct vfs_s_super *super;
sftpfs_super_t *sftpfs_super;
const vfs_path_element_t *path_element;
LIBSSH2_SFTP_HANDLE *handle;
const GString *fixfname;
mc_return_val_if_error (mcerror, NULL);
path_element = vfs_path_get_by_index (vpath, -1);
if (vfs_s_get_path (vpath, &super, 0) == NULL)
return NULL;
sftpfs_super = SFTP_SUPER (super);
fixfname = sftpfs_fix_filename (path_element->path);
while (TRUE)
{
int libssh_errno;
handle =
libssh2_sftp_open_ex (sftpfs_super->sftp_session, fixfname->str, fixfname->len, 0, 0,
LIBSSH2_SFTP_OPENDIR);
if (handle != NULL)
break;
libssh_errno = libssh2_session_last_errno (sftpfs_super->session);
if (!sftpfs_waitsocket (sftpfs_super, libssh_errno, mcerror))
return NULL;
}
sftpfs_dir = g_new0 (sftpfs_dir_data_t, 1);
sftpfs_dir->handle = handle;
sftpfs_dir->super = sftpfs_super;
return (void *) sftpfs_dir;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Get a pointer to a structure representing the next directory entry.
*
* @param data directory data handler
* @param mcerror pointer to the error handler
* @return information about direntry if success, NULL otherwise
*/
struct vfs_dirent *
sftpfs_readdir (void *data, GError ** mcerror)
{
char mem[BUF_MEDIUM];
LIBSSH2_SFTP_ATTRIBUTES attrs;
sftpfs_dir_data_t *sftpfs_dir = (sftpfs_dir_data_t *) data;
int rc;
mc_return_val_if_error (mcerror, NULL);
do
{
rc = libssh2_sftp_readdir (sftpfs_dir->handle, mem, sizeof (mem), &attrs);
if (rc >= 0)
break;
if (!sftpfs_waitsocket (sftpfs_dir->super, rc, mcerror))
return NULL;
}
while (rc == LIBSSH2_ERROR_EAGAIN);
return (rc != 0 ? vfs_dirent_init (NULL, mem, 0) : NULL); /* FIXME: inode */
}
/* --------------------------------------------------------------------------------------------- */
/**
* Close the directory stream.
*
* @param data directory data handler
* @param mcerror pointer to the error handler
* @return 0 if success, negative value otherwise
*/
int
sftpfs_closedir (void *data, GError ** mcerror)
{
int rc;
sftpfs_dir_data_t *sftpfs_dir = (sftpfs_dir_data_t *) data;
mc_return_val_if_error (mcerror, -1);
rc = libssh2_sftp_closedir (sftpfs_dir->handle);
g_free (sftpfs_dir);
return rc;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Create a new directory.
*
* @param vpath path directory
* @param mode mode (see man 2 open)
* @param mcerror pointer to the error handler
* @return 0 if success, negative value otherwise
*/
int
sftpfs_mkdir (const vfs_path_t * vpath, mode_t mode, GError ** mcerror)
{
int res;
struct vfs_s_super *super;
sftpfs_super_t *sftpfs_super;
const vfs_path_element_t *path_element;
const GString *fixfname;
mc_return_val_if_error (mcerror, -1);
path_element = vfs_path_get_by_index (vpath, -1);
if (vfs_s_get_path (vpath, &super, 0) == NULL)
return -1;
if (super == NULL)
return -1;
sftpfs_super = SFTP_SUPER (super);
if (sftpfs_super->sftp_session == NULL)
return -1;
fixfname = sftpfs_fix_filename (path_element->path);
do
{
res =
libssh2_sftp_mkdir_ex (sftpfs_super->sftp_session, fixfname->str, fixfname->len, mode);
if (res >= 0)
break;
if (!sftpfs_waitsocket (sftpfs_super, res, mcerror))
return -1;
}
while (res == LIBSSH2_ERROR_EAGAIN);
return res;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Remove a directory.
*
* @param vpath path directory
* @param mcerror pointer to the error handler
* @return 0 if success, negative value otherwise
*/
int
sftpfs_rmdir (const vfs_path_t * vpath, GError ** mcerror)
{
int res;
struct vfs_s_super *super;
sftpfs_super_t *sftpfs_super;
const vfs_path_element_t *path_element;
const GString *fixfname;
mc_return_val_if_error (mcerror, -1);
path_element = vfs_path_get_by_index (vpath, -1);
if (vfs_s_get_path (vpath, &super, 0) == NULL)
return -1;
if (super == NULL)
return -1;
sftpfs_super = SFTP_SUPER (super);
if (sftpfs_super->sftp_session == NULL)
return -1;
fixfname = sftpfs_fix_filename (path_element->path);
do
{
res = libssh2_sftp_rmdir_ex (sftpfs_super->sftp_session, fixfname->str, fixfname->len);
if (res >= 0)
break;
if (!sftpfs_waitsocket (sftpfs_super, res, mcerror))
return -1;
}
while (res == LIBSSH2_ERROR_EAGAIN);
return res;
}
/* --------------------------------------------------------------------------------------------- */
|