Functions.pm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; either version 2 of the License, or
  4. # (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. package Dpkg::Source::Functions;
  14. use strict;
  15. use warnings;
  16. use base qw(Exporter);
  17. our @EXPORT_OK = qw(erasedir fixperms is_binary);
  18. use Dpkg::ErrorHandling;
  19. use Dpkg::Gettext;
  20. use Dpkg::IPC;
  21. use POSIX;
  22. sub erasedir {
  23. my ($dir) = @_;
  24. if (not lstat($dir)) {
  25. return if $! == ENOENT;
  26. syserr(_g("cannot stat directory %s (before removal)"), $dir);
  27. }
  28. system 'rm','-rf','--',$dir;
  29. subprocerr("rm -rf $dir") if $?;
  30. if (not stat($dir)) {
  31. return if $! == ENOENT;
  32. syserr(_g("unable to check for removal of dir `%s'"), $dir);
  33. }
  34. error(_g("rm -rf failed to remove `%s'"), $dir);
  35. }
  36. sub fixperms {
  37. my ($dir) = @_;
  38. my ($mode, $modes_set, $i, $j);
  39. # Unfortunately tar insists on applying our umask _to the original
  40. # permissions_ rather than mostly-ignoring the original
  41. # permissions. We fix it up with chmod -R (which saves us some
  42. # work) but we have to construct a u+/- string which is a bit
  43. # of a palaver. (Numeric doesn't work because we need [ugo]+X
  44. # and [ugo]=<stuff> doesn't work because that unsets sgid on dirs.)
  45. $mode = 0777 & ~umask;
  46. for ($i = 0; $i < 9; $i += 3) {
  47. $modes_set .= ',' if $i;
  48. $modes_set .= qw(u g o)[$i/3];
  49. for ($j = 0; $j < 3; $j++) {
  50. $modes_set .= $mode & (0400 >> ($i+$j)) ? '+' : '-';
  51. $modes_set .= qw(r w X)[$j];
  52. }
  53. }
  54. system('chmod', '-R', '--', $modes_set, $dir);
  55. subprocerr("chmod -R -- $modes_set $dir") if $?;
  56. }
  57. sub is_binary($) {
  58. my ($file) = @_;
  59. # Use diff to check if it's a binary file
  60. my $diffgen;
  61. my $diff_pid = fork_and_exec(
  62. 'exec' => [ 'diff', '-u', '--', '/dev/null', $file ],
  63. 'env' => { LC_ALL => 'C', LANG => 'C', TZ => 'UTC0' },
  64. 'to_pipe' => \$diffgen
  65. );
  66. my $result = 0;
  67. while (<$diffgen>) {
  68. if (m/^binary/i) {
  69. $result = 1;
  70. last;
  71. } elsif (m/^[-+\@ ]/) {
  72. $result = 0;
  73. last;
  74. }
  75. }
  76. close($diffgen) or syserr("close on diff pipe");
  77. wait_child($diff_pid, nocheck => 1, cmdline => "diff -u -- /dev/null $file");
  78. return $result;
  79. }
  80. # vim: set et sw=4 ts=8
  81. 1;