lib/File/Valet.pm

filter: Statementstmt 56.8% 166 / 292 Branchbran 38.8% 59 / 152 Conditioncond 30.4% 21 / 69 MC/DCmcdc 14.2% 6 / 42 Subroutinesub 81.8% 18 / 22 Podpod 75.0% 9 / 12 totaltotal 47.3% 279 / 589 CC 107 SCAR 41.9
CC
107
Cov
48%
find_home
271.0
rename_vms
132.0
_rename
42.0
CRAP 66.1
« blib/lib/File/Valet.pm ↑ 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 BEGIN {
12 20 ▶ require Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 @ISA = qw(Exporter);
14 20 $VERSION = '1.10';
15 20 @EXPORT = @EXPORT_OK = qw(rd_f wr_f ap_f 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 sub _rename {
28 0 ▶ my ($from, $to) = @_;
Subroutine _rename: not called CC=6 SCAR=37.4
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 sub rename_vms {
59 0 ▶ my ($fn, $dest) = @_;
Subroutine rename_vms: not called CC=11 SCAR=48.8
Pod undocumented
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 sub rd_f {
96 220 ▶ my ($fn) = @_;
Subroutine rd_f: called CC=9 SCAR=23.5
Pod documented
97 220 my ($fh, $buf);
98 220 ▶ if (!defined($fn) || ($fn eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fn or $fn eq "") 2 218 220
Condition100%3/3
not defined $fn or $fn eq ""
l!l&&r!l&&!r
11218
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 218 $! = 0;
103 218 ▶ unless (open($fh, '< :raw', $fn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "< :raw", $fn) 12 206 218
104 12 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', "cannot open for reading", $!, 0+$!);
105 12 return undef;
106 }
107 206 binmode($fh);
108 206 my $file_size = (stat($fn))[7];
109 206 ▶ if ($file_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_size) { } 173 33 206
110 173 my $n_bytes = sysread($fh, $buf, $file_size);
111 173 ▶ if (!defined($n_bytes)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $n_bytes) { } 0 173 173
elsif ($n_bytes != $file_size) { } 0 173 173
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 206 my $res = close($fh);
128 206 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 206 206
129 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'close failed', $!, 0+$!);
130 0 return undef;
131 }
132 206 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
133 206 return $buf;
134 }
135  
136 sub wr_f {
137 5 ▶ my ($fn, $buf) = @_;
Subroutine wr_f: called CC=6 SCAR=19.3
Pod documented
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 sub ap_f {
164 1604 ▶ my ($fn, $buf) = @_;
Subroutine ap_f: called CC=6 SCAR=19.3
Pod documented
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 sub detect_windows {
191 62 ▶ return ($^O eq 'MSWin32' || $Config{'osname'} =~ /windows/i || $Config{'osname'} =~ /winserver/i || $Config{'osname'} =~ /microsoft/i) ? 1 : 0;
Subroutine detect_windows: called CC=5 SCAR=23.0
Pod undocumented
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 sub find_home {
195 42 ▶ for my $d (@_) {
Subroutine find_home: called CC=19 SCAR=56.0
Pod documented
196 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
197 }
198  
199 42 my $is_windows = detect_windows;
200 42 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
201  
202 42 my $env_home = $ENV{HOME};
203 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
204  
205 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
206 0 ▶ if ($is_windows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_windows) { } 0 0 0
207 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
208 0 my $home_path = $ENV{HOMEPATH};
209 0 ▶ if (defined $home_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $home_path) { } 0 0 0
elsif (defined $username) { } 0 0 0
210 0 $env_home = $home_drive . $home_path;
211 }
212 elsif (defined $username) {
213 0 $env_home = $home_drive . '\\Users\\' . $username;
214 }
215 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
216 } else {
217 0 my @row = getpwuid($<);
218 0 ▶ if (@row >= 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (@row >= 9) 0 0 0
219 0 my $home_dir = $row[7];
220 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
221 }
222 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
223 }
224  
225 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'cannot find home directory', $is_windows, 1);
226 0 return undef;
227 }
228  
229 sub find_temp {
230 20 ▶ my $is_windows = detect_windows;
Subroutine find_temp: called CC=12 SCAR=29.9
Pod documented
231 20 my $dir_sep_tok = '/';
232 20 my $home_dir = find_home;
233  
234 20 ▶ push(@_, $ENV{TEMPDIR}) if (defined($ENV{TEMPDIR}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TEMPDIR"} 0 20 20
235 20 ▶ push(@_, $ENV{TEMP}) if (defined($ENV{TEMP}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TEMP"} 0 20 20
236 20 ▶ push(@_, $ENV{TMP}) if (defined($ENV{TMP})); # set in Windows sometimes
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TMP"} 0 20 20
237  
238 20 ▶ if ($is_windows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_windows) 0 20 20
239 0 $dir_sep_tok = '\\';
240 0 push(@_, 'C:\\Windows\\Temp');
241 0 push(@_, 'D:\\Windows\\Temp');
242 0 foreach my $vol (qw(C D E F G W X Y Z)) {
243 0 push(@_, "$vol:\\Temp");
244 }
245 }
246 # might be CygWin, so adding these regardless of OS:
247 20 push(@_, qw (/var/tmp /tmp));
248  
249 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
250 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
251 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
252 20 foreach my $d (@_) {
253 20 ▶ next unless (-d $d);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $d 0 20 20
254 20 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
255 20 ▶ return $d if (-w _);
Branch: 1 branch
BranchTrueFalseTotal
if -w _ 20 0 20
256 }
257 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no appropriate temporary directory found', '', 0);
258 0 return undef;
259 }
260  
261 sub find_bin {
262 21 ▶ return find_bin_win32 (@_) if ($Config::Config{osname} =~ /MSWin/);
Subroutine find_bin: called CC=7 SCAR=19.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $Config::Config{"osname"} =~ /MSWin/ 0 21 21
263 21 my ($bin_name, @bin_dirs) = @_;
264 21 my $home_dir = find_home;
265 21 ▶ push(@bin_dirs, split(/\:/, $ENV{PATH})) if (defined($ENV{PATH}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PATH"} 21 0 21
266 21 ▶ push(@bin_dirs, "$home_dir/bin") if (defined($home_dir));
Branch: 1 branch
BranchTrueFalseTotal
if defined $home_dir 21 0 21
267 21 push(@bin_dirs, ('/usr/local/sbin', '/usr/local/bin', '/sbin', '/bin', '/usr/sbin', '/usr/bin'));
268 21 my %been_there = ();
269 21 foreach my $d (@bin_dirs) {
270 147 ▶ next if (defined($been_there{$d}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $been_there{$d} 42 105 147
271 105 $been_there{$d} = 1;
272 105 my $f = "$d/$bin_name";
273 105 ▶ next unless (-x $f);
Branch: 1 branch
BranchTrueFalseTotal
unless -x $f 84 21 105
274 21 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
275 21 return $f;
276 }
277 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no executable found', '', 0);
278 0 return undef;
279 }
280  
281 sub find_bin_win32 {
282 0 ▶ my ($bin_name, @bin_dirs) = @_;
Subroutine find_bin_win32: not called CC=5 SCAR=34.0
Pod undocumented
283 0 ▶ push(@bin_dirs, split(/\;/, $ENV{PATH})) if (defined($ENV{PATH}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PATH"} 0 0 0
284 0 push(@bin_dirs, ('C:\\WINDOWS\\system32', 'C:\\WINDOWS'));
285 0 my %been_there = ();
286 0 foreach my $d (@bin_dirs) {
287 0 ▶ next if (defined($been_there{$d}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $been_there{$d} 0 0 0
288 0 $been_there{$d} = 1;
289 0 my $f = "$d\\$bin_name";
290 0 ▶ next unless (-x $f);
Branch: 1 branch
BranchTrueFalseTotal
unless -x $f 0 0 0
291 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
292 0 return $f;
293 }
294 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no executable found', '', 0);
295 0 return undef;
296 }
297  
298 # returns 1 on great success, 0 on miserable failure
299 sub lockafile {
300 1600 ▶ my ($f, %opt) = @_;
Subroutine lockafile: called CC=20 SCAR=35.7
Pod documented
301 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
302 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
303 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
304 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
305 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
306 1600 my $tm_start = time();
307 1600 my $lockfile_fh;
308  
309 # TODO - This is fast and simple, but fails to handle expired lockfiles and extending lockfile durations.
310 1600 ▶ if ($LOCKS_HASH{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Valet::LOCKS_HASH{$f}) 0 1600 1600
311 0 $LOCKS_HASH{$f}++;
312 0 return 1;
313 }
314  
315 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) 211 1600 1811
316 211 ▶ if (-e $lockfile_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $lockfile_name) 211 0 211
317 # re-scanning after every sleep(), because it could expire while we are sleeping and someone else might grab it while we are sleeping.
318 211 my $mtime = (stat(_))[9];
319 211 my $txt = File::Valet::rd_f($lockfile_name);
320  
321 211 ▶ if (!defined($txt)) { # handling potential race condition or naughty unreadable lockfile
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $txt) 11 200 211
322 11 ▶ if ((time() - $tm_start) > $opt{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $tm_start > $opt{"limit"}) 0 11 11
323 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'lockfile racy or unreadable', $lockfile_name, 0);
324 0 return 0;
325 }
326 11 select(undef, undef, undef, $opt{sleep_duration});
327 11 next;
328 }
329  
330 200 chomp($txt);
331 200 ▶ if ($txt =~ /^\d+\t/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt =~ /^\d+\t/) 186 14 200
332 186 my ($pid, $lock_duration, $message, $whence) = split(/\t/, $txt);
333 186 ▶ $lock_duration = 30 unless (defined($lock_duration));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lock_duration 0 186 186
334 186 my $locking_process_still_lives = kill(0, $pid);
335 # TODO - Potential race condition; another process might acquire the expired lock after this second stat() and before unlink().
336 # Perhaps use senate? Slow in filesystem, but could use shm on systems which support SysV shared memory.
337 186 ▶ 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 186 186
Condition33%1/3
time > $mtime + $lock_duration or $locking_process_still_lives < 1
l!l&&r!l&&!r
00186
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
338 }
339 }
340  
341 200 ▶ if ((time() - $tm_start) > $opt{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $tm_start > $opt{"limit"}) 0 200 200
342 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0); # Not an error; simply unable to acquire lock within specified duration
343 0 return 0;
344 }
345  
346 200 select(undef, undef, undef, $opt{sleep_duration});
347 }
348 1600 my $msg = sprintf("\%d\t\%d\t%s\t%s\n", $$, $opt{nsec}, $opt{msg}, $0); # populating lockfile with information about locking process
349 1600 syswrite($lockfile_fh, $msg);
350 1600 close($lockfile_fh);
351 1600 $LOCKS_HASH{$f} = 1;
352 1600 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
353 1600 return 1;
354 }
355  
356 sub unlockafile {
357 1600 ▶ my ($f, %opt) = @_;
Subroutine unlockafile: called CC=9 SCAR=27.3
Pod documented
358 1600 my $lockfile_fh;
359 my $dgram;
360 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
361 1600 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
362  
363 # TODO - This is fast and simple, but fails to handle expired lockfiles
364 1600 ▶ if ($LOCKS_HASH{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Valet::LOCKS_HASH{$f}) 1600 0 1600
365 1600 $LOCKS_HASH{$f}--;
366 1600 ▶ return 1 if ($LOCKS_HASH{$f} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $File::Valet::LOCKS_HASH{$f} > 0 0 1600 1600
367 }
368 1600 $! = 0;
369 1600 ▶ unless (sysopen($lockfile_fh, $lockfile_name, &O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $lockfile_fh, $lockfile_name, &O_RDONLY) 0 1600 1600
370 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'open failure', $!, 0+$!);
371 0 return 0;
372 }
373 1600 ▶ unless(my $result = sysread($lockfile_fh, $dgram, 4095)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (my $result = sysread($lockfile_fh, $dgram, 4095)) 0 1600 1600
374 0 ▶ if (defined($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 0 0 0
375 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read zero bytes from lockfile', '', 0+$!);
376 } else {
377 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read error', $!, 0+$!);
378 }
379 0 return 0;
380 }
381 1600 chomp($dgram);
382 1600 my ($lpid, $nsec, $msg, $whence) = split(/\t/, $dgram);
383 1600 close($lockfile_fh);
384 1600 $LOCKS_HASH{$f} = 0;
385 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
386 # oops! not ours anymore
387 0 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'lost lock', $dgram, 0+$!);
388 0 return 0;
389 }
390 1600 unlink($lockfile_name);
391 1600 return 1;
392 }
393  
394 sub unlock_all_the_files {
395 # Best effort only .. eh, make that "some effort only".
396 # That might still be overly charitable.
397 # Truth be told, you're only using this function if you can't be assed to fix the bugs in your own code,
398 # which relieves me somewhat of any moral imperative.
399 0 ▶ my $n_locks = 0;
Subroutine unlock_all_the_files: not called CC=4 SCAR=30.0
Pod documented
400 0 my $n_files = 0;
401 0 my $n_errors = 0;
402 0 foreach my $f (keys %LOCKS_HASH) {
403 0 ▶ next unless ($LOCKS_HASH{$f});
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Valet::LOCKS_HASH{$f} 0 0 0
404 0 $n_locks += $LOCKS_HASH{$f};
405 0 $LOCKS_HASH{$f} = 0;
406 0 unlockafile($f);
407 0 ▶ $n_errors++ unless ($OK eq 'OK');
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Valet::OK eq "OK" 0 0 0
408 0 $n_files++;
409 }
410 0 return ($n_errors, $n_locks, $n_files);
411 }
412  
413 20 1;
414  
415 =head1 NAME
416  
417 File::Valet - Utilities for file slurping, locking, and finding.
418  
419 =head1 SYNOPSIS
420  
421 use File::Valet;
422  
423 # Simple slurp and unslurp with rd_f, wr_f, ap_f:
424  
425 my $text = rd_f('some/file.txt');
426 die "slurp failure: $File::Valet::ERROR ($File::Valet::ERRNO)" unless ($File::Valet::OK eq 'OK');
427 # or, equivalently:
428 die "slurp failure: $File::Valet::ERROR ($File::Valet::ERRNO)" unless (defined($text));
429  
430 # Contents written will be same as that of "some/file.txt",
431 # plus two lines appended at the end:
432  
433 wr_f('another/file.txt', $text);
434 ap_f('another/file.txt', "Oh, and another thing:\n");
435 ap_f('another/file.txt', "STOP BREATHING IN MY CUP\n");
436  
437 # Find a place suited to temporary files:
438 my $tmp = find_temp(); # Likely /var/tmp or /tmp or C:\Windows\Temp
439  
440 # Find the full pathname of an executable:
441 my $shell = find_bin('sh'); # Likely /bin/sh
442  
443 # Use a lockfile for exclusive access to a shared resource:
444 lockafile("$tmp/shared.txt") or die "cannot obtain lock: $File::Valet::ERROR ($File::Valet::ERRNO)";
445 my $text = rd_f("$tmp/shared.txt");
446 unlockafile("$tmp/shared.txt") or die "unlock error: $File::Valet::ERROR ($File::Valet::ERRNO)";
447  
448 # Nested file locking:
449 lockafile("shared.txt") or die "cannot obtain first lock";
450 my $text = rd_f("$tmp/shared.txt");
451 ...
452 lockafile("shared.txt") or die "cannot obtain second lock";
453 ap_f("$tmp/shared.txt", $data);
454 unlockafile("$tmp/shared.txt");
455 ...
456 unlockafile("$tmp/shared.txt");
457  
458 # Your code has bugs, resulting in leaving lockfiles behind, but
459 # instead of debugging you'd rather just remove all your locks:
460 my ($n_errors, $n_locks, $n_files) = unlock_all_the_files();
461  
462 =head1 DESCRIPTION
463  
464 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.
465  
466 =head1 FUNCTIONS
467  
468 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.
469  
470 =over 4
471  
472 =item B<rd_f>
473  
474 my $string = rd_f($filename);
475  
476 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.
477  
478 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.
479  
480 =item B<wr_f>
481  
482 my $success = wr_f($filename, $string);
483  
484 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>.
485  
486 If the specified file does not exist, C<wr_f()> will attempt to create it.
487  
488 Returns 1 on success, or 0 on any failure, and sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
489  
490 =item B<ap_f>
491  
492 my $success = ap_f($filename, $string);
493  
494 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.
495  
496 If the specified file does not exist, C<ap_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<find_home>
501  
502 my $path = find_home;
503 my $path = find_temp("/var/home", "/tmp/home");
504  
505 C<find_home()> performs a best-effort search for the effective user's home, returning a path-string or undef if none is found.
506  
507 If arguments are provided, it will return the first argument for which there is a directory for which the user has write permissions.
508  
509 if C<$ENV{HOME}> is set, C<find_home()> will check there for a writable directory after checking any arguments.
510  
511 Some effort has been made to make it cross-platform.
512  
513 =item B<find_temp>
514  
515 my $path = find_temp();
516 my $path = find_temp("/home/tmp", "/usr/tmp", ...);
517  
518 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.
519  
520 If parameters are passed to C<find_temp()>, it will check those locations first.
521  
522 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.
523  
524 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.
525  
526 =item B<find_home>
527  
528 my $path = find_home();
529 my $path = find_home("/var/home/fred");
530  
531 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.
532  
533 It will return the full absolute path of the home directory on success, or undef on failure.
534  
535 =item B<find_bin>
536  
537 my $pathname = find_bin("ls");
538 my $pathname = find_bin("ls", "/home/ttk/bin", "/opt/bin", ...);
539  
540 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.
541  
542 C<find_bin()> is Windows-savvy, albeit does not search Windows systems as extensively as others.
543  
544 If directory paths are given as additional parameters, C<find_bin()> will check those locations first.
545  
546 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.
547  
548 C<find_bin()> also sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
549  
550 =item B<lockafile>
551  
552 my $success = lockafile("/tmp/foo", %options);
553 my $success = lockafile("/tmp/foo",
554 limit => 2.0, # keep retrying for 2.0 seconds before giving up
555 msg => 'in-channel update', # helpful message for troubleshooting
556 nsec => 0.5, # we expect to hold the lock for less than 0.5 seconds
557 );
558  
559 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.
560  
561 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.
562  
563 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.
564  
565 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).
566  
567 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.
568  
569 Returns 1 on success, or 0 on any failure, and sets C<$File::Valet::OK>, C<$File::Valet::ERROR>, C<$File::Valet::ERRNO> appropriately.
570  
571 =item B<unlockafile>
572  
573 my $success = unlockafile("/tmp/foo", %options);
574  
575 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).
576  
577 C<unlockafile()> will fail if invoked on a file which is not locked, or has been locked by a different process.
578  
579 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.
580  
581 =item B<unlock_all_the_files>
582  
583 C<unlock_all_the_files()> is a convenience wrapper for walking C<%File::Valet::LOCKS_HASH> and safely removing all lockfiles.
584  
585 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.
586  
587 Really, though, you should fix your bugs.
588  
589 Returns three values: A count of errors returned by C<unlockafile()>, a count of locks removed, and a count of lock files removed.
590  
591 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.
592  
593 =item B<LOCKFILE FORMAT>
594  
595 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.
596  
597 Its fields are tab-delimited, and the file terminates with a newline. They appear in this order:
598  
599 * Process identifier of the process which created the lockfile (per "$$"),
600 * The number of seconds the lock should be considered valid (per "nsec" parameter),
601 * The helpful message provided by the programmer (per "msg" parameter),
602 * The name of the program which created the lockfile (per "$0")
603  
604 Example:
605  
606 "4873\t2.0\tThe programmer is lame\t/opt/simon/bin/simond\n"
607  
608 These fields may change in future versions of this module.
609  
610 =item B<FURTHER DEVELOPMENT>
611  
612 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.
613  
614 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.
615  
616 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.
617  
618 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>.
619  
620 =item B<SEE ALSO>
621  
622 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.
623  
624 L<Path::Tiny|https://metacpan.org/pod/Path::Tiny> - a different style for file handling, which some people might prefer.
625  
626 L<File::Temp|https://metacpan.org/pod/File::Temp> - returns name and handle of a temporary file.
627  
628 L<File::SearchPath|https://metacpan.org/pod/File::SearchPath> - searches an environment path for a file.
629  
630 L<Lock::File|https://metacpan.org/pod/Lock::File> - file locker with an automatic out-of-scope unlocking mechanism.
631  
632 L<NL::File::Lock|https://metacpan.org/pod/NL::File::Lock> - file locker with timeout, but no lock expiration.
633  
634 L<File::Lock::Multi|https://metacpan.org/pod/File::Lock::Multi> - file locker with support for nested locks.
635  
636 L<File::TinyLock|https://metacpan.org/pod/File::TinyLock> - a very easy to use file locker.
637  
638 L<Data::Munge|https://metacpan.org/pod/Data::Munge> - a gaggle of useful functions, including a simple slurp().
639  
640 =back
641  
642 =head1 AUTHOR
643  
644 TTK Ciar
645  
646 =head1 LICENSE
647  
648 You can use and distribute this module under the same terms as Perl itself.
649  
650 =cut
« blib/lib/File/Valet.pm ↑ Summary