summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillem Jover <guillem@debian.org>2018-12-24 03:05:05 +0100
committerGuillem Jover <guillem@debian.org>2019-01-15 03:42:39 +0100
commit39eb793a685273f520b25179bf118f8845ece0f6 (patch)
tree6c5ad397c42ee8d9ef98b73faaf5afd5ebc69ae4
parente326eda15c84d0456aa2e1c22c996e89ef6c40f2 (diff)
downloaddpkg-39eb793a685273f520b25179bf118f8845ece0f6.tar.gz
Dpkg::File: Make file_slurp() also accept pathnames in addition to filehandles
This makes several call sites more clear, as we move the logic inside the function.
-rw-r--r--debian/changelog2
-rw-r--r--scripts/Dpkg/File.pm16
-rw-r--r--scripts/Dpkg/Source/Package/V2.pm7
-rwxr-xr-xscripts/dpkg-genchanges.pl5
-rw-r--r--scripts/t/Dpkg_Changelog.t4
-rw-r--r--utils/t/update_alternatives.t7
6 files changed, 23 insertions, 18 deletions
diff --git a/debian/changelog b/debian/changelog
index 9a6e7a47d..64380eb07 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -4,6 +4,8 @@ dpkg (1.19.3) UNRELEASED; urgency=medium
* Perl modules:
- Dpkg::Changelog::Debian: Preserve modelines at EOF. Closes: #916056
Thanks to Chris Lamb <lamby@debian.org> for initial test cases.
+ - Dpkg::File: Make file_slurp() also accept pathnames in addition to
+ filehandles.
* Documentation:
- dpkg(1): Clarify --remove action. Closes: #914478
- dpkg-query(1): Clarify --list option behavior when no arguments are
diff --git a/scripts/Dpkg/File.pm b/scripts/Dpkg/File.pm
index 884923852..6ba49a6e6 100644
--- a/scripts/Dpkg/File.pm
+++ b/scripts/Dpkg/File.pm
@@ -25,12 +25,26 @@ our @EXPORT = qw(
);
use Exporter qw(import);
+use Scalar::Util qw(openhandle);
+
+use Dpkg::ErrorHandling;
+use Dpkg::Gettext;
sub file_slurp {
- my $fh = shift;
+ my $file = shift;
+ my $fh;
+ my $doclose = 0;
+ if (openhandle($file)) {
+ $fh = $file;
+ } else {
+ open $fh, '<', $file or syserr(g_('cannot read %s'), $fh);
+ $doclose = 1;
+ }
local $/;
my $data = <$fh>;
+ close $fh if $doclose;
+
return $data;
}
diff --git a/scripts/Dpkg/Source/Package/V2.pm b/scripts/Dpkg/Source/Package/V2.pm
index fe9a1727d..e2c1b49fb 100644
--- a/scripts/Dpkg/Source/Package/V2.pm
+++ b/scripts/Dpkg/Source/Package/V2.pm
@@ -591,12 +591,8 @@ sub _get_patch_header {
unless (-f $ph) {
$ph = File::Spec->catfile($dir, 'debian', 'source', 'patch-header');
}
- my $text;
if (-f $ph) {
- open(my $ph_fh, '<', $ph) or syserr(g_('cannot read %s'), $ph);
- $text = file_slurp($ph_fh);
- close($ph_fh);
- return $text;
+ return file_slurp($ph);
}
my $ch_info = changelog_parse(offset => 0, count => 1,
file => File::Spec->catfile($dir, 'debian', 'changelog'));
@@ -613,6 +609,7 @@ it.\n";
$header->{'Author'} = $ch_info->{'Maintainer'};
my $yyyy_mm_dd = POSIX::strftime('%Y-%m-%d', gmtime);
+ my $text;
$text = "$header";
run_vendor_hook('extend-patch-header', \$text, $ch_info);
$text .= "\n---
diff --git a/scripts/dpkg-genchanges.pl b/scripts/dpkg-genchanges.pl
index 13ab8a453..690a4e59d 100755
--- a/scripts/dpkg-genchanges.pl
+++ b/scripts/dpkg-genchanges.pl
@@ -415,10 +415,7 @@ foreach (keys %{$changelog}) {
}
if ($changesdescription) {
- open(my $changes_fh, '<', $changesdescription)
- or syserr(g_('cannot read %s'), $changesdescription);
- $fields->{'Changes'} = "\n" . file_slurp($changes_fh);
- close($changes_fh);
+ $fields->{'Changes'} = "\n" . file_slurp($changesdescription);
}
for my $p (keys %p2f) {
diff --git a/scripts/t/Dpkg_Changelog.t b/scripts/t/Dpkg_Changelog.t
index f487ff560..4d046fec1 100644
--- a/scripts/t/Dpkg_Changelog.t
+++ b/scripts/t/Dpkg_Changelog.t
@@ -41,9 +41,7 @@ foreach my $file ("$datadir/countme", "$datadir/shadow", "$datadir/fields",
my $changes = Dpkg::Changelog::Debian->new(verbose => 0);
$changes->load($file);
- open(my $clog_fh, '<', "$file") or die "can't open $file\n";
- my $content = file_slurp($clog_fh);
- close($clog_fh);
+ my $content = file_slurp($file);
cmp_ok($content, 'eq', "$changes", "string output of Dpkg::Changelog on $file");
my $errors = $changes->get_parse_errors();
diff --git a/utils/t/update_alternatives.t b/utils/t/update_alternatives.t
index 491fee07d..84e2080da 100644
--- a/utils/t/update_alternatives.t
+++ b/utils/t/update_alternatives.t
@@ -21,6 +21,7 @@ use Test::More;
use File::Spec;
use Dpkg::IPC;
+use Dpkg::File qw(file_slurp);
use Dpkg::Path qw(find_command);
my $srcdir = $ENV{srcdir} || '.';
@@ -260,11 +261,7 @@ check_choice(0, 'auto', 'initial install 3');
# verify that the administrative file is sorted properly
{
- local $/ = undef;
- open(my $db_fh, '<', "$admindir/generic-test") or die $!;
- my $content = <$db_fh>;
- close($db_fh);
-
+ my $content = file_slurp("$admindir/generic-test");
my $expected =
"auto
$bindir/generic-test