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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/* sftp-session-func.c -- Functions for working with SFTP sessions.
*
* Copyright (C) 2015 Artyom V. Poptsov <poptsov.artyom@gmail.com>
*
* This file is part of Guile-SSH
*
* Guile-SSH 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.
*
* Guile-SSH 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 Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
*/
/* Guile */
#include <libguile.h>
/* libssh */
#include <libssh/libssh.h>
#include <libssh/sftp.h>
/* Guile-SSH */
#include "common.h"
#include "error.h"
#include "sftp-session-type.h"
SCM_GSSH_DEFINE (gssh_sftp_init, "%gssh-sftp-init", 1, (SCM sftp_session))
#define FUNC_NAME s_gssh_sftp_init
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
if (sftp_init (sftp_sd->sftp_session))
{
guile_ssh_error1 (FUNC_NAME, "Could not initialize the SFTP session.",
sftp_session);
}
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_GSSH_DEFINE (gssh_sftp_get_session, "%gssh-sftp-get-session", 1,
(SCM sftp_session))
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
return sftp_sd->session;
}
SCM_GSSH_DEFINE (gssh_sftp_mkdir, "%gssh-sftp-mkdir", 3,
(SCM sftp_session, SCM dirname, SCM mode))
#define FUNC_NAME s_gssh_sftp_mkdir
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
char *c_dirname;
SCM_ASSERT (scm_is_string (dirname), dirname, SCM_ARG2, FUNC_NAME);
SCM_ASSERT (scm_is_number (mode), mode, SCM_ARG3, FUNC_NAME);
scm_dynwind_begin (0);
c_dirname = scm_to_locale_string (dirname);
scm_dynwind_free (c_dirname);
if (sftp_mkdir (sftp_sd->sftp_session, c_dirname, scm_to_uint32 (mode)))
{
guile_ssh_error1 (FUNC_NAME, "Could not create a directory",
scm_list_3 (sftp_session, dirname, mode));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_GSSH_DEFINE (gssh_sftp_rmdir, "%gssh-sftp-rmdir", 2,
(SCM sftp_session, SCM dirname))
#define FUNC_NAME s_gssh_sftp_rmdir
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
char *c_dirname;
SCM_ASSERT (scm_is_string (dirname), dirname, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
c_dirname = scm_to_locale_string (dirname);
scm_dynwind_free (c_dirname);
if (sftp_rmdir (sftp_sd->sftp_session, c_dirname))
{
guile_ssh_error1 (FUNC_NAME, "Could not remove a directory",
scm_list_2 (sftp_session, dirname));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_GSSH_DEFINE (gssh_sftp_mv, "%gssh-sftp-mv", 3,
(SCM sftp_session, SCM source, SCM dest))
#define FUNC_NAME s_gssh_sftp_mv
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
char *c_source;
char *c_dest;
SCM_ASSERT (scm_is_string (source), source, SCM_ARG2, FUNC_NAME);
SCM_ASSERT (scm_is_string (dest), dest, SCM_ARG3, FUNC_NAME);
scm_dynwind_begin (0);
c_source = scm_to_locale_string (source);
scm_dynwind_free (c_source);
c_dest = scm_to_locale_string (dest);
scm_dynwind_free (c_dest);
if (sftp_rename (sftp_sd->sftp_session, c_source, c_dest))
{
guile_ssh_error1 (FUNC_NAME, "Could not move a file",
scm_list_3 (sftp_session, source, dest));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_GSSH_DEFINE (gssh_sftp_chmod, "%gssh-sftp-chmod", 3,
(SCM sftp_session, SCM filename, SCM mode))
#define FUNC_NAME s_gssh_sftp_chmod
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
char *c_filename;
SCM_ASSERT (scm_is_string (filename), filename, SCM_ARG2, FUNC_NAME);
SCM_ASSERT (scm_is_number (mode), mode, SCM_ARG3, FUNC_NAME);
scm_dynwind_begin (0);
c_filename = scm_to_locale_string (filename);
scm_dynwind_free (c_filename);
if (sftp_chmod (sftp_sd->sftp_session, c_filename, scm_to_uint32 (mode)))
{
guile_ssh_error1 (FUNC_NAME, "Could not chmod a file",
scm_list_3 (sftp_session, filename, mode));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_GSSH_DEFINE (gssh_sftp_symlink, "%gssh-sftp-symlink", 3,
(SCM sftp_session, SCM target, SCM dest))
#define FUNC_NAME s_gssh_sftp_symlink
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
char *c_target;
char *c_dest;
SCM_ASSERT (scm_is_string (target), target, SCM_ARG2, FUNC_NAME);
SCM_ASSERT (scm_is_string (dest), dest, SCM_ARG3, FUNC_NAME);
scm_dynwind_begin (0);
c_target = scm_to_locale_string (target);
scm_dynwind_free (c_target);
c_dest = scm_to_locale_string (dest);
scm_dynwind_free (c_dest);
if (sftp_symlink (sftp_sd->sftp_session, c_target, c_dest))
{
guile_ssh_error1 (FUNC_NAME, "Could not create a symlink",
scm_list_3 (sftp_session, target, dest));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_GSSH_DEFINE (gssh_sftp_readlink, "%gssh-sftp-readlink", 2,
(SCM sftp_session, SCM path))
#define FUNC_NAME s_gssh_sftp_readlink
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
char *c_path;
char *ret;
SCM_ASSERT (scm_is_string (path), path, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
c_path = scm_to_locale_string (path);
scm_dynwind_free (c_path);
ret = sftp_readlink (sftp_sd->sftp_session, c_path);
scm_dynwind_end ();
return ret ? scm_take_locale_string (ret) : SCM_BOOL_F;
}
#undef FUNC_NAME
SCM_GSSH_DEFINE (gssh_sftp_unlink, "%gssh-sftp-unlink", 2,
(SCM sftp_session, SCM path))
#define FUNC_NAME s_gssh_sftp_unlink
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
char *c_path;
int ret;
SCM_ASSERT (scm_is_string (path), path, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
c_path = scm_to_locale_string (path);
scm_dynwind_free (c_path);
ret = sftp_unlink (sftp_sd->sftp_session, c_path);
if (ret)
{
guile_ssh_error1 (FUNC_NAME, "Could not unlink a file",
scm_list_2 (sftp_session, path));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
/* Possible SFTP return codes. */
static struct symbol_mapping sftp_return_codes[] = {
{ "fx-ok", SSH_FX_OK },
{ "fx-eof", SSH_FX_EOF },
{ "fx-no-such-file", SSH_FX_NO_SUCH_FILE },
{ "fx-permission-denied", SSH_FX_PERMISSION_DENIED },
{ "fx-failure", SSH_FX_FAILURE },
{ "fx-bad-message", SSH_FX_BAD_MESSAGE },
{ "fx-no-connection", SSH_FX_NO_CONNECTION },
{ "fx-connection-lost", SSH_FX_CONNECTION_LOST },
{ "fx-op-unsupported", SSH_FX_OP_UNSUPPORTED },
{ "fx-invalid-handle", SSH_FX_INVALID_HANDLE },
{ "fx-no-such-path", SSH_FX_NO_SUCH_PATH },
{ "fx-file-already-exist", SSH_FX_FILE_ALREADY_EXISTS },
{ "fx-write-protect", SSH_FX_WRITE_PROTECT },
{ "fx-no-media", SSH_FX_NO_MEDIA },
{ NULL, -1 }
};
SCM_GSSH_DEFINE (gssh_sftp_get_error, "%gssh-sftp-get-error", 1,
(SCM sftp_session))
#define FUNC_NAME s_gssh_sftp_get_error
{
struct sftp_session_data *sftp_sd = _scm_to_sftp_session_data (sftp_session);
int rc = sftp_get_error (sftp_sd->sftp_session);
if (rc < 0)
{
guile_ssh_error1 (FUNC_NAME, "Could not get an error code",
sftp_session);
}
return _ssh_const_to_scm (sftp_return_codes, rc);
}
#undef FUNC_NAME
void
init_sftp_session_func (void)
{
#include "sftp-session-func.x"
}
/* sftp-session-func.c ends here. */
|