summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorGuillem Jover <guillem@debian.org>2018-09-16 23:42:08 +0200
committerGuillem Jover <guillem@debian.org>2018-10-08 11:46:36 +0200
commitffe3d7aeed7e7eb9a1c22d45fd0e5c2af2c8bcad (patch)
tree05526f1a5804ea6fb0cea8a983c97a2cf251359f /scripts
parentb89c8fd35c09df0df61f3424a630241bf4d79111 (diff)
downloaddpkg-ffe3d7aeed7e7eb9a1c22d45fd0e5c2af2c8bcad.tar.gz
Dpkg::Source::Functions: Reimplement is_binary() w/o using diff(1)
The check is very simple, and can be done w/o requiring calling diff(1) for each input file. This makes the code shorter, more portable, and should be faster in the non-binary cases.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/Dpkg/Source/Functions.pm31
1 files changed, 8 insertions, 23 deletions
diff --git a/scripts/Dpkg/Source/Functions.pm b/scripts/Dpkg/Source/Functions.pm
index 262b80ad1..3435f6c5a 100644
--- a/scripts/Dpkg/Source/Functions.pm
+++ b/scripts/Dpkg/Source/Functions.pm
@@ -110,30 +110,15 @@ sub fs_time($) {
sub is_binary($) {
my $file = shift;
- # TODO: might want to reimplement what diff does, aka checking if the
- # file contains \0 in the first 4Kb of data
+ # Perform the same check as diff(1), look for a NUL character in the first
+ # 4 KiB of the file.
+ open my $fh, '<', $file
+ or syserr(g_('cannot open file %s for binary detection'), $file);
+ read $fh, my $buf, 4096, 0;
+ my $res = index $buf, "\0";
+ close $fh;
- # Use diff to check if it's a binary file
- my $diffgen;
- my $diff_pid = spawn(
- exec => [ 'diff', '-u', '--', '/dev/null', $file ],
- env => { LC_ALL => 'C', LANG => 'C', TZ => 'UTC0' },
- to_pipe => \$diffgen,
- );
- my $result = 0;
- local $_;
- while (<$diffgen>) {
- if (m/^(?:binary|[^-+\@ ].*\bdiffer\b)/i) {
- $result = 1;
- last;
- } elsif (m/^[-+\@ ]/) {
- $result = 0;
- last;
- }
- }
- close($diffgen) or syserr('close on diff pipe');
- wait_child($diff_pid, nocheck => 1, cmdline => "diff -u -- /dev/null $file");
- return $result;
+ return $res >= 0;
}
1;