lib/Chj/xhome.pm

filter: Statementstmt 38.8% 26 / 67 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 46.6% 7 / 15 Podpod 50.0% 4 / 8 totaltotal 27.0% 37 / 137 CC 22 SCAR 29.1
CC
22
Cov
23%
xcheck_home
42.0
xsafehome
30.0
xchecked_home
20.0
CRAP 18.3
« .htmlgen/FunctionalPerl/Htmlgen/Sourcelang.pm ↑ Summary lib/Chj/Util/AskYN.pm »
1 #
2 # Copyright (c) 2010-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 Chj::xhome
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 Get the user's home directory in a safe manner.
19  
20 =over 4
21  
22 =item xHOME ()
23  
24 Just the $HOME env var, dieing if not set, and also checked against a
25 couple assertments.
26  
27 In taint mode, is tainted.
28  
29 =item xeffectiveuserhome ()
30  
31 Just the getpwuid setting. Throws unimplemented exception on Windows
32 (Raspberry, not Cygwin Perl).
33  
34 Is not tainted.
35  
36 =item xsafehome ()
37  
38 Always take xeffectiveuserhome (unless on Windows, in which case this
39 is currently the same as xhome), but is asserting that HOME is the
40 same if set.
41  
42 Is untainted except on Windows where it's tainted.
43  
44 =item xhome ()
45  
46 Tries $ENV{HOME} then glob "~" if exists (with assertments), otherwise
47 xeffectiveuserhome.
48  
49 In taint mode, can be (and usually is) tainted.
50  
51 =back
52  
53 =head1 NOTE
54  
55 This is alpha software! Read the status section in the package README
56 or on the L<website|http://functional-perl.org/>.
57  
58 =cut
59  
60 package Chj::xhome;
61 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 2 our @EXPORT = qw(xhome);
67 2 our @EXPORT_OK = qw(xHOME
68 xeffectiveuserhome
69 xsafehome);
70 2 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
71  
72 2 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 # use File::HomeDir qw(home);
76  
77 # But File::HomeDir is not installed with either Cygwin or Strawberry
78 # Perl. Also, HomeDir's `home` returns undef for non-existing paths.
79  
80 0 ▶sub xcheck_home {
Subroutine xcheck_home: not called CC=6 SCAR=37.4
Pod undocumented
81 0 my ($home) = @_;
82 0 ▶ length($home) or die "environment variable HOME is the empty string";
Branch: 1 branch
BranchTrueFalseTotal
length $home or die "environment variable HOME is the empty string" 0 0 0
83 0 ▶ $home or die "environment variable HOME is false";
Branch: 1 branch
BranchTrueFalseTotal
$home or die "environment variable HOME is false" 0 0 0
84 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
85 0 ▶ $home =~ m|^[a-z]+:|i # XX correct letter syntax?
Branch: 1 branch
BranchTrueFalseTotal
$home =~ /^[a-z]+:/i or die "environment variable HOME does not start with a drive designator: '${home}'" 0 0 0
86 or die
87 "environment variable HOME does not start with a drive designator: '$home'";
88 } else {
89 0 ▶ $home =~ m|^/|
Branch: 1 branch
BranchTrueFalseTotal
$home =~ m[^/] or die "environment variable HOME does not start with a slash: '${home}'" 0 0 0
90 or die
91 "environment variable HOME does not start with a slash: '$home'";
92 }
93 }
94  
95 0 ▶sub xHOME {
Subroutine xHOME: not called CC=3 SCAR=24.8
Pod documented
96 0 ▶ @_ == 0 or fp_croak_arity 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 or fp_croak_arity(0) 0 0 0
97 defined(my $home = $ENV{HOME})
98 0 ▶ or die "environment variable HOME is not set";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $home = $ENV{"HOME"}) or die "environment variable HOME is not set" 0 0 0
99 0 xcheck_home $home;
100 0 $home
101 }
102  
103 0 ▶sub xeffectiveuserhome {
Subroutine xeffectiveuserhome: not called CC=3 SCAR=24.8
Pod documented
104  
105 0 ▶ @_ == 0 or fp_croak_arity 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 or fp_croak_arity(0) 0 0 0
106 0 __ "is untainted";
107  
108 # (Don't bother about caching, premature opt & dangerous.)
109 0 my $uid = $>;
110 my (
111 0 ▶ $name, $passwd, $_uid, $gid, $quota,
Branch: 1 branch
BranchTrueFalseTotal
() = getpwuid $uid or die "unknown user for uid $uid" 0 0 0
112 $comment, $gcos, $dir, $shell, $expire
113 )
114 = getpwuid $uid
115 or die "unknown user for uid $uid";
116 0 $dir
117 }
118  
119 0 ▶sub xsafehome {
Subroutine xsafehome: not called CC=5 SCAR=34.0
Pod documented
120 0 ▶ @_ == 0 or fp_croak_arity 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 or fp_croak_arity(0) 0 0 0
121 0 __ "is untainted except on Windows where it's tainted";
122 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
123  
124 # XX or how to look it up on Windows again? If implemented, update pod.
125 0 xhome()
126 } else {
127 0 my $effectiveuserhome = xeffectiveuserhome;
128 0 ▶ if (my $e = $ENV{HOME}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $ENV{"HOME"}) 0 0 0
129 0 ▶ $e eq $effectiveuserhome
Branch: 1 branch
BranchTrueFalseTotal
$e eq $effectiveuserhome or die 'HOME environment variable is set to something other ' . "than the effective user home: '${e}' vs. '${effectiveuserhome}'" 0 0 0
130 or die "HOME environment variable is set to something other "
131 . "than the effective user home: '$e' vs. '$effectiveuserhome'";
132 }
133 $effectiveuserhome
134 0 }
135 }
136  
137 2 our $warned = 0;
138  
139 0 ▶sub xchecked_home {
Subroutine xchecked_home: not called CC=4 SCAR=30.0
Pod undocumented
140 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
141 0 my ($home, $what) = @_;
142 0 xcheck_home $home;
143 0 ▶ if (-d $home) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $home) { } 0 0 0
144 0 $home
145 } else {
146 0 ▶ warn "$what: dir '$home' does not exist, falling back to getpwuid"
Branch: 1 branch
BranchTrueFalseTotal
unless $Chj::xhome::warned++ 0 0 0
147 unless $warned++;
148 undef
149 0 }
150 }
151  
152 0 ▶sub maybe_HOME {
Subroutine maybe_HOME: not called CC=2 SCAR=17.9
Pod undocumented
153 0 __ "is tainted";
154 0 ▶ if (my $home = $ENV{HOME}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $home = $ENV{'HOME'}) { } 0 0 0
155 0 xchecked_home $home, '$ENV{HOME}'
156 } else {
157 undef
158 0 }
159 }
160  
161 0 ▶sub maybe_globhome {
Subroutine maybe_globhome: not called CC=2 SCAR=12.2
Pod undocumented
162 0 __ "is tainted";
163 0 ▶ my ($home) = glob "~";
Subroutine BEGIN: called CC=1 SCAR=0.2
164 0 ▶ if (defined $home) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $home) { } 0 0 0
165 0 xchecked_home $home, "glob '~'";
166 } else {
167 undef
168 0 }
169 }
170  
171 0 ▶sub xhome {
Subroutine xhome: not called CC=4 SCAR=30.0
Pod documented
172 0 ▶ @_ == 0 or fp_croak_arity 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 or fp_croak_arity(0) 0 0 0
173 0 ▶ maybe_HOME() // maybe_globhome() // xeffectiveuserhome()
Condition0%0/6
(maybe_HOME() // maybe_globhome()) // xeffectiveuserhome()
l!l&&r!l&&!r
000
maybe_HOME() // maybe_globhome()
l!l&&r!l&&!r
000
MC/DC0%0/3
(maybe_HOME() // maybe_globhome()) // xeffectiveuserhome()
maybe_HOME() maybe_globhome() xeffectiveuserhome()
Truth table0%0/4
A // B // C
A= maybe_HOME()B= maybe_globhome()C= xeffectiveuserhome()
ABCresult
1XX1
01X1
0011
0000
174 }
175  
176 2 1
« .htmlgen/FunctionalPerl/Htmlgen/Sourcelang.pm ↑ Summary lib/Chj/Util/AskYN.pm »