blib/lib/Lchmod.pm

filter: Statementstmt 98.2% 57 / 58 Branchbran 81.8% 18 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 94.5% 87 / 92 CC 12 SCAR 12.0
CC
12
Cov
94%
lchmod
6.0
import
4.0
_get_errno_func_not_impl
3.0
CRAP 3.3
↑ Summary
1 package Lchmod;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use FFI::Me;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 $Lchmod::VERSION = '0.02';
8  
9 2 my $LCHMOD_AVAILABLE = 0;
10 0 ▶sub LCHMOD_AVAILABLE { return 1 if $LCHMOD_AVAILABLE; return; }
Subroutine LCHMOD_AVAILABLE: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if $LCHMOD_AVAILABLE 16 0 16
11  
12 5 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
13 5 shift;
14 5 my $caller = caller;
15  
16 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 2 5
17  
18 # TODO ?: warn if @_ contains someting other than lchmod or LCHMOD_AVAILABLE
19  
20 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ *{ $caller . "::lchmod" } = \&lchmod if grep m/^lchmod$/, @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep /^lchmod$/, @_ 2 1 3
22 3 ▶ *{ $caller . "::LCHMOD_AVAILABLE" } = \&LCHMOD_AVAILABLE if grep m/^LCHMOD_AVAILABLE$/, @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep /^LCHMOD_AVAILABLE$/, @_ 2 1 3
23 }
24 else {
25 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 *{ $caller . "::lchmod" } = \&lchmod;
27 }
28 }
29  
30 {
31 2 local $@;
32 2 eval { # 1st: try current process
33 2 ffi _sys_lchmod => (
34 rv => ffi::int,
35 arg => [ ffi::str, ffi::int ],
36 sym => 'lchmod',
37 );
38 };
39 2 ▶ $LCHMOD_AVAILABLE = 1 if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 2 0 2
40 }
41  
42 # ? needed ?
43 # if (!$LCHMOD_AVAILABLE) {
44 # local $@;
45 # eval { # 2nd: try explicit libc
46 # ffi _sys_lchmod => (
47 # lib => $^O eq 'darwin' ? 'libc.dylib' : 'libc.so', # or whatever
48 # rv => ffi::int,
49 # arg => [ffi::str,ffi::int],
50 # sym => 'lchmod',
51 # );
52 # };
53 # $LCHMOD_AVAILABLE = 1 if !$@;
54 # }
55  
56 14 ▶sub lchmod {
Subroutine lchmod: called CC=6 SCAR=17.9
Pod documented
57 14 my ( $mode, @files ) = @_;
58  
59 14 ▶ if ( !LCHMOD_AVAILABLE() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (LCHMOD_AVAILABLE()) 1 13 14
60 1 $! = _get_errno_func_not_impl(); # ENOSYS
61 1 return undef; ## no critic
62 }
63  
64 13 my $count = 0;
65 13 my $normalized_mode = sprintf( "%04o", $mode & 07777 ); ## no critic
66  
67 13 for my $path (@files) {
68 17 ▶ if ( -l $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $path) { } 6 11 17
69 6 _sys_lchmod( $path, oct($normalized_mode) );
70 }
71 else {
72 11 chmod( oct($normalized_mode), $path );
73 }
74  
75 17 my $current_mode = ( lstat($path) )[2];
76 17 ▶ next if !$current_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_mode 2 15 17
77  
78 15 $current_mode = sprintf( "%04o", $current_mode & 07777 ); ## no critic
79 15 ▶ $count++ if $current_mode eq $normalized_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $current_mode eq $normalized_mode 9 6 15
80 }
81  
82 13 return $count;
83 }
84  
85 1 ▶sub _get_errno_func_not_impl {
Subroutine _get_errno_func_not_impl: called CC=3 SCAR=11.1
86  
87 # we don't want to load POSIX but if its there we want to use it
88 # CONSTANTs are weird when not defined so we have to:
89  
90 1 local $^W = 0;
91 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 ▶ no strict; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 my $posix = POSIX::ENOSYS;
94 return
95 1 ▶ $posix ne 'POSIX::ENOSYS' ? POSIX::ENOSYS
Branch: 2 branches
BranchTrueFalseTotal
$posix ne 'POSIX::ENOSYS' ? : 0 1 1
$^O =~ /linux/i ? : 1 0 1
96 : $^O =~ /linux/i ? 38
97 : 78;
98 }
99  
100 2 1;
101  
102 __END__
↑ Summary