Quiq/Smb/Client.pm

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