lib/File/Valet.pm

filter: Statementstmt 55.7% 166 / 298 Branchbran 38.9% 60 / 154 Conditioncond 30.4% 21 / 69 MC/DCmcdc 14.2% 6 / 42 Subroutinesub 78.2% 18 / 23 Podpod 69.2% 9 / 13 totaltotal 46.7% 280 / 599 CC 109 SCAR 41.7
CC
109
Cov
47%
find_home
271.0
rename_vms
132.0
_rename
42.0
CRAP 64.6
↑ Summary
1 package File::Valet;
2 20 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Config; # Provides OS-portable means of determining platform type
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use vars qw(@EXPORT @EXPORT_OK @ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 20 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 require Exporter;
13 20 @ISA = qw(Exporter);
14 20 $VERSION = '1.11';
15 20 @EXPORT = @EXPORT_OK = qw(rd_f wr_f ap_f find_first find_home find_temp find_bin lockafile unlockafile unlock_all_the_files);
16 }
17  
18 20 our $OK = 'OK'; # one of "OK", "WARNING" or "ERROR", reflecting most recently performed operation
19 20 our $ERROR = ''; # short invariant description of error, or empty string if none
20 20 our $ERRNO = ''; # variant description of error (such as $!), or empty string if none
21 20 our $ERRNUM = 0; # numerical variant description of error (such as $!), or empty string if none, undocumented, only used for unit tests
22 20 our %LOCKS_HASH; # keys on lockfile to bind count, for supporting nested locks.
23  
24 # File::Copy::move() almost doest the right thing, just needs syscopy() as failover instead of copy().
25 # This _rename() function more or less duplicates the needed functionality of File::Copy::_move's logic after the rename,
26 # but uses syscopy() and captures failures in $OK, $ERROR, $ERRNO, $ERRNUM.
27 0 ▶sub _rename {
Subroutine _rename: not called CC=6 SCAR=37.4
28 0 my ($from, $to) = @_;
29  
30 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
31 0 ▶ return 'OK' if (rename ($from, $to));
Branch: 1 branch
BranchTrueFalseTotal
if rename $from, $to 0 0 0
32  
33 0 my $result = File::Copy::syscopy($from, $to);
34 0 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
35 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'File::Copy::syscopy failed', $!, 0+$!);
36 0 return undef;
37 }
38  
39 0 my @st = stat($from);
40 0 ▶ unless (@st) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@st) 0 0 0
41 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'after-copy stat failed', $!, 0+$!);
42 0 return undef;
43 }
44  
45 0 my ($atime, $mtime) = (@st)[8,9];
46 0 ▶ unless (utime($atime, $mtime, $to)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utime $atime, $mtime, $to) 0 0 0
47 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'after-copy utime failed', $!, 0+$!);
48 0 return undef;
49 }
50  
51 0 ▶ unless (unlink($from)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $from) 0 0 0
52 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'after-copy unlink failed', $!, 0+$!);
53 0 return undef;
54 }
55 0 return 'OK';
56 }
57  
58 0 ▶sub rename_vms {
Subroutine rename_vms: not called CC=11 SCAR=48.8
Pod undocumented
59 0 my ($fn, $dest) = @_;
60 0 ▶ if (!defined($fn) || ($fn eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fn or $fn eq "") 0 0 0
Condition0%0/3
not defined $fn or $fn eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $fn or $fn eq ""
not defined $fn $fn eq ""
Truth table0%0/3
not(A) or B
A= not defined $fnB= $fn eq ""
ABresult
1X1
011
000
61 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
62 0 return undef;
63 }
64 0 ▶ if (!defined($dest) || ($dest eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dest or $dest eq "") 0 0 0
Condition0%0/3
not defined $dest or $dest eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $dest or $dest eq ""
not defined $dest $dest eq ""
Truth table0%0/3
not(A) or B
A= not defined $destB= $dest eq ""
ABresult
1X1
011
000
65 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no destination directory supplied', -1, 0);
66 0 return undef;
67 }
68 0 my $dest_fn = $fn;
69 0 ▶ if (!-d $dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dest) 0 0 0
70 0 ($dest, $dest_fn) = fileparse($dest);
71 }
72 0 ▶ if (!-e $dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dest) 0 0 0
73 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'destination directory does not exist', -1, 0);
74 0 return undef;
75 }
76 0 ▶ if (!-d _) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d _) 0 0 0
77 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'destination directory is not a directory', -1, 0);
78 0 return undef;
79 }
80 0 ▶ if (!-w _) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w _) 0 0 0
81 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'destination directory is not writable', -1, 0);
82 0 return undef;
83 }
84  
85 0 ▶ if (!-e "$dest/$dest_fn") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "$dest/$dest_fn") 0 0 0
86 # degenerate case; just rename it.
87 0 return _rename($fn, "$dest/$dest_fn");
88 }
89  
90 0 my $i = 1;
91 0 ▶ $i++ while(-e "$dest/$dest_fn.$i");
Branch: 1 branch
BranchTrueFalseTotal
-e "$dest/$dest_fn.$i" and do { ++$i } 0 0 0
92 0 return _rename($fn, "$dest/$dest_fn.$i");
93 }
94  
95 226 ▶sub rd_f {
Subroutine rd_f: called CC=9 SCAR=23.5
Pod documented
96 226 my ($fn) = @_;
97 226 my ($fh, $buf);
98 226 ▶ if (!defined($fn) || ($fn eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fn or $fn eq "") 2 224 226
Condition100%3/3
not defined $fn or $fn eq ""
l!l&&r!l&&!r
11224
MC/DC100%2/2
not defined $fn or $fn eq ""
not defined $fn $fn eq ""
Truth table100%3/3
not(A) or B
A= not defined $fnB= $fn eq ""
ABresult
1X1
011
000
99 2 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
100 2 return undef;
101 }
102 224 $! = 0;
103 224 ▶ unless (open($fh, '< :raw', $fn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "< :raw", $fn) 10 214 224
104 10 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', "cannot open for reading", $!, 0+$!);
105 10 return undef;
106 }
107 214 binmode($fh);
108 214 my $file_size = (stat($fn))[7];
109 214 ▶ if ($file_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_size) { } 181 33 214
110 181 my $n_bytes = sysread($fh, $buf, $file_size);
111 181 ▶ if (!defined($n_bytes)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $n_bytes) { } 0 181 181
elsif ($n_bytes != $file_size) { } 0 181 181
112 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read failed', $!, 0+$!);
113 0 return undef;
114 }
115 elsif ($n_bytes != $file_size) {
116 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read underflow', $!, 0+$!);
117 0 return undef;
118 }
119 }
120 else {
121 33 my $res = sysread($fh, $buf, 0x7FFFFFFF);
122 33 ▶ if (!defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 33 33
123 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read failed', $!, 0+$!);
124 0 return undef;
125 }
126 }
127 214 my $res = close($fh);
128 214 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 214 214
129 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'close failed', $!, 0+$!);
130 0 return undef;
131 }
132 214 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
133 214 return $buf;
134 }
135  
136 5 ▶sub wr_f {
Subroutine wr_f: called CC=6 SCAR=19.3
Pod documented
137 5 my ($fn, $buf) = @_;
138 5 my $fh;
139 5 ▶ if (!defined($fn) || ($fn eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fn or $fn eq "") 3 2 5
Condition100%3/3
not defined $fn or $fn eq ""
l!l&&r!l&&!r
122
MC/DC100%2/2
not defined $fn or $fn eq ""
not defined $fn $fn eq ""
Truth table100%3/3
not(A) or B
A= not defined $fnB= $fn eq ""
ABresult
1X1
011
000
140 3 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
141 3 return undef;
142 }
143 2 $! = 0;
144 2 ▶ unless (open($fh, '> :raw', $fn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "> :raw", $fn) 0 2 2
145 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', "cannot open for writing", $!, 0+$!);
146 0 return undef;
147 }
148 2 binmode($fh);
149 2 my $res = syswrite($fh, $buf);
150 2 ▶ unless (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 2 2
151 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'write error', $!, 0+$!);
152 0 return undef;
153 }
154 2 $res = close($fh);
155 2 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 2 2
156 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'close failed', $!, 0+$!);
157 0 return undef;
158 }
159 2 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
160 2 return 'OK';
161 }
162  
163 1604 ▶sub ap_f {
Subroutine ap_f: called CC=6 SCAR=19.3
Pod documented
164 1604 my ($fn, $buf) = @_;
165 1604 my $fh;
166 1604 ▶ if (!defined($fn) || ($fn eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fn or $fn eq "") 2 1602 1604
Condition100%3/3
not defined $fn or $fn eq ""
l!l&&r!l&&!r
111602
MC/DC100%2/2
not defined $fn or $fn eq ""
not defined $fn $fn eq ""
Truth table100%3/3
not(A) or B
A= not defined $fnB= $fn eq ""
ABresult
1X1
011
000
167 2 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
168 2 return undef;
169 }
170 1602 $! = 0;
171 1602 ▶ unless (open($fh, '>> :raw', $fn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">> :raw", $fn) 0 1602 1602
172 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', "cannot open for appending", $!, 0+$!);
173 0 return undef;
174 }
175 1602 binmode($fh);
176 1602 my $res = syswrite($fh, $buf);
177 1602 ▶ unless (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 1602 1602
178 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'write error', $!, 0+$!);
179 0 return undef;
180 }
181 1602 $res = close($fh);
182 1602 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 1602 1602
183 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'close failed', $!, 0+$!);
184 0 return undef;
185 }
186 1602 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
187 1602 return 'OK';
188 }
189  
190 62 ▶sub detect_windows {
Subroutine detect_windows: called CC=5 SCAR=23.0
Pod undocumented
191 62 ▶ return ($^O eq 'MSWin32' || $Config{'osname'} =~ /windows/i || $Config{'osname'} =~ /winserver/i || $Config{'osname'} =~ /microsoft/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' || $File::Valet::Config{'osname'} =~ /windows/i || $File::Valet::Config{'osname'} =~ /winserver/i || $File::Valet::Config{'osname'} =~ /microsoft/i ? : 0 62 62
Condition33%3/9
$^O eq "MSWin32" or $File::Valet::Config{"osname"} =~ /windows/i or $File::Valet::Config{"osname"} =~ /winserver/i or $File::Valet::Config{"osname"} =~ /microsoft/i
l!l&&r!l&&!r
0062
$^O eq "MSWin32" or $File::Valet::Config{"osname"} =~ /windows/i or $File::Valet::Config{"osname"} =~ /winserver/i
l!l&&r!l&&!r
0062
$^O eq "MSWin32" or $File::Valet::Config{"osname"} =~ /windows/i
l!l&&r!l&&!r
0062
MC/DC0%0/4
$^O eq "MSWin32" or $File::Valet::Config{"osname"} =~ /windows/i or $File::Valet::Config{"osname"} =~ /winserver/i or $File::Valet::Config{"osname"} =~ /microsoft/i
$^O eq "MSWin32" $File::Valet::Config{"osname"} =~ /windows/i $File::Valet::Config{"osname"} =~ /winserver/i $File::Valet::Config{"osname"} =~ /microsoft/i
Truth table20%1/5
A or B or C or D
A= $^O eq "MSWin32"B= $File::Valet::Config{"osname"} =~ /windows/iC= $File::Valet::Config{"osname"} =~ /winserver/iD= $File::Valet::Config{"osname"} =~ /microsoft/i
ABCDresult
1XXX1
01XX1
001X1
00011
00000
192 }
193  
194 0 ▶sub find_first {
Subroutine find_first: not called CC=3 SCAR=24.8
Pod undocumented
195 0 foreach my $d (@_) {
196 0 ▶ next unless (-e $d);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $d 0 0 0
197 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
198 0 return $d;
199 }
200 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no file found', '', 0);
201 0 return undef;
202 }
203  
204 42 ▶sub find_home {
Subroutine find_home: called CC=19 SCAR=56.0
Pod documented
205 42 for my $d (@_) {
206 0 ▶ return $d if (defined $d && -d $d && -w _);
Branch: 1 branch
BranchTrueFalseTotal
if defined $d and -d $d and -w _ 0 0 0
Condition0%0/6
defined $d and -d $d and -w _
!ll&&!rl&&r
000
defined $d and -d $d
!ll&&!rl&&r
000
MC/DC0%0/3
defined $d and -d $d and -w _
defined $d -d $d -w _
Truth table0%0/4
A and B and C
A= defined $dB= -d $dC= -w _
ABCresult
0XX0
10X0
1100
1111
207 }
208  
209 42 my $is_windows = detect_windows;
210 42 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
211  
212 42 my $env_home = $ENV{HOME};
213 42 ▶ return $env_home if (defined $env_home && -d $env_home);
Branch: 1 branch
BranchTrueFalseTotal
if defined $env_home and -d $env_home 42 0 42
Condition33%1/3
defined $env_home and -d $env_home
!ll&&!rl&&r
0042
MC/DC0%0/2
defined $env_home and -d $env_home
defined $env_home -d $env_home
Truth table33%1/3
A and B
A= defined $env_homeB= -d $env_home
ABresult
0X0
100
111
214  
215 0 ▶ my $username = $ENV{USER} // $ENV{USERNAME};
Condition0%0/3
$ENV{"USER"} // $ENV{"USERNAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"USER"} // $ENV{"USERNAME"}
$ENV{"USER"} $ENV{"USERNAME"}
Truth table0%0/3
A // B
A= $ENV{"USER"}B= $ENV{"USERNAME"}
ABresult
1X1
011
000
216 0 ▶ if ($is_windows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_windows) { } 0 0 0
217 0 ▶ my $home_drive = $ENV{HOMEDRIVE} // 'C:';
Condition0%0/2
$ENV{"HOMEDRIVE"} // "C:"
l!l
00
MC/DC0%0/1
$ENV{"HOMEDRIVE"} // "C:"
$ENV{"HOMEDRIVE"}
Truth table0%0/2
A
A= $ENV{"HOMEDRIVE"}
Aresult
11
00
218 0 my $home_path = $ENV{HOMEPATH};
219 0 ▶ if (defined $home_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $home_path) { } 0 0 0
elsif (defined $username) { } 0 0 0
220 0 $env_home = $home_drive . $home_path;
221 }
222 elsif (defined $username) {
223 0 $env_home = $home_drive . '\\Users\\' . $username;
224 }
225 0 ▶ return $env_home if (defined $env_home && -d $env_home);
Branch: 1 branch
BranchTrueFalseTotal
if defined $env_home and -d $env_home 0 0 0
Condition0%0/3
defined $env_home and -d $env_home
!ll&&!rl&&r
000
MC/DC0%0/2
defined $env_home and -d $env_home
defined $env_home -d $env_home
Truth table0%0/3
A and B
A= defined $env_homeB= -d $env_home
ABresult
0X0
100
111
226 } else {
227 0 my @row = getpwuid($<);
228 0 ▶ if (@row >= 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (@row >= 9) 0 0 0
229 0 my $home_dir = $row[7];
230 0 ▶ return $home_dir if (defined $home_dir && -d $home_dir);
Branch: 1 branch
BranchTrueFalseTotal
if defined $home_dir and -d $home_dir 0 0 0
Condition0%0/3
defined $home_dir and -d $home_dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $home_dir and -d $home_dir
defined $home_dir -d $home_dir
Truth table0%0/3
A and B
A= defined $home_dirB= -d $home_dir
ABresult
0X0
100
111
231 }
232 0 ▶ return '/root' if (-d '/root' && -w '/root');
Branch: 1 branch
BranchTrueFalseTotal
if -d "/root" and -w "/root" 0 0 0
Condition0%0/3
-d "/root" and -w "/root"
!ll&&!rl&&r
000
MC/DC0%0/2
-d "/root" and -w "/root"
-d "/root" -w "/root"
Truth table0%0/3
A and B
A= -d "/root"B= -w "/root"
ABresult
0X0
100
111
233 }
234  
235 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'cannot find home directory', $is_windows, 1);
236 0 return undef;
237 }
238  
239 20 ▶sub find_temp {
Subroutine find_temp: called CC=12 SCAR=29.9
Pod documented
240 20 my $is_windows = detect_windows;
241 20 my $dir_sep_tok = '/';
242 20 my $home_dir = find_home;
243  
244 20 ▶ push(@_, $ENV{TEMPDIR}) if (defined($ENV{TEMPDIR}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TEMPDIR"} 0 20 20
245 20 ▶ push(@_, $ENV{TEMP}) if (defined($ENV{TEMP}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TEMP"} 0 20 20
246 20 ▶ push(@_, $ENV{TMP}) if (defined($ENV{TMP})); # set in Windows sometimes
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TMP"} 0 20 20
247  
248 20 ▶ if ($is_windows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_windows) 0 20 20
249 0 $dir_sep_tok = '\\';
250 0 push(@_, 'C:\\Windows\\Temp');
251 0 push(@_, 'D:\\Windows\\Temp');
252 0 foreach my $vol (qw(C D E F G W X Y Z)) {
253 0 push(@_, "$vol:\\Temp");
254 }
255 }
256 # might be CygWin, so adding these regardless of OS:
257 20 push(@_, qw (/var/tmp /tmp));
258  
259 20 ▶ push(@_, map {join($dir_sep_tok,("$home_dir",$_))} qw(.tmp .temp tmp temp), $home_dir) if (defined($home_dir));
Branch: 1 branch
BranchTrueFalseTotal
if defined $home_dir 20 0 20
260 20 ▶ push(@_, map {join($dir_sep_tok,("$ENV{PWD}", $_))} qw(.tmp .temp tmp temp), $ENV{PWD} ) if (defined($ENV{PWD} ));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PWD"} 20 0 20
261 20 ▶ push(@_, '/dev/shm') unless ($is_windows); # Lowest priority, since this is typically a ramdisk.
Branch: 1 branch
BranchTrueFalseTotal
unless $is_windows 20 0 20
262 20 foreach my $d (@_) {
263 20 ▶ next unless (-d $d);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $d 0 20 20
264 20 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
265 20 ▶ return $d if (-w _);
Branch: 1 branch
BranchTrueFalseTotal
if -w _ 20 0 20
266 }
267 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no appropriate temporary directory found', '', 0);
268 0 return undef;
269 }
270  
271 21 ▶sub find_bin {
Subroutine find_bin: called CC=7 SCAR=19.9
Pod documented
272 21 ▶ return find_bin_win32 (@_) if ($Config::Config{osname} =~ /MSWin/);
Branch: 1 branch
BranchTrueFalseTotal
if $Config::Config{"osname"} =~ /MSWin/ 0 21 21
273 21 my ($bin_name, @bin_dirs) = @_;
274 21 my $home_dir = find_home;
275 21 ▶ push(@bin_dirs, split(/\:/, $ENV{PATH})) if (defined($ENV{PATH}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PATH"} 21 0 21
276 21 ▶ push(@bin_dirs, "$home_dir/bin") if (defined($home_dir));
Branch: 1 branch
BranchTrueFalseTotal
if defined $home_dir 21 0 21
277 21 push(@bin_dirs, ('/usr/local/sbin', '/usr/local/bin', '/sbin', '/bin', '/usr/sbin', '/usr/bin'));
278 21 my %been_there = ();
279 21 foreach my $d (@bin_dirs) {
280 168 ▶ next if (defined($been_there{$d}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $been_there{$d} 42 126 168
281 126 $been_there{$d} = 1;
282 126 my $f = "$d/$bin_name";
283 126 ▶ next unless (-x $f);
Branch: 1 branch
BranchTrueFalseTotal
unless -x $f 105 21 126
284 21 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
285 21 return $f;
286 }
287 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no executable found', '', 0);
288 0 return undef;
289 }
290  
291 0 ▶sub find_bin_win32 {
Subroutine find_bin_win32: not called CC=5 SCAR=34.0
Pod undocumented
292 0 my ($bin_name, @bin_dirs) = @_;
293 0 ▶ push(@bin_dirs, split(/\;/, $ENV{PATH})) if (defined($ENV{PATH}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PATH"} 0 0 0
294 0 push(@bin_dirs, ('C:\\WINDOWS\\system32', 'C:\\WINDOWS'));
295 0 my %been_there = ();
296 0 foreach my $d (@bin_dirs) {
297 0 ▶ next if (defined($been_there{$d}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $been_there{$d} 0 0 0
298 0 $been_there{$d} = 1;
299 0 my $f = "$d\\$bin_name";
300 0 ▶ next unless (-x $f);
Branch: 1 branch
BranchTrueFalseTotal
unless -x $f 0 0 0
301 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
302 0 return $f;
303 }
304 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no executable found', '', 0);
305 0 return undef;
306 }
307  
308 # returns 1 on great success, 0 on miserable failure
309 1600 ▶sub lockafile {
Subroutine lockafile: called CC=20 SCAR=35.2
Pod documented
310 1600 my ($f, %opt) = @_;
311 1600 ▶ $opt{nsec} = 30 unless (defined($opt{nsec}) && int($opt{nsec}) > 0); # Number of seconds we expect to have the file locked. If we hold the lock for longer than this, other processes are welcome to kill us and take the lock themselves.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"nsec"} and int $opt{"nsec"} > 0 1600 0 1600
Condition33%1/3
defined $opt{"nsec"} and int $opt{"nsec"} > 0
!ll&&!rl&&r
160000
MC/DC0%0/2
defined $opt{"nsec"} and int $opt{"nsec"} > 0
defined $opt{"nsec"} int $opt{"nsec"} > 0
Truth table33%1/3
A and B
A= defined $opt{"nsec"}B= int $opt{"nsec"} > 0
ABresult
0X0
100
111
312 1600 ▶ $opt{msg} = "programmer is lame" unless (defined($opt{msg}) && $opt{msg} ne ''); # Helpful message for the human to understand wtf this lock is about
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"msg"} and $opt{"msg"} ne "" 1600 0 1600
Condition33%1/3
defined $opt{"msg"} and $opt{"msg"} ne ""
!ll&&!rl&&r
160000
MC/DC0%0/2
defined $opt{"msg"} and $opt{"msg"} ne ""
defined $opt{"msg"} $opt{"msg"} ne ""
Truth table33%1/3
A and B
A= defined $opt{"msg"}B= $opt{"msg"} ne ""
ABresult
0X0
100
111
313 1600 ▶ $opt{limit} = 30 unless (defined($opt{limit}) && int($opt{limit}) > 0); # Number of seconds caller is willing to wait for a lock before failing out.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"limit"} and int $opt{"limit"} > 0 1600 0 1600
Condition33%1/3
defined $opt{"limit"} and int $opt{"limit"} > 0
!ll&&!rl&&r
160000
MC/DC0%0/2
defined $opt{"limit"} and int $opt{"limit"} > 0
defined $opt{"limit"} int $opt{"limit"} > 0
Truth table33%1/3
A and B
A= defined $opt{"limit"}B= int $opt{"limit"} > 0
ABresult
0X0
100
111
314 1600 ▶ $opt{sleep_duration} = 0.25 unless (defined($opt{sleep_duration}) && $opt{sleep_duration} > 0.0);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"sleep_duration"} and $opt{"sleep_duration"} > 0 1600 0 1600
Condition33%1/3
defined $opt{"sleep_duration"} and $opt{"sleep_duration"} > 0
!ll&&!rl&&r
160000
MC/DC0%0/2
defined $opt{"sleep_duration"} and $opt{"sleep_duration"} > 0
defined $opt{"sleep_duration"} $opt{"sleep_duration"} > 0
Truth table33%1/3
A and B
A= defined $opt{"sleep_duration"}B= $opt{"sleep_duration"} > 0
ABresult
0X0
100
111
315 1600 ▶ my $lockfile_name = $opt{lockfile_name} || "$f.lock";
Condition50%1/2
$opt{'lockfile_name'} || "$f.lock"
l!l
01600
MC/DC0%0/1
$opt{'lockfile_name'} || "$f.lock"
$opt{'lockfile_name'}
Truth table50%1/2
A
A= $opt{'lockfile_name'}
Aresult
11
00
316 1600 my $tm_start = time();
317 1600 my $lockfile_fh;
318  
319 # TODO - This is fast and simple, but fails to handle expired lockfiles and extending lockfile durations.
320 1600 ▶ if ($LOCKS_HASH{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Valet::LOCKS_HASH{$f}) 0 1600 1600
321 0 $LOCKS_HASH{$f}++;
322 0 return 1;
323 }
324  
325 1600 ▶ while (!sysopen($lockfile_fh, $lockfile_name, &O_RDWR | &O_CREAT | &O_EXCL)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $lockfile_fh, $lockfile_name, &O_RDWR | &O_CREAT | &O_EXCL) 218 1600 1818
326 218 ▶ if (-e $lockfile_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $lockfile_name) 217 1 218
327 # re-scanning after every sleep(), because it could expire while we are sleeping and someone else might grab it while we are sleeping.
328 217 my $mtime = (stat(_))[9];
329 217 my $txt = File::Valet::rd_f($lockfile_name);
330  
331 217 ▶ if (!defined($txt)) { # handling potential race condition or naughty unreadable lockfile
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $txt) 9 208 217
332 9 ▶ if ((time() - $tm_start) > $opt{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $tm_start > $opt{"limit"}) 0 9 9
333 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'lockfile racy or unreadable', $lockfile_name, 0);
334 0 return 0;
335 }
336 9 select(undef, undef, undef, $opt{sleep_duration});
337 9 next;
338 }
339  
340 208 chomp($txt);
341 208 ▶ if ($txt =~ /^\d+\t/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt =~ /^\d+\t/) 187 21 208
342 187 my ($pid, $lock_duration, $message, $whence) = split(/\t/, $txt);
343 187 ▶ $lock_duration = 30 unless (defined($lock_duration));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lock_duration 0 187 187
344 187 my $locking_process_still_lives = kill(0, $pid);
345 # TODO - Potential race condition; another process might acquire the expired lock after this second stat() and before unlink().
346 # Perhaps use senate? Slow in filesystem, but could use shm on systems which support SysV shared memory.
347 187 ▶ unlink($lockfile_name) if ((time() > $mtime + $lock_duration) || ($locking_process_still_lives < 1));
Branch: 1 branch
BranchTrueFalseTotal
if time > $mtime + $lock_duration or $locking_process_still_lives < 1 0 187 187
Condition33%1/3
time > $mtime + $lock_duration or $locking_process_still_lives < 1
l!l&&r!l&&!r
00187
MC/DC0%0/2
time > $mtime + $lock_duration or $locking_process_still_lives < 1
time > $mtime + $lock_duration $locking_process_still_lives < 1
Truth table33%1/3
A or B
A= time > $mtime + $lock_durationB= $locking_process_still_lives < 1
ABresult
1X1
011
000
348 }
349 }
350  
351 209 ▶ if ((time() - $tm_start) > $opt{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $tm_start > $opt{"limit"}) 0 209 209
352 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0); # Not an error; simply unable to acquire lock within specified duration
353 0 return 0;
354 }
355  
356 209 select(undef, undef, undef, $opt{sleep_duration});
357 }
358 1600 my $msg = sprintf("\%d\t\%d\t%s\t%s\n", $$, $opt{nsec}, $opt{msg}, $0); # populating lockfile with information about locking process
359 1600 syswrite($lockfile_fh, $msg);
360 1600 close($lockfile_fh);
361 1600 $LOCKS_HASH{$f} = 1;
362 1600 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
363 1600 return 1;
364 }
365  
366 1600 ▶sub unlockafile {
Subroutine unlockafile: called CC=9 SCAR=27.3
Pod documented
367 1600 my ($f, %opt) = @_;
368 1600 my $lockfile_fh;
369 my $dgram;
370 1600 ▶ my $lockfile_name = $opt{lockfile_name} || "$f.lock";
Condition50%1/2
$opt{'lockfile_name'} || "$f.lock"
l!l
01600
MC/DC0%0/1
$opt{'lockfile_name'} || "$f.lock"
$opt{'lockfile_name'}
Truth table50%1/2
A
A= $opt{'lockfile_name'}
Aresult
11
00
371 1600 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
372  
373 # TODO - This is fast and simple, but fails to handle expired lockfiles
374 1600 ▶ if ($LOCKS_HASH{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Valet::LOCKS_HASH{$f}) 1600 0 1600
375 1600 $LOCKS_HASH{$f}--;
376 1600 ▶ return 1 if ($LOCKS_HASH{$f} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $File::Valet::LOCKS_HASH{$f} > 0 0 1600 1600
377 }
378 1600 $! = 0;
379 1600 ▶ unless (sysopen($lockfile_fh, $lockfile_name, &O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $lockfile_fh, $lockfile_name, &O_RDONLY) 0 1600 1600
380 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'open failure', $!, 0+$!);
381 0 return 0;
382 }
383 1600 ▶ unless(my $result = sysread($lockfile_fh, $dgram, 4095)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (my $result = sysread($lockfile_fh, $dgram, 4095)) 0 1600 1600
384 0 ▶ if (defined($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 0 0 0
385 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read zero bytes from lockfile', '', 0+$!);
386 } else {
387 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read error', $!, 0+$!);
388 }
389 0 return 0;
390 }
391 1600 chomp($dgram);
392 1600 my ($lpid, $nsec, $msg, $whence) = split(/\t/, $dgram);
393 1600 close($lockfile_fh);
394 1600 $LOCKS_HASH{$f} = 0;
395 1600 ▶ if (defined($lpid) && ($lpid ne $$)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lpid and $lpid ne $$) 0 1600 1600
Condition33%1/3
defined $lpid and $lpid ne $$
!ll&&!rl&&r
016000
MC/DC0%0/2
defined $lpid and $lpid ne $$
defined $lpid $lpid ne $$
Truth table33%1/3
A and B
A= defined $lpidB= $lpid ne $$
ABresult
0X0
100
111
396 # oops! not ours anymore
397 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'lost lock', $dgram, 0+$!);
398 0 return 0;
399 }
400 1600 unlink($lockfile_name);
401 1600 return 1;
402 }
403  
404 0 ▶sub unlock_all_the_files {
Subroutine unlock_all_the_files: not called CC=4 SCAR=30.0
Pod documented
405 # Best effort only .. eh, make that "some effort only".
406 # That might still be overly charitable.
407 # Truth be told, you're only using this function if you can't be assed to fix the bugs in your own code,
408 # which relieves me somewhat of any moral imperative.
409 0 my $n_locks = 0;
410 0 my $n_files = 0;
411 0 my $n_errors = 0;
412 0 foreach my $f (keys %LOCKS_HASH) {
413 0 ▶ next unless ($LOCKS_HASH{$f});
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Valet::LOCKS_HASH{$f} 0 0 0
414 0 $n_locks += $LOCKS_HASH{$f};
415 0 $LOCKS_HASH{$f} = 0;
416 0 unlockafile($f);
417 0 ▶ $n_errors++ unless ($OK eq 'OK');
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Valet::OK eq "OK" 0 0 0
418 0 $n_files++;
419 }
420 0 return ($n_errors, $n_locks, $n_files);
421 }
422  
423 20 1;
424  
425 =head1 NAME
426  
427 File::Valet - Utilities for file slurping, locking, and finding.
428  
429 =head1 SYNOPSIS
430  
431 use File::Valet;
432  
433 # Simple slurp and unslurp with rd_f, wr_f, ap_f:
434  
435 my $text = rd_f('some/file.txt');
436 die "slurp failure: $File::Valet::ERROR ($File::Valet::ERRNO)" unless ($File::Valet::OK eq 'OK');
437 # or, equivalently:
438 die "slurp failure: $File::Valet::ERROR ($File::Valet::ERRNO)" unless (defined($text));
439  
440 # Contents written will be same as that of "some/file.txt",
441 # plus two lines appended at the end:
442  
443 wr_f('another/file.txt', $text);
444 ap_f('another/file.txt', "Oh, and another thing:\n");
445 ap_f('another/file.txt', "STOP BREATHING IN MY CUP\n");
446  
447 # Find a place suited to temporary files:
448 my $tmp = find_temp(); # Likely /var/tmp or /tmp or C:\Windows\Temp
449  
450 # Find the full pathname of an executable:
451 my $shell = find_bin('sh'); # Likely /bin/sh
452  
453 # Use a lockfile for exclusive access to a shared resource:
454 lockafile("$tmp/shared.txt") or die "cannot obtain lock: $File::Valet::ERROR ($File::Valet::ERRNO)";
455 my $text = rd_f("$tmp/shared.txt");
456 unlockafile("$tmp/shared.txt") or die "unlock error: $File::Valet::ERROR ($File::Valet::ERRNO)";
457  
458 # Nested file locking:
459 lockafile("shared.txt") or die "cannot obtain first lock";
460 my $text = rd_f("$tmp/shared.txt");
461 ...
462 lockafile("shared.txt") or die "cannot obtain second lock";
463 ap_f("$tmp/shared.txt", $data);
464 unlockafile("$tmp/shared.txt");
465 ...
466 unlockafile("$tmp/shared.txt");
467  
468 # Your code has bugs, resulting in leaving lockfiles behind, but
469 # instead of debugging you'd rather just remove all your locks:
470 my ($n_errors, $n_locks, $n_files) = unlock_all_the_files();
471  
472 =head1 DESCRIPTION
473  
474 B<File::Valet> contains a selection of easy-to-use subroutines for manipulating files and file content. Some effort has been made to establish cross-platform portability, and to make their behavior as unsurprising as possible.
475  
476 =head1 FUNCTIONS
477  
478 The following functions are available through this module for use in other applications. In keeping with the intent of minimizing user keystrokes, all of these functions are exported into the calling namespace.
479  
480 =over 4
481  
482 =item B<rd_f>
483  
484 my $string = rd_f($filename);
485  
486 C<rd_f()> is similar to the well-known C<Slurp::to_scalar()>, in that it reads the entire contents of the named file and returns it as a string. Its principle differences are a slightly shorter name and the insertion of diagnostic information into C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> when the operation failed to complete.
487  
488 The return value is either the contents of the file B<as a string of bytes> (an empty string if the file had no contents), or undef on any error.
489  
490 =item B<wr_f>
491  
492 my $success = wr_f($filename, $string);
493  
494 C<wr_f()> is conceptually the opposite of C<rd_f()>, in that it overwrites the named file's contents with the given B<string of bytes>.
495  
496 If the specified file does not exist, C<wr_f()> will attempt to create it.
497  
498 Returns 1 on success, or 0 on any failure, and sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
499  
500 =item B<ap_f>
501  
502 my $success = ap_f($filename, $string);
503  
504 C<ap_f()> is similar to C<wr_f()>, differing in that the specified B<string of bytes> is appended to the end of the file, rather than overwriting it.
505  
506 If the specified file does not exist, C<ap_f()> will attempt to create it.
507  
508 Returns 1 on success, or 0 on any failure, and sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
509  
510 =item B<find_home>
511  
512 my $path = find_home;
513 my $path = find_temp("/var/home", "/tmp/home");
514  
515 C<find_home()> performs a best-effort search for the effective user's home, returning a path-string or undef if none is found.
516  
517 If arguments are provided, it will return the first argument for which there is a directory for which the user has write permissions.
518  
519 if C<$ENV{HOME}> is set, C<find_home()> will check there for a writable directory after checking any arguments.
520  
521 Some effort has been made to make it cross-platform.
522  
523 =item B<find_temp>
524  
525 my $path = find_temp();
526 my $path = find_temp("/home/tmp", "/usr/tmp", ...);
527  
528 Intended for easy cross-platform programming, C<find_temp()> checks in a number of likely, common filesystem locations for a valid directory for temporary files. It returns the first directory it finds for which the user has write permissions, or undef if none is found.
529  
530 If parameters are passed to C<find_temp()>, it will check those locations first.
531  
532 If C<$ENV{TEMPDIR}>, C<$ENV{TEMP}> or C<$ENV{TMP}> are defined, C<find_temp()> will check those locations after checking the locations provided as parameters.
533  
534 C<find_temp()> is Windows-savvy enough to check such locations as "C:\Windows\Temp", but might try to open locations on network-mounted drives if it is unable to find a local alternative.
535  
536 =item B<find_home>
537  
538 my $path = find_home();
539 my $path = find_home("/var/home/fred");
540  
541 Another function intended for easy cross-platform programming, C<find_home()> will first check $ENV{HOME} on *nix or $ENV{HOMEDIRVE} and $ENV{HOMEPATH (on Windows) if defined, then in the system's passwd database, and then in a number of other likely locations, for a writable home directory for the effective user.
542  
543 It will return the full absolute path of the home directory on success, or undef on failure.
544  
545 =item B<find_bin>
546  
547 my $pathname = find_bin("ls");
548 my $pathname = find_bin("ls", "/home/ttk/bin", "/opt/bin", ...);
549  
550 Another function intended for easy cross-platform programming, C<find_bin()> will first check all of the directories in $ENV{PATH} (if defined), and then in a number of other likely, common locations, for an executable file whose name matches the first parameter. It will return the full absolute pathname of the executable file on success, or undef on failure.
551  
552 C<find_bin()> is Windows-savvy, albeit does not search Windows systems as extensively as others.
553  
554 If directory paths are given as additional parameters, C<find_bin()> will check those locations first.
555  
556 C<find_bin()> is smart enough to only check any given directory once, even if it appears in the parameter list as well as in $ENV{PATH}, or appears multiple times in either.
557  
558 C<find_bin()> also sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
559  
560 =item B<lockafile>
561  
562 my $success = lockafile("/tmp/foo", %options);
563 my $success = lockafile("/tmp/foo",
564 limit => 2.0, # keep retrying for 2.0 seconds before giving up
565 msg => 'in-channel update', # helpful message for troubleshooting
566 nsec => 0.5, # we expect to hold the lock for less than 0.5 seconds
567 );
568  
569 C<lockafile()> applies an advisory lock on the named file, and attempts to be somewhat clever about it, automatically invalidating existing locks set by processes which no longer exist. or set a very long time ago.
570  
571 If the file is already locked by another process, C<lockafile()> will linger and attempt to acquire the lock when the owner of the lock releases the file. This linger time defaults to thirty seconds, and may be overridden with the C<limit> parameter.
572  
573 The advisory lock takes the form of a file, which may be manually deleted to remove the lock, or may be inspected to learn something about the process which created the lock. To facilitate this, a message may be embedded in the lock file describing the reason the file is being locked. It defaults to "programmer is lazy", and may be set by passing the C<msg> parameter.
574  
575 Advisory locks will be respected by other invocations of C<lockafile()> for up to some time before being assumed stale and forceably removed. This period may be increased by passing the C<nsec> parameter (which becomes embedded in the lockfile).
576  
577 C<lockafile()> attempts to manage nested advisory locks via C<%File::Valet::LOCKS_HASH>. C<lockafile()> will keep track of which files the caller has locked, and how many times. Thus if the caller locks the same file two or more times, and unlocks it an equal number of times, the lock file will only be created on the first invocation of C<lockafile()>, and removed only on the last invocation of C<unlockafile()>. See B<FURTHER DEVELOPMENT> for caveats regarding this.
578  
579 Returns 1 on success, or 0 on any failure, and sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
580  
581 =item B<unlockafile>
582  
583 my $success = unlockafile("/tmp/foo", %options);
584  
585 C<unlockafile()> reverses the action of C<lockafile()>, removing an advisory lock on a file (or reducing the count of locks on a multiply-locked file).
586  
587 C<unlockafile()> will fail if invoked on a file which is not locked, or has been locked by a different process.
588  
589 C<unlockafile()> returns 1 on success, and 0 on any failure, and sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
590  
591 =item B<unlock_all_the_files>
592  
593 C<unlock_all_the_files()> is a convenience wrapper for walking C<%File::Valet::LOCKS_HASH> and safely removing all lockfiles.
594  
595 If your code has bugs which cause it to leave lockfiles behind, then calling C<unlock_all_the_files()> before exiting will help prevent that.
596  
597 Really, though, you should fix your bugs.
598  
599 Returns three values: A count of errors returned by C<unlockafile()>, a count of locks removed, and a count of lock files removed.
600  
601 The number of locks can differ from the number of lock files when locks are nested. A file which is locked twice counts as two locks but has only one lock file.
602  
603 =item B<LOCKFILE FORMAT>
604  
605 The lockfiles contain useful bits of information which help C<lockafile> figure out if it should override someone else's lock, and is also useful for gaining insight about the system's behavior, for troubleshooting purposes.
606  
607 Its fields are tab-delimited, and the file terminates with a newline. They appear in this order:
608  
609 * Process identifier of the process which created the lockfile (per "$$"),
610 * The number of seconds the lock should be considered valid (per "nsec" parameter),
611 * The helpful message provided by the programmer (per "msg" parameter),
612 * The name of the program which created the lockfile (per "$0")
613  
614 Example:
615  
616 "4873\t2.0\tThe programmer is lame\t/opt/simon/bin/simond\n"
617  
618 These fields may change in future versions of this module.
619  
620 =item B<FURTHER DEVELOPMENT>
621  
622 A recursive descent function similar to L<File::Find|https://metacpan.org/pod/File::Find> is planned, since C<File::Find> is pretty horrible and unusable.
623  
624 The C<lockafile()> implementation goes through considerable effort to avoid race conditions, but there is still a very short danger window where an overridden lock might get double-clobbered. If a contended lock expires just when two or more other processes call C<lockafile()> on it, it is possible for one process to unlink the lock file, the other process to create a new lock file, and then the first process to overwrite that lock file with its own lock file, leaving both processes under the impression they have acquired the lock. Future implementations may remedy this. In the meantime the possibility can be avoided by setting a sufficiently large "nsec" value when acquiring a lock that it will not expire before the owning process is ready to release it.
625  
626 The nested lock management C<lockafile()> and C<unlockafile()> implement is flawed, in that the lock on the file is only valid for as long specified by the first invocation of C<lockafile()>. Thus if a file is locked for 3 seconds, and then subsequently locked for 30 seconds, other processes contending for the locked file will forceably acquire the lock after 3 seconds after the first lock, not 30 seconds after the second lock. Future implementations may overwrite the lockfile to reflect the parameters of subsequent (nested) locks.
627  
628 The file-slurping functions handle data explicitly as B<bytes> and never as codepoints. This is intentional and unlikely to change. If codepoint handling (utf-8, utf-16, etc) is desired, see L<File::Slurper|https://metacpan.org/pod/File::Slurper>.
629  
630 =item B<SEE ALSO>
631  
632 L<File::Slurper|https://metacpan.org/pod/File::Slurper> is considered the likely successor to C<File::Slurp> for CPAN's primary file-slurping implementation, with proper handling of multibyte-encoded characters (L<which is broken|http://blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html> in C<File::Slurp>). If C<File::Slurper> implemented an appending method, the slurp functions would likely be absent from C<File::Valet>. Until then, C<File::Valet>'s slurping functions provide a simple, robust alternative.
633  
634 L<Path::Tiny|https://metacpan.org/pod/Path::Tiny> - a different style for file handling, which some people might prefer.
635  
636 L<File::Temp|https://metacpan.org/pod/File::Temp> - returns name and handle of a temporary file.
637  
638 L<File::SearchPath|https://metacpan.org/pod/File::SearchPath> - searches an environment path for a file.
639  
640 L<Lock::File|https://metacpan.org/pod/Lock::File> - file locker with an automatic out-of-scope unlocking mechanism.
641  
642 L<NL::File::Lock|https://metacpan.org/pod/NL::File::Lock> - file locker with timeout, but no lock expiration.
643  
644 L<File::Lock::Multi|https://metacpan.org/pod/File::Lock::Multi> - file locker with support for nested locks.
645  
646 L<File::TinyLock|https://metacpan.org/pod/File::TinyLock> - a very easy to use file locker.
647  
648 L<Data::Munge|https://metacpan.org/pod/Data::Munge> - a gaggle of useful functions, including a simple slurp().
649  
650 =back
651  
652 =head1 AUTHOR
653  
654 TTK Ciar
655  
656 =head1 LICENSE
657  
658 You can use and distribute this module under the same terms as Perl itself.
659  
660 =cut
↑ Summary