Functions.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 fs_time is_binary);
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Gettext;
  21. use Dpkg::IPC;
  22. use POSIX qw(:errno_h);
  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. # Touch the file and read the resulting mtime.
  59. #
  60. # If the file doesn't exist, create it, read the mtime and unlink it.
  61. #
  62. # Use this instead of time() when the timestamp is going to be
  63. # used to set file timestamps. This avoids confusion when an
  64. # NFS server and NFS client disagree about what time it is.
  65. sub fs_time($) {
  66. my ($file) = @_;
  67. my $is_temp = 0;
  68. if (not -e $file) {
  69. open(my $temp_fh, '>', $file) or syserr(_g('cannot write %s'));
  70. close($temp_fh);
  71. $is_temp = 1;
  72. } else {
  73. utime(undef, undef, $file) or
  74. syserr(_g('cannot change timestamp for %s'), $file);
  75. }
  76. stat($file) or syserr(_g('cannot read timestamp from %s'), $file);
  77. my $mtime = (stat(_))[9];
  78. unlink($file) if $is_temp;
  79. return $mtime;
  80. }
  81. sub is_binary($) {
  82. my ($file) = @_;
  83. # TODO: might want to reimplement what diff does, aka checking if the
  84. # file contains \0 in the first 4Kb of data
  85. # Use diff to check if it's a binary file
  86. my $diffgen;
  87. my $diff_pid = spawn(
  88. exec => [ 'diff', '-u', '--', '/dev/null', $file ],
  89. env => { LC_ALL => 'C', LANG => 'C', TZ => 'UTC0' },
  90. to_pipe => \$diffgen,
  91. );
  92. my $result = 0;
  93. local $_;
  94. while (<$diffgen>) {
  95. if (m/^(?:binary|[^-+\@ ].*\bdiffer\b)/i) {
  96. $result = 1;
  97. last;
  98. } elsif (m/^[-+\@ ]/) {
  99. $result = 0;
  100. last;
  101. }
  102. }
  103. close($diffgen) or syserr('close on diff pipe');
  104. wait_child($diff_pid, nocheck => 1, cmdline => "diff -u -- /dev/null $file");
  105. return $result;
  106. }
  107. 1;