]> Frank Brehm's Git Trees - my-stuff/postfix.git/commitdiff
Inhaltsvergleich der Dateien ausgeführt
authorFrank Brehm <frank@brehm-online.com>
Mon, 10 Nov 2008 12:28:19 +0000 (12:28 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 10 Nov 2008 12:28:19 +0000 (12:28 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/postfix@26 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

get-lookup-tables.pl

index 98ae51d31f2cd2e4dafa3f3ba2d1a650009d360f..76fc200820acf85d9c8b3687e14e8097c702ef52 100755 (executable)
@@ -152,6 +152,7 @@ sub perform_lookup {
     my $changed = 0;
 
     $changed = 1 unless -f $cur_file;
+    $changed = 1 if diff_files( $new_file, $cur_file, $map_type );
 
     if ( $changed ) {
 
@@ -177,6 +178,239 @@ sub perform_lookup {
 
 #--------------------------------------------------------------------------------------
 
+=head2 diff_files( $file1, $file2, $map_type )
+
+=cut
+
+sub diff_files {
+
+    my $file1 = shift;
+    my $file2 = shift;
+    my $map_type = shift;
+
+    fatal( "Keine Dateinamen zum Überprüfen übergeben." ) unless $file1 and $file2;
+    debug( 2, "Vergleiche Inhalte der Dateien '" . $file1 . "' und '" . $file2 . "'." );
+    return 1 unless -f $file1;
+    return 1 unless -f $file2;
+
+    my $content1 = read_lookup_file( $file1, $map_type );
+    my $content2 = read_lookup_file( $file2, $map_type );
+
+    return 1 unless $content1 and ref($content1) and ref($content1) eq 'ARRAY';
+    return 1 unless $content2 and ref($content2) and ref($content2) eq 'ARRAY';
+
+    return 1 unless scalar(@$content1) == scalar(@$content2);
+    my $length = scalar(@$content1);
+
+    my $i = 0;
+    my $they_are_different = 0;
+
+    for ( $i = 0; $i < $length; $i++ ) {
+
+        my $row1 = $content1->[$i];
+        my $row2 = $content2->[$i];
+
+        unless ( $row1 and ref($row1) and ref($row1) eq 'ARRAY' ) {
+            $they_are_different = 1;
+            last;
+        }
+
+        unless ( $row2 and ref($row2) and ref($row2) eq 'ARRAY' ) {
+            $they_are_different = 1;
+            last;
+        }
+
+        unless ( scalar(@$row1) == scalar(@$row2) ) {
+            $they_are_different = 1;
+            last;
+        }
+
+        if ( defined( $row1->[0] ) ) {
+            unless ( defined( $row2->[0] ) ) {
+                $they_are_different = 1;
+                last;
+            }
+        }
+        elsif ( defined( $row2->[0] ) ) {
+            $they_are_different = 1;
+            last;
+        }
+        else {
+            next;
+        }
+
+        unless ( $row1->[0] eq $row2->[0] ) {
+            $they_are_different = 1;
+            last;
+        }
+
+        my $values1 = $row1->[1];
+        my $values2 = $row2->[1];
+
+        if ( defined($values1) ) {
+            unless ( defined($values2) ) {
+                $they_are_different = 1;
+                last;
+            }
+        }
+        elsif ( defined($values2) ) {
+            $they_are_different = 1;
+            last;
+        }
+        else {
+            next;
+        }
+
+        unless ( $values1 and ref($values1) and ref($values1) eq 'ARRAY' ) {
+            $they_are_different = 1;
+            last;
+        }
+
+        unless ( $values2 and ref($values2) and ref($values2) eq 'ARRAY' ) {
+            $they_are_different = 1;
+            last;
+        }
+
+        unless ( scalar(@$values1) == scalar(@$values2) ) {
+            $they_are_different = 1;
+            last;
+        }
+        next unless scalar(@$values1);
+
+        my $vals_diff = 0;
+        VALUES: for ( my $j = 0; $j < scalar(@$values1); $j++ ) {
+
+            my $v1 = $values1->[$j];
+            my $v2 = $values2->[$j];
+
+            if ( defined($v1) ) {
+                unless ( defined($v2) ) {
+                    $vals_diff = 1;
+                    last VALUES;
+                }
+            }
+            elsif ( defined($v2) ) {
+                $vals_diff = 1;
+                last VALUES;
+            }
+            else {
+                next VALUES;
+            }
+
+            if ( $v1 ne $v2 ) {
+                $vals_diff = 1;
+                last VALUES;
+            }
+
+        }
+
+        if ( $vals_diff ) {
+            $they_are_different = 1;
+            last;
+        }
+
+    }
+
+    debug( 2, "Die beiden Dateien '" . $file1 . "' und '" . $file2 . "' sind " . ( $they_are_different ? 'UNTERSCHIEDLICH' : 'GLEICH' ) . "." );
+    return $they_are_different;
+
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 read_lookup_file( $filename, $map_type )
+
+=cut
+
+sub read_lookup_file {
+
+    my $file = shift;
+    my $map_type = shift;
+    fatal( "Kein Dateiname zum Einlesen übergeben." ) unless $file;
+
+    unless ( -f $file ) {
+        notice( "Datei '" . $file . "' existiert nicht." );
+        return undef;
+    }
+
+    my $content = [];
+
+    my $line = '';
+
+    debug( 3, "Lese Datei '" . $file . "' ..." );
+    unless ( open FILE, "<", $file ) {
+        fatal( "Konnte Datei '" . $file . "' nicht zum Lesen öffnen: " . $! );
+    }
+
+    while ( <FILE> ) {
+        next if /^\s*#/;
+        if ( /^\s*$/ ) {
+            push @$content, parse_line( $line, $map_type ) if $line;
+            $line = '';
+            next;
+        }
+        if ( /^\s/ ) {
+            if ( $line ) {
+                s/\s+$//;
+                $line .= $_;
+                next;
+            }
+            push @$content, parse_line( $_, $map_type );
+            next;
+        }
+        push @$content, parse_line( $line, $map_type ) if $line;
+        $line = $_;
+    }
+    push @$content, parse_line( $line, $map_type ) if $line;
+
+    close FILE;
+
+    debug( 3, "Gelesener Dateiinhalt: ", $content );
+    return $content;
+
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 parse_line( $line, $map_type )
+
+=cut
+
+sub parse_line {
+
+    my $line = shift;
+    my $map_type = shift;
+    return [] unless $line;
+
+    debug( 4, "Parse Zeile '" . $line . "' ..." );
+    $line =~ s/^\s+//;
+    $line =~ s/\s+$//;
+
+    my $res = [];
+
+    return [] if $line =~ /^\s*$/;
+
+    my ( $key, $rest );
+    my $match = $map_type eq 'aliases' ? '^(\\S+)\\s*:\\s*(\\S.*)' : '^(\\S+)\\s+(\\S.*)';
+    debug( 4, "Match-RegEx: '" . $match . "'." );
+
+    unless ( ( $key, $rest ) = $line =~ /$match/ ) {
+        return [];
+    }
+
+    push @$res, $key;
+
+    my $targets = [];
+    @$targets = split /\s*,\s*/, $rest;
+    push @$res, ( scalar( @$targets ) ? $targets : $rest );
+
+    debug( 4, "Geparste Zeile: ", $res );
+    return $res;
+
+}
+
+#--------------------------------------------------------------------------------------
+
 =head2 curtime( )
 
 =cut