Quiq/Smb/Client.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Quiq/Gd/Font.pm ↑ Summary Quiq/PhotoStorage.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Smb::Client - SMB Client
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen SMB-Client. Die Klasse
16 realisiert ihre Funktionalität unter Rückgriff auf
17 L<Filesys::SmbClient|https://metacpan.org/pod/Filesys::SmbClient>,
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 höhere Dateioperationen wie ls(), get() und put() realisiert
26 (ähnlich FTP)
27  
28 =item *
29  
30 Fehler nicht über Returnwerte anzeigt, sondern im Fehlerfall
31 eine Exception wirft
32  
33 =back
34  
35 B<ACHTUNG:> Die Klasse Filesys::SmbClient scheint unsauber programmiert
36 zu sein: Kommen in einem Programm mehrere SMB-Objekte vor,
37 können diese nicht ohne Neuinstantiierung verschränkt genutzt werden.
38 D.h. ein SMB-Objekt muss vor einer Operationen neu instantiiert werden,
39 wenn die Operation davor über einem anderen Objekt stattgefunden hat.
40  
41 =cut
42  
43 # -----------------------------------------------------------------------------
44  
45 package Quiq::Smb::Client;
46 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 our $VERSION = '1.240';
53  
54 1 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 0 ▶use Filesys::SmbClient ();
Subroutine BEGIN: called CC=1 SCAR=2.6
56  
57 # -----------------------------------------------------------------------------
58  
59 =head1 METHODS
60  
61 =head2 Konstruktor
62  
63 =head3 new() - Instantiiere Objekt
64  
65 =head4 Synopsis
66  
67 $smb = $class->new(%args);
68  
69 =head4 Arguments
70  
71 =over 4
72  
73 =item %args
74  
75 Liste von Schlüssel/Wert-Paaren, siehe
76 L<Filesys::SmbClient|https://metacpan.org/pod/Filesys::SmbClient#new-%hash>.
77  
78 =back
79  
80 =head4 Returns
81  
82 Object
83  
84 =head4 Description
85  
86 Instantiiere eine Objekt der Klasse und liefere eine Referenz auf
87 dieses Objekt zurück.
88  
89 Die Eigenschaft C<configuration> gehört nicht zu C<Filesys::SmbClient>,
90 sondern ist speziell hinzugefügt. Wenn gesetzt, wird die Datei
91 C<~/.smb/smb.conf> mit dem angegebenen Text (SMB-Konfiguration)
92 vor der Instantiierung geschrieben. Andernfalls wird eine leere
93 Datei angelegt.
94  
95 =head4 Example
96  
97 Konfiguration SMB 1.0:
98  
99 my $smb = Quiq::Smb->new(
100 username => 'elbrusfse',
101 password => 'geheim',
102 workgroup => 'ZEPPELIN_HV',
103 debug => 0, # 0 .. 10
104 configuration => '
105 [global]
106 client min protocol = CORE
107 client use spnego = no
108 client ntlmv2 auth = no
109 ',
110 );
111  
112 =cut
113  
114 # -----------------------------------------------------------------------------
115  
116 sub new {
117 my ($class,%args) = @_;
118  
119 my $p = Quiq::Path->new;
120  
121 my $confFile = '~/.smb/smb.conf';
122  
123 my $conf = $args{'configuration'} // '';
124 # Klappt beides in Zeppelin-Umgebung nicht
125 # $conf = Quiq:: Unindent->string($conf);
126 # $conf = Quiq:: String->unindent($conf);
127 $p->write($confFile,$conf);
128  
129 return $class->SUPER::new(
130 smb => Filesys::SmbClient->new(%args),
131 );
132 }
133  
134 # -----------------------------------------------------------------------------
135  
136 =head2 Objektmethoden
137  
138 =head3 delete() - Lösche Datei von Server
139  
140 =head4 Synopsis
141  
142 $smb->delete($file);
143  
144 =head4 Arguments
145  
146 =over 4
147  
148 =item $file
149  
150 (String) Pfad der Datei.
151  
152 =back
153  
154 =head4 Description
155  
156 Lösche Datei $file von Server.
157  
158 =cut
159  
160 # -----------------------------------------------------------------------------
161  
162 sub delete {
163 my ($self,$file) = @_;
164  
165 my $smb = $self->{'smb'};
166  
167 my $ret = $smb->unlink($file);
168 if (!$ret) {
169 $self->throw(
170 'SMB-00003: Delete failed',
171 File=>qq|"$file"|,
172 Error=>$!,
173 );
174 }
175
176 return;
177 }
178  
179 # -----------------------------------------------------------------------------
180  
181 =head3 getData() - Hole Daten von Server
182  
183 =head4 Synopsis
184  
185 $data = $smb->getData($file);
186  
187 =head4 Arguments
188  
189 =over 4
190  
191 =item $file
192  
193 (String) Dateiname.
194  
195 =back
196  
197 =head4 Returns
198  
199 (String) Dateininhalt.
200  
201 =head4 Description
202  
203 Liefere den Inhalt der Datei $file.
204  
205 =head4 Example
206  
207 $data = $smb->get('smb://ZBM-MOM-T/XRECHNUNG$/S-001191090X_Original.pdf');
208  
209 =cut
210  
211 # -----------------------------------------------------------------------------
212  
213 sub getData {
214 my ($self,$file) = @_;
215  
216 my $smb = $self->{'smb'};
217  
218 my $data;
219 my $fd = $smb->open($file,'0666');
220 if (!$fd) {
221 $self->throw(
222 'SMB-00002: Open for reading failed',
223 Path=>qq|"$file"|,
224 Error=>$!,
225 );
226 }
227 while (1) {
228 my $buf = $smb->read($fd);
229 if (!$buf) {
230 # eof
231 last;
232 }
233 $data .= $buf;
234 }
235 $smb->close($fd);
236
237 return $data;
238 }
239  
240 # -----------------------------------------------------------------------------
241  
242 =head3 ls() - Liste von Dateien in Server-Verzeichnis
243  
244 =head4 Synopsis
245  
246 @arr|$arr = $sbm->ls($dir);
247  
248 =head4 Arguments
249  
250 =over 4
251  
252 =item $dir
253  
254 Verzeichnis
255  
256 =back
257  
258 =head4 Returns
259  
260 (Array of Strings) Liste von Dateinamen. Im Skalarkontext eine Referenz
261 auf die Liste.
262  
263 =head4 Description
264  
265 Liefere die Liste der Dateien auf dem Server in Verzeichnis $dir.
266  
267 =head4 Example
268  
269 @names = $smb->ls('smb://ZBM-MOM-T/XRECHNUNG$');
270  
271 =cut
272  
273 # -----------------------------------------------------------------------------
274  
275 sub ls {
276 my ($self,$path) = @_;
277  
278 my $smb = $self->{'smb'};
279  
280 my @arr;
281 my $fd = $smb->opendir($path);
282 if (!$fd) {
283 $self->throw(
284 'SMB-00001: ls failed',
285 Path=>qq|"$path"|,
286 Error=>$!,
287 );
288 }
289 for ($smb->readdir($fd)) {
290 if ($_ eq '.' || $_ eq '..') {
291 next;
292 }
293 push @arr,$_;
294 }
295 $smb->closedir($fd);
296
297 return wantarray? @arr: \@arr;
298 }
299  
300 # -----------------------------------------------------------------------------
301  
302 =head3 put() - Übertrage Datei auf Server
303  
304 =head4 Synopsis
305  
306 $smb->put($from,$to);
307  
308 =head4 Arguments
309  
310 =over 4
311  
312 =item $from
313  
314 (String) Pfad der Quelldatei.
315  
316 =item $to
317  
318 (String) Pfad der Zieldatei
319  
320 =back
321  
322 =head4 Description
323  
324 Kopiere Datei $from nach $to.
325  
326 =head4 Example
327  
328 $smb->put('test.xls','smb://zeppelin.corp/zbm/Transfer/elbrusfse/test.xls');
329  
330 =cut
331  
332 # -----------------------------------------------------------------------------
333  
334 sub put {
335 my ($self,$from,$to) = @_;
336  
337 my $data = Quiq::Path->read($from);
338 $self->putData($data,$to);
339
340 return;
341 }
342  
343 # -----------------------------------------------------------------------------
344  
345 =head3 putData() - Übertrage Daten auf Server
346  
347 =head4 Synopsis
348  
349 $smb->putData($data,$file);
350  
351 =head4 Arguments
352  
353 =over 4
354  
355 =item $data
356  
357 (String) Dateiinhalt.
358  
359 =item $file
360  
361 (String) Dateiname.
362  
363 =back
364  
365 =head4 Description
366  
367 Schreibe Datei $file mit Inhalt $data.
368  
369 =head4 Example
370  
371 $smb->putData($data,'smb://ZBM-MOM-T/XRECHNUNG$/S-001191090X_Original.pdf');
372  
373 =cut
374  
375 # -----------------------------------------------------------------------------
376  
377 sub putData {
378 my ($self,$data,$file) = @_;
379  
380 my $smb = $self->{'smb'};
381  
382 my $fd = $smb->open(">$file",'0666');
383 if (!$fd) {
384 $self->throw(
385 'SMB-00002: Open for writing failed',
386 Path=>qq|"$file"|,
387 Error=>$!,
388 );
389 }
390 my $ret = $smb->write($fd,$data);
391 if (!$ret) {
392 $self->throw(
393 'SMB-00003: Write failed',
394 Path=>qq|"$file"|,
395 Error=>$!,
396 );
397 }
398 $smb->close($fd);
399
400 return;
401 }
402  
403 # -----------------------------------------------------------------------------
404  
405 =head3 rename() - Benenne Datei auf Server um
406  
407 =head4 Synopsis
408  
409 $smb->rename($oldname,$newname);
410  
411 =head4 Arguments
412  
413 =over 4
414  
415 =item $oldname
416  
417 (String) Alter Dateiname.
418  
419 =item $newname
420  
421 (String) Neuer Dateiname.
422  
423 =back
424  
425 =head4 Description
426  
427 Benenne Datei $oldname in $newname um.
428  
429 =cut
430  
431 # -----------------------------------------------------------------------------
432  
433 sub rename {
434 my ($self,$oldname,$newname) = @_;
435  
436 my $smb = $self->{'smb'};
437  
438 my $ret = $smb->rename($oldname,$newname);
439 if (!$ret) {
440 $self->throw(
441 'SMB-00003: Rename failed',
442 Path=>qq|"$oldname" => "$newname"|,
443 Error=>$!,
444 );
445 }
446
447 return;
448 }
449  
450 # -----------------------------------------------------------------------------
451  
452 =head1 VERSION
453  
454 1.240
455  
456 =head1 AUTHOR
457  
458 Frank Seitz, L<http://fseitz.de/>
459  
460 =head1 COPYRIGHT
461  
462 Copyright (C) 2026 Frank Seitz
463  
464 =head1 LICENSE
465  
466 This code is free software; you can redistribute it and/or modify
467 it under the same terms as Perl itself.
468  
469 =cut
470  
471 # -----------------------------------------------------------------------------
472  
473 1;
474  
475 # eof
« Quiq/Gd/Font.pm ↑ Summary Quiq/PhotoStorage.pm »