LibSSH.xs

filter: Statementstmt 23.5% 41 / 174 Branchbran 27.0% 26 / 96 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 24.8% 67 / 270 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Net/LibSSH.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_mg_findext
6 #include "ppport.h"
7  
8 #include <libssh/libssh.h>
9 #include <libssh/sftp.h>
10  
11 #define undef &PL_sv_undef
12  
13 /* ====================================================
14 Internal structs (named so Newxz has a type to size)
15 ==================================================== */
16  
17 /* Where the ssh_session is in its one-way life. libssh 0.10.6 cannot
18 reconnect a session it has disconnected -- ssh_connect() sits out the whole
19 timeout -- but measured on the same libssh, disconnect() on a session that
20 was never connected leaves it fully connectable. Only the CONNECTED ->
21 SPENT transition is therefore terminal, which is why this is three states
22 and not a flag. Newxz gives FRESH for free.
23 libssh has nothing to answer this with: ssh_get_status() reports SSH_CLOSED
24 for both a spent session and a never-connected one that saw a disconnect(),
25 and ssh_is_connected() is already 0 by the time connect() would ask. */
26 typedef enum {
27 NLSS_SESSION_FRESH = 0,
28 NLSS_SESSION_CONNECTED,
29 NLSS_SESSION_SPENT
30 } NLSS_SessionState;
31  
32 /* generation / session_generation: ssh_disconnect() frees every ssh_channel
33 the session owns from inside libssh, so a channel or sftp session that
34 outlives a disconnect() holds a non-NULL pointer into freed memory. The
35 session counts its disconnects; each child copies the count it was born
36 under and compares. See nlss_session_stale(). */
37 typedef struct {
38 ssh_session session;
39 unsigned int generation;
40 NLSS_SessionState state;
41 /* The one message this binding raises itself; everything else in error()
42 comes from libssh. Set when connect() refuses a spent session, where
43 ssh_connect() is never called and ssh_get_error() would still hold what
44 stood there before the disconnect (measured: nothing). A string
45 literal, so there is nothing to free, and nothing clears it either --
46 a spent session stays spent, and that is the reason every later call
47 on it fails too. */
48 const char *own_error;
49 } NLSS_Session;
50  
51 typedef struct {
52 ssh_channel channel;
53 SV *session_sv;
54 unsigned int session_generation;
55 } NLSS_Channel;
56  
57 typedef struct {
58 sftp_session sftp;
59 SV *session_sv;
60 unsigned int session_generation;
61 } NLSS_SFTP;
62  
63 /* Pointer typedefs with the xsubpp __ → :: naming convention.
64 Using pointer typedefs (not struct typedefs) means XS signatures
65 need no *. The typemap does not derive its vtable names from these:
66 xsubpp maps :: → __ inside ${type} only from 3.60 on, so a generic
67 T_MAGICEXT entry writing &${type}_magic expands to &Net::LibSSH_magic
68 — not valid C — on the older xsubpp this distribution still supports.
69 Every typemap entry therefore spells its vtable out literally. */
70 typedef NLSS_Session *Net__LibSSH;
71 typedef NLSS_Channel *Net__LibSSH__Channel;
72 typedef NLSS_SFTP *Net__LibSSH__SFTP;
73  
74 /* ====================================================
75 Magic vtables — svt_free replaces DESTROY in XS.
76 Perl's GC calls svt_free automatically when the SV
77 is collected; no explicit DESTROY method needed.
78 ==================================================== */
79  
80 static int
81 3 nlss_session_free(pTHX_ SV *sv, MAGIC *mg)
82 {
83 3 NLSS_Session *self = (NLSS_Session *)(void *)mg->mg_ptr;
84 /* No generation bump here: every channel and sftp session holds a
85 reference on this very SV, so this runs only once the last of them is
86 gone. There is nothing left to invalidate. */
87 3 ▶ if (self->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->session) { 3 0 3
88 3 ssh_disconnect(self->session);
89 3 ssh_free(self->session);
90 }
91 3 Safefree(self);
92 3 return 0;
93 }
94 static const MGVTBL Net__LibSSH_magic = { .svt_free = nlss_session_free };
95  
96 /* Has libssh already freed the C object this child holds?
97 The child reaches its session through session_sv -- the blessed,
98 magic-bearing SV it took a reference on in channel()/sftp(). Since 0.003
99 that is the referent, SvRV(ST(0)), not the reference scalar, so the magic
100 sits on session_sv itself and mg_findext takes it directly; a further
101 SvRV() here would walk off the object.
102 Missing magic can only mean the session SV is not the one we stored, so it
103 counts as stale: refusing is the safe answer for both the method guard and
104 the free path. */
105 static int
106 0 nlss_session_stale(pTHX_ SV *session_sv, unsigned int generation)
107 {
108 0 ▶ if (!session_sv || !SvMAGICAL(session_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!session_sv || !SvMAGICAL(session_sv)) 0 0 0
if (!session_sv || !SvMAGICAL(session_sv)) 0 0 0
109 0 return 1;
110 0 MAGIC *mg = mg_findext(session_sv, PERL_MAGIC_ext, &Net__LibSSH_magic);
111 0 ▶ if (!mg)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) 0 0 0
112 0 return 1;
113 0 return ((NLSS_Session *)(void *)mg->mg_ptr)->generation != generation;
114 }
115  
116 static int
117 0 nlss_channel_free(pTHX_ SV *sv, MAGIC *mg)
118 {
119 0 NLSS_Channel *self = (NLSS_Channel *)(void *)mg->mg_ptr;
120 /* A stale channel was freed inside ssh_disconnect() already; sending eof
121 on it is the crash the caller never asked for. Skip the C teardown
122 only -- the session reference and the struct still have to go, or the
123 crash is merely traded for a leak. The session struct is still there to
124 ask: this child's reference on session_sv is released below, after the
125 question, so nlss_session_free cannot have run yet. */
126 0 ▶ if (self->channel
Branch: 1 branch
BranchTrueFalseTotal
if (self->channel 0 0 0
127 0 ▶ && !nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) { 0 0 0
128 0 ssh_channel_send_eof(self->channel);
129 0 ssh_channel_close(self->channel);
130 0 ssh_channel_free(self->channel);
131 }
132 0 SvREFCNT_dec(self->session_sv);
133 0 Safefree(self);
134 0 return 0;
135 }
136 static const MGVTBL Net__LibSSH__Channel_magic = { .svt_free = nlss_channel_free };
137  
138 static int
139 0 nlss_sftp_free(pTHX_ SV *sv, MAGIC *mg)
140 {
141 0 NLSS_SFTP *self = (NLSS_SFTP *)(void *)mg->mg_ptr;
142 0 ▶ if (self->sftp) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->sftp) { 0 0 0
143 /* Unlike a channel, an sftp_session is not freed by ssh_disconnect()
144 -- only the channel inside it is. Skipping sftp_free() outright
145 would trade the crash for a leak (measured: ~1.6 kB per
146 disconnected-and-dropped sftp session), so cut the dangling channel
147 loose first and let sftp_free() release everything else. libssh
148 declares struct sftp_session_struct in its public sftp.h, and its
149 ssh_channel_send_eof/close/free are all NULL-tolerant. */
150 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
151 0 self->sftp->channel = NULL;
152 0 sftp_free(self->sftp);
153 }
154 0 SvREFCNT_dec(self->session_sv);
155 0 Safefree(self);
156 0 return 0;
157 }
158 static const MGVTBL Net__LibSSH__SFTP_magic = { .svt_free = nlss_sftp_free };
159  
160 /* ====================================================
161 Helper functions
162 ==================================================== */
163  
164 static void
165 1 nlss_croak_error(pTHX_ ssh_session session, const char *prefix)
166 {
167 1 const char *msg = ssh_get_error(session);
168 1 ▶ Perl_croak(aTHX_ "%s: %s", prefix, msg ? msg : "(unknown error)");
Branch: 1 branch
BranchTrueFalseTotal
Perl_croak(aTHX_ "%s: %s", prefix, msg ? msg : "(unknown error)"); 1 0 1
169 }
170  
171 static void
172 0 nlss_channel_check_open(pTHX_ NLSS_Channel *self, const char *prefix)
173 {
174 /* Two distinct states, two distinct messages: the caller closed this
175 channel itself, or the session was disconnected out from under it.
176 Collapsing them would leave a caller unable to tell its own teardown
177 from libssh's. */
178 0 ▶ if (!self->channel)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->channel) 0 0 0
179 0 Perl_croak(aTHX_ "%s: channel is closed", prefix);
180 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
181 0 Perl_croak(aTHX_ "%s: session was disconnected", prefix);
182 0 }
183  
184 static void
185 0 nlss_sftp_check_open(pTHX_ NLSS_SFTP *self, const char *prefix)
186 {
187 /* There is no SFTP close(), so self->sftp is set for the object's whole
188 lifetime; disconnect() is the only thing that stops it being usable. */
189 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
190 0 Perl_croak(aTHX_ "%s: session was disconnected", prefix);
191 0 }
192  
193 static SV *
194 0 nlss_channel_slurp(pTHX_ ssh_channel ch, int is_stderr)
195 {
196 0 SV *buf = newSVpvs("");
197 char tmp[4096];
198 int n;
199 while (1) {
200 0 n = ssh_channel_read(ch, tmp, sizeof(tmp), is_stderr);
201 0 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 0 0
202 0 break;
203 0 sv_catpvn(buf, tmp, n);
204 }
205 0 return buf;
206 }
207  
208 MODULE = Net::LibSSH PACKAGE = Net::LibSSH
209  
210 PROTOTYPES: DISABLE
211  
212 Net::LibSSH
213 new(class)
214 SV *class
215 CODE:
216 3 Newxz(RETVAL, 1, NLSS_Session);
217 3 RETVAL->session = ssh_new();
218 3 ▶ if (!RETVAL->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL->session) { 0 3 3
219 0 Safefree(RETVAL);
220 0 Perl_croak(aTHX_ "Net::LibSSH::new: ssh_new() returned NULL");
221 }
222 OUTPUT:
223 RETVAL
224  
225 void
226 option(self, key, value)
227 Net::LibSSH self
228 const char *key
229 SV *value
230 CODE:
231 12 int rc = SSH_OK;
232 12 ▶ if (strcmp(key, "host") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "host") == 0) { 2 10 12
233 2 rc = ssh_options_set(self->session, SSH_OPTIONS_HOST, SvPV_nolen(value));
234 10 ▶ } else if (strcmp(key, "user") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "user") == 0) { 1 9 10
235 1 rc = ssh_options_set(self->session, SSH_OPTIONS_USER, SvPV_nolen(value));
236 9 ▶ } else if (strcmp(key, "port") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "port") == 0) { 3 6 9
237 3 unsigned int port = (unsigned int) SvUV(value);
238 3 rc = ssh_options_set(self->session, SSH_OPTIONS_PORT, &port);
239 6 ▶ } else if (strcmp(key, "knownhosts") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "knownhosts") == 0) { 1 5 6
240 1 rc = ssh_options_set(self->session, SSH_OPTIONS_KNOWNHOSTS, SvPV_nolen(value));
241 5 ▶ } else if (strcmp(key, "timeout") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "timeout") == 0) { 2 3 5
242 2 long t = (long) SvIV(value);
243 2 rc = ssh_options_set(self->session, SSH_OPTIONS_TIMEOUT, &t);
244 3 ▶ } else if (strcmp(key, "compression") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "compression") == 0) { 1 2 3
245 1 rc = ssh_options_set(self->session, SSH_OPTIONS_COMPRESSION, SvPV_nolen(value));
246 2 ▶ } else if (strcmp(key, "log_verbosity") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "log_verbosity") == 0) { 1 1 2
247 1 int v = SvIV(value);
248 1 rc = ssh_options_set(self->session, SSH_OPTIONS_LOG_VERBOSITY, &v);
249 1 ▶ } else if (strcmp(key, "strict_hostkeycheck") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "strict_hostkeycheck") == 0) { 0 1 1
250 0 int v = SvTRUE(value) ? 1 : 0;
251 0 rc = ssh_options_set(self->session, SSH_OPTIONS_STRICTHOSTKEYCHECK, &v);
252 } else {
253 1 Perl_croak(aTHX_ "Net::LibSSH::option: unknown option '%s'", key);
254 }
255 11 ▶ if (rc != SSH_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_OK) 1 10 11
256 1 nlss_croak_error(aTHX_ self->session, "Net::LibSSH::option");
257  
258 int
259 connect(self)
260 Net::LibSSH self
261 CODE:
262 1 ▶ if (self->state == NLSS_SESSION_SPENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == NLSS_SESSION_SPENT) { 0 1 1
263 /* Refuse without calling libssh: ssh_connect() on a disconnected
264 session waits out the full timeout and then reports "Timeout
265 connecting to <host>", which names the wrong problem. Still 0 and
266 still no croak -- connect()'s contract is 1 or 0 with the message
267 on error(), and Rex::LibSSH relies on it. */
268 0 self->own_error = "session was disconnected and cannot be reconnected";
269 0 RETVAL = 0;
270 1 ▶ } else if (ssh_connect(self->session) == SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ssh_connect(self->session) == SSH_OK) { 0 1 1
271 0 self->state = NLSS_SESSION_CONNECTED;
272 0 RETVAL = 1;
273 } else {
274 1 RETVAL = 0;
275 }
276 OUTPUT:
277 RETVAL
278  
279 void
280 disconnect(self)
281 Net::LibSSH self
282 CODE:
283 /* Only a session that actually got connected is spent by this: measured
284 on libssh 0.10.6, disconnect() on a never-connected session leaves it
285 connectable, and marking it would refuse a call that works. */
286 0 ▶ if (self->state == NLSS_SESSION_CONNECTED)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == NLSS_SESSION_CONNECTED) 0 0 0
287 0 self->state = NLSS_SESSION_SPENT;
288 /* Bump before disconnecting: ssh_disconnect() frees every channel the
289 session owns, so every channel and sftp session opened on it has to be
290 stale by the time that memory goes away. */
291 0 self->generation++;
292 0 ssh_disconnect(self->session);
293  
294 SV *
295 error(self)
296 Net::LibSSH self
297 CODE:
298 4 const char *msg = self->own_error ? self->own_error
299 2 ▶ : ssh_get_error(self->session);
Branch: 1 branch
BranchTrueFalseTotal
: ssh_get_error(self->session); 0 2 2
300 2 ▶ RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef; 2 0 2
RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef; 1 1 2
301 OUTPUT:
302 RETVAL
303  
304 int
305 auth_password(self, password)
306 Net::LibSSH self
307 const char *password
308 CODE:
309 0 RETVAL = (ssh_userauth_password(self->session, NULL, password)
310 0 ▶ == SSH_AUTH_SUCCESS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
== SSH_AUTH_SUCCESS) ? 1 : 0; 0 0 0
311 OUTPUT:
312 RETVAL
313  
314 int
315 auth_agent(self)
316 Net::LibSSH self
317 CODE:
318 0 int rc = ssh_userauth_agent(self->session, NULL);
319 0 ▶ if (rc != SSH_AUTH_SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_AUTH_SUCCESS) 0 0 0
320 0 rc = ssh_userauth_publickey_auto(self->session, NULL, NULL);
321 0 ▶ RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0; 0 0 0
322 OUTPUT:
323 RETVAL
324  
325 int
326 auth_publickey(self, privkey_path)
327 Net::LibSSH self
328 const char *privkey_path
329 CODE:
330 0 ssh_key key = NULL;
331 0 int rc = ssh_pki_import_privkey_file(privkey_path, NULL, NULL, NULL, &key);
332 0 ▶ if (rc != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_OK) { 0 0 0
333 0 RETVAL = 0;
334 } else {
335 0 rc = ssh_userauth_publickey(self->session, NULL, key);
336 0 ssh_key_free(key);
337 0 RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0;
338 }
339 OUTPUT:
340 RETVAL
341  
342 Net::LibSSH::Channel
343 channel(self)
344 Net::LibSSH self
345 CODE:
346 0 ssh_channel ch = ssh_channel_new(self->session);
347 0 ▶ if (!ch)
Branch: 1 branch
BranchTrueFalseTotal
if (!ch) 0 0 0
348 0 XSRETURN_UNDEF;
349 0 ▶ if (ssh_channel_open_session(ch) != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssh_channel_open_session(ch) != SSH_OK) { 0 0 0
350 0 ssh_channel_free(ch);
351 0 XSRETURN_UNDEF;
352 }
353 0 Newxz(RETVAL, 1, NLSS_Channel);
354 0 RETVAL->channel = ch;
355 0 RETVAL->session_sv = SvREFCNT_inc(SvRV(ST(0)));
356 0 RETVAL->session_generation = self->generation;
357 OUTPUT:
358 RETVAL
359  
360 Net::LibSSH::SFTP
361 sftp(self)
362 Net::LibSSH self
363 CODE:
364 0 sftp_session sftp = sftp_new(self->session);
365 0 ▶ if (!sftp)
Branch: 1 branch
BranchTrueFalseTotal
if (!sftp) 0 0 0
366 0 XSRETURN_UNDEF;
367 0 ▶ if (sftp_init(sftp) != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (sftp_init(sftp) != SSH_OK) { 0 0 0
368 0 sftp_free(sftp);
369 0 XSRETURN_UNDEF;
370 }
371 0 Newxz(RETVAL, 1, NLSS_SFTP);
372 0 RETVAL->sftp = sftp;
373 0 RETVAL->session_sv = SvREFCNT_inc(SvRV(ST(0)));
374 0 RETVAL->session_generation = self->generation;
375 OUTPUT:
376 RETVAL
377  
378  
379 MODULE = Net::LibSSH PACKAGE = Net::LibSSH::Channel
380  
381 int
382 exec(self, cmd)
383 Net::LibSSH::Channel self
384 const char *cmd
385 CODE:
386 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::exec");
387 0 ▶ RETVAL = (ssh_channel_request_exec(self->channel, cmd) == SSH_OK) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ssh_channel_request_exec(self->channel, cmd) == SSH_OK) ? 1 : 0; 0 0 0
388 OUTPUT:
389 RETVAL
390  
391 SV *
392 read(self, ...)
393 Net::LibSSH::Channel self
394 CODE:
395 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::read");
396 0 int is_stderr = 0;
397 0 int len = -1;
398 0 ▶ if (items >= 2) len = SvIV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) len = SvIV(ST(1)); 0 0 0
399 0 ▶ if (items >= 3) is_stderr = SvTRUE(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 3) is_stderr = SvTRUE(ST(2)); 0 0 0
400 0 ▶ if (len < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) { 0 0 0
401 0 RETVAL = nlss_channel_slurp(aTHX_ self->channel, is_stderr);
402 } else {
403 char *buf;
404 int n;
405 0 Newx(buf, len + 1, char);
406 0 n = ssh_channel_read(self->channel, buf, len, is_stderr);
407 0 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 0 0 0
408 0 Safefree(buf);
409 0 RETVAL = newSVpvs("");
410 } else {
411 0 RETVAL = newSVpvn(buf, n);
412 0 Safefree(buf);
413 }
414 }
415 OUTPUT:
416 RETVAL
417  
418 int
419 write(self, data)
420 Net::LibSSH::Channel self
421 SV *data
422 CODE:
423 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::write");
424 STRLEN len;
425 0 const char *ptr = SvPV(data, len);
426 0 RETVAL = ssh_channel_write(self->channel, ptr, (uint32_t) len);
427 OUTPUT:
428 RETVAL
429  
430 void
431 send_eof(self)
432 Net::LibSSH::Channel self
433 CODE:
434 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::send_eof");
435 0 ssh_channel_send_eof(self->channel);
436  
437 int
438 eof(self)
439 Net::LibSSH::Channel self
440 CODE:
441 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::eof");
442 0 RETVAL = ssh_channel_is_eof(self->channel);
443 OUTPUT:
444 RETVAL
445  
446 int
447 exit_status(self)
448 Net::LibSSH::Channel self
449 CODE:
450 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::exit_status");
451 0 RETVAL = ssh_channel_get_exit_status(self->channel);
452 OUTPUT:
453 RETVAL
454  
455 void
456 close(self)
457 Net::LibSSH::Channel self
458 CODE:
459 /* Free the C channel now; svt_free will later release
460 session_sv and Safefree the struct when the SV is GC'd.
461 Deliberately unguarded, so it stays idempotent for svt_free -- but a
462 channel invalidated by disconnect() is only dropped, never freed twice.
463 No croak either: `$ssh->disconnect; $ch->close` is teardown, and
464 teardown that has already happened is not an error. */
465 0 ▶ if (self->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->channel) { 0 0 0
466 0 ▶ if (!nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) { 0 0 0
467 0 ssh_channel_send_eof(self->channel);
468 0 ssh_channel_close(self->channel);
469 0 ssh_channel_free(self->channel);
470 }
471 0 self->channel = NULL;
472 }
473  
474  
475 MODULE = Net::LibSSH PACKAGE = Net::LibSSH::SFTP
476  
477 SV *
478 stat(self, path)
479 Net::LibSSH::SFTP self
480 const char *path
481 CODE:
482 0 nlss_sftp_check_open(aTHX_ self, "Net::LibSSH::SFTP::stat");
483 0 sftp_attributes attr = sftp_stat(self->sftp, path);
484 0 ▶ if (!attr)
Branch: 1 branch
BranchTrueFalseTotal
if (!attr) 0 0 0
485 0 XSRETURN_UNDEF;
486 0 HV *h = newHV();
487 0 ▶ hv_stores(h, "name", newSVpv(attr->name ? attr->name : path, 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "name", newSVpv(attr->name ? attr->name : path, 0)); 0 0 0
488 0 hv_stores(h, "size", newSVuv(attr->size));
489 0 hv_stores(h, "uid", newSVuv(attr->uid));
490 0 hv_stores(h, "gid", newSVuv(attr->gid));
491 0 hv_stores(h, "mode", newSVuv(attr->permissions));
492 0 ▶ hv_stores(h, "atime", newSVuv(attr->atime64 ? attr->atime64 : attr->atime));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "atime", newSVuv(attr->atime64 ? attr->atime64 : attr->atime)); 0 0 0
493 0 ▶ hv_stores(h, "mtime", newSVuv(attr->mtime64 ? attr->mtime64 : attr->mtime));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "mtime", newSVuv(attr->mtime64 ? attr->mtime64 : attr->mtime)); 0 0 0
494 0 sftp_attributes_free(attr);
495 0 RETVAL = newRV_noinc((SV *) h);
496 OUTPUT:
497 RETVAL
↑ Summary blib/lib/Net/LibSSH.pm »