[LDAPsh-cvs] ldapsh ldapsh,1.20,1.20.8.1
Status: Beta
Brought to you by:
rcorvalan
From: <j-d...@us...> - 2003-07-27 07:47:28
|
Update of /cvsroot/ldapsh/ldapsh In directory sc8-pr-cvs1:/tmp/cvs-serv4795 Modified Files: Tag: PATCH-778369 ldapsh Log Message: The "vi" command now supports editing! Index: ldapsh =================================================================== RCS file: /cvsroot/ldapsh/ldapsh/ldapsh,v retrieving revision 1.20 retrieving revision 1.20.8.1 diff -C2 -d -r1.20 -r1.20.8.1 *** ldapsh 27 Jul 2003 07:03:01 -0000 1.20 --- ldapsh 27 Jul 2003 07:47:25 -0000 1.20.8.1 *************** *** 1415,1440 **** - =head3 vi - - B<Synopsis>: C<vi E<lt>expansionE<gt>> - - Show the entries returned by the expansion (see L<Expansion|expansion>) in a vi editor, in read-only mode. Any changes made to the file has no effect on LDAP entries. - - =cut - - sub vi { - my $entries = _entriesExpander undef, @_; - return 0 unless defined $entries; - - require Net::LDAP::LDIF; - require File::Temp; - my (undef, $tempfile) = File::Temp::tempfile('LDAPShell_vi_XXXXXXXXXX', SUFFIX => '.ldif'); - Net::LDAP::LDIF->new($tempfile,"w")->write(@$entries); - system($Globals->{EDITOR}{VALUE}, $tempfile); - unlink $tempfile; - return $entries; - } - - =head3 dump --- 1415,1418 ---- *************** *** 1603,1606 **** --- 1581,1680 ---- =head2 Changing entries + + =head3 vi + + B<Synopsis>: C<vi E<lt>expansionE<gt>> + + Show the entries matched by the expansion (see L<Expansion|expansion>) in a text editor. + Entries are saved to a temporary file for editing in the LDIF format. + If the temporary file is modified, it will be re-read as an LDIF file and used to modify + the search results. Any changes can be viewed using L<changes|changes> and will need to + be committed using L<commit|commit>. + + =cut + + sub vi { + my $entries = _entriesExpander undef, @_; + return 0 unless defined $entries; + + require Net::LDAP::LDIF; + require File::Temp; + + my (undef, $tempfile) = File::Temp::tempfile('LDAPShell_vi_XXXXXXXXXX', SUFFIX => '.ldif'); + Net::LDAP::LDIF->new($tempfile,"w")->write(@$entries); + my $mtime = (stat($tempfile))[9]; + + system($Globals->{EDITOR}{VALUE}, $tempfile); + + # If modified, load as LDIF entry and compare to the same DNs in our directory (if they exist). + if ( (stat($tempfile))[9] > $mtime ) { + my $ldif = Net::LDAP::LDIF->new($tempfile,"r"); + my %hash = map { $_->dn => $_ } @$entries; + my $examined = 0; + while (not $ldif->eof()) { + my $entry = $ldif->read_entry(); + if ($ldif->error()) { + print STDERR "Encountered error reading LDIF format.\n\n"; + last; + } elsif (defined($entry)) { + $examined++; + print STDERR "Comparing " . $entry->dn . "..."; + if (defined $hash{$entry->dn}) { + my $original = $hash{$entry->dn}; + my %attrs = map { lc $_ => 1 } ($original->attributes(), $entry->attributes()); # unique keys + foreach my $attr (sort(keys %attrs)) { + my $pre_values = $original->get_value($attr, asref => 1 ); + my $post_values = $entry->get_value($attr, asref => 1 ); + if ($pre_values && $post_values) { + if (scalar(@$pre_values) != scalar(@$post_values)) { + foreach my $value ($post_values) { + $original->replace($attr => $value); + } + } + else { # try harder + my @a = sort @$pre_values; + my @b = sort @$post_values; + foreach my $i (0 .. scalar(@a)-1) { + if ($a[$i] ne $b[$i]) { + foreach my $value ($post_values) { + $original->replace($attr => $value); + } + last; + } + } + } + } + elsif ($pre_values) { + $original->delete($attr); + } + elsif ($post_values) { + foreach my $value ($post_values) { + $original->add($attr => $value); + } + } + } + + my $changes = $original->{changes}; + if (defined($changes) and scalar(@$changes)) { + print STDERR "changed\n\n"; + } + else { + print STDERR "no modifications.\n\n"; + } + } + else { + print STDERR "skipped.\n"; + print STDERR "Warning: cannot change a DN or add an entry using vi.\n\n"; + } + } + } + if ($examined != scalar(@$entries)) { + print STDERR "Warning: some entries were absent from the edited file.\n\n"; + } + } + unlink $tempfile; + return $entries; + } + =head3 change |