Functions.pm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. our $VERSION = "0.01";
  17. use base qw(Exporter);
  18. our @EXPORT_OK = qw(erasedir fixperms is_binary);
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Gettext;
  21. use Dpkg::IPC;
  22. use POSIX;
  23. sub erasedir {
  24. my ($dir) = @_;
  25. if (not lstat($dir)) {
  26. return if $! == ENOENT;
  27. syserr(_g("cannot stat directory %s (before removal)"), $dir);
  28. }
  29. system 'rm','-rf','--',$dir;
  30. subprocerr("rm -rf $dir") if $?;
  31. if (not stat($dir)) {
  32. return if $! == ENOENT;
  33. syserr(_g("unable to check for removal of dir `%s'"), $dir);
  34. }
  35. error(_g("rm -rf failed to remove `%s'"), $dir);
  36. }
  37. sub fixperms {
  38. my ($dir) = @_;
  39. my ($mode, $modes_set, $i, $j);
  40. # Unfortunately tar insists on applying our umask _to the original
  41. # permissions_ rather than mostly-ignoring the original
  42. # permissions. We fix it up with chmod -R (which saves us some
  43. # work) but we have to construct a u+/- string which is a bit
  44. # of a palaver. (Numeric doesn't work because we need [ugo]+X
  45. # and [ugo]=<stuff> doesn't work because that unsets sgid on dirs.)
  46. $mode = 0777 & ~umask;
  47. for ($i = 0; $i < 9; $i += 3) {
  48. $modes_set .= ',' if $i;
  49. $modes_set .= qw(u g o)[$i/3];
  50. for ($j = 0; $j < 3; $j++) {
  51. $modes_set .= $mode & (0400 >> ($i+$j)) ? '+' : '-';
  52. $modes_set .= qw(r w X)[$j];
  53. }
  54. }
  55. system('chmod', '-R', '--', $modes_set, $dir);
  56. subprocerr("chmod -R -- $modes_set $dir") if $?;
  57. }
  58. sub is_binary($) {
  59. my ($file) = @_;
  60. # TODO: might want to reimplement what diff does, aka checking if the
  61. # file contains \0 in the first 4Kb of data
  62. # Use diff to check if it's a binary file
  63. my $diffgen;
  64. my $diff_pid = spawn(
  65. 'exec' => [ 'diff', '-u', '--', '/dev/null', $file ],
  66. 'env' => { LC_ALL => 'C', LANG => 'C', TZ => 'UTC0' },
  67. 'to_pipe' => \$diffgen
  68. );
  69. my $result = 0;
  70. while (<$diffgen>) {
  71. if (m/^(?:binary|[^-+\@ ].*\bdiffer\b)/i) {
  72. $result = 1;
  73. last;
  74. } elsif (m/^[-+\@ ]/) {
  75. $result = 0;
  76. last;
  77. }
  78. }
  79. close($diffgen) or syserr("close on diff pipe");
  80. wait_child($diff_pid, nocheck => 1, cmdline => "diff -u -- /dev/null $file");
  81. return $result;
  82. }
  83. # vim: set et sw=4 ts=8
  84. 1;