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