fix: Escape percent signs in plaintext scripts for RPM spec

Scripts (preinst, postinst, prerm, postrm) may contain literal '%'
characters (e.g., in printf format strings). When these scripts are
written directly into the RPM spec file, rpmbuild interprets '%' as
the start of a macro, causing build failures.
This commit is contained in:
Anton Farygin
2025-12-23 10:45:41 +03:00
committed by Syed Shahrukh Hussain
parent 8bba6dfdc5
commit 5836229bae

View File

@@ -483,22 +483,22 @@ sub prep {
if ($this->usescripts) { if ($this->usescripts) {
if ($this->preinst) { if ($this->preinst) {
print OUT "\%pre\n"; print OUT "\%pre\n";
print OUT $this->preinst."\n"; print OUT escape_percents($this->preinst)."\n";
print OUT "\n"; print OUT "\n";
} }
if ($this->postinst) { if ($this->postinst) {
print OUT "\%post\n"; print OUT "\%post\n";
print OUT $this->postinst."\n"; print OUT escape_percents($this->postinst)."\n";
print OUT "\n"; print OUT "\n";
} }
if ($this->prerm) { if ($this->prerm) {
print OUT "\%preun\n"; print OUT "\%preun\n";
print OUT $this->prerm."\n"; print OUT escape_percents($this->prerm)."\n";
print OUT "\n"; print OUT "\n";
} }
if ($this->postrm) { if ($this->postrm) {
print OUT "\%postun\n"; print OUT "\%postun\n";
print OUT $this->postrm."\n"; print OUT escape_percents($this->postrm)."\n";
print OUT "\n"; print OUT "\n";
} }
} }
@@ -659,6 +659,7 @@ sub _script_helper {
$this->{$script} = $value; $this->{$script} = $value;
} }
$this->{$script} = shift if @_; $this->{$script} = shift if @_;
return $this->{$script};
# get # get
return unless defined wantarray; # optimization return unless defined wantarray; # optimization
@@ -694,6 +695,11 @@ sub prerm {
my $this=shift; my $this=shift;
$this->_script_helper('prerm', @_); $this->_script_helper('prerm', @_);
} }
sub escape_percents {
my $s=shift;
$s =~ s/\%/\%\%/g;
return $s;
}
=item arch =item arch