Skip to content

Commit

Permalink
Item12225: Patch files matching by MD5 sig.
Browse files Browse the repository at this point in the history
The patch should be able to apply to multiple releases.  However since
Text::Patch doesn't do fuzzy patching, and we want this to be relatively
safe, verify the md5sum of each file before attempting to patch it.

Still need backup of the files to be patched, and some documentation
might be helpful.

git-svn-id: http://svn.foswiki.org/trunk@15960 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Nov 8, 2012
1 parent 48152e5 commit 0c1d126
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 34 deletions.
56 changes: 51 additions & 5 deletions PatchFoswikiContrib/lib/Foswiki/Configure/PatchFile.pm
Expand Up @@ -36,17 +36,28 @@ sub parsePatch {
}

my $foundPatch = 'summary';
my $md5 = 'na';

foreach my $line (@contents) {
if ( $line =~ /^diff --git (.*?)\s/ ) {
$foundPatch = _fixupFile($1);
if ( $line =~ /^##PATCH\s+([^\s]+)\s+(.*?)$/ ) {
$md5 = $1;
$foundPatch = _fixupFile($2);
next;
}
$patches{$foundPatch} .= $line;
$patches{$foundPatch}{$md5} .= $line;
}

return %patches;
}

sub _getMD5 {

my $filename = shift;
open( my $fh, '<', $filename ) or die "Can't open '$filename': $!";
binmode($fh);
return Digest::MD5->new->addfile($fh)->hexdigest;
}

sub _fixupFile {
my $patchFile = shift;

Expand Down Expand Up @@ -79,7 +90,7 @@ sub updateFile {
my $diff = shift;
my $write = shift;

return 'Not a file' unless ( -f $file );
return "$file is not a file" unless ( -f $file );

local $/ = undef;
open( my $fh, '<', $file ) || return "read of $file failed: $!";
Expand All @@ -103,9 +114,44 @@ sub updateFile {
$mode = $1;
chmod( $mode, "$file" );

return '';
return "Update successful for $file\n";
}

sub applyPatch {
my $root = shift;
my $patchRef = shift;

my $msgs = '';
my $match = 0;

foreach my $key ( keys %{$patchRef} ) {
next if ( $key eq 'summary' );
foreach my $md5 ( keys %{ $patchRef->{$key} } ) {

my $file = Foswiki::Configure::Util::mapTarget( $root, $key );
$msgs .= "Processing File $key, MD5 $md5 \n";

my $origMD5 = _getMD5($file);
next unless ( $origMD5 eq $md5 );
$msgs .= "MD5 Matched - applying patch.\n";
$match++;

my $rc =
Foswiki::Configure::PatchFile::updateFile( $file,
$patchRef->{$key}{$md5} );

$msgs .= "$rc.\n" if $rc;
}

}

$msgs =
($match)
? "$match files patched\n"
: "No files matched patch signatures\n";
return $msgs;

}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down
@@ -1,28 +1,22 @@

my $this = shift;

foreach my $key ( grep /^working.*\.patch/, keys %{ $this->{_manifest}} ) {
foreach my $key ( grep /^working.*\.patch/, keys %{ $this->{_manifest} } ) {

my $patchFile = Foswiki::Configure::Util::mapTarget( $this->{_root}, $key );

print STDERR "Mapped $key to $patchFile\n";
$this->log("Processing $patchFile\n");

require Foswiki::Configure::PatchFile;
my %result = Foswiki::Configure::PatchFile::parsePatch( $patchFile );
my %result = Foswiki::Configure::PatchFile::parsePatch($patchFile);

foreach my $key ( keys %result ) {
print STDERR "Processing $key\n";
next if ( $key eq 'summary' );
$this->log( $result{error} ) if ( $result{error} );
$this->log( $result{summary}{na} ) if ( $result{summary} );

my $file = Foswiki::Configure::Util::mapTarget( $this->{_root}, $key );
my $msgs =
Foswiki::Configure::PatchFile::applyPatch( $this->{_root}, \%result );

print STDERR "Patching $file with (($result{$key}))\n";

my $rc = Foswiki::Configure::PatchFile::updateFile( $file, $result{$key} );

die if $rc;

}
$this->log($msgs);

}

Expand Up @@ -10,9 +10,11 @@ use Error qw( :try );
use File::Temp();
use FindBin;
use File::Path qw(mkpath rmtree);
use Digest::MD5;

use Foswiki::Configure::Util ();
use Foswiki::Configure::PatchFile ();
use File::Copy qw( copy );

sub set_up {
my $this = shift;
Expand Down Expand Up @@ -94,8 +96,7 @@ Date: Mon Nov 5 05:07:25 2012 +0000
git-svn-id: http://svn.foswiki.org/trunk@15909 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
diff --git a/core/lib/Foswiki/Configure/Dependency.pm b/core/lib/Foswiki/Configure/Dependency.pm
index 135df1f..a13b03b 100644
##PATCH fdeeb7f236608b7792ad0845bf2279f9 lib/Foswiki/Configure/Dependency.pm
--- a/core/lib/Foswiki/Configure/Dependency.pm
+++ b/core/lib/Foswiki/Configure/Dependency.pm
@@ -220,7 +220,7 @@ sub studyInstallation {
Expand All @@ -107,8 +108,7 @@ index 135df1f..a13b03b 100644
}
last;
}
diff --git a/core/lib/Foswiki/Configure/UIs/EXTENSIONS.pm b/core/lib/Foswiki/Configure/UIs/EXTENSIONS.pm
index 2c95644..b355217 100755
##PATCH 76e28354522a6d6cccc76c66f99d2424 lib/Foswiki/Configure/UIs/EXTENSIONS.pm
--- a/core/lib/Foswiki/Configure/UIs/EXTENSIONS.pm
+++ b/core/lib/Foswiki/Configure/UIs/EXTENSIONS.pm
@@ -339,7 +339,7 @@ sub _rawExtensionRows {
Expand All @@ -126,22 +126,48 @@ DONE
$this->{tempdir} . '/TestFile.patch' );

foreach my $key ( keys %result ) {
print STDERR "KEY $key \n";
next if ( $key eq 'summary' );
foreach my $md5 ( keys %{ $result{$key} } ) {
print "MD5 $md5\n patch $result{$key}{$md5} \n";

my $savepath = $Foswiki::foswikiLibPath;
$Foswiki::foswikiLibPath = '/tmp';
my $file = Foswiki::Configure::Util::mapTarget( 'root', $key );
$Foswiki::foswikiLibPath = $savepath;
my $origFile = Foswiki::Configure::Util::mapTarget( '/tmp', $key );

my $rc =
Foswiki::Configure::PatchFile::updateFile( $file, $result{$key} );
my $savepath = $Foswiki::foswikiLibPath;
$Foswiki::foswikiLibPath = '/tmp/lib';
mkpath($Foswiki::foswikiLibPath);

$this->assert( !$rc, "Failed with $rc\n" );
my $file = Foswiki::Configure::Util::mapTarget( '/tmp', $key );
$Foswiki::foswikiLibPath = $savepath;

my ( $fv, $fp, $fn ) = File::Spec->splitpath( $file, 0 );
mkpath($fp);
copy( $origFile, $file );
my $origMD5 = _getMD5($origFile);
$this->assert( ( $origMD5 eq $md5 ), "$file $md5 ne $origMD5" );

print STDERR
"$key mapped to $file\n - Vol $fv, path $fp, name $fn \n";

my $rc =
Foswiki::Configure::PatchFile::updateFile( $file,
$result{$key}{$md5} );

$this->assert( !$rc, "Failed with $rc\n" );
}

}

}

sub _getMD5 {

my $filename = shift;
open( my $fh, '<', $filename ) or die "Can't open '$filename': $!";
binmode($fh);
return Digest::MD5->new->addfile($fh)->hexdigest;
}

sub _makefile {
my $path = shift;
my $file = shift;
Expand Down
7 changes: 3 additions & 4 deletions PatchFoswikiContrib/working/configure/patch/Item11267.patch
Expand Up @@ -11,8 +11,7 @@ Date: Mon Nov 5 05:07:25 2012 +0000

git-svn-id: http://svn.foswiki.org/trunk@15909 0b4bb1d4-4e5a-0410-9cc4-b2b747904278

diff --git a/core/lib/Foswiki/Configure/Dependency.pm b/core/lib/Foswiki/Configure/Dependency.pm
index 135df1f..a13b03b 100644
##PATCH fdeeb7f236608b7792ad0845bf2279f9 lib/Foswiki/Configure/Dependency.pm
--- lib/Foswiki/Configure/Dependency.pm 2012-11-07 23:46:56.378148979 -0500
+++ lib/Foswiki/Configure/Dependency.pm.new 2012-11-07 23:46:14.091649218 -0500
@@ -220,7 +220,7 @@
Expand All @@ -24,8 +23,8 @@ index 135df1f..a13b03b 100644
}
last;
}
diff --git a/core/lib/Foswiki/Configure/UIs/EXTENSIONS.pm b/core/lib/Foswiki/Configure/UIs/EXTENSIONS.pm
index 2c95644..b355217 100755

##PATCH 76e28354522a6d6cccc76c66f99d2424 lib/Foswiki/Configure/UIs/EXTENSIONS.pm
--- lib/Foswiki/Configure/UIs/EXTENSIONS.pm 2012-11-07 23:45:21.269274066 -0500
+++ lib/Foswiki/Configure/UIs/EXTENSIONS.pm.new 2012-11-07 23:46:39.844344573 -0500
@@ -280,7 +280,7 @@
Expand Down

0 comments on commit 0c1d126

Please sign in to comment.