Masato Taruishi
taru****@users*****
2004年 10月 30日 (土) 21:21:14 JST
=================================================================== RCS file: utils/misc/ldifdiff/ldifdiff,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- utils/misc/ldifdiff/ldifdiff 2004/10/30 06:34:55 1.10 +++ utils/misc/ldifdiff/ldifdiff 2004/10/30 12:21:14 1.11 @@ -12,7 +12,7 @@ =head1 DESCRIPTION -B<ldifdiff> compares two ldif files and prints the difference +B<ldifdiff> compares two ldif(5) files and prints the difference in the format of the reduced slapd.replog(5) for ldapmodify(1). If you modify the directory which consists of entries specified @@ -23,7 +23,7 @@ =head1 SEE ALSO -slapd.replog(5), ldapmodify(1) +slapd.replog(5), ldapmodify(1), ldif(5) =head1 AUTHOR @@ -32,6 +32,7 @@ =cut use strict; +use MIME::Base64; use Getopt::Long; use File::Temp qw/ tempfile /; @@ -52,7 +53,7 @@ } sub debug { -# print STDERR join('\n', @_ ); +# print STDERR join('\n', @_ ); } GetOptions( @@ -64,94 +65,121 @@ my $buf = ""; my $e = ""; while ( <$in> ) { - if ( $_ eq "\n" ) { - goto BREAK; - } - if( $_ =~ /^([^:]+):/ ) { - $buf = $buf . $_ if ! $exclude_attr{$1}; - } else { - if ( $_ =~ /^\s+(.*)/ ) { - chomp $buf; - $buf = $buf . $1; + if ( !( $_ =~ /^#/ )) { + if ( $_ eq "\n" ) { + goto BREAK; + } + if( $_ =~ /^([^:]+):/ ) { + $buf = $buf . $_ if ! $exclude_attr{$1}; + } else { + if ( $_ =~ /^\s+(.*)/ ) { + chomp $buf; + $buf = $buf . $1; + } } } } BREAK: - foreach (split /\n/, $buf ) { - if( $_ =~ /^([^:]+):<\s*(.*)/ ) { - # TODO: handle :< attribute value - die "format ':<' not supported yet" + return $buf; +} + +sub decode { + my $buf = shift; + my $dec = ""; + foreach (split(/\n/, $buf) ) { + if( $_ =~ /(\S+)::\s*(.*)/ ) { + $dec = $dec . $1 . ": " . decode_base64( $2 ); } else { - $e = $e . $_; + $dec = $dec . $_; } - $e = $e . "\n"; + $dec = $dec . "\n"; } - return $e; + return join("\n", sort ( split /\n/, $dec )); } sub modify { - ( my $oldentry, my $newentry, my $dn, my $modfh ) = @_; + ( my $oldentry, my $newentry, my $oldentry_decode, my $newentry_decode, my $dn, my $modfh ) = @_; debug "different: $dn\n"; - debug "$oldentry\n"; - debug "$newentry\n"; + debug "$oldentry_decode\n"; + debug "$newentry_decode\n"; my %oldattr = (); + my %oldattr_decode = (); my %newattr = (); + my %newattr_decode = (); print $modfh "dn: $dn\n"; - print $modfh "changetype: modify\n"; + print $modfh "changetype: modify\n"; - foreach (split /\n/, $oldentry) { - if ( $_ =~ /(([^:]+)(:+)\s*.+)/ ) { - $oldattr{$2} = "" if ! $oldattr{$2}; - $oldattr{$2} = $oldattr{$2} . $1; - } else { - print STDERR "Unsupported ldif format: $_\n"; - } - } + foreach (split /\n/, $oldentry) { + if ( $_ =~ /(([^:]+)(:+)\s*.+)/ ) { + $oldattr{$2} = "" if ! $oldattr{$2}; + $oldattr{$2} = $oldattr{$2} . $1; + } else { + print STDERR "Unsupported ldif format: $_\n"; + } + } - foreach (split /\n/, $newentry) { - if ( $_ =~ /(([^:]+)(:+)\s*.+)/ ) { - $newattr{$2} = "" if ! $newattr{$2}; - $newattr{$2} = $newattr{$2} . $1; - } else { - print STDERR "Unsupported ldif format: $_\n"; - } - } + foreach (split /\n/, $oldentry_decode) { + if ( $_ =~ /(([^:]+)(:+)\s*.+)/ ) { + $oldattr_decode{$2} = "" if ! $oldattr_decode{$2}; + $oldattr_decode{$2} = $oldattr_decode{$2} . $1; + } else { + print STDERR "Unsupported ldif format: $_\n"; + } + } - foreach (keys %oldattr) { - if( ! $newattr{$_} ) { - debug "attr delete: $_\n"; - print $modfh "delete: $_\n"; - print "-\n"; - } else { - if( !($oldattr{$_} eq $newattr{$_}) ) { - debug "attr modify: $_ -> $newattr{$_}\n"; - print $modfh "replace: $_\n"; - foreach my $v (split /\n/, $newattr{$_}) { - print $modfh "$v\n"; - } - print $modfh "-\n"; - } - } - delete $oldattr{$_}; - delete $newattr{$_}; - } - - foreach (keys %newattr) { - debug "attr add: $_\n"; - print $modfh "add: $_\n"; + foreach (split /\n/, $newentry) { + if ( $_ =~ /(([^:]+)(:+)\s*.+)/ ) { + $newattr{$2} = "" if ! $newattr{$2}; + $newattr{$2} = $newattr{$2} . $1; + } else { + print STDERR "Unsupported ldif format: $_\n"; + } + } + + foreach (split /\n/, $newentry_decode) { + if ( $_ =~ /(([^:]+)(:+)\s*.+)/ ) { + $newattr_decode{$2} = "" if ! $newattr_decode{$2}; + $newattr_decode{$2} = $newattr_decode{$2} . $1; + } else { + print STDERR "Unsupported ldif format: $_\n"; + } + } + + foreach (keys %oldattr) { + if( ! $newattr{$_} ) { + debug "attr delete: $_\n"; + print $modfh "delete: $_\n"; + print "-\n"; + } else { + if( !($oldattr_decode{$_} eq $newattr_decode{$_}) ) { + debug "attr modify: $_ -> $newattr{$_}\n"; + print $modfh "replace: $_\n"; foreach my $v (split /\n/, $newattr{$_}) { print $modfh "$v\n"; } print $modfh "-\n"; - delete $newattr{$_}; } + } + delete $oldattr{$_}; + delete $newattr{$_}; + } + + foreach (keys %newattr) { + debug "attr add: $_\n"; + print $modfh "add: $_\n"; + foreach my $v (split /\n/, $newattr{$_}) { + print $modfh "$v\n"; + } + print $modfh "-\n"; + delete $newattr{$_}; + } - print $modfh "\n"; + print $modfh "\n"; } if ( $#ARGV != 1 ) { @@ -163,7 +191,9 @@ my $newfile = $ARGV[1]; my %oldentry = (); +my %oldentry_decode = (); my %newentry = (); +my %newentry_decode = (); (my $modfh, my $modfile) = tempfile(); @@ -195,23 +225,34 @@ # Check wether the current entry exists both if ( $newentry{$odn} && $oldentry{$odn} ) { - if ( ! ("$newentry{$odn}" eq "$oldentry{$odn}") ) { - modify($oldentry{$odn}, $newentry{$odn}, $odn, $modfh); + debug "checking $odn\n"; + $oldentry_decode{$odn} = decode($oldentry{$odn}) if ! $oldentry_decode{$odn}; + + $newentry_decode{$odn} = decode($newentry{$odn}) if ! $newentry_decode{$odn} ; + if ( ! ("$newentry_decode{$odn}" eq "$oldentry_decode{$odn}") ) { + modify($oldentry{$odn}, $oldentry_decode{$odn}, $newentry{$odn}, + $newentry_decode{$odn}, $odn, $modfh); } else { debug "same: $odn\n"; } delete $oldentry{$odn}; + delete $oldentry_decode{$odn}; delete $newentry{$odn}; + delete $newentry_decode{$odn}; } if ( $newentry{$ndn} && $oldentry{$ndn} ) { - if ( ! ("$newentry{$ndn}" eq "$oldentry{$ndn}") ) { - modify($oldentry{$ndn}, $newentry{$ndn}, $ndn, $modfh); + $oldentry_decode{$ndn} = decode($oldentry{$ndn}) if ! $oldentry_decode{$ndn} ; + $newentry_decode{$ndn} = decode($newentry{$ndn}) if ! $newentry_decode{$ndn} ; + if ( ! ("$newentry_decode{$ndn}" eq "$oldentry_decode{$ndn}") ) { + modify($oldentry{$ndn}, $newentry{$ndn}, $oldentry_decode{$ndn}, $newentry_decode{$ndn}, $ndn, $modfh); } else { debug "same: $ndn\n"; } delete $oldentry{$ndn}; + delete $oldentry_decode{$ndn}; delete $newentry{$ndn}; + delete $newentry_decode{$ndn}; } }