Dpkg/Source/Functions.pm

filter: Statementstmt 76.8% 53 / 69 Branchbran 42.8% 12 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 5 totaltotal 66.3% 77 / 116 CC 17 SCAR 17.5
CC
17
Cov
67%
chmod_if_needed
12.0
erasedir
10.5
fixperms
6.0
CRAP 5.8
« Dpkg/Source/Format.pm ↑ Summary Dpkg/ErrorHandling.pm »
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  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Dpkg::Source::Functions - miscellaneous source package handling functions
21  
22 =head1 DESCRIPTION
23  
24 This module provides a set of miscellaneous helper functions to handle
25 source packages.
26  
27 B<Note>: This is a private module, its API can change at any time.
28  
29 =cut
30  
31 package Dpkg::Source::Functions 0.01;
32  
33 10 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 8 our @EXPORT_OK = qw(
36 erasedir
37 fixperms
38 chmod_if_needed
39 fs_time
40 is_binary
41 );
42  
43 10 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 10 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 10 ▶use Fcntl qw(:mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 10 ▶use File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 10 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 10 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 10 ▶use Dpkg::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 10 ▶use Dpkg::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 ▶sub erasedir {
Subroutine erasedir: called CC=6 SCAR=23.5
Pod undocumented
54 2 my $dir = shift;
55 2 ▶ if (not lstat($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (lstat $dir) 0 2 2
56 0 ▶ return if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 0 0 0
57 0 syserr(g_('cannot stat directory %s (before removal)'), $dir);
58 }
59 2 system 'rm', '-rf', '--', $dir;
60 2 ▶ subprocerr("rm -rf $dir") if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 2 2
61 2 ▶ if (not stat($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (stat $dir) 2 0 2
62 2 ▶ return if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 2 0 2
63 0 syserr(g_("cannot check for removal of directory '%s'"), $dir);
64 }
65 0 error(g_("cannot remove directory '%s' with '%s' command"), $dir, 'rm -rf');
66 }
67  
68 1 ▶sub fixperms {
Subroutine fixperms: called CC=6 SCAR=17.9
Pod undocumented
69 1 my $dir = shift;
70 1 my ($mode, $modes_set);
71 # Unfortunately tar insists on applying our umask _to the original
72 # permissions_ rather than mostly-ignoring the original
73 # permissions. We fix it up with chmod -R (which saves us some
74 # work) but we have to construct a u+/- string which is a bit
75 # of a palaver. (Numeric does not work because we need [ugo]+X
76 # and [ugo]=<stuff> does not work because that unsets sgid on dirs.)
77 1 $mode = 0o777 & ~umask;
78 1 for my $i (0 .. 2) {
79 3 ▶ $modes_set .= ',' if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 2 1 3
80 3 $modes_set .= qw(u g o)[$i];
81 3 for my $j (0 .. 2) {
82 9 ▶ $modes_set .= $mode & (0o400 >> ($i * 3 + $j)) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 256 >> $i * 3 + $j ? : 7 2 9
83 9 $modes_set .= qw(r w X)[$j];
84 }
85 }
86 1 system('chmod', '-R', '--', $modes_set, $dir);
87 1 ▶ subprocerr("chmod -R -- $modes_set $dir") if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 1 1
88 }
89  
90 # Only change the pathname permissions if they differ from the desired.
91 #
92 # To be able to build a source tree, a user needs write permissions on it,
93 # but not necessarily ownership of those files.
94 0 ▶sub chmod_if_needed {
Subroutine chmod_if_needed: not called CC=3 SCAR=24.8
Pod undocumented
95 0 my ($newperms, $pathname) = @_;
96 0 my $st = File::stat::stat($pathname);
97 0 ▶ return 0 if ! defined $st;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $st 0 0 0
98 0 my $oldperms = S_IMODE($st->mode);
99  
100 0 ▶ return 1 if $oldperms == $newperms;
Branch: 1 branch
BranchTrueFalseTotal
if $oldperms == $newperms 0 0 0
101 0 return chmod $newperms, $pathname;
102 }
103  
104 # Touch the file and read the resulting mtime.
105 #
106 # If the file does not exist, create it, read the mtime and unlink it.
107 #
108 # Use this instead of time() when the timestamp is going to be
109 # used to set file timestamps. This avoids confusion when an
110 # NFS server and NFS client disagree about what time it is.
111 4 ▶sub fs_time {
Subroutine fs_time: called CC=4 SCAR=14.2
Pod undocumented
112 4 my $file = shift;
113 4 my $is_temp = 0;
114 4 ▶ if (not -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 4 0 4
115 4 $is_temp = 1;
116 }
117 4 file_touch($file);
118 4 ▶ my $st = File::stat::stat($file)
Branch: 1 branch
BranchTrueFalseTotal
my $st = &File::stat::stat($file) or syserr(g_("cannot read timestamp from %s"), $file) 4 0 4
119 or syserr(g_('cannot read timestamp from %s'), $file);
120 4 ▶ unlink($file) if $is_temp;
Branch: 1 branch
BranchTrueFalseTotal
if $is_temp 4 0 4
121 4 return $st->mtime;
122 }
123  
124 0 ▶sub is_binary {
Subroutine is_binary: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my $file = shift;
126  
127 # Perform the same check as diff(1), look for a NUL character in the first
128 # 4 KiB of the file.
129 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or syserr(g_("cannot open file %s for binary detection"), $file) 0 0 0
130 or syserr(g_('cannot open file %s for binary detection'), $file);
131 0 read $fh, my $buf, 4096, 0;
132 0 my $res = index $buf, "\0";
133 0 close $fh;
134  
135 0 return $res >= 0;
136 }
137  
138 =head1 CHANGES
139  
140 =head2 Version 0.xx
141  
142 This is a private module.
143  
144 =cut
145  
146 8 1;
« Dpkg/Source/Format.pm ↑ Summary Dpkg/ErrorHandling.pm »