Quiq/Path.pm

filter: Statementstmt 47.3% 490 / 1036 Branchbran 41.1% 185 / 450 Conditioncond 39.5% 68 / 172 MC/DCmcdc 24.5% 26 / 106 Subroutinesub 69.3% 79 / 114 Podpod 100% 77 / 77 totaltotal 47.3% 925 / 1955 CC 303 SCAR 38.0
CC
303
Cov
45%
numberPaths
272.0
partionizeDir
240.0
numberBasePaths
182.0
CRAP 44.9
« Quiq/ImagePool/Sequence.pm ↑ Summary Quiq/Timeseries/Synchronizer.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Path - Dateisystem-Operationen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Die Klasse definiert alle grundlegenden (link, mkdir, rename, symlink
16 usw.) und komplexen (copy, glob, find usw.) Dateisystem-Operationen.
17 Eine Dateisystem-Operation ist eine Operation auf einem I<Pfad>.
18  
19 =cut
20  
21 # -----------------------------------------------------------------------------
22  
23 package Quiq::Path;
24 270 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
25 270 ▶ $INC{'Quiq/Path.pm'} ||= __FILE__;
Condition50%1/2
$INC{"Quiq/Path.pm"} ||= __FILE__
l!l
2700
MC/DC0%0/1
$INC{"Quiq/Path.pm"} ||= __FILE__
$INC{"Quiq/Path.pm"}
Truth table50%1/2
A
A= $INC{"Quiq/Path.pm"}
Aresult
11
00
26 }
27 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 270 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 270 our $VERSION = '1.240';
35  
36 270 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 270 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 270 ▶use Quiq::TempFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 270 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 270 ▶use Quiq::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 270 ▶use Encode::Guess ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 270 ▶use Quiq::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 270 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 270 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 270 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 270 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 270 ▶use Fcntl qw/:DEFAULT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 270 ▶use Quiq::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 270 ▶use Quiq::DirHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 270 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 270 ▶use Quiq::Exit;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 270 ▶use Quiq::TempDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 270 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 270 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 270 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 270 ▶use Quiq::Time;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 270 ▶use Quiq::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # -----------------------------------------------------------------------------
60  
61 =head1 METHODS
62  
63 =head2 Konstruktor
64  
65 =head3 new() - Instantiiere Objekt
66  
67 =head4 Synopsis
68  
69 $p = $class->new;
70  
71 =head4 Returns
72  
73 Path-Objekt
74  
75 =head4 Description
76  
77 Instantiiere ein Objekt der Klasse und liefere eine Referenz auf
78 dieses Objekt zurück. Da die Klasse ausschließlich Klassenmethoden
79 enthält, hat das Objekt ausschließlich die Funktion, eine abkürzende
80 Aufrufschreibweise zu ermöglichen.
81  
82 =cut
83  
84 # -----------------------------------------------------------------------------
85  
86 51 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
87 51 my $class = shift;
88 51 return bless \(my $dummy),$class;
89 }
90  
91 # -----------------------------------------------------------------------------
92  
93 =head2 Datei-Operationen
94  
95 =head3 append() - Hänge Daten an Datei an
96  
97 =head4 Synopsis
98  
99 $this->append($file,$data,@opt);
100  
101 =head4 Arguments
102  
103 =over 4
104  
105 =item $file
106  
107 Pfad der Datei.
108  
109 =item $data
110  
111 Daten, die auf die Datei geschrieben werden.
112  
113 =back
114  
115 =head4 Options
116  
117 =over 4
118  
119 =item -lock => $bool (Default: 0)
120  
121 Locke die Datei während des Schreibens exklusiv.
122  
123 =back
124  
125 =head4 Description
126  
127 Hänge Daten $data an Datei $file an.
128  
129 =cut
130  
131 # -----------------------------------------------------------------------------
132  
133 2 ▶sub append {
Subroutine append: called CC=1 SCAR=0.0
Pod documented
134 2 shift->write(@_,-append=>1);
135 2 return;
136 }
137  
138 # -----------------------------------------------------------------------------
139  
140 =head3 checkFileSecurity() - Prüfe, ob Datei geschützt ist
141  
142 =head4 Synopsis
143  
144 $this->checkFileSecurity($file); # nur Owner darf schreiben und lesen
145 $this->checkFileSecurity($file,$readableByOthers); # nur Owner darf schreiben
146  
147 =head4 Arguments
148  
149 =over 4
150  
151 =item $file
152  
153 Datei, deren Rechte geprüft werden.
154  
155 =item $readableByOthers
156  
157 Wenn wahr, dürfen auch andere die Datei lesen.
158  
159 =back
160  
161 =head4 Description
162  
163 Prüfe, ob die Datei $file vor unerlaubtem Zugriff geschützt ist.
164 Wenn nicht, wirf eine Exception.
165  
166 Per Default darf die Datei nur für ihren Owner lesbar und schreibbar
167 sein, muss also die Zugriffsrechte rw------- besitzen.
168  
169 Ist $readable wahr, darf die Datei von der Gruppe und anderen
170 gelesen werden, darf also die Zugriffsrechte rw-r--r-- besitzen.
171  
172 =cut
173  
174 # -----------------------------------------------------------------------------
175  
176 5 ▶sub checkFileSecurity {
Subroutine checkFileSecurity: called CC=4 SCAR=13.9
Pod documented
177 5 my ($this,$file,$readable) = @_;
178  
179 5 my $mode = $this->mode($file);
180  
181 # Die Datei darf nur für ihren Owner schreibbar sein
182  
183 5 ▶ if ($mode & 00022) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode & 18) 1 4 5
184 1 $this->throw(
185 'PATH-00099: File is writeable by others',
186 File => $file,
187 );
188 }
189  
190 4 ▶ if (!$readable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($readable) 3 1 4
191 # Die Datei darf nur für ihren Owner lesbar sein
192  
193 3 ▶ if ($mode & 00044) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode & 36) 1 2 3
194 1 $this->throw(
195 'PATH-00099: File is readable by others',
196 File => $file,
197 );
198 }
199 }
200  
201 3 return;
202 }
203  
204 # -----------------------------------------------------------------------------
205  
206 =head3 compare() - Prüfe, ob Inhalt differiert
207  
208 =head4 Synopsis
209  
210 $bool = $class->compare($file1,$file2);
211  
212 =head4 Description
213  
214 Prüfe, ob der Inhalt der Dateien $file1 und $file2 differiert.
215 Ist dies der Fall, liefere I<wahr> (1 oder 2), andernfalls I<falsch> (0).
216 Differiert $file2, wird 1 geliefert, existiert $file2 nicht,
217 wird 2 geliefert.
218  
219 =cut
220  
221 # -----------------------------------------------------------------------------
222  
223 0 ▶sub compare {
Subroutine compare: not called CC=4 SCAR=30.0
Pod documented
224 0 my $class = shift;
225 0 my $file1 = $class->expandTilde(shift);
226 0 my $file2 = $class->expandTilde(shift);
227  
228 0 ▶ if (!-e $file2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file2) 0 0 0
229 0 return 2;
230 }
231  
232 0 ▶ if (-s $file1 != -s $file2) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $file1 != -s $file2) 0 0 0
233 0 return 1;
234 }
235  
236 0 ▶ return $class->read($file1) eq $class->read($file2)? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
$class->read($file1) eq $class->read($file2) ? : 0 0 0
237 }
238  
239 # -----------------------------------------------------------------------------
240  
241 =head3 compareData() - Prüfe, ob Datei-Inhalt von Daten differiert
242  
243 =head4 Synopsis
244  
245 $bool = $class->compareData($file,$data);
246 $bool = $class->compareData($file,$data,$encoding);
247  
248 =head4 Alias
249  
250 different()
251  
252 =head4 Description
253  
254 Prüfe, ob der Inhalt der Datei $file von $data differiert. Ist dies
255 der Fall, liefere I<wahr>, andernfalls I<falsch>. Die Datei $file muss
256 nicht existieren.
257  
258 =cut
259  
260 # -----------------------------------------------------------------------------
261  
262 4 ▶sub compareData {
Subroutine compareData: called CC=4 SCAR=15.0
Pod documented
263 4 my ($class,$file,$data,$encoding) = @_;
264  
265 4 require bytes;
266 4 ▶ if (!-e $file || -s $file != bytes::length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $file or -s $file != &bytes::length($data)) 0 4 4
Condition33%1/3
not -e $file or -s $file != &bytes::length($data)
l!l&&r!l&&!r
004
MC/DC0%0/2
not -e $file or -s $file != &bytes::length($data)
not -e $file -s $file != &bytes::length($data)
Truth table33%1/3
not(A) or B
A= not -e $fileB= -s $file != &bytes::length($data)
ABresult
1X1
011
000
267 0 return 1;
268 }
269  
270 4 my $fileData = $class->read($file,-decode=>$encoding);
271 4 ▶ return $fileData eq $data? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
$fileData eq $data ? : 3 1 4
272 }
273  
274 {
275 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
276 270 *different = \&compareData;
277 }
278  
279 # -----------------------------------------------------------------------------
280  
281 =head3 convertEncoding() - Wandele Character Encoding
282  
283 =head4 Synopsis
284  
285 $this->convertEncoding($from,$to,$file);
286  
287 =head4 Arguments
288  
289 =over 4
290  
291 =item $from
292  
293 Aktuelles Encoding der Datei
294  
295 =item $to
296  
297 Zukünftiges Encoding der Datei
298  
299 =item $file
300  
301 Datei, deren Encoding gewandelt wird
302  
303 =back
304  
305 =head4 Description
306  
307 Wandele das Encoding der Datei $file von Encoding $from in Encoding $to.
308 Die Wandelung findet "in place" statt.
309  
310 =cut
311  
312 # -----------------------------------------------------------------------------
313  
314 0 ▶sub convertEncoding {
Subroutine convertEncoding: not called CC=1 SCAR=6.9
Pod documented
315 0 my $this = shift;
316 0 my $from = shift;
317 0 my $to = shift;
318 0 my $file = $this->expandTilde(shift);
319  
320 0 my $data = $this->read($file,-decode=>$from);
321 0 $this->write($file,$data,-encode=>$to);
322  
323 0 return;
324 }
325  
326 # -----------------------------------------------------------------------------
327  
328 =head3 copy() - Kopiere Datei
329  
330 =head4 Synopsis
331  
332 $this->copy($srcPath,$destPath,@opt);
333  
334 =head4 Options
335  
336 =over 4
337  
338 =item -createDir => $bool (Default: 0)
339  
340 Erzeuge Zielverzeichnis, falls es nicht existiert.
341  
342 =item -log => $stream (Default: undef)
343  
344 Schreibe Meldung auf Dateihandle. Beispiel:
345  
346 -log => \*STDOUT
347  
348 =item -move => $bool (Default: 0)
349  
350 Lösche Quelldatei $srcPath nach dem Kopieren.
351  
352 =item -overwrite => $bool (Default: 1)
353  
354 Wenn gesetzt, wird die Zieldatei $destPath überschrieben, falls sie
355 existiert. Andernfalls wird eine Exception geworfen.
356  
357 =item -preserve => $bool (Default: 0)
358  
359 Behalte den Zeitpunkt der letzten Änderung bei.
360  
361 =back
362  
363 =head4 Description
364  
365 Kopiere Datei $srcPath nach $destPath. Ist eine Nulloperation,
366 wenn die Ziledatei existiert und identisch zur Quelldatei ist
367 (gleicher Inode).
368  
369 =cut
370  
371 # -----------------------------------------------------------------------------
372  
373 3 ▶sub copy {
Subroutine copy: called CC=11 SCAR=25.3
Pod documented
374 3 my $this = shift;
375 3 my $srcPath = $this->expandTilde(shift);
376 3 my $destPath = $this->expandTilde(shift);
377 # @_: @opt
378  
379 # Optionen
380  
381 3 my $createDir = 0;
382 3 my $log => undef;
383 3 my $move = 0;
384 3 my $overwrite = 1;
385 3 my $preserve = 0;
386  
387 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
388 2 Quiq::Option->extract(\@_,
389 -createDir => \$createDir,
390 -log => \$log,
391 -move => \$move,
392 -overwrite => \$overwrite,
393 -preserve => \$preserve,
394 );
395 }
396  
397 # Operation ausführen
398  
399 3 ▶ if (-e $destPath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $destPath) 1 2 3
400 1 ▶ if ($this->sameFile($srcPath,$destPath)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->sameFile($srcPath, $destPath)) 0 1 1
401 # Wenn Quell- und Zieldatei identisch sind
402 # (gleicher Inode), machen wir nichts
403 0 return;
404 }
405 1 ▶ if (!$overwrite) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($overwrite) 0 1 1
406 0 $this->throw(
407 'PATH-00099: Destination path already exists',
408 Path => $destPath,
409 );
410 }
411 }
412  
413 3 my $fh1 = Quiq::FileHandle->new('<',$srcPath);
414  
415 3 ▶ if ($createDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($createDir) 0 3 3
416 0 my ($destDir) = $this->split($destPath);
417 0 $this->mkdir($destDir,-recursive=>1);
418 }
419  
420 3 my $fh2 = Quiq::FileHandle->new('>',$destPath);
421 3 ▶ while (<$fh1>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh1)) 6 3 9
422 6 ▶ print $fh2 $_ or $this->throw(
Branch: 1 branch
BranchTrueFalseTotal
print $fh2 $_ or $this->throw("PATH-00007: Write failed", "SourcePath", $srcPath, "DestinationPath", $destPath) 6 0 6
423 'PATH-00007: Write failed',
424 SourcePath => $srcPath,
425 DestinationPath => $destPath,
426 );
427 }
428 3 $fh2->close;
429 3 $fh1->close;
430  
431 3 ▶ if ($preserve) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve) 2 1 3
432 2 $this->mtime($destPath,$this->mtime($srcPath));
433 }
434  
435 3 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 0 3 3
436 0 print $log "$srcPath => $destPath\n";
437 }
438  
439 3 ▶ if ($move) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move) 0 3 3
440 0 $this->delete($srcPath);
441 }
442  
443 3 return;
444 }
445  
446 # -----------------------------------------------------------------------------
447  
448 =head3 copyToDir() - Kopiere Datei in Verzeichnis
449  
450 =head4 Synopsis
451  
452 $class->copyToDir($srcFile,$destDir,@opt);
453  
454 =head4 Options
455  
456 =over 4
457  
458 =item -createDir => $bool (Default: 0)
459  
460 Erzeuge Zielverzeichnis, falls es nicht existiert.
461  
462 =item -log => $stream (Default: undef)
463  
464 Schreibe Meldung auf Dateihandle. Beispiel:
465  
466 -log => \*STDOUT
467  
468 =item -move => $bool (Default: 0)
469  
470 Lösche Quelldatei $srcPath nach dem Kopieren.
471  
472 =item -overwrite => $bool (Default: 1)
473  
474 Wenn gesetzt, wird die Zieldatei $destPath überschrieben, falls sie
475 existiert. Andernfalls wird eine Exception geworfen.
476  
477 =item -preserve => $bool (Default: 0)
478  
479 Behalte den Zeitpunkt der letzten Änderung bei.
480  
481 =back
482  
483 =head4 Description
484  
485 Kopiere Datei $srcPath nach $destPath.
486  
487 =cut
488  
489 # -----------------------------------------------------------------------------
490  
491 0 ▶sub copyToDir {
Subroutine copyToDir: not called CC=1 SCAR=6.9
Pod documented
492 0 my $class = shift;
493 0 my $srcFile = shift;
494 0 my $destDir = shift;
495 # @_: @opt
496  
497 0 my $destFile = sprintf '%s/%s',$destDir,$class->filename($srcFile);
498 0 $class->copy($srcFile,$destFile,@_);
499  
500 0 return;
501 }
502  
503 # -----------------------------------------------------------------------------
504  
505 =head3 copyN() - Kopiere N Dateien
506  
507 =head4 Synopsis
508  
509 $this->copyN($n,$destDir,@globPaths,%options);
510  
511 =head4 Options
512  
513 =over 4
514  
515 =item -createDir => $bool (Default: 0)
516  
517 Erzeuge Zielverzeichnis, falls es nicht existiert.
518  
519 =item -log => $stream (Default: undef)
520  
521 Schreibe Meldung auf Dateihandle. Beispiel:
522  
523 -log => \*STDOUT
524  
525 =item -move => $bool (Default: 0)
526  
527 Lösche Quelldatei $srcPath nach dem Kopieren.
528  
529 =item -overwrite => $bool (Default: 1)
530  
531 Wenn gesetzt, wird die Zieldatei $destPath überschrieben, falls sie
532 existiert. Andernfalls wird eine Exception geworfen.
533  
534 =item -preserve => $bool (Default: 1)
535  
536 Behalte den Zeitpunkt der letzten Änderung bei.
537  
538 =back
539  
540 =head4 Description
541  
542 Kopiere N Dateien aus @globPaths nach $destPath.
543  
544 =cut
545  
546 # -----------------------------------------------------------------------------
547  
548 0 ▶sub copyN {
Subroutine copyN: not called CC=5 SCAR=34.0
Pod documented
549 0 my $this = shift;
550 # @_: $n,$destDir,@globPaths,%options
551  
552 # Optionen und Argumente
553  
554 0 my $createDir = 0;
555 0 my $log => undef;
556 0 my $move = 0;
557 0 my $overwrite = 1;
558 0 my $preserve = 1;
559  
560 0 my $argA = $this->parameters(3,undef,\@_,
561 -createDir => \$createDir,
562 -log => \$log,
563 -move => \$move,
564 -overwrite => \$overwrite,
565 -preserve => \$preserve,
566 );
567 0 my $n = shift @$argA;
568 0 my $destDir = $this->expandTilde(shift @$argA);
569 0 my @globPaths = map {$this->expandTilde($_)} @$argA;
570  
571 # Operation ausführen
572  
573 0 ▶ if (!-e $destDir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $destDir) 0 0 0
574 0 ▶ if ($createDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($createDir) { } 0 0 0
575 0 $this->mkdir($destDir);
576 }
577 else {
578 0 $this->throw(
579 'PATH-00099: Destination dir does not exist',
580 Dir => $destDir,
581 );
582 }
583 }
584  
585 0 my @files;
586 0 for my $globPath (@globPaths) {
587 0 push @files,$this->glob($globPath);
588 }
589 0 @files = sort @files;
590 0 @files = splice @files,0,$n;
591  
592 0 for my $file (@files) {
593 0 $this->copyToDir($file,$destDir,
594 -createDir => $createDir,
595 -log => $log,
596 -move => $move,
597 -overwrite => $overwrite,
598 -preserve => $preserve,
599 );
600 }
601  
602 0 return;
603 }
604  
605 # -----------------------------------------------------------------------------
606  
607 =head3 duplicate() - Kopiere, bewege, linke oder symlinke Datei
608  
609 =head4 Synopsis
610  
611 $this->duplicate($method,$srcPath,$destPath,@opt);
612  
613 =head4 Arguments
614  
615 =over 4
616  
617 =item $method
618  
619 Anzuwendende Pfadoperation:
620  
621 copy
622 move -or- rename
623 link
624 symlink
625  
626 =item $srcPath
627  
628 (String) Quellpfad
629  
630 =item $destPath
631  
632 (String) Zielpfad
633  
634 =back
635  
636 =head4 Options
637  
638 =over 4
639  
640 =item -preserve => $bool (Default: 0)
641  
642 Behalte den Zeitpunkt der letzten Änderung bei (nur bei 'copy'
643 relevant).
644  
645 =back
646  
647 =head4 Description
648  
649 Mache Datei $srcPath nach Methode $method unter $destPath verfügbar.
650 Werte für $method:
651  
652 copy
653 move -or- rename
654 link
655 symlink
656  
657 =cut
658  
659 # -----------------------------------------------------------------------------
660  
661 0 ▶sub duplicate {
Subroutine duplicate: not called CC=7 SCAR=40.3
Pod documented
662 0 my $this = shift;
663 0 my $method = shift;
664 0 my $srcPath = $this->expandTilde(shift);
665 0 my $destPath = $this->expandTilde(shift);
666 # @_: @opt
667  
668 # Optionen
669  
670 0 my $preserve = 0;
671  
672 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
673 0 Quiq::Option->extract(\@_,
674 -preserve => \$preserve,
675 );
676 }
677  
678 # Operation ausführen
679  
680 0 ▶ if ($method eq 'copy') {
Branch: 4 branches
BranchTrueFalseTotal
if ($method eq 'copy') { } 0 0 0
elsif ($method eq 'move' or $method eq 'rename') { } 0 0 0
elsif ($method eq 'link') { } 0 0 0
elsif ($method eq 'symlink') { } 0 0 0
681 0 $this->copy($srcPath,$destPath,-preserve=>1);
682 }
683 ▶ elsif ($method eq 'move' || $method eq 'rename') {
Condition0%0/3
$method eq "move" or $method eq "rename"
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "move" or $method eq "rename"
$method eq "move" $method eq "rename"
Truth table0%0/3
A or B
A= $method eq "move"B= $method eq "rename"
ABresult
1X1
011
000
684 0 $this->rename($srcPath,$destPath);
685 }
686 elsif ($method eq 'link') {
687 0 $this->link($srcPath,$destPath);
688 }
689 elsif ($method eq 'symlink') {
690 0 $this->symlinkRelative($srcPath,$destPath);
691 }
692 else {
693 0 $this->throw;
694 }
695  
696 0 return;
697 }
698  
699 # -----------------------------------------------------------------------------
700  
701 =head3 edit() - Bearbeite Datei oder Daten im Editor
702  
703 =head4 Synopsis
704  
705 $changed = $this->edit($file,%options);
706 $changed = $this->edit(\$data,%options);
707  
708 =head4 Arguments
709  
710 =over 4
711  
712 =item $file
713  
714 Datei, die bearbeitet werden soll.
715  
716 =item $data
717  
718 Daten, die bearbeitet werden sollen.
719  
720 =back
721  
722 =head4 Options
723  
724 =over 4
725  
726 =item -create => $bool (Default: 0)
727  
728 Erzeuge die Datei, wenn sie nicht existiert.
729  
730 =back
731  
732 =head4 Returns
733  
734 Boolschen Wert, der anzeigt, ob die Datei oder die Daten
735 verändert wurden.
736  
737 =head4 Description
738  
739 Öffne Datei $file oder Daten $data im Editor, so dass diese vom
740 Benutzer bearbeitet werden können. Die Methode prüft nach
741 Verlassen des Editors, ob die Datei bzw. Daten geändert
742 wurden. Falls ja, wird der Benutzer gefragt, ob er die Änderungen
743 beibehalten möchte. Falls ja, liefert die Methode wahr,
744 andernfalls falsch.
745  
746 =cut
747  
748 # -----------------------------------------------------------------------------
749  
750 0 ▶sub edit {
Subroutine edit: not called CC=11 SCAR=48.8
Pod documented
751 0 my $this = shift;
752 # @_: $file -or- \$data
753  
754 # Parameter
755  
756 0 my $create = 0;
757  
758 0 my $argA = $this->parameters(1,1,\@_,
759 -create => \$create,
760 );
761 0 my $arg = shift @$argA;
762 0 ▶ my ($file,$dataR) = ref $arg? (undef,$arg): ($arg,undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 0 0 0
763  
764 # Operation ausführen
765  
766 0 my $encoding = 'utf-8';
767 0 my $changed = 0;
768 0 my $tmpFile = Quiq::TempFile->new;
769  
770 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
771 0 ▶ if (!$this->exists($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->exists($file)) 0 0 0
772 0 $this->write($file,'');
773 }
774  
775 # Erzeuge eine temporäre Kopie
776 0 $this->copy($file,$tmpFile);
777 }
778 else { # $dataR
779 # Schreibe Daten auf temporäre Datei
780 0 $this->write($tmpFile,$$dataR,-encode=>$encoding);
781 }
782  
783 # Öffne Tempdatei im Editor
784  
785 0 ▶ my $editor = $ENV{'EDITOR'} || 'vi';
Condition0%0/2
$ENV{'EDITOR'} || 'vi'
l!l
00
MC/DC0%0/1
$ENV{'EDITOR'} || 'vi'
$ENV{'EDITOR'}
Truth table0%0/2
A
A= $ENV{'EDITOR'}
Aresult
11
00
786 0 Quiq::Shell->exec("$editor $tmpFile");
787  
788 0 my $ask = 0;
789 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
790 0 ▶ if ($this->compare($tmpFile,$file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->compare($tmpFile, $file)) 0 0 0
791 0 $ask = 1;
792 }
793 }
794 else {
795 0 ▶ if ($this->compareData($tmpFile,$$dataR,$encoding)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->compareData($tmpFile, $$dataR, $encoding)) 0 0 0
796 0 $ask = 1;
797 }
798 }
799  
800 0 ▶ if ($ask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ask) 0 0 0
801 # Rückfrage an Benutzer
802  
803 0 my $answ = Quiq::Terminal->askUser(
804 "Confirm changes?",
805 -values => 'y/n',
806 -default => 'y',
807 );
808 0 ▶ if ($answ eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answ eq "y") 0 0 0
809 # Schreibe die Änderungen auf die Datei
810  
811 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
812 0 $this->copy($tmpFile,$file);
813 }
814 else {
815 0 $$dataR = $this->read($tmpFile,-decode=>'utf-8');
816 }
817 0 $changed = 1;
818 }
819 }
820  
821 0 return $changed;
822 }
823  
824 # -----------------------------------------------------------------------------
825  
826 =head3 detectEncoding() - Liefere das Encoding der Datei
827  
828 =head4 Synopsis
829  
830 $encoding = $class->detectEncoding($path,$altEncoding);
831  
832 =head4 Alias
833  
834 encoding()
835  
836 =head4 Description
837  
838 Analysiere Datei $path hinsichtlich ihres Character-Encodings
839 und liefere den Encoding-Bezeichner zurück. Unterschieden werden:
840  
841 =over 2
842  
843 =item *
844  
845 ASCII
846  
847 =item *
848  
849 UTF-8
850  
851 =item *
852  
853 UTF-16/32 mit BOM
854  
855 =back
856  
857 und $altEncoding. Ist $altEncoding nicht angegeben, wird
858 'ISO-8859-1' angenommen.
859  
860 Anmerkung: Die Datei wird zur Zeichensatz-Analyse vollständig eingelesen.
861 Bei großen Dateien kann dies ineffizient sein.
862  
863 =cut
864  
865 # -----------------------------------------------------------------------------
866  
867 0 ▶sub detectEncoding {
Subroutine detectEncoding: not called CC=4 SCAR=30.0
Pod documented
868 0 my $class = shift;
869 0 my $path = shift;
870 0 ▶ my $altEncoding = shift // 'ISO-8859-1';
Condition0%0/2
shift() // "ISO-8859-1"
l!l
00
MC/DC0%0/1
shift() // "ISO-8859-1"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
871  
872 0 my $data = $class->read($path);
873 0 my $dec = Encode::Guess->guess($data);
874 0 ▶ if (ref $dec) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dec) { } 0 0 0
elsif ($dec =~ /No appropriate encodings found/i) { } 0 0 0
875 0 return $dec->name;
876 }
877 elsif ($dec =~ /No appropriate encodings found/i) {
878 0 return $altEncoding;
879 }
880  
881 # Unerwarteter Fehler
882  
883 $class->throw(
884 0 'PATH-00099: Can\'t decode file content',
885 Path => $path,
886 Message => $dec,
887 );
888 }
889  
890 {
891 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
892 270 *encoding = \&detectEncoding;
893 }
894  
895 # -----------------------------------------------------------------------------
896  
897 =head3 link() - Erzeuge (Hard)Link
898  
899 =head4 Synopsis
900  
901 $this->link($path,$link);
902  
903 =head4 Options
904  
905 =over 4
906  
907 =item -force => $bool (Default: 0)
908  
909 Lösche die Datei $link, falls sie bereits existiert.
910  
911 =back
912  
913 =head4 Description
914  
915 Erzeuge einen Hardlink $link auf Pfad $path.
916 Die Methode liefert keinen Wert zurück.
917  
918 =cut
919  
920 # -----------------------------------------------------------------------------
921  
922 0 ▶sub link {
Subroutine link: not called CC=4 SCAR=30.0
Pod documented
923 0 my $this = shift;
924  
925 0 my $force = 0;
926  
927 0 my $argA = $this->parameters(2,2,\@_,
928 -force => \$force,
929 );
930 0 my $path = $this->expandTilde(shift @$argA);
931 0 my $link = $this->expandTilde(shift @$argA);
932  
933 0 ▶ if (-f $link && $force) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $link and $force) 0 0 0
Condition0%0/3
-f $link and $force
!ll&&!rl&&r
000
MC/DC0%0/2
-f $link and $force
-f $link $force
Truth table0%0/3
A and B
A= -f $linkB= $force
ABresult
0X0
100
111
934 0 $this->delete($link);
935 }
936
937 0 ▶ CORE::link $path,$link or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::link $path, $link or do { $this->throw("FS-00002: Kann Link nicht erzeugen", "Path", $path, "Link", $link, "Error", $!) } 0 0 0
938 0 $this->throw(
939 'FS-00002: Kann Link nicht erzeugen',
940 Path => $path,
941 Link => $link,
942 Error => $!,
943 );
944 };
945  
946 0 return;
947 }
948  
949 # -----------------------------------------------------------------------------
950  
951 =head3 mimeTypeByExtension() - Liefere MIME Type zur Extension
952  
953 =head4 Synopsis
954  
955 $mineType = $this->mimeTypeByExtension($path);
956  
957 =head4 Description
958  
959 Ermittele den MIME Type der Datei auf Basis der Extension und liefere
960 diesen zurück.
961  
962 =cut
963  
964 # -----------------------------------------------------------------------------
965  
966 2 ▶sub mimeTypeByExtension {
Subroutine mimeTypeByExtension: called CC=2 SCAR=6.9
Pod documented
967 2 my ($this,$path) = @_;
968  
969 2 my $ext = lc $this->extension($path);
970 my $mimeType = {
971 pdf => 'application/pdf',
972 2 }->{$ext};
973  
974 2 ▶ if (!$mimeType) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mimeType) 1 1 2
975 1 $this->throw(
976 'PATH-00099: Unknown path extension',
977 Extension => $path,
978 );
979 }
980  
981 1 return $mimeType;
982 }
983  
984 # -----------------------------------------------------------------------------
985  
986 =head3 newlineStr() - Ermittele Zeilentrenner
987  
988 =head4 Synopsis
989  
990 $nl = $class->newlineStr($file);
991  
992 =head4 Description
993  
994 Ermittele den physischen Zeilentrenner (CR, LF oder CRLF) der Datei
995 $file und liefere diesen zurück. Wird kein Zeilentrenner gefunden,
996 liefere undef.
997  
998 =head4 Example
999  
1000 local $/ = Quiq::Path->newlineStr($file);
1001
1002 while (<$fh>) {
1003 chomp;
1004 # Zeile verarbeiten
1005 }
1006  
1007 =cut
1008  
1009 # -----------------------------------------------------------------------------
1010  
1011 3 ▶sub newlineStr {
Subroutine newlineStr: called CC=6 SCAR=17.9
Pod documented
1012 3 my ($class,$file) = @_;
1013  
1014 3 my $fh = Quiq::FileHandle->new('<',$file);
1015 3 $fh->binmode;
1016  
1017 3 my $nl;
1018 3 ▶ while (defined(my $c = getc $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $c = getc $fh)) 21 0 21
1019 21 ▶ if ($c eq "\cM") {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq "\r") { } 2 19 21
elsif ($c eq "\n") { } 1 18 19
1020 2 $c = getc $fh;
1021 2 ▶ if (defined($c) && $c eq "\cJ") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c eq "\n") 1 1 2
Condition66%2/3
defined $c and $c eq "\n"
!ll&&!rl&&r
011
MC/DC50%1/2
defined $c and $c eq "\n"
defined $c $c eq "\n"
Truth table66%2/3
A and B
A= defined $cB= $c eq "\n"
ABresult
0X0
100
111
1022 1 $nl = "\cM\cJ";
1023 1 last;
1024 }
1025 1 $nl = "\cM";
1026 1 last;
1027 }
1028 elsif ($c eq "\cJ") {
1029 1 $nl = "\cJ";
1030 1 last;
1031 }
1032 }
1033 3 $fh->close;
1034  
1035 3 return $nl;
1036 }
1037  
1038 # -----------------------------------------------------------------------------
1039  
1040 =head3 nextFile() - Generiere nächsten Dateinamen
1041  
1042 =head4 Synopsis
1043  
1044 $file = $this->nextFile($name,$n,$ext);
1045  
1046 =head4 Arguments
1047  
1048 =over 4
1049  
1050 =item $name
1051  
1052 Grundname der Datei. Kann leer (Leerstring oder C<undef>) sein,
1053 dann besteht der Dateiname nur aus aus der laufenden Nummer.
1054  
1055 =item $n
1056  
1057 Anzahl der Stellen der laufenden Nummer.
1058  
1059 =item $ext
1060  
1061 Extension der Datei. Kann leer (Leerstring oder C<undef>) sein.
1062  
1063 =back
1064  
1065 =head4 Description
1066  
1067 Ermittele und liefere den nächsten Namen einer Datei. Der Dateiname
1068 hat den Aufbau
1069  
1070 NAME-NNNN.EXT
1071  
1072 Die laufende Nummer NNNN (deren Breite durch den zweiten Parameter
1073 festgelegt) wird anhand der vorhandenen Dateien im Dateisystem
1074 ermittelt und um 1 erhöht.
1075  
1076 =head4 Example
1077  
1078 Es liegt noch keine Datei vor:
1079  
1080 $file = Quiq::Path->nextFile('myfile',3,'log');
1081 =>
1082 myfile-001.log
1083  
1084 Die Datei mit der höchsten Nummer ist myfile-031.log:
1085  
1086 $file = Quiq::Path->nextFile('myfile',3,'log');
1087 =>
1088 myfile-032.log
1089  
1090 =cut
1091  
1092 # -----------------------------------------------------------------------------
1093  
1094 0 ▶sub nextFile {
Subroutine nextFile: not called CC=2 SCAR=17.9
Pod documented
1095 0 my ($this,$name,$n,$ext) = @_;
1096  
1097 0 my @files = sort $this->glob("$name-*.$ext");
1098 0 ▶ my $file = $files[-1] // sprintf '%s-%0*d.%s',$name,$n,0,$ext;
Condition0%0/2
$files[-1] // sprintf("%s-%0*d.%s", $name, $n, 0, $ext)
l!l
00
MC/DC0%0/1
$files[-1] // sprintf("%s-%0*d.%s", $name, $n, 0, $ext)
$files[-1]
Truth table0%0/2
A
A= $files[-1]
Aresult
11
00
1099 0 my ($i) = $file =~ /^\Q$name\E-(\d+).\Q$ext\E/;
1100 0 $file = sprintf "%s-%0*d.%s",$name,$n,++$i,$ext;
1101
1102 0 return $file;
1103 }
1104  
1105 # -----------------------------------------------------------------------------
1106  
1107 =head3 nextNumber() - Generiere nächste Dateinamen-Nummer
1108  
1109 =head4 Synopsis
1110  
1111 $num = $this->nextNumber($dir,$width);
1112  
1113 =head4 Arguments
1114  
1115 =over 4
1116  
1117 =item $dir
1118  
1119 Verzeichnis mit nummierten Dateien.
1120  
1121 =item $width
1122  
1123 Anzahl der Stellen der Nummer.
1124  
1125 =back
1126  
1127 =head4 Returns
1128  
1129 Nummer mit der angegebenen Anzahl Stellen, ggf. mit führenden 0en
1130 aufgefüllt.
1131  
1132 =head4 Description
1133  
1134 Ermittele und liefere die nächste Nummer eines Verzeichnisses mit
1135 nummerierten Dateien. Die Dateinamen haben einen beliebigen
1136 Aufbau, müssen aber eine Nummer mit genau $width Stellen
1137 besitzen. Die laufende Nummer NNNN (deren Breite durch den zweiten
1138 Parameter festgelegt ist) wird anhand der vorhandenen Dateien
1139 im Verzeichnis $dir ermittelt und um 1 erhöht.
1140  
1141 =head4 Example
1142  
1143 Es liegt noch keine Datei mit einer dreistelligen Nummer NNN vor:
1144  
1145 $num = Quiq::Path->nextNumber($dir,3);
1146 =>
1147 001
1148  
1149 Die Datei mit der höchsten dreistelligen Nummer NNN enthält 031:
1150  
1151 $num = Quiq::Path->nextNumber($dir,3);
1152 =>
1153 032
1154  
1155 =cut
1156  
1157 # -----------------------------------------------------------------------------
1158  
1159 0 ▶sub nextNumber {
Subroutine nextNumber: not called CC=4 SCAR=30.0
Pod documented
1160 0 my ($this,$dir,$width) = @_;
1161  
1162 0 my $n = 0;
1163 0 for my $file ($this->glob("$dir/*")) {
1164 0 ▶ if ($file =~ /(?:^|\D)(\d{$width})(?:$|\D)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /(?:^|\D)(\d{$width})(?:$|\D)/) 0 0 0
1165 0 ▶ if ($1 > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 > $n) 0 0 0
1166 0 $n = $1+0;
1167 }
1168 }
1169 }
1170  
1171 0 return sprintf '%0*d',$width,++$n;
1172 }
1173  
1174 # -----------------------------------------------------------------------------
1175  
1176 =head3 read() - Lies Datei
1177  
1178 =head4 Synopsis
1179  
1180 $data = $class->read($file,@opt);
1181  
1182 =head4 Options
1183  
1184 =over 4
1185  
1186 =item -autoDecode => $bool (Default: 0)
1187  
1188 Auto-Dekodiere die gelesenen Daten als Text und entscheide selbständig,
1189 ob es sich um UTF-8 oder ISO-8859-1 Encoding handelt.
1190  
1191 =item -decode => $encoding (Default: undef)
1192  
1193 Decodiere die Datei gemäß dem Encoding $encoding.
1194  
1195 =item -delete => $bool (Default: 0)
1196  
1197 Lösche Datei nach dem Lesen.
1198  
1199 =item -maxLines => $n (Default: 0)
1200  
1201 Lies höchstens $n Zeilen. Die Zählung beginnt nach den
1202 Skiplines (s. Option -skipLines). 0 bedeutet, lies alle Zeilen.
1203  
1204 =item -skip => $regex (Default: keiner)
1205  
1206 Überlies alle Zeilen, die das Muster $regex erfüllen. $regex
1207 wird als Zeichenkette angegeben. Die Option kann beispielsweise dazu
1208 verwendet werden, um Kommentarzeilen zu überlesen.
1209  
1210 =item -skipLines => $n (Default: 0)
1211  
1212 Überlies die ersten $n Zeilen.
1213  
1214 =item -sloppy => $bool (Default: 0)
1215  
1216 Existiert die Datei nicht, liefere C<undef> und wirf keine Exception.
1217  
1218 =back
1219  
1220 =head4 Description
1221  
1222 Lies den Inhalt der Datei und liefere diesen zurück.
1223  
1224 =cut
1225  
1226 # -----------------------------------------------------------------------------
1227  
1228 41 ▶sub read {
Subroutine read: called CC=17 SCAR=28.6
Pod documented
1229 41 my $class = shift;
1230 41 my $file = shift;
1231 # @_: Optionen
1232  
1233 # Optionen
1234  
1235 41 my $autoDecode = 0;
1236 41 my $decode = undef;
1237 41 my $delete = 0;
1238 41 my $maxLines = 0;
1239 41 my $skip = undef;
1240 41 my $skipLines = 0;
1241 41 my $sloppy = 0;
1242  
1243 41 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 30 41
1244 11 Quiq::Option->extract(\@_,
1245 -autoDecode => \$autoDecode,
1246 -decode => \$decode,
1247 -delete => \$delete,
1248 -maxLines => \$maxLines,
1249 -skip => \$skip,
1250 -skipLines => \$skipLines,
1251 -sloppy => \$sloppy,
1252 );
1253 }
1254  
1255 # Datei lesen
1256  
1257 41 $file = $class->expandTilde($file);
1258 41 ▶ if ($sloppy && !-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sloppy and not -e $file) 0 41 41
Condition33%1/3
$sloppy and not -e $file
!ll&&!rl&&r
4100
MC/DC0%0/2
$sloppy and not -e $file
$sloppy not -e $file
Truth table33%1/3
A and not(B)
A= $sloppyB= not -e $file
ABresult
0X0
100
111
1259 0 return undef;
1260 }
1261  
1262 41 my $fh = Quiq::FileHandle->new('<',$file);
1263  
1264 41 my $data = '';
1265 41 ▶ if ($maxLines || $skip || $skipLines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxLines or $skip or $skipLines) { } 3 38 41
Condition100%6/6
$maxLines or $skip or $skipLines
l!l&&r!l&&!r
2138
$maxLines or $skip
l!l&&r!l&&!r
1139
MC/DC100%3/3
$maxLines or $skip or $skipLines
$maxLines $skip $skipLines
Truth table100%4/4
A or B or C
A= $maxLinesB= $skipC= $skipLines
ABCresult
1XX1
01X1
0011
0000
1266 3 my $i = 0;
1267 3 my $j = 0;
1268 3 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 12 2 14
1269 12 ▶ next if $skipLines && $i++ < $skipLines;
Branch: 1 branch
BranchTrueFalseTotal
if $skipLines and $i++ < $skipLines 2 10 12
Condition100%3/3
$skipLines and $i++ < $skipLines
!ll&&!rl&&r
912
MC/DC100%2/2
$skipLines and $i++ < $skipLines
$skipLines $i++ < $skipLines
Truth table100%3/3
A and B
A= $skipLinesB= $i++ < $skipLines
ABresult
0X0
100
111
1270 10 ▶ last if $maxLines && ++$j > $maxLines;
Branch: 1 branch
BranchTrueFalseTotal
if $maxLines and ++$j > $maxLines 1 9 10
Condition100%3/3
$maxLines and ++$j > $maxLines
!ll&&!rl&&r
631
MC/DC100%2/2
$maxLines and ++$j > $maxLines
$maxLines ++$j > $maxLines
Truth table100%3/3
A and B
A= $maxLinesB= ++$j > $maxLines
ABresult
0X0
100
111
1271 9 ▶ next if $skip && /$skip/; # Zeile überlesen
Branch: 1 branch
BranchTrueFalseTotal
if $skip and /$skip/ 2 7 9
Condition100%3/3
$skip and /$skip/
!ll&&!rl&&r
432
MC/DC100%2/2
$skip and /$skip/
$skip /$skip/
Truth table100%3/3
A and B
A= $skipB= /$skip/
ABresult
0X0
100
111
1272 7 $data .= $_;
1273 }
1274 }
1275 else {
1276 38 local $/ = undef;
1277 38 $data = <$fh>;
1278 }
1279  
1280 41 $fh->close;
1281  
1282 41 ▶ if ($delete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) 2 39 41
1283 2 $class->delete($file);
1284 }
1285  
1286 41 ▶ if ($decode) {
Branch: 2 branches
BranchTrueFalseTotal
if ($decode) { } 0 41 41
elsif ($autoDecode) { } 0 41 41
1287 0 $data = Encode::decode($decode,$data);
1288 }
1289 elsif ($autoDecode) {
1290 0 $data = Quiq::String->autoDecode($data);
1291 }
1292  
1293 41 return $data;
1294 }
1295  
1296 # -----------------------------------------------------------------------------
1297  
1298 =head3 sha1() - SHA1 Hex Digest
1299  
1300 =head4 Synopsis
1301  
1302 $this->sha1($file);
1303  
1304 =head4 Arguments
1305  
1306 =over 4
1307  
1308 =item $file
1309  
1310 Pfad der Datei.
1311  
1312 =back
1313  
1314 =head4 Description
1315  
1316 Ermittele den SHA1 Hex Digest der Datei $file und liefere diesen zurück.
1317  
1318 =cut
1319  
1320 # -----------------------------------------------------------------------------
1321  
1322 0 ▶sub sha1 {
Subroutine sha1: not called CC=1 SCAR=6.9
Pod documented
1323 0 my $this = shift;
1324 0 my $file = $this->expandTilde(shift);
1325  
1326 0 my $sha = Digest::SHA->new(1);
1327 0 $sha->addfile($file);
1328  
1329 0 return $sha->hexdigest;;
1330 }
1331  
1332 # -----------------------------------------------------------------------------
1333  
1334 =head3 size() - Größe der Datei in Bytes
1335  
1336 =head4 Synopsis
1337  
1338 $size = $this->size($file);
1339  
1340 =head4 Arguments
1341  
1342 =over 4
1343  
1344 =item $file
1345  
1346 Pfad der Datei.
1347  
1348 =back
1349  
1350 =head4 Description
1351  
1352 Ermittele die Größe der Datei in Bytes und liefere diesen Wert zurück.
1353 Im Unterschied zu
1354  
1355 -s $file
1356  
1357 führt die Methode eine Tilde-Expansion durch.
1358  
1359 =cut
1360  
1361 # -----------------------------------------------------------------------------
1362  
1363 0 ▶sub size {
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
1364 0 my $this = shift;
1365 0 my $file = $this->expandTilde(shift);
1366  
1367 0 ▶ if (!-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
1368 0 $this->throw(
1369 'PATH-00099: File not found',
1370 Path => $file,
1371 );
1372 }
1373  
1374 0 return -s $file;
1375 }
1376  
1377 # -----------------------------------------------------------------------------
1378  
1379 =head3 tail() - Überwache Datei und gib hinzugefügte Daten kontinuierlich aus
1380  
1381 =head4 Synopsis
1382  
1383 $data = $this->tail($file,@opt);
1384  
1385 =head4 Arguments
1386  
1387 =over 4
1388  
1389 =item $file
1390  
1391 (String) Pfad der Datei.
1392  
1393 =back
1394  
1395 =head4 Options
1396  
1397 =over 4
1398  
1399 =item -offset => $pos (Default: 0)
1400  
1401 Beginne die Überwachung bei Datei-Offset $pos.
1402  
1403 =item -sleepInterval => $s (Default: 0.1)
1404  
1405 Zeitraum, der zwischen den Prüfungen auf Änderung gewartet wird.
1406  
1407 =item -timeout => $s (Default: I<überwache unendlich lange>)
1408  
1409 Beende die Überwachung der Datei, wenn $s Sekunden (Sekundenbruchteile
1410 erlaubt, z.B. C<0.5>) lang keine Änderung an der Datei erfolgt ist.
1411 Der Werte sollte größer sein als das Sleep Interval (s. C{-sleepInterval).
1412  
1413 =back
1414  
1415 =head4 Returns
1416  
1417 (String) Die während der Überwachung hinzugefügte Daten.
1418  
1419 =head4 Description
1420  
1421 Überwache Datei $file und gib die am Ende hinzugefügten Daten
1422 kontinuierlich aus. Die Überwachung endet, wenn der Datiinhalt sich
1423 $s Sekunden lang nicht geändert hat (Option C<--timeout>).
1424 Liefere die hinzugefügten Daten zurück.
1425  
1426 =cut
1427  
1428 # -----------------------------------------------------------------------------
1429  
1430 0 ▶sub tail {
Subroutine tail: not called CC=4 SCAR=30.0
Pod documented
1431 0 my $this = shift;
1432 0 my $file = $this->expandTilde(shift);
1433  
1434 # Options
1435  
1436 0 my $offset = 0;
1437 0 my $sleepInterval = 0.1;
1438 0 my $timeout = undef;
1439  
1440 0 $this->parameters(\@_,
1441 -offset => \$offset,
1442 -timeout => \$timeout,
1443 );
1444  
1445 0 my $str = '';
1446 0 my $lastChange = Time::HiRes::gettimeofday;
1447 0 while (1) {
1448 0 my $size = -s $file;
1449 0 ▶ if ($size > $offset) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size > $offset) { } 0 0 0
elsif (defined $timeout) { } 0 0 0
1450 0 my $fh = Quiq::FileHandle->new('<',$file);
1451 0 $fh->seek($offset);
1452 0 $fh->setEncoding('UTF-8');
1453 0 my $data = $fh->slurp;
1454 0 $str .= $data;
1455 0 print $data;
1456 0 $offset = $fh->tell;
1457 0 $fh->close;
1458 0 $lastChange = Time::HiRes::gettimeofday;
1459 }
1460 elsif (defined $timeout) {
1461 0 my $now = Time::HiRes::gettimeofday;
1462 0 ▶ if ($now-$lastChange > $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $lastChange > $timeout) 0 0 0
1463 0 last; # Ende der Überwachung
1464 }
1465 }
1466 0 Time::HiRes::usleep(int $sleepInterval*1000_000);
1467 }
1468  
1469 0 return $str;
1470 }
1471  
1472 # -----------------------------------------------------------------------------
1473  
1474 =head3 truncate() - Kürze Datei
1475  
1476 =head4 Synopsis
1477  
1478 $this->truncate($file);
1479  
1480 =head4 Arguments
1481  
1482 =over 4
1483  
1484 =item $file
1485  
1486 Pfad der Datei.
1487  
1488 =back
1489  
1490 =head4 Description
1491  
1492 Kürze Datei $file auf Länge 0, falls sie existiert. Existiert die
1493 Datei nicht, geschieht nichts.
1494  
1495 =cut
1496  
1497 # -----------------------------------------------------------------------------
1498  
1499 2 ▶sub truncate {
Subroutine truncate: called CC=2 SCAR=7.0
Pod documented
1500 2 my $this = shift;
1501 2 my $file = $this->expandTilde(shift);
1502  
1503 2 ▶ if ($this->exists($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->exists($file)) 2 0 2
1504 2 Quiq::FileHandle->new('>',$file)->truncate;
1505 }
1506  
1507 2 return;
1508 }
1509  
1510 # -----------------------------------------------------------------------------
1511  
1512 =head3 tempFile() - Erzeuge temporäre Datei
1513  
1514 =head4 Synopsis
1515  
1516 $file = $this->tempFile(@opt);
1517 $file = $this->tempFile($data,@opt);
1518  
1519 =head4 Arguments
1520  
1521 Daten, die in die temporäre Datei geschrieben werden.
1522  
1523 =head4 Options
1524  
1525 Siehe Quiq::TempFile->new().
1526  
1527 =head4 Returns
1528  
1529 =over 4
1530  
1531 =item $file
1532  
1533 Tempdatei-Objekt
1534  
1535 =back
1536  
1537 =head4 Description
1538  
1539 Erzeuge eine temporäre Datei, beschreibe sie mit den Daten $data und
1540 liefere eine Referenz auf das Objekt zurück.
1541  
1542 =head4 See Also
1543  
1544 Quiq::TempFile
1545  
1546 =cut
1547  
1548 # -----------------------------------------------------------------------------
1549  
1550 9 ▶sub tempFile {
Subroutine tempFile: called CC=1 SCAR=0.0
Pod documented
1551 9 my $this = shift;
1552 9 return Quiq::TempFile->new(@_);
1553 }
1554  
1555 # -----------------------------------------------------------------------------
1556  
1557 =head3 unindent() - Entferne Einrückung
1558  
1559 =head4 Synopsis
1560  
1561 $this->unindent($file);
1562  
1563 =head4 Arguments
1564  
1565 =over 4
1566  
1567 =item $file
1568  
1569 Pfad der Datei.
1570  
1571 =back
1572  
1573 =head4 Description
1574  
1575 Lies den Inhalt der Datei $file, entferne dessen Einrückung (per
1576 Quiq::Unindent->hereDoc) und schreibe den uneingerückten Inhalt auf
1577 die Datei zurück. Besitzt der Dateiinhalt keine Einrückung, findet keine
1578 Änderung des Dateiinhalts statt.
1579  
1580 =cut
1581  
1582 # -----------------------------------------------------------------------------
1583  
1584 2 ▶sub unindent {
Subroutine unindent: called CC=2 SCAR=6.9
Pod documented
1585 2 my ($this,$file) = @_;
1586  
1587 2 my $data1 = $this->read($file);
1588 2 my $data2 = Quiq::Unindent->hereDoc($data1);
1589 2 ▶ if ($data1 ne $data2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data1 ne $data2) 1 1 2
1590 1 $this->write($file,$data2);
1591 }
1592  
1593 2 return;
1594 }
1595  
1596 # -----------------------------------------------------------------------------
1597  
1598 =head3 write() - Schreibe Datei
1599  
1600 =head4 Synopsis
1601  
1602 $class->write($file); # leere Datei
1603 $class->write($file,$data,@opt);
1604 $class->write($file,\$data,@opt);
1605  
1606 =head4 Options
1607  
1608 =over 4
1609  
1610 =item -append => $bool (Default: 0)
1611  
1612 Öffne die Datei im Append-Modus, d.h. hänge die Daten an die Datei an.
1613  
1614 =item -encode => $encoding (Default: keiner)
1615  
1616 Encodiere $data gemäß dem Encoding $encoding.
1617  
1618 =item -lock => $bool (Default: 0)
1619  
1620 Setze während des Schreibens einen Exclusive-Lock auf die Datei.
1621 Dies kann im Falle von -append sinnvoll sein.
1622  
1623 =item -log => $stream (Default: undef)
1624  
1625 Schreibe Meldung auf Dateihandle. Beispiel:
1626  
1627 -log => \*STDOUT
1628  
1629 =item -mode => $mode (Default: keiner)
1630  
1631 Setze die Permissions der Datei auf $mode. Beispiel: -mode=>0775
1632  
1633 =item -recursive => $bool (Default: 1)
1634  
1635 Erzeuge übergeordnete Verzeichnisse, wenn nötig.
1636  
1637 =item -unindent => $bool (Default: 0)
1638  
1639 Wende Quiq::Unindent->trimNl() auf die Daten $data an. Dies ist für
1640 inline geschriebenen Text nützlich.
1641  
1642 =back
1643  
1644 =cut
1645  
1646 # -----------------------------------------------------------------------------
1647  
1648 54 ▶sub write {
Subroutine write: called CC=17 SCAR=32.8
Pod documented
1649 54 my $class = shift;
1650 54 my $file = shift;
1651 54 my $data = shift;
1652 # @_: @opt
1653  
1654 # Optionen
1655  
1656 54 my $append = 0;
1657 54 my $encode = undef;
1658 54 my $lock = 0;
1659 54 my $log = undef;
1660 54 my $mode = undef;
1661 54 my $recursive = 1;
1662 54 my $unindent = 0;
1663  
1664 54 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 49 54
1665 5 Quiq::Option->extract(\@_,
1666 -append => \$append,
1667 -encode => \$encode,
1668 -lock => \$lock,
1669 -log => \$log,
1670 -mode => \$mode,
1671 -recursive => \$recursive,
1672 -unindent => \$unindent,
1673 );
1674 }
1675  
1676 54 ▶ my $ref = ref $data? $data: \$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 0 54 54
1677  
1678 # Tilde-Expansion
1679 54 $file = $class->expandTilde($file);
1680  
1681 # Erzeuge Verzeichnis, wenn nötig
1682  
1683 54 ▶ if ($recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive) 54 0 54
1684 54 my $dir = ($class->split($file))[0];
1685 54 ▶ if ($dir && !-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir and not -d $dir) 0 54 54
Condition66%2/3
$dir and not -d $dir
!ll&&!rl&&r
2520
MC/DC0%0/2
$dir and not -d $dir
$dir not -d $dir
Truth table66%2/3
A and not(B)
A= $dirB= not -d $dir
ABresult
0X0
100
111
1686 0 $class->mkdir($dir,-recursive=>1);
1687 }
1688 }
1689  
1690 54 my $flags = Fcntl::O_WRONLY|Fcntl::O_CREAT;
1691 54 ▶ $flags |= $append? Fcntl::O_APPEND: Fcntl::O_TRUNC;
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 3 51 54
1692  
1693 54 local *F;
1694 54 ▶ sysopen(F,$file,$flags) || do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen F, $file, $flags or do { $class->throw("PATH-00006: Datei kann nicht zum Schreiben ge\x{f6}ffnet werden", "Path", $file, "Error", "$!") } 54 0 54
1695 0 $class->throw(
1696 'PATH-00006: Datei kann nicht zum Schreiben geöffnet werden',
1697 Path => $file,
1698 Error => "$!",
1699 );
1700 };
1701  
1702 54 ▶ if ($lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock) 0 54 54
1703 0 ▶ flock(F,Fcntl::LOCK_EX) || do {
Branch: 1 branch
BranchTrueFalseTotal
flock F, 2 or do { $class->throw("PATH-00099: Can't get exclusive lock", "Path", $file, "Error", "$!") } 0 0 0
1704 0 $class->throw(
1705 'PATH-00099: Can\'t get exclusive lock',
1706 Path => $file,
1707 Error => "$!",
1708 );
1709 };
1710 }
1711  
1712 54 ▶ if ($encode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encode) 0 54 54
1713 0 Quiq::Perl->binmode(*F,":encoding($encode)");
1714 }
1715  
1716 # Wenn keine Daten zu schreiben sind, print auslassen,
1717 # da sonst eine Exception ausgelöst wird.
1718  
1719 54 ▶ if (defined($$ref) && $$ref ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$ref and $$ref ne "") 41 13 54
Condition100%3/3
defined $$ref and $$ref ne ""
!ll&&!rl&&r
8541
MC/DC100%2/2
defined $$ref and $$ref ne ""
defined $$ref $$ref ne ""
Truth table100%3/3
A and B
A= defined $$refB= $$ref ne ""
ABresult
0X0
100
111
1720 # Unindent
1721  
1722 41 ▶ if ($unindent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unindent) 0 41 41
1723 0 $$ref = Quiq::Unindent->trimNl($$ref);
1724 }
1725  
1726 41 ▶ print F $$ref or do {
Branch: 1 branch
BranchTrueFalseTotal
print F $$ref or do { my $errStr = "$!"; close F; $class->throw("PATH-00007: Schreiben auf Datei fehlgeschlagen", "Path", $file, "Error", $errStr) } 41 0 41
1727 0 my $errStr = "$!";
1728 0 close F;
1729 0 $class->throw(
1730 'PATH-00007: Schreiben auf Datei fehlgeschlagen',
1731 Path => $file,
1732 Error => $errStr,
1733 );
1734 };
1735  
1736 41 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 0 41 41
1737 0 print $log "$file written\n";
1738 }
1739 }
1740  
1741 54 close F;
1742  
1743 54 ▶ if (defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) 0 54 54
1744 # Permissions setzen
1745 0 $class->chmod($file,$mode);
1746 }
1747
1748 54 return;
1749 }
1750  
1751 # -----------------------------------------------------------------------------
1752  
1753 =head3 writeIfDifferent() - Schreibe Datei, wenn Inhalt differiert
1754  
1755 =head4 Synopsis
1756  
1757 $class->writeIfDifferent($file,$data);
1758  
1759 =cut
1760  
1761 # -----------------------------------------------------------------------------
1762  
1763 0 ▶sub writeIfDifferent {
Subroutine writeIfDifferent: not called CC=2 SCAR=17.9
Pod documented
1764 0 my $class = shift;
1765 0 my $file = shift;
1766 # @_: $data
1767  
1768 0 ▶ if ($class->compareData($file,$_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->compareData($file, $_[0])) 0 0 0
1769 0 $class->write($file,$_[0]);
1770 0 return 1;
1771 }
1772  
1773 0 return 0;
1774 }
1775  
1776 # -----------------------------------------------------------------------------
1777  
1778 =head3 writeInline() - Schreibe Inline-Daten in Datei
1779  
1780 =head4 Synopsis
1781  
1782 $class->writeInline($file,<<'__EOT__',@opt);
1783 DATA
1784 ...
1785 __EOT__
1786  
1787 =cut
1788  
1789 # -----------------------------------------------------------------------------
1790  
1791 0 ▶sub writeInline {
Subroutine writeInline: not called CC=1 SCAR=6.9
Pod documented
1792 0 my ($class,$file,$data) = splice @_,0,3;
1793 # @_: @opt
1794  
1795 0 $data = Quiq::Unindent->hereDoc($data);
1796 0 $class->write($file,$data,@_);
1797
1798 0 return;
1799 }
1800  
1801 # -----------------------------------------------------------------------------
1802  
1803 =head2 Verzeichnis-Operationen
1804  
1805 =head3 count() - Anzahl der Verzeichniseinträge
1806  
1807 =head4 Synopsis
1808  
1809 $n = $this->count($dir);
1810  
1811 =head4 Arguments
1812  
1813 =over 4
1814  
1815 =item $dir
1816  
1817 Pfad des Verzeichnisses.
1818  
1819 =back
1820  
1821 =head4 Returns
1822  
1823 Anzahl Verzeichniseinträge (Integer)
1824  
1825 =head4 Description
1826  
1827 Ermittele die Anzahl Einträge des Verzeichnisses $dir und liefere diese
1828 zurück. Die Einträge C<.> und C<..> werden I<nicht> mitgezählt.
1829  
1830 =cut
1831  
1832 # -----------------------------------------------------------------------------
1833  
1834 0 ▶sub count {
Subroutine count: not called CC=4 SCAR=30.0
Pod documented
1835 0 my $this = shift;
1836 0 my $dir = $this->expandTilde(shift);
1837  
1838 0 my $n = 0;
1839 0 my $dh = Quiq::DirHandle->new($dir);
1840 0 ▶ while (my $entry = $dh->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $dh->next) 0 0 0
1841 0 ▶ if ($entry eq '.' || $entry eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry eq "." or $entry eq "..") 0 0 0
Condition0%0/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table0%0/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
1842 0 next;
1843 }
1844 0 $n++;
1845 }
1846 0 $dh->close;
1847  
1848 0 return $n;
1849 }
1850  
1851 # -----------------------------------------------------------------------------
1852  
1853 =head3 deleteContent() - Lösche Inhalt des Verzeichnisses
1854  
1855 =head4 Synopsis
1856  
1857 $this->deleteContent($dir);
1858  
1859 =head4 Arguments
1860  
1861 =over 4
1862  
1863 =item $dir
1864  
1865 Pfad des Verzeichnisses
1866  
1867 =back
1868  
1869 =head4 Description
1870  
1871 Lösche den Inhalt des Verzeichnisses $dir.
1872  
1873 =cut
1874  
1875 # -----------------------------------------------------------------------------
1876  
1877 0 ▶sub deleteContent {
Subroutine deleteContent: not called CC=2 SCAR=17.9
Pod documented
1878 0 my ($this,$dir) = @_;
1879  
1880 0 for my $entry ($this->entries($dir)) {
1881 0 $this->delete("$dir/$entry");
1882 }
1883  
1884 0 return;
1885 }
1886  
1887 # -----------------------------------------------------------------------------
1888  
1889 =head3 entries() - Liste der Verzeichniseinträge
1890  
1891 =head4 Synopsis
1892  
1893 @paths | $pathA = $this->entries($dir,@opt);
1894  
1895 =head4 Arguments
1896  
1897 =over 4
1898  
1899 =item $dir
1900  
1901 Pfad des Verzeichnisses.
1902  
1903 =back
1904  
1905 =head4 Options
1906  
1907 =over 4
1908  
1909 =item -encoding => $charset (Default: 'utf-8')
1910  
1911 Dekodiere die Verzeichniseinträge gemäß Zeichensatz $charset.
1912  
1913 =back
1914  
1915 =head4 Returns
1916  
1917 Liste der Verzeichniseinträge (Array of Strings). Im Skalarkontext
1918 eine Referenz auf die Liste.
1919  
1920 =head4 Description
1921  
1922 Ermittele die Einträge des Verzeichnisses $dir und liefere diese
1923 als Liste zurück. Die Liste umfasst alle Verzeichniseinträge
1924 außer "." und "..".
1925  
1926 =cut
1927  
1928 # -----------------------------------------------------------------------------
1929  
1930 0 ▶sub entries {
Subroutine entries: not called CC=6 SCAR=37.4
Pod documented
1931 0 my $this = shift;
1932 # @_: $dir,@opt
1933  
1934 # Options
1935  
1936 0 my $encoding = 'utf-8';
1937  
1938 0 my $argA = $this->parameters(1,1,\@_,
1939 -encoding => \$encoding,
1940 );
1941 0 my $dir = $this->expandTilde(shift @$argA);
1942  
1943 # Operation ausführen
1944  
1945 0 my @arr;
1946 0 my $dh = Quiq::DirHandle->new($dir);
1947 0 ▶ while (my $entry = $dh->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $dh->next) 0 0 0
1948 0 ▶ if ($entry eq '.' || $entry eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry eq "." or $entry eq "..") 0 0 0
Condition0%0/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table0%0/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
1949 0 next;
1950 }
1951 0 ▶ push @arr,$encoding? Encode::decode($encoding,$entry): $entry;
Branch: 1 branch
BranchTrueFalseTotal
$encoding ? : 0 0 0
1952 }
1953 0 $dh->close;
1954  
1955 0 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1956 }
1957  
1958 # -----------------------------------------------------------------------------
1959  
1960 =head3 find() - Liefere Pfade innerhalb eines Verzeichnisses
1961  
1962 =head4 Synopsis
1963  
1964 @paths|$pathA = $class->find($path,@opt);
1965  
1966 =head4 Options
1967  
1968 =over 4
1969  
1970 =item -decode => $encoding
1971  
1972 Dekodiere die Dateinamen gemäß dem angegebenen Encoding.
1973  
1974 =item -exclude => $regex (Default: keiner)
1975  
1976 Schließe alle Pfade aus, die Muster $regex erfüllen. Directories
1977 werden gepruned, d.h. sie werden nicht durchsucht. Matcht ein Pfad
1978 die Pattern sowohl von -pattern als auch -exclude, hat der
1979 exclude-Pattern Vorrang, d.h. die Datei wird ausgeschlossen.
1980  
1981 =item -excludeRoot => $bool (Default: 0)
1982  
1983 Nimm das Wurzelverzeichnis $path nicht in die Pfadliste mit auf.
1984  
1985 =item -follow => $bool (Default: 1)
1986  
1987 Folge Symbolic Links.
1988  
1989 =item -followSkip => $val (Default: 2)
1990  
1991 Definiert, was geschehen soll, wenn einem Symbolischen Link
1992 ein zweites Mal gefolgt wird. Siehe Doku.
1993  
1994 =item -leavesOnly => $bool (Default: 0)
1995  
1996 Liefere nur Pfade, die kein Anfang eines anderen Pfads sind.
1997 Anwendungsfall: nur die Blatt-Verzeichnisse eines Verzeichnisbaums.
1998  
1999 =item -olderThan => $seconds (Default: 0)
2000  
2001 Liefere nur Dateien, die vor mindestens $seconds zuletzt geändert
2002 wurden. Diese Option ist z.B. nützlich, um veraltete temporäre Dateien
2003 zu finden.
2004  
2005 =item -outHandle => $fh (Default: \*STDOUT)
2006  
2007 Filehandle, auf die die Ausgabe im Falle von -verbose=>1 geschrieben
2008 werden.
2009  
2010 =item -pattern => $regex (Default: keiner)
2011  
2012 Schränke die Treffer auf jene Pfade ein, die Muster $regex
2013 erfüllen. Matcht ein Pfad die Pattern sowohl von -pattern als
2014 auch -exclude, hat der exclude-Pattern Vorrang, d.h. die Datei
2015 wird ausgeschlossen.
2016  
2017 =item -slash => $bool (Default: 0)
2018  
2019 Füge einen Slash (/) am Ende von Directory-Namen hinzu.
2020  
2021 =item -sloppy => $bool (Default: 0)
2022  
2023 Wirf keine Exception, wenn $path nicht existiert, sondern liefere
2024 undef bzw. eine leere Liste.
2025  
2026 =item -sort = $bool (Default: 0)
2027  
2028 Sortiere die Pfade alphanumerisch.
2029  
2030 =item -subPath => $bool (Default: 0)
2031  
2032 Liefere nur den Subpfad, entferne also $path am Anfang.
2033  
2034 =item -testSub => sub {} (Default: undef)
2035  
2036 Subroutine, die den Pfad als Argument erhält und einen boolschen
2037 Wert liefert, der angibt, ob der Pfad zur Ergebnismenge gehört
2038 oder nicht.
2039  
2040 =item -type => 'd' | 'f' | undef (Default: undef)
2041  
2042 Liefere nur Verzeichnisse ('d') oder nur, was kein Verzeichnis ist ('f'),
2043 oder liefere alles (undef).
2044  
2045 =item -verbose => $bool (Default: 0)
2046  
2047 Schreibe Meldungen auf Ausgabe-Handle (s. Option -outHandle).
2048  
2049 =back
2050  
2051 =head4 Description
2052  
2053 Finde alle Dateien und Verzeichnisse unterhalb von und einschließlich
2054 Verzeichnis $path und liefere die Liste der gefundenen Pfade
2055 zurück. Im Skalarkontext liefere eine Referenz auf die Liste.
2056  
2057 Ist $dir Null (Leerstring oder undef), wird das aktuelle Verzeichnis
2058 ('.') durchsucht.
2059  
2060 Die Reihenfolge der Dateien ist undefiniert.
2061  
2062 =cut
2063  
2064 # -----------------------------------------------------------------------------
2065  
2066 7 ▶sub find {
Subroutine find: called CC=16 SCAR=34.4
Pod documented
2067 7 my $class = shift;
2068 7 my $dir = $class->expandTilde(shift);
2069 # @_: @opt
2070  
2071 # Optionen
2072  
2073 7 my $decode = undef;
2074 7 my $exclude = undef;
2075 7 my $excludeRoot = 0;
2076 7 my $follow = 1;
2077 7 my $followSkip = 2;
2078 7 my $leavesOnly = 0;
2079 7 my $olderThan = 0;
2080 7 my $outHandle = \*STDOUT;
2081 7 my $pattern = undef;
2082 7 my $slash = 0;
2083 7 my $sloppy = 0;
2084 7 my $sort = 0;
2085 7 my $subPath = 0;
2086 7 my $testSub = undef;
2087 7 my $type = undef;
2088 7 my $verbose = 0;
2089  
2090 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 2 7
2091 5 Quiq::Option->extract(\@_,
2092 -decode => \$decode,
2093 -exclude => \$exclude,
2094 -excludeRoot => \$excludeRoot,
2095 -follow => \$follow,
2096 -followSkip => \$followSkip,
2097 -leavesOnly => \$leavesOnly,
2098 -olderThan => \$olderThan,
2099 -outHandle => \$outHandle,
2100 -pattern => \$pattern,
2101 -slash => \$slash,
2102 -sloppy => \$sloppy,
2103 -sort => \$sort,
2104 -subPath => \$subPath,
2105 -testSub => \$testSub,
2106 -type => \$type,
2107 -verbose => \$verbose,
2108 );
2109 }
2110  
2111 # Parameter-Tests
2112  
2113 7 ▶ if (!defined $dir || $dir eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $dir or $dir eq '') { } 0 7 7
elsif (not -e $dir) { } 2 5 7
elsif (not -d $dir) { } 0 5 5
Condition33%1/3
not defined $dir or $dir eq ""
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $dir or $dir eq ""
not defined $dir $dir eq ""
Truth table33%1/3
not(A) or B
A= not defined $dirB= $dir eq ""
ABresult
1X1
011
000
2114 0 $dir = '.';
2115 }
2116 elsif (!-e $dir) {
2117 2 ▶ if ($sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sloppy) 1 1 2
2118 1 ▶ return wantarray? (): undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
2119 }
2120 1 $class->throw('PATH-00011: Verzeichnis existiert nicht',
2121 Dir => $dir,
2122 );
2123 }
2124 elsif (!-d $dir) {
2125 0 $class->throw('PATH-00013: Pfad ist kein Verzeichnis',
2126 Path => $dir,
2127 );
2128 }
2129  
2130 # Liste der Pfade
2131 5 my @paths;
2132  
2133 # Zeitpunkt der Suche (für Zeitvergleich bei -olderThan)
2134 5 my $time = time;
2135  
2136 30 ▶ my $sub = sub {
Subroutine __ANON__: called CC=25 SCAR=40.1
2137 30 $File::Find::name =~ s|^\./||; # ./ am Anfang entfernen
2138  
2139 30 ▶ if ($excludeRoot && $File::Find::name eq $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excludeRoot and $File::Find::name eq $dir) 0 30 30
Condition33%1/3
$excludeRoot and $File::Find::name eq $dir
!ll&&!rl&&r
3000
MC/DC0%0/2
$excludeRoot and $File::Find::name eq $dir
$excludeRoot $File::Find::name eq $dir
Truth table33%1/3
A and B
A= $excludeRootB= $File::Find::name eq $dir
ABresult
0X0
100
111
2140 0 return;
2141 }
2142  
2143 30 ▶ if ($exclude && $File::Find::name =~ /$exclude/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude and $File::Find::name =~ /$exclude/) 2 28 30
Condition100%3/3
$exclude and $File::Find::name =~ /$exclude/
!ll&&!rl&&r
2442
MC/DC100%2/2
$exclude and $File::Find::name =~ /$exclude/
$exclude $File::Find::name =~ /$exclude/
Truth table100%3/3
A and B
A= $excludeB= $File::Find::name =~ /$exclude/
ABresult
0X0
100
111
2144 2 ▶ if (-d) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_) 0 2 2
2145 # warn "PRUNE: $File::Find::name\n";
2146 0 $File::Find::prune = 1;
2147 }
2148 2 return;
2149 }
2150  
2151 28 ▶ if ($pattern && $File::Find::name !~ /$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern and not $File::Find::name =~ /$pattern/) 0 28 28
Condition33%1/3
$pattern and not $File::Find::name =~ /$pattern/
!ll&&!rl&&r
2800
MC/DC0%0/2
$pattern and not $File::Find::name =~ /$pattern/
$pattern not $File::Find::name =~ /$pattern/
Truth table33%1/3
A and not(B)
A= $patternB= not $File::Find::name =~ /$pattern/
ABresult
0X0
100
111
2152 0 return;
2153 }
2154  
2155 28 ▶ if ($type || $slash || $olderThan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type or $slash or $olderThan) 16 12 28
Condition66%4/6
$type or $slash or $olderThan
l!l&&r!l&&!r
16012
$type or $slash
l!l&&r!l&&!r
16012
MC/DC33%1/3
$type or $slash or $olderThan
$type $slash $olderThan
Truth table50%2/4
A or B or C
A= $typeB= $slashC= $olderThan
ABCresult
1XX1
01X1
0011
0000
2156 # Test muss auf $_ erfolgen, da abgestiegen wird!
2157 16 my $isDir = -d;
2158  
2159 16 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 16 0 16
2160 16 ▶ if ($type eq 'd' && !$isDir || $type eq 'f' && $isDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "d" and not $isDir or $type eq "f" and $isDir) 8 8 16
Condition100%9/9
$type eq "d" and not $isDir or $type eq "f" and $isDir
l!l&&r!l&&!r
268
$type eq "d" and not $isDir
!ll&&!rl&&r
862
$type eq "f" and $isDir
!ll&&!rl&&r
626
MC/DC75%3/4
$type eq "d" and not $isDir or $type eq "f" and $isDir
$type eq "d" not $isDir $type eq "f" $isDir
Truth table57%4/7
A and not(B) or C and D
A= $type eq "d"B= not $isDirC= $type eq "f"D= $isDir
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
2161 8 return;
2162 }
2163 }
2164 8 ▶ if ($olderThan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($olderThan) 0 8 8
2165 # Datei ist jünger als $olderThan Sekunden
2166 0 ▶ return if (stat $File::Find::name)[9] > $time-$olderThan;
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::stat $File::Find::name)[9] > $time - $olderThan 0 0 0
2167 }
2168 8 ▶ if ($slash && $isDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slash and $isDir) 0 8 8
Condition33%1/3
$slash and $isDir
!ll&&!rl&&r
800
MC/DC0%0/2
$slash and $isDir
$slash $isDir
Truth table33%1/3
A and B
A= $slashB= $isDir
ABresult
0X0
100
111
2169 0 $File::Find::name .= '/';
2170 }
2171 }
2172  
2173 20 ▶ if ($testSub && !$testSub->($File::Find::name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($testSub and not &$testSub($File::Find::name)) 0 20 20
Condition33%1/3
$testSub and not &$testSub($File::Find::name)
!ll&&!rl&&r
2000
MC/DC0%0/2
$testSub and not &$testSub($File::Find::name)
$testSub not &$testSub($File::Find::name)
Truth table33%1/3
A and not(B)
A= $testSubB= not &$testSub($File::Find::name)
ABresult
0X0
100
111
2174 0 return;
2175 }
2176  
2177 20 ▶ if ($subPath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subPath) 0 20 20
2178 # Pfadanfang entfernen
2179 0 $File::Find::name =~ s|^\Q$dir/||;
2180 }
2181  
2182 # $File::Find::name =~ s|^\./||; # ./ am Anfang entfernen
2183  
2184 20 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 20 20
2185 0 print $outHandle $File::Find::name,"\n";
2186 }
2187  
2188 20 ▶ if ($decode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decode) { } 0 20 20
2189 0 push @paths,Encode::decode($decode,$File::Find::name);
2190 }
2191 else {
2192 20 push @paths,$File::Find::name;
2193 }
2194 5 };
2195 5 File::Find::find({
2196 wanted=>$sub,
2197 follow=>$follow,
2198 follow_skip=>$followSkip,
2199 },$dir);
2200  
2201 5 ▶ if ($leavesOnly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leavesOnly) 0 5 5
2202 0 my @arr;
2203 0 ▶ for (my $i = 0; $i < @paths; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @paths) 0 0 0
2204 0 my $ok = 1;
2205 0 ▶ for (my $j = 0; $j < @paths; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @paths) 0 0 0
2206 0 ▶ if ($j != $i && index($paths[$j],$paths[$i]) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j != $i and index($paths[$j], $paths[$i]) == 0) 0 0 0
Condition0%0/3
$j != $i and index($paths[$j], $paths[$i]) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$j != $i and index($paths[$j], $paths[$i]) == 0
$j != $i index($paths[$j], $paths[$i]) == 0
Truth table0%0/3
A and B
A= $j != $iB= index($paths[$j], $paths[$i]) == 0
ABresult
0X0
100
111
2207 0 $ok = 0;
2208 0 last;
2209 }
2210 }
2211 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
2212 0 push @arr,$paths[$i];
2213 }
2214 }
2215 0 @paths = @arr;
2216 }
2217  
2218 5 ▶ if ($sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) 0 5 5
2219 0 @paths = sort @paths;
2220 }
2221  
2222 5 ▶ return wantarray? @paths: \@paths;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
2223 }
2224  
2225 # -----------------------------------------------------------------------------
2226  
2227 =head3 findProgram() - Ermittele Pfad zu Programm
2228  
2229 =head4 Synopsis
2230  
2231 $path = $class->findProgram($program);
2232 $path = $class->findProgram($program,$sloppy);
2233  
2234 =head4 Arguments
2235  
2236 =over 4
2237  
2238 =item $program
2239  
2240 Name des Programms.
2241  
2242 =item $sloppy
2243  
2244 Wenn wahr, wird keine Exception geworfen, wenn das Programm nicht
2245 gefunden wird, sondern undef zurück geliefert.
2246  
2247 =back
2248  
2249 =head4 Returns
2250  
2251 Programmpfad (String)
2252  
2253 =head4 Description
2254  
2255 Suche Programm $program über den Suchpfad der Shell und liefere
2256 den vollständigen Pfad zurück. Wird das Programm nicht gefunden,
2257 wird eine Exception geworfen.
2258  
2259 =cut
2260  
2261 # -----------------------------------------------------------------------------
2262  
2263 4 ▶sub findProgram {
Subroutine findProgram: called CC=3 SCAR=11.0
Pod documented
2264 4 my ($class,$program,$sloppy) = @_;
2265  
2266 # FIXME: PATH selbst absuchen
2267  
2268 4 my $cmd = "which $program 2>/dev/null";
2269 4 my $path = qx/$cmd/;
2270 4 chomp $path;
2271 4 ▶ if (!$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sloppy) 1 3 4
2272 # Quiq::Shell->checkError($?,$!,$cmd);
2273 1 Quiq::Exit->check($?,$cmd);
2274 }
2275  
2276 3 ▶ return $path eq ''? undef: $path;
Branch: 1 branch
BranchTrueFalseTotal
$path eq '' ? : 2 1 3
2277 }
2278  
2279 # -----------------------------------------------------------------------------
2280  
2281 =head3 maxFilename() - Liefere den lexikalisch größten Dateinamen
2282  
2283 =head4 Synopsis
2284  
2285 $max = $class->maxFilename($dir);
2286  
2287 =head4 Description
2288  
2289 Liefere den lexikalisch größten Dateinamen aus Verzeichnis $dir.
2290  
2291 =cut
2292  
2293 # -----------------------------------------------------------------------------
2294  
2295 0 ▶sub maxFilename {
Subroutine maxFilename: not called CC=6 SCAR=37.4
Pod documented
2296 0 my ($class,$dir) = @_;
2297  
2298 0 my $max;
2299 0 my $dh = Quiq::DirHandle->new($dir);
2300 0 ▶ while (my $file = $dh->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $dh->next) 0 0 0
2301 0 ▶ if ($file eq '.' || $file eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "." or $file eq "..") 0 0 0
Condition0%0/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table0%0/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
2302 0 next;
2303 }
2304 0 ▶ if (!defined($max) || $file gt $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $max or $file gt $max) 0 0 0
Condition0%0/3
not defined $max or $file gt $max
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $max or $file gt $max
not defined $max $file gt $max
Truth table0%0/3
not(A) or B
A= not defined $maxB= $file gt $max
ABresult
1X1
011
000
2305 0 $max = $file;
2306 }
2307 }
2308 0 $dh->close;
2309  
2310 0 return $max;
2311 }
2312  
2313 # -----------------------------------------------------------------------------
2314  
2315 =head3 maxFileNumber() - Liefere den numerisch größten Dateinamen
2316  
2317 =head4 Synopsis
2318  
2319 $max = $class->maxFileNumber($dir,@opt);
2320  
2321 =head4 Options
2322  
2323 =over 4
2324  
2325 =item -sloppy => $bool (Default: 0)
2326  
2327 Wirf keine Exception, wenn ein Dateiname nicht mit einer Nummer
2328 beginnt.
2329  
2330 =back
2331  
2332 =head4 Description
2333  
2334 Liefere den numerisch größten Dateinamen aus Verzeichnis $dir.
2335 Die Methode ist nützlich, wenn die Dateinamen mit einer Zahl
2336 NNNNNN beginnen und man die Datei mit der größten Zahl ermitteln
2337 möchte um einer neu erzeugten Datei die nächsthöhere Nummer
2338 zuzuweisen.
2339  
2340 =cut
2341  
2342 # -----------------------------------------------------------------------------
2343  
2344 0 ▶sub maxFileNumber {
Subroutine maxFileNumber: not called CC=7 SCAR=40.3
Pod documented
2345 0 my ($class,$dir) = splice @_,0,2;
2346 # @_: @opt
2347  
2348 # Options
2349  
2350 0 my $sloppy = 0;
2351
2352 0 Quiq::Option->extract(\@_,
2353 -sloppy => \$sloppy,
2354 );
2355  
2356 # Verarbeitung
2357
2358 0 my $max = 0;
2359 0 my $dh = Quiq::DirHandle->new($dir);
2360 0 ▶ while (my $file = $dh->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $dh->next) 0 0 0
2361 0 ▶ if ($file eq '.' || $file eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "." or $file eq "..") 0 0 0
Condition0%0/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table0%0/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
2362 0 next;
2363 }
2364 0 my ($n) = $file =~ /^(\d+)/;
2365 0 ▶ if (!defined($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 0 0
2366 0 ▶ if ($sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sloppy) 0 0 0
2367 0 next;
2368 }
2369 $class->throw(
2370 0 'PATH-00099: Dateiname beginnt nicht mit Ziffernfolge',
2371 File => $file,
2372 );
2373 }
2374 0 ▶ if ($n+0 > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n + 0 > $max) 0 0 0
2375 0 $max = $n+0;
2376 }
2377 }
2378 0 $dh->close;
2379  
2380 0 return $max;
2381 }
2382  
2383 # -----------------------------------------------------------------------------
2384  
2385 =head3 minFileNumber() - Liefere den numerisch größten Dateinamen
2386  
2387 =head4 Synopsis
2388  
2389 $min = $class->minFileNumber($dir,$max,@opt);
2390  
2391 =head4 Arguments
2392  
2393 =over 4
2394  
2395 =item $dir
2396  
2397 Pfad des Verzeichnisses
2398  
2399 =item $max
2400  
2401 Der Wert, mit dem die Zählung beginnt.
2402  
2403 =back
2404  
2405 =head4 Options
2406  
2407 =over 4
2408  
2409 =item -sloppy => $bool (Default: 0)
2410  
2411 Wirf keine Exception, wenn ein Dateiname nicht mit einer Nummer
2412 beginnt.
2413  
2414 =back
2415  
2416 =head4 Description
2417  
2418 Liefere den numerisch kleinsten Dateinamen aus Verzeichnis $dir.
2419 Die Methode ist nützlich, wenn die Dateinamen mit einer Zahl
2420 NNNNNN beginnen und man die Datei mit der kleinsten Zahl ermitteln
2421 möchte um einer neu erzeugten Datei die nächstniedrigere Nummer
2422 zuzuweisen.
2423  
2424 =cut
2425  
2426 # -----------------------------------------------------------------------------
2427  
2428 0 ▶sub minFileNumber {
Subroutine minFileNumber: not called CC=7 SCAR=40.3
Pod documented
2429 0 my ($class,$dir,$max) = splice @_,0,3;
2430 # @_: @opt
2431  
2432 # Options
2433  
2434 0 my $sloppy = 0;
2435
2436 0 Quiq::Option->extract(\@_,
2437 -sloppy => \$sloppy,
2438 );
2439  
2440 # Verarbeitung
2441  
2442 0 my $min = $max;
2443 0 my $dh = Quiq::DirHandle->new($dir);
2444 0 ▶ while (my $file = $dh->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $dh->next) 0 0 0
2445 0 ▶ if ($file eq '.' || $file eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "." or $file eq "..") 0 0 0
Condition0%0/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table0%0/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
2446 0 next;
2447 }
2448 0 my ($n) = $file =~ /^(\d+)/;
2449 0 ▶ if (!defined($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 0 0
2450 0 ▶ if ($sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sloppy) 0 0 0
2451 0 next;
2452 }
2453 $class->throw(
2454 0 'PATH-00099: Dateiname beginnt nicht mit Ziffernfolge',
2455 File => $file,
2456 );
2457 }
2458 0 ▶ if ($n+0 < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n + 0 < $min) 0 0 0
2459 0 $min = $n+0;
2460 }
2461 }
2462 0 $dh->close;
2463  
2464 0 return $min;
2465 }
2466  
2467 # -----------------------------------------------------------------------------
2468  
2469 =head3 mkdir() - Erzeuge Verzeichnis
2470  
2471 =head4 Synopsis
2472  
2473 $dir = $class->mkdir($dir,@opt);
2474  
2475 =head4 Options
2476  
2477 =over 4
2478  
2479 =item -createParent => $bool (Default: 0)
2480  
2481 Erzeuge nicht den angegebenen Pfad, sondern den Parent-Pfad.
2482 Dies ist nützlich, wenn der übergebene Pfad ein Dateiname ist,
2483 dessen Verzeichnis bei Nicht-Existenz erzeugt werden soll.
2484 Impliziert -recursive=>1, wenn nicht explizit -recursive=>0
2485 gesetzt ist.
2486  
2487 =item -forceMode => $mode (Default: keiner)
2488  
2489 Setze Verzeichnisrechte auf $mode ohne Berücksichtigung
2490 der umask des Prozesses.
2491  
2492 =item -mode => $mode (Default: 0775)
2493  
2494 Setze Verzeichnisrechte auf $mode mit Berücksichtigung
2495 der umask des Prozesses.
2496  
2497 =item -mustNotExist => $bool (Default: 0)
2498  
2499 Das Verzeichnis darf nicht existieren. Wenn es existiert, wird
2500 eine Exception geworfen.
2501  
2502 =item -recursive => 0 | 1 (Default: 0)
2503  
2504 Erzeuge übergeordnete Verzeichnisse, wenn nötig.
2505  
2506 =back
2507  
2508 =head4 Returns
2509  
2510 (String) Pfad des erzeugten Verzeichnisses.
2511  
2512 =head4 Description
2513  
2514 Erzeuge Verzeichnis $dir. Existiert das Verzeichnis bereits, hat
2515 der Aufruf keinen Effekt. Kann das Verzeichnis nicht angelegt
2516 werden, wird eine Exception ausgelöst.
2517  
2518 =cut
2519  
2520 # -----------------------------------------------------------------------------
2521  
2522 45 ▶sub mkdir {
Subroutine mkdir: called CC=12 SCAR=26.7
Pod documented
2523 45 my $class = shift;
2524 45 my $dir = $class->expandTilde(shift);
2525 # @_: @opt
2526  
2527 45 my $createParent = 0;
2528 45 my $forceMode = undef;
2529 45 my $mode = 0755;
2530 45 my $mustNotExist = 0;
2531 45 my $recursive = undef;
2532  
2533 45 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 38 7 45
2534 38 Quiq::Option->extract(-dontExtract=>1,\@_,
2535 -createParent => \$createParent,
2536 -forceMode => \$forceMode,
2537 -mode => \$mode,
2538 -mustNotExist => \$mustNotExist,
2539 -recursive => \$recursive,
2540 );
2541 }
2542  
2543 45 ▶ if ($createParent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($createParent) 0 45 45
2544 0 ($dir) = $class->split($dir);
2545 0 ▶ if (!defined $recursive) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $recursive) 0 0 0
2546 0 $recursive = 1;
2547 }
2548 }
2549  
2550 45 ▶ return $dir if !defined($dir) || $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $dir or $dir eq "" 6 39 45
Condition66%2/3
not defined $dir or $dir eq ""
l!l&&r!l&&!r
0639
MC/DC50%1/2
not defined $dir or $dir eq ""
not defined $dir $dir eq ""
Truth table66%2/3
not(A) or B
A= not defined $dirB= $dir eq ""
ABresult
1X1
011
000
2551  
2552 39 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 9 30 39
2553 9 ▶ if ($mustNotExist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mustNotExist) 0 9 9
2554 0 $class->throw(
2555 'PATH-00005: Verzeichnis existiert bereits',
2556 Dir => $dir,
2557 );
2558 }
2559 9 return $dir;
2560 }
2561  
2562 30 ▶ if ($recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive) 23 7 30
2563 23 my ($parentDir) = $class->split($dir);
2564 23 $class->mkdir($parentDir,
2565 @_,
2566 -createParent => 0,
2567 -mustNotExist => 0,
2568 -recursive => 1,
2569 );
2570 }
2571  
2572 30 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 0 30 30
2573 # Hack, damit rekursiv erzeugte Pfade wie /tmp/a/b/c/..
2574 # angelegt werden können. Ohne diesen zusätzlichen
2575 # Existenz-Test schlägt sonst das folgende mkdir fehl.
2576 0 return $dir;
2577 }
2578  
2579 30 ▶ CORE::mkdir($dir,$mode) || do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::mkdir $dir, $mode or do { $class->throw("PATH-00004: Kann Verzeichnis nicht erzeugen", "Path", $dir) } 29 1 30
2580 1 $class->throw(
2581 'PATH-00004: Kann Verzeichnis nicht erzeugen',
2582 Path => $dir,
2583 );
2584 };
2585  
2586 29 ▶ if ($forceMode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forceMode) 0 29 29
2587 0 $class->chmod($dir,$forceMode);
2588 }
2589  
2590 29 return $dir;
2591 }
2592  
2593 # -----------------------------------------------------------------------------
2594  
2595 =head3 mtimeDir() - Jüngste Modifikationszeit
2596  
2597 =head4 Synopsis
2598  
2599 $mtime = $this->mtimeDir($dir);
2600  
2601 =head4 Arguments
2602  
2603 =over 4
2604  
2605 =item $dir
2606  
2607 Pfad des Verzeichnisses.
2608  
2609 =back
2610  
2611 =head4 Returns
2612  
2613 Zeit in Unix Epoch (Integer)
2614  
2615 =head4 Description
2616  
2617 Ermittele über allen Pfaden in und einschließlch $dir den jüngsten
2618 Modifikationszeitpunkt (mtime) und liefere diesen zurück.
2619  
2620 =cut
2621  
2622 # -----------------------------------------------------------------------------
2623  
2624 0 ▶sub mtimeDir {
Subroutine mtimeDir: not called CC=3 SCAR=24.8
Pod documented
2625 0 my ($this,$dir) = @_;
2626  
2627 0 my $mtime = 0;
2628 0 for my $path ($this->find($dir)) {
2629 0 my $time = $this->mtime($path);
2630 0 ▶ if ($time > $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time > $mtime) 0 0 0
2631 0 $mtime = $time;
2632 }
2633 }
2634  
2635 0 return $mtime;
2636 }
2637  
2638 # -----------------------------------------------------------------------------
2639  
2640 =head3 rmdir() - Lösche Verzeichnis
2641  
2642 =head4 Synopsis
2643  
2644 $class->rmdir($dir);
2645  
2646 =head4 Arguments
2647  
2648 =over 4
2649  
2650 =item $dir
2651  
2652 Pfad des Verzeichnisses
2653  
2654 =back
2655  
2656 =head4 Returns
2657  
2658 nichts
2659  
2660 =head4 Description
2661  
2662 Lösche Verzeichnis $dir, falls dieses leer ist. Kann das
2663 Verzeichnis nicht gelöscht werden, wird eine Exception ausgelöst.
2664  
2665 =cut
2666  
2667 # -----------------------------------------------------------------------------
2668  
2669 0 ▶sub rmdir {
Subroutine rmdir: not called CC=2 SCAR=17.9
Pod documented
2670 0 my $class = shift;
2671 0 my $dir = shift;
2672  
2673 0 ▶ CORE::rmdir($dir) || do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::rmdir $dir or do { $class->throw("PATH-00005: Verzeichnis kann nicht gel\x{f6}scht werden", "Path", $dir) } 0 0 0
2674 0 $class->throw(
2675 'PATH-00005: Verzeichnis kann nicht gelöscht werden',
2676 Path => $dir,
2677 );
2678 };
2679  
2680 0 return;
2681 }
2682  
2683 # -----------------------------------------------------------------------------
2684  
2685 =head3 tempDir() - Erzeuge temporäres Verzeichnis
2686  
2687 =head4 Synopsis
2688  
2689 $dir = $this->tempDir(@opt);
2690  
2691 =head4 Options
2692  
2693 I<< siehe Quiq::TempDir >>
2694  
2695 =head4 Returns
2696  
2697 =over 4
2698  
2699 =item $dir
2700  
2701 Tempdir-Objekt
2702  
2703 =back
2704  
2705 =head4 Description
2706  
2707 Erzeuge ein temporäres Verzeichnis und liefere eine Referenz auf
2708 das Objekt zurück.
2709  
2710 =head4 See Also
2711  
2712 Quiq::TempDir
2713  
2714 =cut
2715  
2716 # -----------------------------------------------------------------------------
2717  
2718 1 ▶sub tempDir {
Subroutine tempDir: called CC=1 SCAR=0.0
Pod documented
2719 1 my $this = shift;
2720 1 return Quiq::TempDir->new(@_);
2721 }
2722  
2723 # -----------------------------------------------------------------------------
2724  
2725 =head2 Pfad-Operationen
2726  
2727 =head3 absolute() - Expandiere Pfad zu absolutem Pfad
2728  
2729 =head4 Synopsis
2730  
2731 $absolutePath = $this->absolute($path);
2732  
2733 =head4 Alias
2734  
2735 realPath()
2736  
2737 =head4 Description
2738  
2739 Ist $path ein relativer Pfad, expandiere ihn zu einem absolutem Pfad
2740 und liefere diesen zurück. Ist $path bereits absolut, liefere ihn
2741 unverändert.
2742  
2743 =cut
2744  
2745 # -----------------------------------------------------------------------------
2746  
2747 4 ▶sub absolute {
Subroutine absolute: called CC=2 SCAR=6.9
Pod documented
2748 4 my $this = shift;
2749 4 ▶ my $path = shift // '';
Condition100%2/2
shift() // ""
l!l
31
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
2750  
2751 4 $path = $this->expandTilde($path);
2752 4 return Cwd::realpath($path);
2753 }
2754  
2755 {
2756 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
2757 270 *realPath = \&absolute;
2758 }
2759  
2760 # -----------------------------------------------------------------------------
2761  
2762 =head3 age() - Alter der letzten Modifikation
2763  
2764 =head4 Synopsis
2765  
2766 $duration = $this->age($path);
2767  
2768 =head4 Arguments
2769  
2770 =over 4
2771  
2772 =item $path
2773  
2774 Pfad.
2775  
2776 =back
2777  
2778 =head4 Returns
2779  
2780 Anzahl Sekunden (Integer)
2781  
2782 =head4 Description
2783  
2784 Ermittele das Alter der letzten Modifiktion des Pfad-Objekts in
2785 Sekunden und liefere diesen Wert zurück. Existiert der Pfad nicht,
2786 wird eine Exception geworfen.
2787  
2788 =cut
2789  
2790 # -----------------------------------------------------------------------------
2791  
2792 1 ▶sub age {
Subroutine age: called CC=2 SCAR=7.4
Pod documented
2793 1 my $this = shift;
2794 1 my $path = $this->expandTilde(shift);
2795  
2796 1 ▶ if (!-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 0 1 1
2797 0 $this->throw(
2798 'PATH-00099: Path does not exist',
2799 Path => $path,
2800 );
2801 }
2802  
2803 1 return time-$this->mtime($path);
2804 }
2805  
2806 # -----------------------------------------------------------------------------
2807  
2808 =head3 basename() - Grundname eines Pfads
2809  
2810 =head4 Synopsis
2811  
2812 $basename = $class->basename($path,@opt);
2813  
2814 =head4 Alias
2815  
2816 baseName()
2817  
2818 =head4 Options
2819  
2820 =over 4
2821  
2822 =item -all => $bool (Default: 0)
2823  
2824 Entferne alle, nicht nur die erste Extension.
2825  
2826 =back
2827  
2828 =head4 Description
2829  
2830 Liefere den Grundnamen des Pfads, d.h. ohne Pfadanfang und Extension.
2831  
2832 =cut
2833  
2834 # -----------------------------------------------------------------------------
2835  
2836 3 ▶sub basename {
Subroutine basename: called CC=3 SCAR=11.8
Pod documented
2837 3 my $class = shift;
2838 3 my $path = shift;
2839  
2840 # Optionen
2841  
2842 3 my $all = 0;
2843  
2844 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
2845 0 Quiq::Option->extract(\@_,
2846 -all => \$all,
2847 );
2848 }
2849  
2850 3 my $basename = ($class->split($path))[2];
2851 3 ▶ if ($all) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all) 0 3 3
2852 0 $basename =~ s/\..*//;
2853 }
2854  
2855 3 return $basename;
2856 }
2857  
2858 {
2859 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
2860 270 *baseName = \&basename;
2861 }
2862  
2863 # -----------------------------------------------------------------------------
2864  
2865 =head3 basePath() - Pfad ohne Extension
2866  
2867 =head4 Synopsis
2868  
2869 $basePath = $class->basePath($path);
2870  
2871 =head4 Description
2872  
2873 Entferne eine etwaig vorhandene Extension von $path und liefere das
2874 Resultat zurück.
2875  
2876 =cut
2877  
2878 # -----------------------------------------------------------------------------
2879  
2880 4 ▶sub basePath {
Subroutine basePath: called CC=1 SCAR=0.0
Pod documented
2881 4 my ($class,$path) = @_;
2882 4 $path =~ s/\.([^.]+)$//;
2883 4 return $path;
2884 }
2885  
2886 # -----------------------------------------------------------------------------
2887  
2888 =head3 chmod() - Setze Zugriffsrechte
2889  
2890 =head4 Synopsis
2891  
2892 $this->chmod($path,$mode);
2893  
2894 =head4 Description
2895  
2896 Setze Zugriffsrechte $mode auf Pfad $path.
2897  
2898 =cut
2899  
2900 # -----------------------------------------------------------------------------
2901  
2902 6 ▶sub chmod {
Subroutine chmod: called CC=2 SCAR=6.9
Pod documented
2903 6 my $this = shift;
2904 6 my $path = $this->expandTilde(shift);
2905 6 my $mode = shift;
2906  
2907 6 ▶ CORE::chmod $mode,$path or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::chmod $mode, $path or do { $this->throw("PATH-00003: Setzen von Zugriffsrechten fehlgeschlagen", "Path", $path, "Mode", $mode) } 5 1 6
2908 1 $this->throw(
2909 'PATH-00003: Setzen von Zugriffsrechten fehlgeschlagen',
2910 Path => $path,
2911 Mode => $mode,
2912 );
2913 };
2914  
2915 5 return;
2916 }
2917  
2918 # -----------------------------------------------------------------------------
2919  
2920 =head3 delete() - Lösche Pfad (rekursiv)
2921  
2922 =head4 Synopsis
2923  
2924 $this->delete($path);
2925  
2926 =head4 Options
2927  
2928 =over 4
2929  
2930 =item -sloppy => $bool (Default: 0)
2931  
2932 Wirf keine Exception, wenn das Verzeichnis oder die Datei nicht
2933 gelöscht werden kann.
2934  
2935 =back
2936  
2937 =head4 Description
2938  
2939 Lösche den Pfad aus dem Dateisystem, also die Datei oder das Verzeichnis
2940 einschließlich Inhalt. Es ist kein Fehler, wenn der Pfad nicht existiert.
2941  
2942 =cut
2943  
2944 # -----------------------------------------------------------------------------
2945  
2946 63 ▶sub delete {
Subroutine delete: called CC=10 SCAR=25.5
Pod documented
2947 63 my $this = shift;
2948 63 my $path = $this->expandTilde(shift);
2949  
2950 # Options
2951  
2952 63 my $sloppy = 0;
2953
2954 63 Quiq::Option->extract(\@_,
2955 -sloppy => \$sloppy,
2956 );
2957  
2958 # Operation ausführen
2959  
2960 63 ▶ if (!defined($path) || $path eq '' || !-e $path && !-l $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $path or $path eq '' or not -e $path || -l $path) { } 8 55 63
elsif (-d $path) { } 18 37 55
Condition66%6/9
not defined $path or $path eq "" or not -e $path || -l $path
l!l&&r!l&&!r
0855
not defined $path or $path eq ""
l!l&&r!l&&!r
0063
-e $path || -l $path
l!l&&r!l&&!r
5328
MC/DC50%2/4
not defined $path or $path eq "" or not -e $path || -l $path
not defined $path $path eq "" -e $path -l $path
Truth table60%3/5
not(A) or B or not(C || D)
A= not defined $pathB= $path eq ""C= -e $pathD= -l $path
ABCDresult
1XXX1
01XX1
00001
001X0
00010
2961 # Bei Nichtexistenz nichts tun, aber nur, wenn es
2962 # kein Symlink ist. Bei Symlinks schlägt -e fehl, wenn
2963 # das Ziel nicht existiert!
2964 }
2965 elsif (-d $path) {
2966 # Verzeichnis löschen
2967 18 (my $dir = $path) =~ s/'/\\'/g; # ' quoten
2968 # eval {Quiq::Shell->exec("/bin/rm -r --interactive=never".
2969 # " '$dir' >/dev/null 2>&1")};
2970 18 eval {Quiq::Shell->exec("/bin/rm -r '$dir' >/dev/null 2>&1")};
2971 18 ▶ if ($@ && !$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $sloppy) 0 18 18
Condition33%1/3
$@ and not $sloppy
!ll&&!rl&&r
1800
MC/DC0%0/2
$@ and not $sloppy
$@ not $sloppy
Truth table33%1/3
A and not(B)
A= $@B= not $sloppy
ABresult
0X0
100
111
2972 0 $this->throw(
2973 'PATH-00002: Can\'t delete directory',
2974 Error => $@,
2975 Path => $path,
2976 );
2977 }
2978 }
2979 else {
2980 # Datei löschen
2981 37 ▶ if (!CORE::unlink($path) && !$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $path or $sloppy) 0 37 37
Condition33%1/3
unlink $path or $sloppy
l!l&&r!l&&!r
3700
MC/DC0%0/2
unlink $path or $sloppy
unlink $path $sloppy
Truth table33%1/3
A or B
A= unlink $pathB= $sloppy
ABresult
1X1
011
000
2982 0 $this->throw(
2983 'PATH-00003: Can\'t delete file',
2984 Path => $path,
2985 );
2986 }
2987 }
2988  
2989 63 return;
2990 }
2991  
2992 # -----------------------------------------------------------------------------
2993  
2994 =head3 dir() - Pfad ohne letzten Bestandteil
2995  
2996 =head4 Synopsis
2997  
2998 $dir = $class->dir($path);
2999  
3000 =head4 Alias
3001  
3002 parent()
3003  
3004 =head4 Returns
3005  
3006 String
3007  
3008 =head4 Description
3009  
3010 Entferne den letzten Pfadbestandteil von $path und liefere
3011 den Rest zurück. Enthält $path keinen Slash (/), wird ein Leerstring
3012 geliefert.
3013  
3014 =cut
3015  
3016 # -----------------------------------------------------------------------------
3017  
3018 2 ▶sub dir {
Subroutine dir: called CC=2 SCAR=6.9
Pod documented
3019 2 my ($class,$path) = @_;
3020 2 ▶ return $path =~ m|(.*)/|? $1: '';
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[(.*)/] ? : 1 1 2
3021 }
3022  
3023 {
3024 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
3025 270 *parent = \&dir;
3026 }
3027  
3028 # -----------------------------------------------------------------------------
3029  
3030 =head3 exists() - Prüfe Existenz
3031  
3032 =head4 Synopsis
3033  
3034 $bool = $this->exists($path);
3035  
3036 =head4 Description
3037  
3038 Prüfe, ob Pfad $path existiert und liefere den entsprechenden
3039 Wahrheitswert zurück. Die Methode expandiert ~ am Pfadanfang.
3040  
3041 =cut
3042  
3043 # -----------------------------------------------------------------------------
3044  
3045 18 ▶sub exists {
Subroutine exists: called CC=2 SCAR=6.9
Pod documented
3046 18 my ($this,$path) = @_;
3047 18 ▶ return -e $this->expandTilde($path)? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
-e $this->expandTilde($path) ? : 7 11 18
3048 }
3049  
3050 # -----------------------------------------------------------------------------
3051  
3052 =head3 expandTilde() - Expandiere Tilde
3053  
3054 =head4 Synopsis
3055  
3056 $path = $class->expandTilde($path);
3057  
3058 =head4 Returns
3059  
3060 Pfad (String)
3061  
3062 =head4 Description
3063  
3064 Ersetze eine Tilde am Pfadanfang durch das Home-Verzeichnis des
3065 Benutzers und liefere den resultierenden Pfad zurück.
3066  
3067 =cut
3068  
3069 # -----------------------------------------------------------------------------
3070  
3071 389 ▶sub expandTilde {
Subroutine expandTilde: called CC=4 SCAR=14.0
Pod documented
3072 389 my ($class,$path) = @_;
3073  
3074 # Unter einem Daemon ist $HOME typischerweise nicht gesetzt, daher
3075 # prüfen wir zunächst, ob wir $HOME überhaupt expandieren müssen
3076  
3077 389 ▶ if ($path && substr($path,0,1) eq '~') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and substr($path, 0, 1) eq "~") 7 382 389
Condition100%3/3
$path and substr($path, 0, 1) eq "~"
!ll&&!rl&&r
73757
MC/DC100%2/2
$path and substr($path, 0, 1) eq "~"
$path substr($path, 0, 1) eq "~"
Truth table100%3/3
A and B
A= $pathB= substr($path, 0, 1) eq "~"
ABresult
0X0
100
111
3078 7 ▶ if (!exists $ENV{'HOME'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{"HOME"}) 0 7 7
3079 0 $class->throw(
3080 'PATH-00016: Envvar HOME does not exist',
3081 );
3082 }
3083 7 substr($path,0,1) = $ENV{'HOME'};
3084 }
3085
3086 389 return $path;
3087 }
3088  
3089 # -----------------------------------------------------------------------------
3090  
3091 =head3 extension() - Extension des Pfads
3092  
3093 =head4 Synopsis
3094  
3095 $ext = $class->extension($path);
3096  
3097 =head4 Description
3098  
3099 Ermittele die Extension des Pfads $path und liefere diese zurück.
3100 Besitzt der Pfad keine Extension, liefere einen Leerstring ('').
3101  
3102 =cut
3103  
3104 # -----------------------------------------------------------------------------
3105  
3106 3 ▶sub extension {
Subroutine extension: called CC=2 SCAR=7.2
Pod documented
3107 3 my ($class,$path) = @_;
3108 3 ▶ return $path =~ /\.([^.]+)$/? $1: '';
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /\.([^.]+)$/ ? : 3 0 3
3109 }
3110  
3111 # -----------------------------------------------------------------------------
3112  
3113 =head3 filename() - Letzte Pfadkomponente
3114  
3115 =head4 Synopsis
3116  
3117 $filename = $class->filename($path);
3118  
3119 =head4 Alias
3120  
3121 lastName()
3122  
3123 =head4 Description
3124  
3125 Liefere die letzte Komponente des Pfads.
3126  
3127 =cut
3128  
3129 # -----------------------------------------------------------------------------
3130  
3131 2 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
3132 2 my ($class,$path) = @_;
3133 2 $path =~ s|.*/||;
3134 2 return $path;
3135 }
3136  
3137 {
3138 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
3139 270 *lastName = \&filename;
3140 }
3141  
3142 # -----------------------------------------------------------------------------
3143  
3144 =head3 getFirst() - Liefere ersten existenten Pfad
3145  
3146 =head4 Synopsis
3147  
3148 $path = $this->getFirst(@patterns);
3149  
3150 =head4 Arguments
3151  
3152 =over 4
3153  
3154 =item @patterns
3155  
3156 Liste an Shell-Patterns (wie sie glob() erwartet)
3157  
3158 =back
3159  
3160 =head4 Description
3161  
3162 Durchlaufe die Pfad-Pattern @patterns und liefere den ersten existenten
3163 Pfad zurück. Ist I<kein> Pattern erfüllt oder erfüllt ein Pattern
3164 I<mehrere> Pfade, wirf eine Exception.
3165  
3166 =cut
3167  
3168 # -----------------------------------------------------------------------------
3169  
3170 0 ▶sub getFirst {
Subroutine getFirst: not called CC=6 SCAR=33.9
Pod documented
3171 0 my $this = shift;
3172  
3173 # Argumente und Optionen
3174  
3175 0 my $sloppy = 0;
3176  
3177 0 my $argA = $this->parameters(1,undef,\@_,
3178 -sloppy => \$sloppy,
3179 );
3180 # @$argA MEMO: Hier ist keine Tilde-Expansion nötig
3181  
3182 # Operation ausführen
3183  
3184 0 for my $pattern (@$argA) {
3185 0 ▶ my @paths = CORE::glob $pattern;
Subroutine BEGIN: called CC=1 SCAR=0.2
3186 0 ▶ if (@paths == 1 && $this->exists($paths[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (@paths == 1 and $this->exists($paths[0])) { } 0 0 0
elsif (@paths > 1) { } 0 0 0
Condition0%0/3
@paths == 1 and $this->exists($paths[0])
!ll&&!rl&&r
000
MC/DC0%0/2
@paths == 1 and $this->exists($paths[0])
@paths == 1 $this->exists($paths[0])
Truth table0%0/3
A and B
A= @paths == 1B= $this->exists($paths[0])
ABresult
0X0
100
111
3187 0 return $paths[0];
3188 }
3189 elsif (@paths > 1) {
3190 0 $this->throw(
3191 'PATH-00099: Pattern matches more than one path',
3192 Pattern => $pattern,
3193 Paths => join(', ',@paths),
3194 );
3195 }
3196 }
3197  
3198 0 ▶ if (!$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sloppy) 0 0 0
3199 0 $this->throw(
3200 'PATH-00099: No pattern matches a path',
3201 Patterns => join(', ',@$argA),
3202 );
3203 }
3204  
3205 0 return undef;
3206 }
3207  
3208 # -----------------------------------------------------------------------------
3209  
3210 =head3 glob() - Liefere Pfade, die Shell-Pattern erfüllen
3211  
3212 =head4 Synopsis
3213  
3214 $path = $this->glob($pat);
3215 @paths = $this->glob($pat);
3216  
3217 =head4 Description
3218  
3219 Liefere die Pfad-Objekte, die Shell-Pattern $pat erfüllen.
3220 Im Skalarkontext liefere den ersten Pfad, der dann
3221 der einzig erfüllbare Pfad sein muss, sonst wird eine Exception
3222 geworfen.
3223  
3224 =cut
3225  
3226 # -----------------------------------------------------------------------------
3227  
3228 7 ▶sub glob {
Subroutine glob: called CC=4 SCAR=13.9
Pod documented
3229 7 my ($this,$pat) = @_; # MEMO: Hier ist keine Tilde-Expansion nötig
3230  
3231 7 my @arr = CORE::glob $pat;
3232 7 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 3 4 7
3233 3 return @arr;
3234 }
3235  
3236 4 ▶ if (!@arr) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @arr) { } 1 3 4
elsif (@arr > 1) { } 2 1 3
3237 1 $this->throw(
3238 'PATH-00014: Path does not exist',
3239 Pattern => $pat,
3240 );
3241 }
3242 elsif (@arr > 1) {
3243 2 $this->throw(
3244 'PATH-00015: More than one path matches pattern',
3245 Pattern => $pat,
3246 );
3247 }
3248  
3249 1 return $arr[0];
3250 }
3251  
3252 # -----------------------------------------------------------------------------
3253  
3254 =head3 globExists() - Prüfe, ob Shell-Pattern erfüllt ist
3255  
3256 =head4 Synopsis
3257  
3258 $bool = $this->globExists($pat);
3259  
3260 =head4 Description
3261  
3262 Prüfe, ob Pfad-Objekte existieren, die Shell-Pattern $pat erfüllen.
3263 Wenn ja, liefere I<wahr>, sonst I<falsch>.
3264  
3265 =cut
3266  
3267 # -----------------------------------------------------------------------------
3268  
3269 0 ▶sub globExists {
Subroutine globExists: not called CC=2 SCAR=17.9
Pod documented
3270 0 my ($this,$pat) = @_; # MEMO: Hier ist keine Tilde-Expansion nötig
3271  
3272 0 my @arr = CORE::glob $pat;
3273 0 ▶ return @arr? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
@arr ? : 0 0 0
3274 }
3275  
3276 # -----------------------------------------------------------------------------
3277  
3278 =head3 isEmpty() - Prüfe, ob Datei oder Verzeichnis leer ist
3279  
3280 =head4 Synopsis
3281  
3282 $bool = $class->isEmpty($path);
3283  
3284 =cut
3285  
3286 # -----------------------------------------------------------------------------
3287  
3288 5 ▶sub isEmpty {
Subroutine isEmpty: called CC=7 SCAR=19.5
Pod documented
3289 5 my ($class,$path) = @_;
3290  
3291 5 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 2 3 5
3292 2 local *D;
3293  
3294 2 my $i = 0;
3295 2 ▶ unless (opendir D,$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir D, $path) 0 2 2
3296 0 $class->throw(
3297 'PATH-00005: Verzeichnis kann nicht geöffnet werden',
3298 Path => $path,
3299 Error => "$!",
3300 );
3301 }
3302 2 ▶ while (readdir D) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readdir D)) 5 1 6
3303 5 ▶ last if ++$i > 2;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i > 2 1 4 5
3304 }
3305 2 closedir D;
3306  
3307 2 ▶ return $i <= 2? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$i <= 2 ? : 1 1 2
3308 }
3309 else {
3310 3 ▶ return -z $path? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
-z $path ? : 2 1 3
3311 }
3312 }
3313  
3314 # -----------------------------------------------------------------------------
3315  
3316 =head3 mode() - Liefere Zugriffsrechte
3317  
3318 =head4 Synopsis
3319  
3320 $mode = $this->mode($path);
3321  
3322 =head4 Description
3323  
3324 Liefere die Zugriffsrechte des Pfads $path.
3325  
3326 =head4 Examples
3327  
3328 =over 2
3329  
3330 =item *
3331  
3332 Permissions oktal anzeigen
3333  
3334 printf "%04o\n",Quiq::Path->mode('/etc/passwd');
3335 0644
3336  
3337 =item *
3338  
3339 Prüfen, ob eine Datei für andere lesbar oder schreibbar ist
3340  
3341 if ($mode & 00066) {
3342 die "ERROR: File ist readable or writable for others\n";
3343 }
3344  
3345 =back
3346  
3347 =cut
3348  
3349 # -----------------------------------------------------------------------------
3350  
3351 7 ▶sub mode {
Subroutine mode: called CC=2 SCAR=7.2
Pod documented
3352 7 my ($this,$path) = @_;
3353  
3354 7 $path = $this->expandTilde($path);
3355  
3356 7 my @stat = CORE::stat $path;
3357 7 ▶ unless (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@stat) 0 7 7
3358 0 $this->throw(
3359 'PATH-00001: stat ist fehlgeschlagen',
3360 Path => $path,
3361 );
3362 }
3363  
3364 7 return $stat[2] & 07777;
3365 }
3366  
3367 # -----------------------------------------------------------------------------
3368  
3369 =head3 moveToDateSubDir() - Verschiebe Pfad in Datums-Subverzeichnis
3370  
3371 =head4 Synopsis
3372  
3373 $this->moveToDateSubDir($path);
3374 $this->moveToDateSubDir($path,$rootDir);
3375  
3376 =head4 Arguments
3377  
3378 =over 4
3379  
3380 =item $path
3381  
3382 Pfad, der verschoben wird.
3383  
3384 =item $rootDir (Default: '.')
3385  
3386 Verzeichnis, in dem das Datums-Subverzeichnis angelegt wird.
3387  
3388 =back
3389  
3390 =head4 Options
3391  
3392 =over 4
3393  
3394 =item -verbose => $bool (Default: 1)
3395  
3396 Gib Information aus.
3397  
3398 =item -dirSuffix => $suffix
3399  
3400 Füge an das Datums-Subverzeichnis den Suffix $suffix an. Beispiel:
3401  
3402 Quiq::Path->moveToDateSubDir($_,-dirSuffix=>'.tmp')
3403  
3404 =back
3405  
3406 =head4 Description
3407  
3408 Ermitte das Modifikationsdatum des Pfads $path, erzeuge ein Subverzeichnis
3409 C<YYYY-MM-DD> in $rootDir und verschiebe den Pfad dorthin. Diese Methode
3410 ist nützlich, wenn der Inhalt eines großen Verzeichnisses, der sich
3411 nach-und-nach aufgebaut hat, auf Subverzeichnisse aufgeteilt werden soll.
3412  
3413 =head4 Example
3414  
3415 Innerhalb eines Verzeichnisses:
3416  
3417 $ perl -MQuiq::Path -E 'Quiq::Path->moveToDateSubDir($_) for glob("*.jpg")'
3418  
3419 Mit Zielverzeichnis ("dest"):
3420  
3421 $ perl -MQuiq::Path -E 'Quiq::Path->moveToDateSubDir($_,"dest") for glob("src/*.jpg")'
3422  
3423 =cut
3424  
3425 # -----------------------------------------------------------------------------
3426  
3427 0 ▶sub moveToDateSubDir {
Subroutine moveToDateSubDir: not called CC=4 SCAR=30.0
Pod documented
3428 0 my $this = shift;
3429  
3430 # Optionen und Argumente
3431  
3432 0 my $verbose = 1;
3433 0 my $dirSuffix = undef;
3434  
3435 0 my $argA = $this->parameters(1,2,\@_,
3436 -verbose => \$verbose,
3437 -dirSuffix => \$dirSuffix,
3438 );
3439 0 my ($path,$rootDir) = @$argA;
3440  
3441 0 my $date = POSIX::strftime('%Y-%m-%d',localtime $this->mtime($path));
3442 0 ▶ my $destDir = $rootDir? "$rootDir/$date": $date;
Branch: 1 branch
BranchTrueFalseTotal
$rootDir ? : 0 0 0
3443 0 ▶ if ($dirSuffix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirSuffix) 0 0 0
3444 0 $destDir .= $dirSuffix;
3445 }
3446 0 my $name = $this->filename($path);
3447 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
3448 0 say "$path => $destDir/$name";
3449 }
3450 0 $this->mkdir($destDir,-recursive=>1);
3451 0 $this->duplicate('move',$path,"$destDir/$name");
3452  
3453 0 return;
3454 }
3455  
3456 # -----------------------------------------------------------------------------
3457  
3458 =head3 partionizeDir() - Verschiebe Dateien in Subverzeichnisse
3459  
3460 =head4 Synopsis
3461  
3462 $this->partionizeDir($dir,%options);
3463  
3464 =head4 Arguments
3465  
3466 =over 4
3467  
3468 =item $dir
3469  
3470 Verzeichnis, dessen Dateien verschoben werden
3471  
3472 =back
3473  
3474 =head4 Options
3475  
3476 =over 4
3477  
3478 =item -destDir => $destDir (Default: $dir)
3479  
3480 Zielverzeichnis, in dem die Subverzeichnisse für die Dateien angelegt werden
3481  
3482 =item -maxFiles => $maxFiles (Default: 100)
3483  
3484 (Maximale) Anzahl an Dateien je Subverzeichnis
3485  
3486 =item -verbose => $bool (Default: 1)
3487  
3488 Gib Information aus
3489  
3490 =item -width => $width (Default: 5)
3491  
3492 Länge des (numerischen) Subverzeichnisnamens
3493  
3494 =back
3495  
3496 =head4 Description
3497  
3498 Ermittele alle Dateien in Verzeichnis $dir und verteile sie auf
3499 Subverzeichnisse im Zielverzeichnis $destDir mit der der maximalen Anzahl
3500 $maxFiles. Der Vereichnisname ist ein numerischer Wert mit der Breite $width.
3501  
3502 =head4 Example
3503  
3504 $ perl -MQuiq::Path -E 'Quiq::Path->partionizeDir($dir)'
3505  
3506 =cut
3507  
3508 # -----------------------------------------------------------------------------
3509  
3510 0 ▶sub partionizeDir {
Subroutine partionizeDir: not called CC=15 SCAR=54.8
Pod documented
3511 0 my ($this,$dir) = splice @_,0,2;
3512  
3513 # Optionen und Argumente
3514  
3515 0 my $destDir = $dir;
3516 0 my $maxFiles = 100;
3517 0 my $verbose = 1;
3518 0 my $width = 5;
3519  
3520 0 $this->parameters(\@_,
3521 -destDir => \$destDir,
3522 -maxFiles => \$maxFiles,
3523 -verbose => \$verbose,
3524 -width => \$width,
3525 );
3526  
3527 # if ($verbose) {
3528 # say "$dir $destDir $maxFiles $verbose $width";
3529 # }
3530  
3531 0 my $p = Quiq::Path->new;
3532  
3533 0 for ($dir,$destDir) {
3534 0 ▶ if (!$p->exists($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->exists($_)) 0 0 0
3535 0 $this->throw(
3536 'PATH-00099: Directory does not exist',
3537 Direcory => $_,
3538 );
3539 }
3540 }
3541  
3542 # Ermittele das Verzeichnis mit der maximalen Nummer ($maxSubDir) und
3543 # baue die Liste der Dateien auf (@files).
3544  
3545 0 my $maxSubDir = 0;
3546 0 my @files;
3547 0 my $dh = Quiq::DirHandle->new($dir);
3548 0 ▶ while (my $e = $dh->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $dh->next) 0 0 0
3549 0 ▶ if ($e eq '.' || $e eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e eq "." or $e eq "..") 0 0 0
Condition0%0/3
$e eq "." or $e eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$e eq "." or $e eq ".."
$e eq "." $e eq ".."
Truth table0%0/3
A or B
A= $e eq "."B= $e eq ".."
ABresult
1X1
011
000
3550 0 next;
3551 }
3552 0 ▶ if (-d "$dir/$e" && $e =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$dir/$e" and $e =~ /^\d+$/) 0 0 0
Condition0%0/3
-d "$dir/$e" and $e =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
-d "$dir/$e" and $e =~ /^\d+$/
-d "$dir/$e" $e =~ /^\d+$/
Truth table0%0/3
A and B
A= -d "$dir/$e"B= $e =~ /^\d+$/
ABresult
0X0
100
111
3553 0 ▶ if (!$maxSubDir || $e > $maxSubDir) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $maxSubDir or $e > $maxSubDir) 0 0 0
Condition0%0/3
not $maxSubDir or $e > $maxSubDir
l!l&&r!l&&!r
000
MC/DC0%0/2
not $maxSubDir or $e > $maxSubDir
not $maxSubDir $e > $maxSubDir
Truth table0%0/3
not(A) or B
A= not $maxSubDirB= $e > $maxSubDir
ABresult
1X1
011
000
3554 0 $maxSubDir = $e;
3555 }
3556 0 next;
3557 }
3558 0 push @files,$e;
3559 }
3560 0 $dh->close;
3561  
3562 0 @files = sort @files;
3563  
3564 # Verteile die Dateien auf Subverzeichnisse
3565  
3566 0 ▶ $maxSubDir ||= 1;
Condition0%0/2
$maxSubDir ||= 1
l!l
00
MC/DC0%0/1
$maxSubDir ||= 1
$maxSubDir
Truth table0%0/2
A
A= $maxSubDir
Aresult
11
00
3567 0 my $subDir = sprintf '%0*d',$width,$maxSubDir;
3568 0 ▶ my $subDirCount = $p->exists("$destDir/$subDir")?
Branch: 1 branch
BranchTrueFalseTotal
$p->exists("$destDir/$subDir") ? : 0 0 0
3569 $p->count("$destDir/$subDir"): 0;
3570 # say $subDirCount;
3571 0 ▶ while (my $name = shift @files) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift @files) 0 0 0
3572 0 ▶ if ($subDirCount >= $maxFiles) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subDirCount >= $maxFiles) 0 0 0
3573 0 $maxSubDir++;
3574 0 $subDir = sprintf '%0*d',$width,$maxSubDir;
3575 0 $subDirCount = 0;
3576 }
3577 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
3578 0 say "$dir/$name => $destDir/$subDir/$name ($subDirCount)";
3579 }
3580 0 $p->mkdir("$destDir/$subDir");
3581 0 $this->duplicate('move',"$dir/$name","$destDir/$subDir/$name");
3582 0 $subDirCount++;
3583 }
3584  
3585 0 return;
3586 }
3587  
3588 # -----------------------------------------------------------------------------
3589  
3590 =head3 mtime() - Setze/Liefere Modifikationszeit
3591  
3592 =head4 Synopsis
3593  
3594 $mtime = $class->mtime($path);
3595 $mtime = $class->mtime($path,$mtime);
3596  
3597 =head4 Description
3598  
3599 Liefere die Zeit der letzten Modifikation des Pfads $path. Wenn der
3600 Pfad nicht existiert, liefere 0.
3601  
3602 Ist ein zweiter Parameter $mtime angegeben, setze die Zeit auf den
3603 angegebenen Wert. In dem Fall muss der Pfad existieren.
3604  
3605 =cut
3606  
3607 # -----------------------------------------------------------------------------
3608  
3609 15 ▶sub mtime {
Subroutine mtime: called CC=6 SCAR=18.7
Pod documented
3610 15 my $class = shift;
3611 15 my $path = $class->expandTilde(shift);
3612 # @_: $mtime
3613  
3614 15 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 9 15
3615 6 my $mtime = shift;
3616  
3617 6 ▶ if (!-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 0 6 6
3618 0 $class->throw(
3619 'PATH-00011: Pfad existiert nicht',
3620 Path => $path,
3621 );
3622 }
3623 6 my $atime = (stat($path))[8]; # atime lesen, die nicht ändern
3624 6 ▶ if (!utime $atime,$mtime,$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utime $atime, $mtime, $path) 0 6 6
3625 0 $class->throw(
3626 'PATH-00012: Kann mtime nicht setzen',
3627 Path => $path,
3628 Error => "$!",
3629 );
3630 }
3631 }
3632  
3633 15 ▶ return defined($path) && -e $path? (stat($path))[9]: 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $path && -e $path ? : 14 1 15
Condition66%2/3
defined $path and -e $path
!ll&&!rl&&r
0114
MC/DC50%1/2
defined $path and -e $path
defined $path -e $path
Truth table66%2/3
A and B
A= defined $pathB= -e $path
ABresult
0X0
100
111
3634 }
3635  
3636 # -----------------------------------------------------------------------------
3637  
3638 =head3 mtimePaths() - Setze mtime inkrementierend
3639  
3640 =head4 Synopsis
3641  
3642 $this->mtimePaths(\@paths,$startTime,$step);
3643  
3644 =head4 Arguments
3645  
3646 =over 4
3647  
3648 =item @path
3649  
3650 Die Pfade, die zu nummerieren sind.
3651  
3652 =item $startTime
3653  
3654 Startzeitpunkt im Format "YYYY-MM-DD HH:MM:SS".
3655  
3656 =item $step
3657  
3658 Schrittweite in Sekunden.
3659  
3660 =back
3661  
3662 =head4 Example
3663  
3664 $ perl -MQuiq::Path -E '$p = Quiq::Path->new; $p->mtimePaths([$p->glob("*.jpg")],"2019-10-08 20:00:00",60)'
3665  
3666 =cut
3667  
3668 # -----------------------------------------------------------------------------
3669  
3670 0 ▶sub mtimePaths {
Subroutine mtimePaths: not called CC=2 SCAR=17.9
Pod documented
3671 0 my ($this,$pathA,$startTime,$step) = splice @_,0,4;
3672  
3673 0 my $mtime = Quiq::Time->new(ymdhms=>$startTime)->epoch;
3674  
3675 0 for my $path (@$pathA) {
3676 0 $this->mtime($path,$mtime);
3677 0 $mtime += $step;
3678 }
3679  
3680 0 return;
3681 }
3682  
3683 # -----------------------------------------------------------------------------
3684  
3685 =head3 newer() - Vergleiche Modifikationsdatum zweier Pfade
3686  
3687 =head4 Synopsis
3688  
3689 $bool = $class->newer($path1,$path2);
3690  
3691 =head4 Description
3692  
3693 Prüfe, ob Pfad $path1 ein jüngeres Modifikationsdatum besitzt als
3694 $path2. Ist dies der Fall, liefere 1, andernfalls 0. Liefere
3695 ebenfalls 1, wenn Datei $path2 nicht existiert. Pfad
3696 $path1 muss existieren.
3697  
3698 Pfad $path2 kann eine Zeichenkette oder ein Pfad-Objekt sein.
3699  
3700 Dieser Test ist nützlich, wenn $path2 aus $path1 erzeugt wird
3701 und geprüft werden soll, ob eine Neuerzeugung notwendig ist.
3702  
3703 =cut
3704  
3705 # -----------------------------------------------------------------------------
3706  
3707 2 ▶sub newer {
Subroutine newer: called CC=4 SCAR=14.5
Pod documented
3708 2 my ($class,$path1,$path2) = @_;
3709  
3710 2 ▶ if (!-e $path1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path1) 0 2 2
3711 0 $class->throw(
3712 'PATH-00011: Pfad existiert nicht',
3713 Path => $path1,
3714 );
3715 }
3716  
3717 2 my $t1 = (stat $path1)[9];
3718 2 ▶ my $t2 = (stat $path2)[9] || 0;
Condition50%1/2
(CORE::stat $path2)[9] || 0
l!l
20
MC/DC0%0/1
(CORE::stat $path2)[9] || 0
(CORE::stat $path2)[9]
Truth table50%1/2
A
A= (CORE::stat $path2)[9]
Aresult
11
00
3719  
3720 2 ▶ return $t1 > $t2? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$t1 > $t2 ? : 1 1 2
3721 }
3722  
3723 # -----------------------------------------------------------------------------
3724  
3725 =head3 newExtension() - Setze eine neue Extension
3726  
3727 =head4 Synopsis
3728  
3729 $newPath = $this->newExtension($path,$ext);
3730  
3731 =head4 Description
3732  
3733 Entferne die bestehende Extension von Pfad $path und füge $ext als
3734 neue Extension hinzu. Besitzt $path keine Extension, wird
3735 $ext hinzugefügt. Etension $ext kann mit oder ohne Punkt am Anfang
3736 angegeben werden.
3737  
3738 =cut
3739  
3740 # -----------------------------------------------------------------------------
3741  
3742 3 ▶sub newExtension {
Subroutine newExtension: called CC=1 SCAR=0.0
Pod documented
3743 3 my ($this,$path,$ext) = @_;
3744  
3745 3 $ext =~ s/^\.//; # Wir entfernen einen optionalen .
3746 3 $path =~ s/\.([^.]+)$//; # Wir entfernen die bestehende Extension
3747 3 $path .= ".$ext"; # Wir fügen die neue Extension hinzu
3748  
3749 3 return $path;
3750 }
3751  
3752 # -----------------------------------------------------------------------------
3753  
3754 =head3 readlink() - Liefere Ziel des Symlink
3755  
3756 =head4 Synopsis
3757  
3758 $path = $class->readlink($symlinkPath);
3759  
3760 =head4 Alias
3761  
3762 readLink()
3763  
3764 =head4 Description
3765  
3766 Liefere den Pfad, auf den der Symlink $symlinkPath zeigt.
3767  
3768 =cut
3769  
3770 # -----------------------------------------------------------------------------
3771  
3772 0 ▶sub readlink {
Subroutine readlink: not called CC=2 SCAR=17.9
Pod documented
3773 0 my $class = shift;
3774 0 my $symlinkPath = shift;
3775  
3776 0 ▶ return readlink($symlinkPath) // do {
Condition0%0/3
CORE::readlink $symlinkPath // do { $class->throw("PATH-00099: Kann Symlink-Zielpfad nicht ermitteln", "Path", $symlinkPath, "Error", "$!") }
l!l&&r!l&&!r
000
MC/DC0%0/2
CORE::readlink $symlinkPath // do { $class->throw("PATH-00099: Kann Symlink-Zielpfad nicht ermitteln", "Path", $symlinkPath, "Error", "$!") }
CORE::readlink $symlinkPath do { $class->throw("PATH-00099: Kann Symlink-Zielpfad nicht ermitteln", "Path", $symlinkPath, "Error", "$!") }
Truth table0%0/3
A // B
A= CORE::readlink $symlinkPathB= do { $class->throw("PATH-00099: Kann Symlink-Zielpfad nicht ermitteln", "Path", $symlinkPath, "Error", "$!") }
ABresult
1X1
011
000
3777 0 $class->throw(
3778 'PATH-00099: Kann Symlink-Zielpfad nicht ermitteln',
3779 Path => $symlinkPath,
3780 Error => "$!",
3781 );
3782 };
3783 }
3784  
3785 {
3786 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
3787 270 *readLink = \&readlink;
3788 }
3789  
3790 # -----------------------------------------------------------------------------
3791  
3792 =head3 reduceToEnvVar() - Ersetze Pfandanfang durch Name von Environment-Variable
3793  
3794 =head4 Synopsis
3795  
3796 $pathNew = $class->reduceToEnvVar($envVar,$path);
3797  
3798 =head4 Arguments
3799  
3800 =over 4
3801  
3802 =item $path
3803  
3804 Ein Pfad
3805  
3806 =back
3807  
3808 =head4 Returns
3809  
3810 Pfad (String)
3811  
3812 =head4 Description
3813  
3814 Ersetze den Pfadanfang von $path durch den Namen der Environmen-Variable
3815 $envVar, wenn dieser dem Wert der Variable entspricht, und liefere
3816 resultierenden Pfad zurück. Beginnt der Pfad nicht mit dem Wert der
3817 Variable, bleibt er unverändert.
3818  
3819 =cut
3820  
3821 # -----------------------------------------------------------------------------
3822  
3823 0 ▶sub reduceToEnvVar {
Subroutine reduceToEnvVar: not called CC=2 SCAR=17.9
Pod documented
3824 0 my ($class,$envVar,$path) = @_;
3825  
3826 0 ▶ if (!exists $ENV{$envVar}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{$envVar}) 0 0 0
3827 0 $class->throw(
3828 'PATH-00016: Environment variable does not exist',
3829 EnvVar => $envVar
3830 );
3831 }
3832  
3833 0 my $envPath = $class->expandTilde($ENV{$envVar});
3834 0 $path = $class->expandTilde($path);
3835 0 $path =~ s#^$envPath#\$$envVar#;
3836
3837 0 return $path;
3838 }
3839  
3840 # -----------------------------------------------------------------------------
3841  
3842 =head3 reduceToTilde() - Ersetze Pfandanfang durch Tilde
3843  
3844 =head4 Synopsis
3845  
3846 $pathNew = $class->reduceToTilde($path);
3847  
3848 =head4 Arguments
3849  
3850 =over 4
3851  
3852 =item $path
3853  
3854 Ein Pfad
3855  
3856 =back
3857  
3858 =head4 Returns
3859  
3860 Pfad (String)
3861  
3862 =head4 Description
3863  
3864 Ersetze das Homedir am Pfadanfang durch eine Tilde und liefere den
3865 resultierenden Pfad zurück. Beginnt der Pfad nicht mit dem Homedir,
3866 bleibt er unverändert.
3867  
3868 =cut
3869  
3870 # -----------------------------------------------------------------------------
3871  
3872 0 ▶sub reduceToTilde {
Subroutine reduceToTilde: not called CC=2 SCAR=17.9
Pod documented
3873 0 my ($class,$path) = @_;
3874  
3875 0 ▶ if (!exists $ENV{'HOME'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{"HOME"}) 0 0 0
3876 0 $class->throw(
3877 'PATH-00016: Envar HOME does not exist',
3878 );
3879 }
3880  
3881 0 $path =~ s#^$ENV{'HOME'}#~#;
3882
3883 0 return $path;
3884 }
3885  
3886 # -----------------------------------------------------------------------------
3887  
3888 =head3 removeExtension() - Entferne Extension
3889  
3890 =head4 Synopsis
3891  
3892 $newPath = $class->removeExtension($path);
3893  
3894 =head4 Description
3895  
3896 Entferne die Extension von Pfad $path und liefere den
3897 resultierenden Pfad zurück. Besitzt $path keine Extension, sind
3898 $path und $newPath identisch.
3899  
3900 =cut
3901  
3902 # -----------------------------------------------------------------------------
3903  
3904 3 ▶sub removeExtension {
Subroutine removeExtension: called CC=1 SCAR=0.0
Pod documented
3905 3 my ($class,$path) = @_;
3906 3 $path =~ s|\.[^./]+$||;
3907 3 return $path;
3908 }
3909  
3910 # -----------------------------------------------------------------------------
3911  
3912 =head3 rename() - Benenne Pfad um
3913  
3914 =head4 Synopsis
3915  
3916 $this->rename($oldPath,$newPath,@opt);
3917  
3918 =head4 Options
3919  
3920 =over 4
3921  
3922 =item -overwrite => $bool (Default: 1)
3923  
3924 Wenn gesetzt, wird die Datei $newPath überschrieben, falls sie
3925 existiert. Wenn nicht gesetzt, wird eine Exception geworfen,
3926 falls sie existiert.
3927  
3928 =item -recursive => 0 | 1 (Default: 0)
3929  
3930 Erzeuge nicht-existente Verzeichnisse des Zielpfads
3931 und entferne leere Verzeichnisse des Quellpfads.
3932  
3933 =back
3934  
3935 =head4 Description
3936  
3937 Benenne Pfad $oldPath in $newPath um. Die Methode liefert keinen
3938 Wert zurück.
3939  
3940 =head4 Example
3941  
3942 Zielpfad erzeugen, Quellpfad entfernen mit -recursive=>1.
3943 Ausgangslage: Unterhalb von /tmp existieren weder a noch x.
3944  
3945 my $srcPath = '/tmp/a/b/c/d/f';
3946 my $destPath = '/tmp/x/b/c/d/f';
3947 Quiq::Path->write($srcPath,'',-recursive=>1);
3948 Quiq::Path->rename($srcPath,$destPath,-recursive=>1);
3949  
3950 Nach Ausführung existiert der der Pfad /tmp/x/b/c/d/f, aber der Pfad
3951 /tmp/a nicht mehr.
3952  
3953 =cut
3954  
3955 # -----------------------------------------------------------------------------
3956  
3957 2 ▶sub rename {
Subroutine rename: called CC=10 SCAR=32.7
Pod documented
3958 2 my $this = shift;
3959 2 my $oldPath = $this->expandTilde(shift);
3960 2 my $newPath = $this->expandTilde(shift);
3961 # @_: @opt
3962  
3963 # Nichts tun, wenn die Pfade identisch sind
3964  
3965 2 ▶ if ($oldPath eq $newPath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldPath eq $newPath) 0 2 2
3966 0 return;
3967 }
3968  
3969 # Optionen
3970  
3971 2 my $overwrite = 1;
3972 2 my $recursive = 0;
3973  
3974 2 Quiq::Option->extract(\@_,
3975 -overwrite => \$overwrite,
3976 -recursive => \$recursive,
3977 );
3978  
3979 2 ▶ if (!$overwrite && -e $newPath) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $overwrite and -e $newPath) 0 2 2
Condition33%1/3
not $overwrite and -e $newPath
!ll&&!rl&&r
200
MC/DC0%0/2
not $overwrite and -e $newPath
not $overwrite -e $newPath
Truth table33%1/3
not(A) and B
A= not $overwriteB= -e $newPath
ABresult
0X0
100
111
3980 0 $this->throw(
3981 'PATH-00099: Zieldatei existiert bereits',
3982 Path => $newPath,
3983 );
3984 }
3985  
3986 # Erzeuge Zielverzeichnis, wenn nicht vorhanden
3987  
3988 2 ▶ if ($recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive) 0 2 2
3989 0 my $newDir = (Quiq::Path->split($newPath))[0];
3990 0 ▶ if ($newDir && !-d $newDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newDir and not -d $newDir) 0 0 0
Condition0%0/3
$newDir and not -d $newDir
!ll&&!rl&&r
000
MC/DC0%0/2
$newDir and not -d $newDir
$newDir not -d $newDir
Truth table0%0/3
A and not(B)
A= $newDirB= not -d $newDir
ABresult
0X0
100
111
3991 0 $this->mkdir($newDir,-recursive=>1);
3992 }
3993 }
3994  
3995 2 ▶ CORE::rename $oldPath,$newPath or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::rename $oldPath, $newPath or do { $this->throw("PATH-00010: Kann Pfad nicht umbenennen", "Error", "$!", "OldPath", $oldPath, "NewPath", $newPath) } 1 1 2
3996 1 $this->throw(
3997 'PATH-00010: Kann Pfad nicht umbenennen',
3998 Error => "$!",
3999 OldPath => $oldPath,
4000 NewPath => $newPath,
4001 );
4002 };
4003  
4004 # Lösche Quellverzeichnisse, sofern sie leer sind
4005  
4006 1 ▶ if ($recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive) 0 1 1
4007 0 while (1) {
4008 0 ($oldPath) = Quiq::Path->split($oldPath);
4009 0 eval {Quiq::Path->rmdir($oldPath)};
4010 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
4011 0 last;
4012 }
4013 }
4014 }
4015  
4016 1 return;
4017 }
4018  
4019 # -----------------------------------------------------------------------------
4020  
4021 =head3 numberBasePaths() - Nummeriere die (kurzen) Basisnamen der Pfade
4022  
4023 =head4 Synopsis
4024  
4025 $this->numberBasePaths(\@paths,$width,$step,@opts);
4026  
4027 =head4 Arguments
4028  
4029 =over 4
4030  
4031 =item @path
4032  
4033 Die Menge der Pfade.
4034  
4035 =item $width
4036  
4037 Die Breite (Anzahl der Stellen) der Nummer.
4038  
4039 =item $step
4040  
4041 Die Schrittweite der Nummerierung.
4042  
4043 =back
4044  
4045 =head4 Options
4046  
4047 =over 4
4048  
4049 =item -start => $n (Default: $step)
4050  
4051 Startwert.
4052  
4053 =item -verbose => $bool (Default: 0)
4054  
4055 Gib Information aus.
4056  
4057 =back
4058  
4059 =head4 Description
4060  
4061 Sortiere die (kurzen) Basisnamen der Pfade B<@paths> lexikalisch und
4062 nummeriere ihre Basisnamen durch, beginnend mit Nummer B<$step> und
4063 Schrittweite B<$step>. Pfade mit dem gleichen (kurzen) Basisnamen aber
4064 unterschiedlichen (lange) Extensions erhalten die gleiche Nummer, behalten
4065 aber ihre unterschiedliche (lange) Extension.
4066  
4067 =head4 Example
4068  
4069 $ ls
4070 a.jpg
4071 a.xcf
4072 x.jpg
4073
4074 $ perl -MQuiq::Path -E 'Quiq::Path->numberBasePaths([$p->glob("*")],5,10)'
4075
4076 $ ls
4077 00010.jpg
4078 00010.xcf
4079 00020.jpg
4080  
4081 =cut
4082  
4083 # -----------------------------------------------------------------------------
4084  
4085 0 ▶sub numberBasePaths {
Subroutine numberBasePaths: not called CC=13 SCAR=52.0
Pod documented
4086 0 my $this = shift;
4087  
4088 # Optionen und Argumente
4089  
4090 0 my $start = undef;
4091 0 my $verbose = 0;
4092  
4093 0 my $argA = $this->parameters(3,3,\@_,
4094 -start => \$start,
4095 -verbose => \$verbose,
4096 );
4097 0 my ($pathA,$width,$step) = @$argA;
4098 0 ▶ $start //= $step;
Condition0%0/3
$start //= $step
l!l&&r!l&&!r
000
MC/DC0%0/2
$start //= $step
$start $step
Truth table0%0/3
A //= B
A= $startB= $step
ABresult
1X1
011
000
4099  
4100 # Baue Liste der Basisnamen auf
4101  
4102 0 my %basePath;
4103 0 for my $path (@$pathA) {
4104 # my ($dir,undef,$base,$ext) = $this->split($path);
4105 0 my ($dir,undef,undef,undef,$base,$ext) = $this->split($path);
4106  
4107 0 my $l = length $base;
4108 0 ▶ if ($l != $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l != $width) 0 0 0
4109 0 $this->throw(
4110 'PATH-00099: Wrong base width',
4111 Path => $path,
4112 Base => $base,
4113 AllowedWidth => $width,
4114 );
4115 }
4116  
4117 0 ▶ if ($dir ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir ne "") 0 0 0
4118 0 $base = "$dir/$base";
4119 }
4120 0 ▶ my $arr = $basePath{$base} //= [];
Condition0%0/2
$basePath{$base} //= []
l!l
00
MC/DC0%0/1
$basePath{$base} //= []
$basePath{$base}
Truth table0%0/2
A
A= $basePath{$base}
Aresult
11
00
4121 0 push @$arr,$ext;
4122 }
4123  
4124 0 my $n = $start;
4125 0 my @newPaths;
4126 0 for (sort keys %basePath) {
4127 0 my $oldBasePath = $_;
4128 0 my ($dir) = $this->split($oldBasePath);
4129
4130 0 my $newBasePath;
4131 0 ▶ if ($dir ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir ne "") 0 0 0
4132 0 $newBasePath = "$dir/";
4133 }
4134 0 $newBasePath .= sprintf '%0*d',$width,$n;
4135 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
4136 0 say "$oldBasePath => $newBasePath";
4137 }
4138 0 for my $ext (@{$basePath{$oldBasePath}}) {
4139 0 my $oldPath = $oldBasePath;
4140 0 my $newPath = $newBasePath;
4141 0 ▶ if ($ext eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq '') { } 0 0 0
4142 0 $newPath .= ".tmp";
4143 }
4144 else {
4145 0 $oldPath .= ".$ext";
4146 0 $newPath .= ".$ext.tmp";
4147 }
4148 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
4149 0 say " $oldPath => $newPath";
4150 }
4151 0 $this->rename($oldPath,$newPath,-overwrite=>0);
4152 0 push @newPaths,$newPath;
4153 }
4154  
4155 0 $n += $step;
4156 }
4157  
4158 # Entferne Endung .tmp
4159  
4160 0 for my $tmpPath (@newPaths) {
4161 0 (my $newPath = $tmpPath) =~ s/\.tmp$//;
4162 0 $this->rename($tmpPath,$newPath,-overwrite=>0);
4163 }
4164  
4165 0 return;
4166 }
4167  
4168 # -----------------------------------------------------------------------------
4169  
4170 =head3 numberPaths() - Nummeriere Pfade
4171  
4172 =head4 Synopsis
4173  
4174 $this->numberPaths(\@paths,$width,$step,@opts);
4175  
4176 =head4 Arguments
4177  
4178 =over 4
4179  
4180 =item @path
4181  
4182 Die Pfade, die zu nummerieren sind.
4183  
4184 =item $width
4185  
4186 Die Breite (Anzahl der Stellen) der Nummer.
4187  
4188 =item $step
4189  
4190 Die Schrittweite der Nummerierung.
4191  
4192 =back
4193  
4194 =head4 Options
4195  
4196 =over 4
4197  
4198 =item -move => [$after,$from,$to]
4199  
4200 Ordne alle Pfade von $from bis $to (lexikalisch sortiert) nach der
4201 Datei $after ein. Ist $after leer (Leerstring oder undef), werden
4202 die Pfade an den Anfang gestellt. Alle Angaben I<vor> der
4203 Nummerierung.
4204  
4205 =item -start => $n (Default: $step)
4206  
4207 Verwende $n als Startwert.
4208  
4209 =back
4210  
4211 =head4 Description
4212  
4213 Nummeriere die Pfade @paths, gemäß der gegebenen Reihenfolge.
4214 Der Basisname der jeweiligen Datei/des Directory aus @paths wird
4215 hierbei durch eine Nummer der Breite $width ersetzt. Die Extension
4216 (sofern vorhanden) bleibt erhalten. Die Nummerierung erfolgt mit
4217 Schrittweite $width.
4218  
4219 =head4 Example
4220  
4221 Der Aufruf
4222  
4223 $ perl -MQuiq::Path -E '$p = Quiq::Path->new; $p->numberPaths([$p->glob("*")],5,10)'
4224  
4225 benennt alle Dateien, gleichgültig wie sie heißen, im aktuellen
4226 Verzeichnis um in
4227  
4228 00010.EXT
4229 00020.EXT
4230 00030.EXT
4231 ...
4232  
4233 wobei EXT die Extension ist, die die Datei vorher hatte, d.h. diese
4234 bleibt als einziges vom ursprünglichen Dateinamen erhalten.
4235  
4236 =cut
4237  
4238 # -----------------------------------------------------------------------------
4239  
4240 0 ▶sub numberPaths {
Subroutine numberPaths: not called CC=16 SCAR=56.1
Pod documented
4241 0 my $this = shift;
4242 # @_: $pathA,$width,$step,@opts
4243  
4244 # Optionen
4245  
4246 0 my $moveA = undef;
4247 0 my $start = undef;
4248  
4249 0 my $argA = $this->parameters(3,3,\@_,
4250 -move => \$moveA,
4251 -start => \$start,
4252 );
4253 0 my ($pathA,$width,$step) = @$argA;
4254  
4255 0 ▶ if ($moveA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($moveA) 0 0 0
4256 0 my ($after,$from,$to) = @$moveA;
4257  
4258 # Prüfe Angaben
4259  
4260 0 scalar $this->glob("$after.*");
4261 0 my $fromPath = $this->glob("$from.*");
4262 0 my $toPath = $this->glob("$to.*");
4263  
4264 0 ▶ if ($fromPath gt $toPath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fromPath gt $toPath) 0 0 0
4265 0 $this->throw(
4266 'PATH-00099: fromPath must be less than toPath',
4267 FromPath => $fromPath,
4268 ToPath => $toPath,
4269 );
4270 }
4271  
4272 # Pfadliste neu aufbauen
4273 0 my @paths = @$pathA;
4274  
4275 # Pfade $from bis $to von @paths nach @movePaths bewegen
4276  
4277 0 my ($pos,@movePaths);
4278 0 ▶ if (!defined $after || $after eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $after or $after eq "") 0 0 0
Condition0%0/3
not defined $after or $after eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $after or $after eq ""
not defined $after $after eq ""
Truth table0%0/3
not(A) or B
A= not defined $afterB= $after eq ""
ABresult
1X1
011
000
4279 0 $pos = 0;
4280 }
4281 0 ▶ for (my $i = 0; $i < @paths; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @paths) 0 0 0
4282 0 my $path = $this->removeExtension($paths[$i]);
4283 0 ▶ if (!defined $pos && $after eq $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pos and $after eq $path) 0 0 0
Condition0%0/3
not defined $pos and $after eq $path
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $pos and $after eq $path
not defined $pos $after eq $path
Truth table0%0/3
not(A) and B
A= not defined $posB= $after eq $path
ABresult
0X0
100
111
4284 0 $pos = $i+1;
4285 0 next;
4286 }
4287 0 ▶ if ($path ge $from && $path le $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path ge $from and $path le $to) 0 0 0
Condition0%0/3
$path ge $from and $path le $to
!ll&&!rl&&r
000
MC/DC0%0/2
$path ge $from and $path le $to
$path ge $from $path le $to
Truth table0%0/3
A and B
A= $path ge $fromB= $path le $to
ABresult
0X0
100
111
4288 0 push @movePaths,splice @paths,$i--,1;
4289 }
4290 }
4291 0 ▶ if (!defined $pos) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos) 0 0 0
4292 0 $this->throw(
4293 'PATH-00099: Path (after) not found',
4294 Path => $after,
4295 );
4296 }
4297 0 splice @paths,$pos,0,@movePaths;
4298  
4299 # Umgeschriebene Pfadliste einsetzen
4300 0 $pathA = \@paths;
4301 }
4302  
4303 # Nummeriere Dateien mit Endung .tmp
4304  
4305 0 ▶ my $n = defined $start? $start: $step;
Branch: 1 branch
BranchTrueFalseTotal
defined $start ? : 0 0 0
4306 0 my @tmpPath;
4307 0 for my $path (@$pathA) {
4308 0 my ($dir,undef,$base,$ext) = $this->split($path);
4309  
4310 0 my $tmpPath;
4311 0 ▶ if ($dir ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir ne "") 0 0 0
4312 0 $tmpPath = "$dir/";
4313 }
4314 0 $tmpPath .= sprintf '%0*d',$width,$n;
4315 0 ▶ if ($ext ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext ne "") 0 0 0
4316 0 $tmpPath .= ".$ext";
4317 }
4318 0 $tmpPath .= '.tmp';
4319  
4320 0 push @tmpPath,$tmpPath;
4321  
4322 0 $this->rename($path,$tmpPath,-overwrite=>0);
4323  
4324 0 $n += $step;
4325 }
4326  
4327 # Entferne Endung .tmp
4328  
4329 0 for my $tmpPath (@tmpPath) {
4330 0 (my $newPath = $tmpPath) =~ s/\.tmp$//;
4331 0 $this->rename($tmpPath,$newPath,-overwrite=>0);
4332 }
4333  
4334 0 return;
4335 }
4336  
4337 # -----------------------------------------------------------------------------
4338  
4339 =head3 sameFile() - Prüfe auf identische Datei
4340  
4341 =head4 Synopsis
4342  
4343 $bool = $this->sameFile($path1,$path2);
4344  
4345 =head4 Arguments
4346  
4347 =over 4
4348  
4349 =item $path1
4350  
4351 Erster Pfad
4352  
4353 =item $path2
4354  
4355 Zweiter Pfad
4356  
4357 =back
4358  
4359 =head4 Returns
4360  
4361 (Integer)
4362  
4363 =head4 Description
4364  
4365 Prüfe, ob die beiden Pfade $path1 und $path2 auf dieselbe
4366 Datei (deselben Inode) verweisen. Wenn ja, liefere 1, sonst 0.
4367 Funktioniert auch bei Symlinks.
4368  
4369 =cut
4370  
4371 # -----------------------------------------------------------------------------
4372  
4373 1 ▶sub sameFile {
Subroutine sameFile: called CC=2 SCAR=7.2
Pod documented
4374 1 my ($this,$path1,$path2) = @_;
4375 1 ▶ return ($this->stat($path1))[1] == ($this->stat($path2))[1]? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
($this->stat($path1))[1] == ($this->stat($path2))[1] ? : 0 1 1
4376 }
4377  
4378 # -----------------------------------------------------------------------------
4379  
4380 =head3 split() - Zerlege Pfad in seine Komponenten
4381  
4382 =head4 Synopsis
4383  
4384 ($dir,$file,$base,$ext,$shortBase,$longExt) = $class->split($path);
4385  
4386 =head4 Description
4387  
4388 Zerlege Pfad in die vier Komponenten Verzeichnisname, Dateiname,
4389 Basisname (= Dateiname ohne Extension) und Extension und liefere diese
4390 zurück.
4391  
4392 Für eine Komponente, die nicht existiert, wird ein Leerstring
4393 geliefert.
4394  
4395 =cut
4396  
4397 # -----------------------------------------------------------------------------
4398  
4399 94 ▶sub split {
Subroutine split: called CC=4 SCAR=13.9
Pod documented
4400 94 my ($class,$path) = @_;
4401  
4402 94 my ($dir,$file,$base,$ext,$shortBase,$longExt) = ('') x 6;
4403  
4404 94 ▶ $dir = $1 if $path =~ s|(.*)/||;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ s[(.*)/][] 81 13 94
4405 94 $file = $path;
4406  
4407 94 ▶ $ext = $1 if $path =~ s/\.([^.]+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ s/\.([^.]+)$// 37 57 94
4408 94 $base = $path;
4409  
4410 94 $longExt = $ext;
4411 94 ▶ if (($shortBase = $base) =~ s/\.(.+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if (($shortBase = $base) =~ s/\.(.+)$//) 2 92 94
4412 2 $longExt = "$1.$longExt";
4413 }
4414  
4415 94 return ($dir,$file,$base,$ext,$shortBase,$longExt);
4416 }
4417  
4418 # -----------------------------------------------------------------------------
4419  
4420 =head3 spreadToSubDirs() - Kopiere, bewege, linke oder symlinke Dateien in Subverzeichnisse
4421  
4422 =head4 Synopsis
4423  
4424 $this->%METHOD($method,$destDir,$maxPerDir);
4425  
4426 =head4 Arguments
4427  
4428 =over 4
4429  
4430 =item $method
4431  
4432 Anzuwendende Pfadoperation (siehe Methode duplicate()):
4433  
4434 copy
4435 move -or- rename
4436 link
4437 symlink
4438  
4439 =item $destDir
4440  
4441 (String) Verzeichnis, in dem die Subverzeichnisse angelegt werden.
4442 Das Verzeichnis muss existieren.
4443  
4444 =item $maxPerDir
4445  
4446 (Integer) Maximale Anzahl Dateien pro Unterverzeichnis.
4447  
4448 =back
4449  
4450 =head4 Description
4451  
4452 Lies Pfade von <STDIN> und verteile die Pfade auf dynamisch erzeugte
4453 Subverzeichnisse.
4454  
4455 =head4 Example
4456  
4457 $ find SRCDIR -type f | sort | perl -MQuiq::Path -E 'Quiq::Path->spreadToSubDirs("link",$destDir,100)'
4458  
4459 =cut
4460  
4461 # -----------------------------------------------------------------------------
4462  
4463 0 ▶sub spreadToSubDirs {
Subroutine spreadToSubDirs: not called CC=3 SCAR=24.8
Pod documented
4464 0 my $this = shift;
4465 0 my $method = shift;
4466 0 my $destDir = $this->expandTilde(shift);
4467 0 my $maxPerDir = shift;
4468  
4469 0 my $i = 0;
4470 0 my $dir;
4471 0 ▶ while (<STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline STDIN)) 0 0 0
4472 0 chomp;
4473 0 ▶ if ($i%$maxPerDir == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % $maxPerDir == 0) 0 0 0
4474 0 $dir = sprintf '%s/%04d',$destDir,int($i/$maxPerDir)+1;
4475 0 $this->mkdir($dir);
4476 0 say $dir;
4477 }
4478 0 $i++;
4479 0 my $file = $this->filename($_);
4480 0 $this->duplicate($method,$_,"$dir/$file");
4481 }
4482  
4483 0 return;
4484 }
4485  
4486 # -----------------------------------------------------------------------------
4487  
4488 =head3 stat() - Statusinformation eines Pfads
4489  
4490 =head4 Synopsis
4491  
4492 @arr = $class->%METHOD($path);
4493  
4494 =head4 Arguments
4495  
4496 =over 4
4497  
4498 =item $path
4499  
4500 Datei- oder Verzeichnispfad.
4501  
4502 =back
4503  
4504 =head4 Returns
4505  
4506 Statusinformation (List)
4507  
4508 =head4 Description
4509  
4510 Ermittele die Statusinformation eines Pfads und liefere diese
4511 Information als 13-elementige Liste zurück.
4512  
4513 Der Aufruf ist äquivalent zu
4514  
4515 stat $path
4516  
4517 mit dem Unterschied, dass
4518  
4519 =over 2
4520  
4521 =item *
4522  
4523 eine Tilde-Expansion stattfindet
4524  
4525 =item *
4526  
4527 eine Exception geworfen, wird, wenn $path nicht existiert
4528  
4529 =back
4530  
4531 =head4 See Also
4532  
4533 =over 2
4534  
4535 =item *
4536  
4537 perldoc -f stat
4538  
4539 =back
4540  
4541 =cut
4542  
4543 # -----------------------------------------------------------------------------
4544  
4545 3 ▶sub stat {
Subroutine stat: called CC=2 SCAR=7.4
Pod documented
4546 3 my $class = shift;
4547 3 my $path = $class->expandTilde(shift);
4548  
4549 3 ▶ if (!-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 0 3 3
4550 0 $class->throw(
4551 'PATH-00099: Path does not exist',
4552 Path => $path,
4553 );
4554 }
4555  
4556 3 return stat $path;
4557 }
4558  
4559 # -----------------------------------------------------------------------------
4560  
4561 =head3 swap() - Vertausche in einem Verzeichnis zwei Namen
4562  
4563 =head4 Synopsis
4564  
4565 $this->%METHOD($dir,$glob1,$glob2);
4566  
4567 =head4 Arguments
4568  
4569 =over 4
4570  
4571 =item $dir
4572  
4573 Pfad des Verzeichnisses
4574  
4575 =item $glob1
4576  
4577 Datei oder Verzeichnisname
4578  
4579 =item $glob2
4580  
4581 Datei oder Verzeichnisname
4582  
4583 =back
4584  
4585 =head4 Description
4586  
4587 Vertausche in Verzeichnis $dir die beiden (Datei- oder Verzeichnis-)Namen
4588 $glob1 und $glob2.
4589  
4590 =head4 Example
4591  
4592 $ perl -MQuiq::Path -E 'Quiq::Path->swap("06-album","0000940.*","0010200.*")'
4593  
4594 =cut
4595  
4596 # -----------------------------------------------------------------------------
4597  
4598 0 ▶sub swap {
Subroutine swap: not called CC=6 SCAR=37.4
Pod documented
4599 0 my $this = shift;
4600 0 my $dir = $this->expandTilde(shift);
4601 0 my $glob1 = shift;
4602 0 my $glob2 = shift;
4603  
4604 0 my @paths1 = $this->glob("$dir/$glob1");
4605 0 ▶ if (!@paths1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths1) 0 0 0
4606 0 $this->throw(
4607 'PATH-00099: Glob pattern not found',
4608 Pattern => "$dir/$glob1",
4609 );
4610 }
4611 0 my @paths2 = $this->glob("$dir/$glob2");
4612 0 ▶ if (!@paths2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths2) 0 0 0
4613 0 $this->throw(
4614 'PATH-00099: Glob pattern not found',
4615 Pattern => "$dir/$glob2",
4616 );
4617 }
4618  
4619 0 (my $name1 = $glob1) =~ s/\*//g;
4620 0 (my $name2 = $glob2) =~ s/\*//g;
4621  
4622 0 my @newPaths;
4623  
4624 # $name1 in $name2 umbenennen und Endung .tmp hinzufügen
4625  
4626 0 for my $path (@paths1) {
4627 0 (my $newPath = $path) =~ s/$name1/$name2/;
4628 0 $newPath .= '.tmp';
4629 0 $this->rename($path,$newPath);
4630 0 push @newPaths,$newPath;
4631 }
4632  
4633 # $name2 in $name1 umbenennen und Endung .tmp hinzufügen
4634  
4635 0 for my $path (@paths2) {
4636 0 (my $newPath = $path) =~ s/$name2/$name1/;
4637 0 $newPath .= '.tmp';
4638 0 $this->rename($path,$newPath);
4639 0 push @newPaths,$newPath;
4640 }
4641  
4642 # Endung .tmp entfernen
4643  
4644 0 for my $path (@newPaths) {
4645 0 (my $newPath = $path) =~ s/\.tmp//;
4646 0 $this->rename($path,$newPath);
4647 }
4648  
4649 0 return;
4650 }
4651  
4652 # -----------------------------------------------------------------------------
4653  
4654 =head3 symlink() - Erzeuge Symlink
4655  
4656 =head4 Synopsis
4657  
4658 $class->symlink($path,$symlink);
4659  
4660 =head4 Description
4661  
4662 Erzeuge Symlink $symlink für Pfad $path.
4663 Die Methode liefert keinen Wert zurück.
4664  
4665 =cut
4666  
4667 # -----------------------------------------------------------------------------
4668  
4669 6 ▶sub symlink {
Subroutine symlink: called CC=4 SCAR=16.1
Pod documented
4670 6 my ($class,$path,$symlink) = splice @_,0,3;
4671  
4672 # Optionen
4673  
4674 6 my $force = 0;
4675  
4676 6 Quiq::Option->extract(\@_,
4677 -force => \$force,
4678 );
4679  
4680 6 ▶ if ($force && -l $symlink) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force and -l $symlink) 0 6 6
Condition33%1/3
$force and -l $symlink
!ll&&!rl&&r
600
MC/DC0%0/2
$force and -l $symlink
$force -l $symlink
Truth table33%1/3
A and B
A= $forceB= -l $symlink
ABresult
0X0
100
111
4681 0 $class->delete($symlink);
4682 }
4683  
4684 6 ▶ CORE::symlink $path,$symlink or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::symlink $path, $symlink or do { $class->throw("FS-00001: Kann Symlink nicht erzeugen", "Path", $path, "Symlink", $symlink, "Error", $!) } 6 0 6
4685 0 $class->throw(
4686 'FS-00001: Kann Symlink nicht erzeugen',
4687 Path => $path,
4688 Symlink => $symlink,
4689 Error => $!,
4690 );
4691 };
4692  
4693 6 return;
4694 }
4695  
4696 # -----------------------------------------------------------------------------
4697  
4698 =head3 symlinkRelative() - Erzeuge Symlink mit relativem Zielpfad
4699  
4700 =head4 Synopsis
4701  
4702 $class->symlinkRelative($path,$symlink,@opt);
4703  
4704 =head4 Options
4705  
4706 =over 4
4707  
4708 =item -createDir => $bool (Default: 0)
4709  
4710 Erzeuge nicht-existente Verzeichnisse des Zielpfads.
4711  
4712 =item -dryRun => $bool (Default: 0)
4713  
4714 Führe das Kommando nicht aus. Speziell Verbindung mit
4715 -verbose=>1 sinnvoll, um Code zu testen.
4716  
4717 =item -verbose => $bool (Default: 0)
4718  
4719 Gib Informationen über die erzeugten Symlinks auf STDOUT aus.
4720  
4721 =back
4722  
4723 =head4 Description
4724  
4725 Erzeuge einen Symlink $symlink, der auf den Pfad $path verweist.
4726 Die Methode liefert keinen Wert zurück.
4727  
4728 Die Methode zeichnet sich gegenüber der Methode symlink() dadurch
4729 aus, dass sie, wenn $path ein relativer Pfad ist, diesen so korrigiert,
4730 dass er von $symlink aus korrekt ist. Denn der Pfad $path múss als
4731 relativer Pfad als Fortsetzung von $symlink gesehen werden.
4732  
4733 =head4 Example
4734  
4735 Quiq::Path->symlinkRelative('a','x')
4736 # x => a
4737
4738 Quiq::Path->symlinkRelative('a/b','x')
4739 # x => a/b
4740
4741 Quiq::Path->symlinkRelative('a/b','x/y')
4742 # x/y => ../a/b
4743
4744 Quiq::Path->symlinkRelative('a/b','x/y/z')
4745 # x/y/z => ../../a/b
4746  
4747 =cut
4748  
4749 # -----------------------------------------------------------------------------
4750  
4751 5 ▶sub symlinkRelative {
Subroutine symlinkRelative: called CC=11 SCAR=28.0
Pod documented
4752 5 my $class = shift;
4753 5 my $path = shift;
4754 5 my $symlink = shift;
4755 5 my %opt = @_;
4756  
4757 5 my $createDir = delete $opt{'-createDir'};
4758 5 my $dryRun = delete $opt{'-dryRun'};
4759 5 my $verbose = delete $opt{'-verbose'};
4760 5 ▶ if (%opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opt) 0 5 5
4761 0 $class->throw(
4762 'FILESYS-00001: Unbekannte Option(en)',
4763 Options => join(', ',keys %opt),
4764 );
4765 }
4766  
4767 # Erzeuge den Zielpfad, falls er nicht existiert
4768  
4769 5 ▶ if ($createDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($createDir) 0 5 5
4770 0 my $dir = (Quiq::Path->split($symlink))[0];
4771 0 ▶ if ($dir && !-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir and not -d $dir) 0 0 0
Condition0%0/3
$dir and not -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$dir and not -d $dir
$dir not -d $dir
Truth table0%0/3
A and not(B)
A= $dirB= not -d $dir
ABresult
0X0
100
111
4772 0 $class->mkdir($dir,-recursive=>1);
4773 }
4774 }
4775  
4776 # Sonderbehandlung, wenn der Pfad $path, auf den der Symlink zeigt,
4777 # relativ ist. Da der Pfad $path relativ zum Symlink gilt
4778 # und nicht relativ zum aktuellen Verzeichnis des Aufrufers
4779 # interpretiert wird, muss der Zielpfad ergänzt werden,
4780 # wenn der Symlink-Pfad nicht im aktuellen Verzeichnis liegt.
4781 # Die Pfad-Umschreibung nimmt diese Methode vor.
4782  
4783 5 ▶ if ($path !~ m|^/| && $symlink =~ m|/|) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path =~ m[^/] and $symlink =~ m[/]) 3 2 5
Condition66%2/3
not $path =~ m[^/] and $symlink =~ m[/]
!ll&&!rl&&r
023
MC/DC50%1/2
not $path =~ m[^/] and $symlink =~ m[/]
not $path =~ m[^/] $symlink =~ m[/]
Truth table66%2/3
not(A) and B
A= not $path =~ m[^/]B= $symlink =~ m[/]
ABresult
0X0
100
111
4784 3 ▶ if ($symlink !~ m|^/|) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $symlink =~ m[^/]) { } 3 0 3
4785 # Wenn $symlink relativ ist, $path die Anzahl der
4786 # $symlink-Directories voranstellen
4787  
4788 3 my $n = $symlink =~ tr|/||;
4789 3 my $prefix = '';
4790 3 ▶ for (my $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 3 3 6
4791 3 $prefix .= '../';
4792 }
4793 3 $path = "$prefix$path";
4794 }
4795 else {
4796 # Wenn $symlink absolut ist, $path das aktuelle
4797 # Verzeichnis voranstellen.
4798 0 $path = sprintf '%s/%s',Quiq::Process->cwd,$path;
4799 }
4800 }
4801 5 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 5 5
4802 0 print "$symlink => $path\n";
4803 }
4804 5 ▶ if (!$dryRun) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryRun) 5 0 5
4805 5 $class->symlink($path,$symlink);
4806 }
4807  
4808 5 return;
4809 }
4810  
4811 # -----------------------------------------------------------------------------
4812  
4813 =head3 touch() - Setze Modifikationszeit auf aktuellen Zeitpunkt
4814  
4815 =head4 Synopsis
4816  
4817 $mtime = $this->touch($path);
4818  
4819 =head4 Arguments
4820  
4821 =over 4
4822  
4823 =item $path
4824  
4825 Pfad der Datei oder des Verzeichnisses, dessen mtime gesetzt werden soll.
4826  
4827 =back
4828  
4829 =head4 Returns
4830  
4831 Modifikationszeit in Unix Epoch (Integer)
4832  
4833 =head4 Description
4834  
4835 Setze den Zeitpunkt der letzten Modifikation (mtime) des Pfades $path
4836 auf den aktuellen Zeitpunkt.
4837  
4838 =cut
4839  
4840 # -----------------------------------------------------------------------------
4841  
4842 1 ▶sub touch {
Subroutine touch: called CC=1 SCAR=0.0
Pod documented
4843 1 my ($this,$path) = @_;
4844 1 return $this->mtime($path,time);
4845 }
4846  
4847 # -----------------------------------------------------------------------------
4848  
4849 =head3 uid() - UID des Owners der Datei
4850  
4851 =head4 Synopsis
4852  
4853 $uid = $this->%METHOD($path);
4854  
4855 =head4 Description
4856  
4857 Ermittele die UID des Owners der Datei und liefere diese zurück.
4858  
4859 =cut
4860  
4861 # -----------------------------------------------------------------------------
4862  
4863 1 ▶sub uid {
Subroutine uid: called CC=1 SCAR=0.0
Pod documented
4864 1 my ($this,$path) = @_;
4865 1 return ($this->stat($path))[4];
4866 }
4867  
4868 # -----------------------------------------------------------------------------
4869  
4870 =head1 VERSION
4871  
4872 1.240
4873  
4874 =head1 AUTHOR
4875  
4876 Frank Seitz, L<http://fseitz.de/>
4877  
4878 =head1 COPYRIGHT
4879  
4880 Copyright (C) 2026 Frank Seitz
4881  
4882 =head1 LICENSE
4883  
4884 This code is free software; you can redistribute it and/or modify
4885 it under the same terms as Perl itself.
4886  
4887 =cut
4888  
4889 # -----------------------------------------------------------------------------
4890  
4891 270 1;
4892  
4893 # eof
« Quiq/ImagePool/Sequence.pm ↑ Summary Quiq/Timeseries/Synchronizer.pm »