fix set/badip so that it appends to the existing file
[spider.git] / perl / DXCIDR.pm
index 88a26d5c552631e4b66c173eb0c3262400e17d0d..5a6272aa4747e0c813b96431b5932b5df22f1516 100644 (file)
@@ -88,14 +88,13 @@ sub append
        
        if ($suffix) {
                my $fn = _fn() . ".$suffix";
-               my $r = rand;
-               my $fh = IO::File->new (">>$fn.$r");
-               if ($fh) {
+               my $fh = IO::File->new;
+               if ($fh->open("$fn", "a+")) {
+                       $fh->seek(0, 2);        # belt and braces !!
                        print $fh "$_\n" for @in;
                        $fh->close;
-                       move "$fn.$r", $fn;
                } else {
-                       LogDbg('err', "DXCIDR::append error appending to $fn.$r $!");
+                       LogDbg('err', "DXCIDR::append error appending to $fn $!");
                }
        } else {
                LogDbg('err', "DXCIDR::append require badip suffix");
@@ -202,10 +201,23 @@ sub reload
 {
        new();
 
-       my $count = _load('base');
-       $count += _load('local');
-
-       LogDbg('DXProt', "DXCIDR::reload $count ip addresses found (IPV4: $count4 IPV6: $count6)" );
+       my $count = 0;
+       my $files = 0;
+
+       LogDbg('DXProt', "DXCIDR::reload reload database" );
+
+       my $dir;
+       opendir($dir, $main::local_data);
+       while (my $fn = readdir $dir) {
+               next unless my ($suffix) = $fn =~ /^badip\.(\w+)$/;
+               my $c = _load($suffix);
+               LogDbg('DXProt', "DXCIDR::reload: $fn read containing $c ip addresses" );
+               $count += $c;
+               $files++;
+       }
+       closedir $dir;
+       
+       LogDbg('DXProt', "DXCIDR::reload $count ip addresses found (IPV4: $count4 IPV6: $count6) in $files badip files" );
 
        return $count;
 }