Functions.pm 3.7 KB

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