Functions.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright © 2008-2010, 2012-2015 Guillem Jover <guillem@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Functions;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. our @EXPORT_OK = qw(
  20. erasedir
  21. fixperms
  22. fs_time
  23. is_binary
  24. );
  25. use Exporter qw(import);
  26. use POSIX qw(:errno_h);
  27. use Dpkg::ErrorHandling;
  28. use Dpkg::Gettext;
  29. use Dpkg::IPC;
  30. sub erasedir {
  31. my $dir = shift;
  32. if (not lstat($dir)) {
  33. return if $! == ENOENT;
  34. syserr(g_('cannot stat directory %s (before removal)'), $dir);
  35. }
  36. system 'rm', '-rf', '--', $dir;
  37. subprocerr("rm -rf $dir") if $?;
  38. if (not stat($dir)) {
  39. return if $! == ENOENT;
  40. syserr(g_("unable to check for removal of directory '%s'"), $dir);
  41. }
  42. error(g_("rm -rf failed to remove '%s'"), $dir);
  43. }
  44. sub fixperms {
  45. my $dir = shift;
  46. my ($mode, $modes_set);
  47. # Unfortunately tar insists on applying our umask _to the original
  48. # permissions_ rather than mostly-ignoring the original
  49. # permissions. We fix it up with chmod -R (which saves us some
  50. # work) but we have to construct a u+/- string which is a bit
  51. # of a palaver. (Numeric doesn't work because we need [ugo]+X
  52. # and [ugo]=<stuff> doesn't work because that unsets sgid on dirs.)
  53. $mode = 0777 & ~umask;
  54. for my $i (0 .. 2) {
  55. $modes_set .= ',' if $i;
  56. $modes_set .= qw(u g o)[$i];
  57. for my $j (0 .. 2) {
  58. $modes_set .= $mode & (0400 >> ($i * 3 + $j)) ? '+' : '-';
  59. $modes_set .= qw(r w X)[$j];
  60. }
  61. }
  62. system('chmod', '-R', '--', $modes_set, $dir);
  63. subprocerr("chmod -R -- $modes_set $dir") if $?;
  64. }
  65. # Touch the file and read the resulting mtime.
  66. #
  67. # If the file doesn't exist, create it, read the mtime and unlink it.
  68. #
  69. # Use this instead of time() when the timestamp is going to be
  70. # used to set file timestamps. This avoids confusion when an
  71. # NFS server and NFS client disagree about what time it is.
  72. sub fs_time($) {
  73. my $file = shift;
  74. my $is_temp = 0;
  75. if (not -e $file) {
  76. open(my $temp_fh, '>', $file) or syserr(g_('cannot write %s'));
  77. close($temp_fh);
  78. $is_temp = 1;
  79. } else {
  80. utime(undef, undef, $file) or
  81. syserr(g_('cannot change timestamp for %s'), $file);
  82. }
  83. stat($file) or syserr(g_('cannot read timestamp from %s'), $file);
  84. my $mtime = (stat(_))[9];
  85. unlink($file) if $is_temp;
  86. return $mtime;
  87. }
  88. sub is_binary($) {
  89. my $file = shift;
  90. # TODO: might want to reimplement what diff does, aka checking if the
  91. # file contains \0 in the first 4Kb of data
  92. # Use diff to check if it's a binary file
  93. my $diffgen;
  94. my $diff_pid = spawn(
  95. exec => [ 'diff', '-u', '--', '/dev/null', $file ],
  96. env => { LC_ALL => 'C', LANG => 'C', TZ => 'UTC0' },
  97. to_pipe => \$diffgen,
  98. );
  99. my $result = 0;
  100. local $_;
  101. while (<$diffgen>) {
  102. if (m/^(?:binary|[^-+\@ ].*\bdiffer\b)/i) {
  103. $result = 1;
  104. last;
  105. } elsif (m/^[-+\@ ]/) {
  106. $result = 0;
  107. last;
  108. }
  109. }
  110. close($diffgen) or syserr('close on diff pipe');
  111. wait_child($diff_pid, nocheck => 1, cmdline => "diff -u -- /dev/null $file");
  112. return $result;
  113. }
  114. 1;