diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2019-11-26 14:00:30 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2019-11-26 14:00:30 +0300 |
commit | 414ea1706306e061fc44a8b5ce3042d4f0728489 (patch) | |
tree | ef0b2c4eac79e479ed686a5d88d7b3b954717824 /scripts/Dpkg/Source/Functions.pm | |
parent | ed2b463626bd721942143baa6207f2ccac67a616 (diff) | |
parent | 89afa9af7cd589eb8384ed96b6d86dd59d56bdf5 (diff) | |
download | dpkg-414ea1706306e061fc44a8b5ce3042d4f0728489.tar.gz |
Merge https://salsa.debian.org/dpkg-team/dpkg
Diffstat (limited to 'scripts/Dpkg/Source/Functions.pm')
-rw-r--r-- | scripts/Dpkg/Source/Functions.pm | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/scripts/Dpkg/Source/Functions.pm b/scripts/Dpkg/Source/Functions.pm index 0a940463e..3435f6c5a 100644 --- a/scripts/Dpkg/Source/Functions.pm +++ b/scripts/Dpkg/Source/Functions.pm @@ -22,6 +22,7 @@ our $VERSION = '0.01'; our @EXPORT_OK = qw( erasedir fixperms + chmod_if_needed fs_time is_binary ); @@ -70,6 +71,18 @@ sub fixperms { subprocerr("chmod -R -- $modes_set $dir") if $?; } +# Only change the pathname permissions if they differ from the desired. +# +# To be able to build a source tree, a user needs write permissions on it, +# but not necessarily ownership of those files. +sub chmod_if_needed { + my ($newperms, $pathname) = @_; + my $oldperms = (stat $pathname)[2] & 07777; + + return 1 if $oldperms == $newperms; + return chmod $newperms, $pathname; +} + # Touch the file and read the resulting mtime. # # If the file doesn't exist, create it, read the mtime and unlink it. @@ -97,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; |