Quiq/Sftp/Client.pm

filter: Statementstmt 25.0% 27 / 108 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 8 / 16 Podpod 100% 8 / 8 totaltotal 27.3% 43 / 157 CC 13 SCAR 19.7
CC
13
Cov
20%
get
12.0
ls
12.0
put
12.0
CRAP 7.1
« Quiq/PerlModule.pm ↑ Summary Quiq/Hash.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Sftp::Client - SFTP Client
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen SFTP-Client. Die Klasse
16 realisiert ihre Funktionalität unter Rückgriff auf
17 L<Net::SFTP::Foreign|https://metacpan.org/pod/Net::SFTP::Foreign>,
18 allerdings nicht durch Ableitung, sondern durch Einbettung.
19 Die Klasse zeichnet sich dadurch aus, dass sie
20  
21 =over 2
22  
23 =item *
24  
25 Fehler nicht über Returnwerte anzeigt, sondern im Fehlerfall
26 eine Exception wirft (allerdings schreibt Net::SFTP::Foreign
27 zusätzlich Meldungen nach STDERR, siehe
28 L<Net::SFTP::Foreign#stderr_fh|https://metacpan.org/pod/Net::SFTP::Foreign#stderr_fh-=%3E-$fh>
29 um dies ggf. zu verbessern)
30  
31 =back
32  
33 =cut
34  
35 # -----------------------------------------------------------------------------
36  
37 package Quiq::Sftp::Client;
38 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 our $VERSION = '1.240';
45  
46 1 ▶use Net::SFTP::Foreign ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use File::Slurp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # -----------------------------------------------------------------------------
51  
52 =head1 METHODS
53  
54 =head2 Konstruktor
55  
56 =head3 new() - Instantiiere Objekt
57  
58 =head4 Synopsis
59  
60 $ftp = $class->new(%args);
61  
62 =head4 Arguments
63  
64 =over 4
65  
66 =item %args
67  
68 Siehe L<Net::SFTP::Foreign|https://metacpan.org/pod/Net::SFTP::Foreign#Net::SFTP::Foreign-%3Enew(%args)>.
69  
70 =back
71  
72 =head4 Returns
73  
74 Object
75  
76 =head4 Description
77  
78 Instantiiere eine Objekt der Klasse und liefere eine Referenz auf
79 dieses Objekt zurück.
80  
81 =head4 Example
82  
83 my $smb = Quiq::Sftp->new(
84 user => 'fs',
85 password => 'geheim',
86 host => 'ftp.fseitz.de',
87 );
88  
89 =cut
90  
91 # -----------------------------------------------------------------------------
92  
93 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
94 0 my $class = shift;
95 # @_: %args
96  
97 0 my $sftp = eval {Net::SFTP::Foreign->new(
98 autodie => 1,
99 @_,
100 )};
101 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
102 0 $@ =~ s/ at .*//;
103 0 $class->throw("new - $@");
104 }
105  
106 0 return $class->SUPER::new(
107 sftp => $sftp,
108 );
109 }
110  
111 # -----------------------------------------------------------------------------
112  
113 =head2 Objektmethoden
114  
115 =head3 cd() - Wechsele Server-Verzeichnis
116  
117 =head4 Synopsis
118  
119 $sftp->cd($dir);
120  
121 =head4 Arguments
122  
123 =over 4
124  
125 =item $dir
126  
127 (String) Verzeichnis-Pfad.
128  
129 =back
130  
131 =head4 Description
132  
133 Wechsele auf dem Server in Verzeichnis $dir.
134  
135 =cut
136  
137 # -----------------------------------------------------------------------------
138  
139 0 ▶sub cd {
Subroutine cd: not called CC=2 SCAR=17.9
Pod documented
140 0 my ($self,$dir) = @_;
141  
142 0 my $sftp = $self->{'sftp'};
143 0 eval {$sftp->setcwd($dir)};
144 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
145 0 $@ =~ s/ at .*//;
146 0 $self->throw("cd - $@");
147 }
148  
149 0 return;
150 }
151  
152 # -----------------------------------------------------------------------------
153  
154 =head3 delete() - Lösche Datei auf Server
155  
156 =head4 Synopsis
157  
158 $sftp->delete($file);
159  
160 =head4 Alias
161  
162 remove()
163  
164 =head4 Arguments
165  
166 =over 4
167  
168 =item $file
169  
170 (String) Datei-Pfad.
171  
172 =back
173  
174 =head4 Description
175  
176 Lösche die Datei $file auf dem Server.
177  
178 =cut
179  
180 # -----------------------------------------------------------------------------
181  
182 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
183 0 my ($self,$file) = @_;
184  
185 0 my $sftp = $self->{'sftp'};
186 0 eval {$sftp->remove($file)};
187 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
188 0 $@ =~ s/ at .*//;
189 0 $self->throw("delete - $@");
190 }
191  
192 0 return;
193 }
194  
195 {
196 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
197 1 *remove = \&delete;
198 }
199  
200 # -----------------------------------------------------------------------------
201  
202 =head3 get() - Hole Datei von Server
203  
204 =head4 Synopsis
205  
206 $sftp->get($remote,%opts);
207 $sftp->get($remote,$local,%opts);
208  
209 =head4 Arguments
210  
211 =over 4
212  
213 =item $remote
214  
215 (String) Pfad der entfernten Datei.
216  
217 =item $local
218  
219 (String) Pfad der lokalen Datei.
220  
221 =back
222  
223 =head4 Options
224  
225 =over 4
226  
227 =item %opts
228  
229 Siehe L<Net::SFTP::Foreign|https://metacpan.org/pod/Net::SFTP::Foreign#$sftp->get($remote,-$local,-%options)>.
230  
231 =back
232  
233 =head4 Description
234  
235 Hole Datei $remote vom Server und speichere sie unter dem
236 Pfad $local.
237  
238 =cut
239  
240 # -----------------------------------------------------------------------------
241  
242 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
243 0 my $self = shift;
244 # @_: $remote,%opts -or- $remote,$local,%opts
245  
246 0 ▶ if (@_%2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
247 0 my $local = $_[0];
248 0 $local =~ s|.*/||;
249 0 splice @_,1,0,$local;
250 }
251  
252 0 my $sftp = $self->{'sftp'};
253 0 eval {$sftp->get(@_)};
254 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
255 0 $@ =~ s/ at .*//;
256 0 $self->throw("get - $@");
257 }
258  
259 0 return;
260 }
261  
262 # -----------------------------------------------------------------------------
263  
264 =head3 getData() - Hole Daten von Server
265  
266 =head4 Synopsis
267  
268 $data = $sftp->getData($remote,%opts);
269  
270 =head4 Arguments
271  
272 =over 4
273  
274 =item $remote
275  
276 (String) Pfad der entfernten Datei.
277  
278 =back
279  
280 =head4 Options
281  
282 =over 4
283  
284 =item %opts
285  
286 Siehe L<Net::SFTP::Foreign|https://metacpan.org/pod/Net::SFTP::Foreign#$sftp->put($local,-$remote,-%opts)>.
287  
288 =back
289  
290 =head4 Description
291  
292 Hole die Datei $remote vom Server und liefere dessen Inhalt zurück.
293  
294 =cut
295  
296 # -----------------------------------------------------------------------------
297  
298 0 ▶sub getData {
Subroutine getData: not called CC=2 SCAR=17.9
Pod documented
299 0 my ($self,$remote) = splice @_,0,2;
300 # @_: %opts
301  
302 0 my $sftp = $self->{'sftp'};
303 0 my $data = eval {
304 0 my $fh = File::Temp->new;
305 0 my $tmpFile = $fh->filename;
306 0 $sftp->get($remote,$tmpFile,@_);
307 0 my $data = File::Slurp::read_file($tmpFile);
308 0 close $fh; # temporäre Datei wird gelöscht
309 0 return $data;
310 };
311 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
312 0 $@ =~ s/ at .*//;
313 0 $self->throw("putData - $@");
314 }
315  
316 0 return $data;
317 }
318  
319 # -----------------------------------------------------------------------------
320  
321 =head3 ls() - Liste von Dateien in Server-Verzeichnis
322  
323 =head4 Synopsis
324  
325 @arr|$arr = $sftp->ls(%opts);
326 @arr|$arr = $sftp->ls($path,%opts);
327  
328 =head4 Arguments
329  
330 =over 4
331  
332 =item $path
333  
334 (String) Verzeichnis-Pfad.
335  
336 =back
337  
338 =head4 Options
339  
340 =over 4
341  
342 =item %opts
343  
344 Siehe L<Net::SFTP::Foreign|https://metacpan.org/pod/Net::SFTP::Foreign#$sftp->ls($remote,-%opts)>.
345  
346 =back
347  
348 =head4 Returns
349  
350 (Array of Strings) Liste von Datei- oder Verzeichnisnamen.
351 Im Skalarkontext eine Referenz auf die Liste.
352  
353 =head4 Description
354  
355 Liefere die Liste der Dateien unter dem Remote-Pfad $path.
356  
357 =cut
358  
359 # -----------------------------------------------------------------------------
360  
361 0 ▶sub ls {
Subroutine ls: not called CC=3 SCAR=24.8
Pod documented
362 0 my $self = shift;
363 # @_: %opts -or- $path,%opts
364  
365 0 my $sftp = $self->{'sftp'};
366 0 my @arr = eval {@{$sftp->ls(@_,names_only=>1)}};
367 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
368 0 $@ =~ s/ at .*//;
369 0 $self->throw("ls - $@");
370 }
371  
372 0 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
373 }
374  
375 # -----------------------------------------------------------------------------
376  
377 =head3 put() - Übertrage Datei auf Server
378  
379 =head4 Synopsis
380  
381 $sftp->put($local,%opts);
382 $sftp->put($local,$remote,%opts);
383  
384 =head4 Arguments
385  
386 =over 4
387  
388 =item $local
389  
390 (String) Pfad der lokalen Datei.
391  
392 =item $remote
393  
394 (String) Pfad der entfernten Datei.
395  
396 =back
397  
398 =head4 Options
399  
400 =over 4
401  
402 =item %opts
403  
404 Siehe L<Net::SFTP::Foreign|https://metacpan.org/pod/Net::SFTP::Foreign#$sftp->put($local,-$remote,-%opts)>.
405  
406 =back
407  
408 =head4 Description
409  
410 Übertrage Datei $lokal auf den Server und speichere sie unter dem
411 Pfad $remote.
412  
413 =cut
414  
415 # -----------------------------------------------------------------------------
416  
417 0 ▶sub put {
Subroutine put: not called CC=3 SCAR=24.8
Pod documented
418 0 my $self = shift;
419 # @_: $local,%opts -or- $local,$remote,%opts
420  
421 0 ▶ if (@_%2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
422 0 my $remote = $_[0];
423 0 $remote =~ s|.*/||;
424 0 splice @_,1,0,$remote;
425 }
426  
427 0 my $sftp = $self->{'sftp'};
428 0 eval {$sftp->put(@_)};
429 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
430 0 $@ =~ s/ at .*//;
431 0 $self->throw("put - $@");
432 }
433  
434 0 return;
435 }
436  
437 # -----------------------------------------------------------------------------
438  
439 =head3 putData() - Übertrage Daten auf Server
440  
441 =head4 Synopsis
442  
443 $sftp->putData($data,$remote,%opts);
444  
445 =head4 Arguments
446  
447 =over 4
448  
449 =item $data
450  
451 (String) Daten, die übertragen werden sollen
452  
453 =item $remote
454  
455 (String) Pfad der entfernten Datei.
456  
457 =back
458  
459 =head4 Options
460  
461 =over 4
462  
463 =item %opts
464  
465 Siehe L<Net::SFTP::Foreign|https://metacpan.org/pod/Net::SFTP::Foreign#$sftp->put($local,-$remote,-%opts)>.
466  
467 =back
468  
469 =head4 Description
470  
471 Übertrage Daten $data auf den Server und speichere sie unter dem
472 Pfad $remote.
473  
474 =cut
475  
476 # -----------------------------------------------------------------------------
477  
478 0 ▶sub putData {
Subroutine putData: not called CC=3 SCAR=24.8
Pod documented
479 0 my ($self,$data,$remote) = splice @_,0,3;
480 # @_: %opts
481  
482 0 my $sftp = $self->{'sftp'};
483 0 eval {
484 0 my $fh = File::Temp->new;
485 0 my $tmpFile = $fh->filename;
486 0 ▶ syswrite($fh,$data) // die "syswrite failed\n";
Condition0%0/2
syswrite($fh, $data) // die("syswrite failed\n")
l!l
00
MC/DC0%0/1
syswrite($fh, $data) // die("syswrite failed\n")
syswrite($fh, $data)
Truth table0%0/2
A
A= syswrite($fh, $data)
Aresult
11
00
487 0 $sftp->put($tmpFile,$remote,@_);
488 0 close $fh; # temporäre Datei wird gelöscht
489 };
490 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
491 0 $@ =~ s/ at .*//;
492 0 $self->throw("putData - $@");
493 }
494  
495 0 return;
496 }
497  
498 # -----------------------------------------------------------------------------
499  
500 =head1 VERSION
501  
502 1.240
503  
504 =head1 AUTHOR
505  
506 Frank Seitz, L<http://fseitz.de/>
507  
508 =head1 COPYRIGHT
509  
510 Copyright (C) 2026 Frank Seitz
511  
512 =head1 LICENSE
513  
514 This code is free software; you can redistribute it and/or modify
515 it under the same terms as Perl itself.
516  
517 =cut
518  
519 # -----------------------------------------------------------------------------
520  
521 1 1;
522  
523 # eof
« Quiq/PerlModule.pm ↑ Summary Quiq/Hash.pm »