AnyEvent-MCP
view release on metacpan or search on metacpan
if ($! == EEXIST) {
$file .= "~";
} elsif ($! == ENAMETOOLONG) {
substr $file, (length $file) * .75, 2, "~";
} else {
return undef, "open tempfile: $!"
}
}
};
# deliberately self-contained: patch validation must not load host modules.
my $check_patch_output = sub {
my ($status, $output, @max_offset) = @_;
my (@hunks, @unparsed);
for (split /\n/, $output) {
if (/^Hunk #(\d+) (succeeded|FAILED|ignored) at (-?\d+)(?: with fuzz (\d+))?(?: \(offset (-?\d+) lines?\))?\.\z/) {
push @hunks, [0 + $1, $2 eq "succeeded", 0 + $4, 0 + $5];
} elsif (!length
|| /^Hmm\.\.\. +Looks like a unified diff to me\.\.\.\z/
|| /^The text leading up to this was:\z/
|| /^-+\z/ || /^\|/
|| /^patching file / || /^checking file /
|| /^Using Plan [AB]\.\.\.\z/ || /^done\z/) {
# GNU patch --verbose framing.
} else {
push @unparsed, $_;
}
}
$status == 0
or die "patch failed (wait status $status):\n$output";
@unparsed == 0
or die "patch: unrecognized output:\n" . (join "\n", @unparsed) . "\n";
@hunks == @max_offset && @hunks
or die "patch: expected " . @max_offset . " hunks, got " . @hunks . "\n";
for my $i (0 .. $#hunks) {
my ($number, $success, $fuzz, $offset) = @{$hunks[$i]};
$number == $i + 1
or die "patch: unexpected hunk number $number\n";
$success
or die "patch: hunk $number failed\n";
!$fuzz
or die "patch: hunk $number used fuzz $fuzz\n";
abs ($offset) <= $max_offset[$i]
or die "patch: hunk $number offset $offset exceeds $max_offset[$i]\n";
}
};
my $run_patch = sub {
my ($newname, $path, $diff) = @_;
my ($errfh, $errname) = $create_tempfile->("/tmp/mcp-fs~~patch~~");
$errfh
or return undef, $errname;
local $ENV{LC_ALL} = "C";
open my $write, "|-", "sh", "-c",
q{exec "$4" --unified --verbose --binary --batch --forward --get=0 --fuzz=0 --reject-file=- --no-backup-if-mismatch -o "$2" "$1" >"$3" 2>&1},
"--", $path, $newname, $errname, $patch
or return undef, "open patch pipe: $!";
my $write_error;
{
print $write $diff
or $write_error = "write patch: $!";
close $write;
}
my $status = $?;
unlink $errname
or return undef, "unlink patch stderr: $!";
defined sysseek $errfh, 0, 0
or return undef, "seek patch stderr: $!";
my $output = do { local $/; <$errfh> };
$output = "" unless defined $output;
$output =~ s/\s+\z//;
return undef, $write_error
if $write_error and !$status;
$status, $output
};
my $replace_file = sub {
my ($path, $before, $newfh, $newname) = @_;
chown $before->[4], $before->[5], $newfh;
chmod $before->[2], $newfh;
my @after = lstat $path;
@after
or return undef, "lstat: $!";
(join "\0", @$before[0, 1, 2, 3, 4, 5, 7, 9, 10])
eq (join "\0", @after[0, 1, 2, 3, 4, 5, 7, 9, 10])
or return undef, "file changed while applying patch";
my @new = stat $newfh;
$after[3] == 1
or return undef, "refusing to replace multiply-linked file";
(join "\0", @after[2, 4, 5]) eq (join "\0", @new[2, 4, 5])
or return undef, "cannot preserve file metadata";
rename $newname, $path
or return undef, "rename: $!";
$new[7], undef
};
my @before = lstat $path;
@before
or return 0, "lstat: $!";
-f _
or return 0, "not a regular file";
my ($newfh, $newname) = $create_tempfile->("$path~~new~~");
$newfh
or return 0, $newname;
( run in 1.637 second using v1.01-cache-2.11-cpan-99fa8243a42 )