1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
|
# -*- perl -*-
#
# Copyright (C) Heinz-Josef Claes (2001-2012)
# hjclaes@web.de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
push @VERSION, '$Id: fileDir.pl 364 2012-02-12 14:14:44Z hjc $ ';
use Fcntl qw(O_RDWR O_CREAT O_WRONLY O_EXCL);
use POSIX;
require 'prLog.pl';
require 'forkProc.pl';
use strict;
# copies a file
# returns:
# 1: ok
# 0: error
############################################################
sub copyFile
{
my $source = shift;
my $target = shift;
local *SOURCE;
local *TARGET;
unless (sysopen(SOURCE, $source, O_RDONLY))
{
return 0;
}
unless (sysopen(TARGET, $target, O_CREAT | O_WRONLY))
{
return 0;
}
my $buffer;
while (sysread(SOURCE, $buffer, 10*1024**2))
{
syswrite(TARGET, $buffer);
}
close(TARGET);
close(SOURCE);
return 1;
}
# checks if a file is a symlink and deletes it if wanted
# return values (if not exiting):
# 0: no symlink
# -1: found symlink
############################################################
sub checkDelSymLink
{
my $file = shift; # name of the file
my $prLog = shift;
my $delExit = shift; # set bits:
# bit 0: 0 = do not delete
# 1 = delete symlink
# if not possible, exit
# bit 1: 0 = do not exit (if exists)
# 1 = exit
return 0 unless -l $file;
if ($delExit & 0x02)
{
$prLog->print('-kind' => 'E',
'-str' =>
["found symbolic link at <$file>, exiting "],
'-exit' => 1);
}
if ($delExit & 0x01)
{
$prLog->print('-kind' => 'W',
'-str' => ["unlinking symbolic link <$file>"]);
unlink $file or
$prLog->print('-kind' => 'E',
'-str' => ["cannot unlink <$file>, exiting"],
'-exit' => 1);
}
return -1;
}
############################################################
sub splitFileDir
{
my $name = shift;
return (undef, undef) unless $name;
return ('.', $name) unless ($name =~/\//); # nur einfacher Dateiname
my ($dir, $file) = $name =~ /^(.*)\/(.*)$/s;
$dir = '/' if ($dir eq ''); # gilt, falls z.B. /filename
return ($dir, $file);
}
############################################################
# Parameter kann directory oder Datei sein
%main::absolutePathCache = ();
@main::absolutePathCache = ();
sub absolutePath
{
my $dir = shift;
return undef unless $dir;
# Erst im Hash nachsehen, ob das schon ermittelt wurde
return $main::absolutePathCache{$dir}
if exists $main::absolutePathCache{$dir};
my $dirSave = $dir;
# Falls Datei selbst ein symlink ist, solange folgen, bis aufgelst
my $file = '';
$dir = $ENV{'PWD'} . "/$dir" unless ($dir =~ /^\//); # relativer Pfad
if (-e $dir)
{
$file = $dir;
while (-l $file)
{
my $link = readlink($file);
if (substr($link, 0, 1) ne "/")
{
$file =~ s/[^\/]+$/$link/;
}
else
{
$file = $link;
}
}
($dir, $file) = &splitFileDir($file);
$file = "/$file";
}
my $ret = (-e "$dir$file") ? "$dir$file" : undef;
$ret =~ s/\/+/\//g; # // -> /
while ($ret =~ s/\/\.\//\//g) {} # /./ -> /
$ret =~ s/\A(.+?)\/+\Z/$1/; # remove trailing /
$ret =~ s/\A\.\///; # ./xxx -> xxx
$ret =~ s/\/\.\Z//; # xxx/. -> xxx
# eliminate /../
my (@ret) = split(/\//, $ret);
if (@ret)
{
my (@rev) = reverse(@ret);
(@ret) = ();
my ($i, $j);
for ($i = $j = 0 ; $i < @rev ; $i++, $j++)
{
if ($rev[$i] eq '..')
{
$j++;
}
else
{
push @ret, $rev[$j] if ($j < @rev);
}
}
$ret = join('/', reverse @ret);
}
if (@main::absolutePathCache > 100)
{
my $del = shift @main::absolutePathCache; # delete oldest
delete $main::absolutePathCache{$del};
}
push @main::absolutePathCache, $ret;
return $main::absolutePathCache{$dirSave} = $ret;
}
########################################
sub clearAbsolutePathCache
{
%main::absolutePathCache = ();
@main::absolutePathCache = ();
}
############################################################
sub relPath
{
# calculate relative path from
my $dir = shift; # this file or directory
my $target = shift; # to this directory or file
return undef unless $dir or $target;
$dir = ::absolutePath($dir) if substr($dir, 0, 1) ne '/';
$target = ::absolutePath($dir) if substr($target, 0, 1) ne '/';
my (@dir) = split(/\//, $dir);
my (@target) = split(/\//, $target);
shift @dir;
shift @target;
my $min = (@dir < @target) ? @dir : @target;
my $i;
for ($i = 0 ; $i < $min ; $i++)
{
last if ($dir[$i] ne $target[$i]);
}
my $relPath = '../' x (@dir - $i) . join('/', @target[$i..@target-1]);
$relPath = '.' unless $relPath;
return $relPath;
}
############################################################
sub uniqFileName
{
my $prefix = shift; # z.B. '/tmp/test-'
my $suffix;
do
{
$suffix = sprintf '%08x%08x', rand 0xffffffff, rand 0xffffffff;
}
while (-e $prefix . $suffix);
return $prefix . $suffix;
}
############################################################
# testet, ob subDir ein Unterverzeichnis von dir ist
sub isSubDir
{
my $dir = shift;
my $subDir = shift;
$dir = &::absolutePath($dir);
$dir .= '/' unless $dir eq '/';
$subDir = &::absolutePath($subDir);
$subDir .= '/' unless $subDir eq '/';
return (index($subDir, $dir) == 0) ? 1 : 0;
}
############################################################
# substract pathLong - pathShort = relPath
sub substractPath
{
my $pathLong = shift; # longer path
my $pathShort = shift; # shorter path
$pathLong =~ s/\/+$//; # remove trailing /
$pathShort =~ s/\/+$//; # remove trailing /
$pathLong =~ s/\/\//\//g; # // -> /
$pathShort =~ s/\/\//\//g; # // -> /
my $relPath;
if ($pathShort eq '/')
{
$relPath = substr($pathLong, 1);
}
else
{
$relPath = substr($pathLong, length($pathShort) + 1);
}
return $relPath;
}
############################################################
# makes the directories to a file
sub makeFilePath
{
my $path = shift;
my $prLog = shift;
$path =~ m#\A(.*)/.*\Z#s;
&makeDirPath($1, $prLog);
}
############################################################
# like `mkdir -p`, all permissions set to 0700
sub makeDirPath
{
my $path = shift;
my $prLog = shift;
my @p;
return if -e $path;
my ($p1, $p2) = $path =~ m#\A(.*)/(.*)\Z#s;
&makeDirPath($p1, $prLog);
unless (mkdir $path, 0700)
{
return if -e $path;
$prLog->print('-kind' => 'E',
'-str' => ["cannot create directory <$path>"]);
}
return;
}
############################################################
# liest Typ der File Systeme und liefert sortiert nach Lnge,
# die lngsten zuerst, Liefert Zeiger auf Liste von Hashes
sub getFileSystemInfosSorted
{
my $prLog = shift;
my $tmpdir = shift;
my $fs = forkProc->new('-exec' => 'mount',
'-outRandom' => "$tmpdir/mount-",
'-prLog' => $prLog);
$fs->wait();
my $out = $fs->getSTDERR();
$prLog->print('-kind' => 'E',
'-str' => ['STDERR of command mount (exit status ' .
$fs->get('-what' => 'status') . "):",
@$out, 'exiting'],
'-exit' => 1)
if (@$out > 0);
$out = $fs->getSTDOUT();
my ($o, @fstypes);
foreach $o (@$out)
{
my ($origin, $dir, $type, $flags) = $o =~
/^(.*) on (.*) type (\w+) \((.*)\)/;
# print "<$origin> <$dir> <$type> <$flags>\n";
push @fstypes, {'origin' => $origin,
'dir' => $dir,
'type' => $type,
'flags' => $flags};
}
@fstypes = sort { length($b->{'dir'}) <=> length($a->{'dir'}) } @fstypes;
return \@fstypes;
}
############################################################
sub checkLockFile
{
my ($lockFile, $prLog) = @_;
local *FILE;
if (-f $lockFile)
{
open(FILE, "< $lockFile") or
$prLog->print('-kind' => 'E',
'-str' => ["cannot read lock file <$lockFile>"],
'-exit' => 1);
my $pid = <FILE>;
chop $pid;
close(FILE);
$prLog->print('-kind' => 'E',
'-str' => ["strange format in lock file <$lockFile>, " .
"line is <$pid>"],
'-exit' => 1)
unless ($pid =~ /\A\d+\Z/o);
if (kill(0, $pid) == 1) # alte Instanz luft noch
{
$prLog->print('-kind' => 'E',
'-str' => ["cannot start, old instance with pid " .
"<$pid> is allready running"],
'-exit' => 1);
}
else
{
$prLog->print('-kind' => 'I',
'-str' => ["removing old lock file of process <$pid>"]
);
}
}
$prLog->print('-kind' => 'I',
'-str' => ["creating lock file <$lockFile>"]);
&::checkDelSymLink($lockFile, $prLog, 0x01);
sysopen(FILE, $lockFile, O_WRONLY | O_CREAT | O_EXCL) or
$prLog->print('-kind' => 'E',
'-str' => ["cannot create lock file <$lockFile>"],
'-exit' => 1);
print FILE "$$\n";
close(FILE);
}
############################################################
sub calcMD5
{
my $filename = shift;
my $prLog = shift;
local *FROM;
unless (sysopen(FROM, $filename, O_RDONLY))
{
return undef;
}
my $md5 = Digest::MD5->new();
my $buffer;
while (sysread(FROM, $buffer, 1024*1024))
{
$md5->add($buffer);
}
close(FROM);
return $md5->hexdigest();
}
############################################################
# Objekt kann zum (wiederholten) Abfragen von Informationen
# ber eine Datei verwendet werden.
# Liefert: alles von stat, md5sum
############################################################
package singleFileInfo;
##################################################
sub new
{
my ($class) = shift;
my ($self) = {};
my (%params) = ('-filename' => undef,
'-prLog' => undef
);
&::checkObjectParams(\%params, \@_, 'singleFileInfo::new',
['-filename', '-prLog']);
&::setParamsDirect($self, \%params);
my (@statStruct) = (stat($params{'-filename'}));
$self->{'stat'} = \@statStruct;
bless $self, $class;
}
##################################################
sub getFilename
{
my $self = shift;
return $self->{'filename'};
}
##################################################
sub getInfo
{
my $self = shift;
my (%params) = ('-kind' => undef
);
&::checkObjectParams(\%params, \@_, 'singleFileInfo::getInfo',
['-kind']);
my $kind = $params{'-kind'};
my $prLog = $self->{'prLog'};
if ($kind eq 'md5')
{
if (defined $self->{'md5'})
{
return $self->{'md5'};
}
my $f = forkProc->new('-exec' => 'md5sum',
'-stdout' => "/tmp/out.$$",
'-stderr' => "/tmp/err.$$",
'-param' => [$self->{'filename'}],
'-prLog' => $prLog);
$f->wait();
my $x = $f->getSTDERR();
if (@$x > 0)
{
$prLog->print('-kind' => 'E',
'-str' => ["md5sum " . $self->{'filename'} .
" generated the following error " .
"message, exiting:", @$x],
'-exit' => 1);
}
$x = $f->getSTDOUT();
if (@$x != 1)
{
$prLog->print('-kind' => 'E',
'-str' => ["md5sum " . $self->{'filename'} .
" generated incorrect output " .
"exiting:", @$x],
'-exit' => 1);
}
# Filtern der md5 Summe
my ($md5) = $$x[0] =~ /^(\w+)/;
$self->{'md5'} = $md5;
return $md5;
}
my (%kind) = ('inode' => 1, # index ist von stat
'mode' => 2,
'nlink' => 3,
'uid' => 4,
'gid' => 5,
'size' => 7,
'atime' => 8,
'mtime' => 9,
'ctime' => 10);
return undef unless (defined $kind{$kind});
return $self->{'stat'}[$kind{$kind}];
}
############################################################
# Liefert directories, files und symbolic links
package recursiveReadDir;
########################################
sub new
{
my $class = shift;
my $self = {};
my (%params) = ('-dirs' => [],# zu durchsuchende dirs
'-exceptDirs' => [],# zu berspringende dirs
'-includeDirs' => [],# except all but these dirs
# if empty, ignore this option
'-followLinks' => 0, # nicht folgen, wenn 1, dann
# in erster Ebene folgen, mehr
# geht bisher nicht
'-prLog' => undef,
'-prLogError' => 'E',
'-prLogWarn' => 'W',
'-exitIfError' => 1, # Errorcode bei Fehler
'-verbose' => undef,
'-ignoreReadError' => 'no', # no, yes, onlyPrintMessage
'-printDepth' => 'no',
'-printDepthPrlogKind' => 'I'
);
&::checkObjectParams(\%params, \@_, 'recursiveReadDir::new',
['-dirs', '-prLog']);
&::setParamsDirect($self, \%params);
@{$self->{'files'}} = (); # in 'dirs' und 'files' werden die
# files bzw. dirs abgelegt, die noch
# auszuliefern bzw. zu durchsuchen sind
@{$self->{'types'}} = (); # Typ der Datei: 'f', 'd' oder 'l'
# nach Optionen von test -f, etc.
my $e;
my %except;
foreach $e (@{$params{'-exceptDirs'}})
{
$e = &::absolutePath($e);
$except{$e} = 1;
}
$self->{'except'} = \%except;
my %include;
foreach $e (@{$params{'-includeDirs'}})
{
$e = &::absolutePath($e);
$include{$e} = 1;
}
$self->{'include'} = \%include;
my @depths;
for ($e = 0 ; $e < @{$self->{'dirs'}} ; $e++)
{
push @depths, 0; # Initalwert, wichtig falls 'followLinks' > 0
}
$self->{'depths'} = \@depths;
$self->{'printedDepth'} = -1;
bless $self, $class;
}
########################################
sub next
{
my $self = shift;
my $dirs = $self->{'dirs'};
while (@{$self->{'files'}} == 0 and @$dirs > 0)
{
$self->readDir();
}
if (@{$self->{'files'}} > 0)
{
my $f = shift @{$self->{'files'}};
my $t = shift @{$self->{'types'}};
#print "--> $t $f\n";
return ($f, $t);
}
return ();
}
########################################
sub readDir
{
my $self = shift;
my $prLog = $self->{'prLog'};
my $prLogErr = $self->{'prLogError'};
my $prLogWarn = $self->{'prLogWarn'};
my $exit = $self->{'exitIfError'};
my $dirs = $self->{'dirs'};
my $dir = shift @$dirs;
my $depths = $self->{'depths'};
my $depth = shift @$depths;
my $files = $self->{'files'};
my $types = $self->{'types'};
my $except = $self->{'except'};
my $include = $self->{'include'};
my $includeDirs = $self->{'includeDirs'};
my $ignoreReadError = $self->{'ignoreReadError'};
if ($self->{'printDepth'} eq 'yes' and defined $depth
and $self->{'printedDepth'} != $depth)
{
$self->{'printedDepth'} = $depth;
$prLog->print('-kind' => $self->{'printDepthPrlogKind'},
'-str' => ["reading directories at depth $depth"]);
}
return unless $dir;
if (@$includeDirs)
{
my $ignore = 1;
if (exists $$include{$dir}) # if directly
{ # an included dir
$ignore = 0;
}
else
{
my $i;
foreach $i (@$includeDirs)
{
$i = &::absolutePath($i);
if (&::isSubDir($dir, $i)) # on the way to includeDir
{
# get all and only includeDirs to which I'm on the way
my (%yetGot) = (); # avoid duplicates
my $id;
foreach $id (@$includeDirs)
{
my $id = &::absolutePath($id);
next
unless &::isSubDir($dir, $id);
my $next = &::substractPath($id, $dir);
($next) = split(/\/+/, $next);
if ($dir eq '/')
{
$next = "/$next";
}
else
{
$next = $dir . '/' . $next;
}
next if exists $yetGot{$next};
$yetGot{$next} = $next;
push @$files, $next;
push @$types, 'd';
push @$dirs, $next;
push @$depths, ($depth + 1);
}
last;
}
elsif (&::isSubDir($i, $dir)) # inside includDir
{
$ignore = 0;
last;
}
}
}
return if $ignore;
}
local *DIR;
unless (opendir(DIR, $dir))
{
if ($ignoreReadError eq 'no')
{
$prLog->print('-kind' => $prLogErr,
'-str' =>
["cannot opendir <$dir>, exiting",
"check your source and clean your backup!"],
'-exit' => $exit);
}
elsif ($ignoreReadError eq 'onlyPrintMessage')
{
$prLog->print('-kind' => $prLogErr,
'-str' => ["cannot opendir <$dir>"]);
return;
}
else
{
return;
}
}
my $entry;
my @notPlainFiles;
while ($entry = readdir DIR)
{
next if ($entry eq '.' or $entry eq '..');
$entry = $dir . '/' . $entry;
if (-l $entry)
{
if ($self->{'followLinks'} > $depth and
-d "$entry/.")
{
if (exists $$except{&::absolutePath($entry)})
{
$prLog->print('-kind' => 'I',
'-str' => ["ignoring directory <$entry>"])
if $self->{'verbose'};
push @$files, $entry;
push @$types, 'd';
next;
}
push @$files, $entry;
push @$types, 'd';
push @$dirs, $entry;
push @$depths, ($depth + 1);
}
else
{
push @$files, $entry;
push @$types, 'l';
}
next;
}
unless (-r $entry)
{
$prLog->print('-kind' => $prLogWarn,
'-str' => ["no permissions to read <$entry>"]);
next;
}
if (-d $entry) # Dieses Directory mu beim Kopieren
{ # z.B. mu angelegt werden!
push @$files, $entry;
push @$types, 'd';
next if exists $$except{&::absolutePath($entry)};
push @$dirs, $entry;
push @$depths, ($depth + 1);
next;
}
if (-f $entry)
{
push @$files, $entry;
push @$types, 'f';
next;
}
if (-p $entry) # named pipe
{
push @$files, $entry;
push @$types, 'p';
next;
}
if (-S $entry) # socket
{
push @$files, $entry;
push @$types, 'S';
next;
}
if (-b $entry) # block special file
{
push @$files, $entry;
push @$types, 'b';
next;
}
if (-c $entry) # character special file
{
push @$files, $entry;
push @$types, 'c';
next;
}
$prLog->print('-kind' => $prLogWarn,
'-str' => ["unsupported file type for <$entry>"]);
}
closedir DIR;
}
############################################################
# Lscht directories, liefert Anzahl Dateien und Gre zurck
package recursiveDelDir;
########################################
sub new
{
my $class = shift;
my $self = {};
my (%params) = ('-dir' => undef, # einzelne Datei ist auch mglich
'-prLog' => undef);
&::checkObjectParams(\%params, \@_, 'recursiveDelDir::new',
['-dir', '-prLog']);
$self->{'prLog'} = $params{'-prLog'};
$self->{'dirs'} = 0; # hier wurde ein Directory gelscht
$self->{'files'} = 0; # hier wurde eine Datei gelscht
$self->{'bytes'} = 0; # hier wurde eine Datei gelscht,
# Datei hatte entsprechend bytes
$self->{'links'} = 0; # hier wurde nur ein Link weggenommen
$self->{'stayBytes'} = 0; # hier wurde nur ein Link weggenommen
# Anzahl Bytes bleiben bestehen
my $dir = $params{'-dir'};
my $ret = bless $self, $class;
if (-d $dir and not -l $dir) # ist ein Directory
{
$self->_oneDir($dir);
}
else
{
$self->_delFile($dir, $self->{'prLog'});
}
return $ret;
}
########################################
sub getStatistics
{
my $self = shift;
return ($self->{'dirs'},
$self->{'files'},
$self->{'bytes'},
$self->{'links'},
$self->{'stayBytes'});
}
########################################
sub _oneDir
{
my $self = shift;
my ($aktDir) = shift;
my $prLog = $self->{'prLog'};
unless (-w $aktDir)
{
if (chmod(0700, $aktDir) != 1)
{
$prLog->print('-kind' => 'E',
'-str' => ["no permissions to delete <$aktDir"]);
return;
}
}
local *DIR;
unless (opendir(DIR, $aktDir))
{
$prLog->print('-kind' => 'E',
'-str' => ["cannot opendir <$aktDir>"]);
return;
}
my ($e, @dirs);
while ($e = readdir DIR)
{
next if ($e eq '.' or $e eq '..');
$e = "$aktDir/$e";
push @dirs, $self->_delFile($e, $prLog);
}
closedir(DIR) or
$prLog->print('-kind' => 'E',
'-str' => ["cannot closedir <$aktDir>"]);;
foreach $e (@dirs)
{
$self->_oneDir($e);
}
unless (rmdir $aktDir)
{
$prLog->print('-kind' => 'E',
'-str' => ["cannot delete directory <$aktDir>"]);
}
else
{
++$self->{'dirs'};
}
}
########################################
sub _delFile
{
my $self = shift;
my $e = shift; # zu lschende Datei
my $prLog = shift;
if (-l $e)
{
my ($nlink, $size) = (lstat($e))[3,7];
unless (unlink $e)
{
$prLog->print('-kind' => 'E',
'-str' => ["cannot delete symlink <$e>"]);
next;
}
if ($nlink == 1)
{
$self->{'bytes'} += $size;
++$self->{'files'};
}
else
{
$self->{'stayBytes'} += $size;
++$self->{'links'};
}
}
elsif (-d $e)
{
return ($e);
}
else
{
my ($nlink, $size) = (stat($e))[3,7];
unless (unlink $e)
{
$prLog->print('-kind' => 'E',
'-str' => ["cannot delete <$e>"]);
next;
}
if ($nlink == 1)
{
$self->{'bytes'} += $size;
++$self->{'files'};
}
else
{
$self->{'stayBytes'} += $size;
++$self->{'links'};
}
}
return ();
}
1
|