blib/lib/File/Valet.pm untested

filter: Statementstmt 0.0% 0 / 300 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 69 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 554 CC 75 SCAR 42.9
CC
75
Cov
0%
lockafile
182.0
find_home
110.0
find_temp
110.0
CRAP 72.8
1 package File::Valet;
2 use 5.010;
3 use strict;
4 use warnings;
5 use Config; # Provides OS-portable means of determining platform type
6 use POSIX;
7 use File::Basename qw(fileparse);
8 use File::Copy;
9 use vars qw(@EXPORT @EXPORT_OK @ISA $VERSION);
10  
11 BEGIN {
12 require Exporter;
13 @ISA = qw(Exporter);
14 $VERSION = '1.10';
15 @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 our $OK = 'OK'; # one of "OK", "WARNING" or "ERROR", reflecting most recently performed operation
19 our $ERROR = ''; # short invariant description of error, or empty string if none
20 our $ERRNO = ''; # variant description of error (such as $!), or empty string if none
21 our $ERRNUM = 0; # numerical variant description of error (such as $!), or empty string if none, undocumented, only used for unit tests
22 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 my ($from, $to) = @_;
29  
30 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
31 return 'OK' if (rename ($from, $to));
32  
33 my $result = File::Copy::syscopy($from, $to);
34 unless ($result) {
35 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'File::Copy::syscopy failed', $!, 0+$!);
36 return undef;
37 }
38  
39 my @st = stat($from);
40 unless (@st) {
41 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'after-copy stat failed', $!, 0+$!);
42 return undef;
43 }
44  
45 my ($atime, $mtime) = (@st)[8,9];
46 unless (utime($atime, $mtime, $to)) {
47 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'after-copy utime failed', $!, 0+$!);
48 return undef;
49 }
50  
51 unless (unlink($from)) {
52 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'after-copy unlink failed', $!, 0+$!);
53 return undef;
54 }
55 return 'OK';
56 }
57  
58 sub rename_vms {
59 my ($fn, $dest) = @_;
60 if (!defined($fn) || ($fn eq '')) {
61 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
62 return undef;
63 }
64 if (!defined($dest) || ($dest eq '')) {
65 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no destination directory supplied', -1, 0);
66 return undef;
67 }
68 my $dest_fn = $fn;
69 if (!-d $dest) {
70 ($dest, $dest_fn) = fileparse($dest);
71 }
72 if (!-e $dest) {
73 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'destination directory does not exist', -1, 0);
74 return undef;
75 }
76 if (!-d _) {
77 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'destination directory is not a directory', -1, 0);
78 return undef;
79 }
80 if (!-w _) {
81 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'destination directory is not writable', -1, 0);
82 return undef;
83 }
84  
85 if (!-e "$dest/$dest_fn") {
86 # degenerate case; just rename it.
87 return _rename($fn, "$dest/$dest_fn");
88 }
89  
90 my $i = 1;
91 $i++ while(-e "$dest/$dest_fn.$i");
92 return _rename($fn, "$dest/$dest_fn.$i");
93 }
94  
95 sub rd_f {
96 my ($fn) = @_;
97 my ($fh, $buf);
98 if (!defined($fn) || ($fn eq '')) {
99 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
100 return undef;
101 }
102 $! = 0;
103 unless (open($fh, '< :raw', $fn)) {
104 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', "cannot open for reading", $!, 0+$!);
105 return undef;
106 }
107 binmode($fh);
108 my $file_size = (stat($fn))[7];
109 if ($file_size) {
110 my $n_bytes = sysread($fh, $buf, $file_size);
111 if (!defined($n_bytes)) {
112 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read failed', $!, 0+$!);
113 return undef;
114 }
115 elsif ($n_bytes != $file_size) {
116 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read underflow', $!, 0+$!);
117 return undef;
118 }
119 }
120 else {
121 my $res = sysread($fh, $buf, 0x7FFFFFFF);
122 if (!defined $res) {
123 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read failed', $!, 0+$!);
124 return undef;
125 }
126 }
127 my $res = close($fh);
128 unless ($res) {
129 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'close failed', $!, 0+$!);
130 return undef;
131 }
132 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
133 return $buf;
134 }
135  
136 sub wr_f {
137 my ($fn, $buf) = @_;
138 my $fh;
139 if (!defined($fn) || ($fn eq '')) {
140 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
141 return undef;
142 }
143 $! = 0;
144 unless (open($fh, '> :raw', $fn)) {
145 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', "cannot open for writing", $!, 0+$!);
146 return undef;
147 }
148 binmode($fh);
149 my $res = syswrite($fh, $buf);
150 unless (defined $res) {
151 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'write error', $!, 0+$!);
152 return undef;
153 }
154 $res = close($fh);
155 unless ($res) {
156 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'close failed', $!, 0+$!);
157 return undef;
158 }
159 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
160 return 'OK';
161 }
162  
163 sub ap_f {
164 my ($fn, $buf) = @_;
165 my $fh;
166 if (!defined($fn) || ($fn eq '')) {
167 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'no filename supplied', -1, 0);
168 return undef;
169 }
170 $! = 0;
171 unless (open($fh, '>> :raw', $fn)) {
172 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', "cannot open for appending", $!, 0+$!);
173 return undef;
174 }
175 binmode($fh);
176 my $res = syswrite($fh, $buf);
177 unless (defined $res) {
178 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'write error', $!, 0+$!);
179 return undef;
180 }
181 $res = close($fh);
182 unless ($res) {
183 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'close failed', $!, 0+$!);
184 return undef;
185 }
186 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
187 return 'OK';
188 }
189  
190 sub detect_windows {
191 return ($^O eq 'MSWin32' || $Config{'osname'} =~ /windows/i || $Config{'osname'} =~ /winserver/i || $Config{'osname'} =~ /microsoft/i) ? 1 : 0;
192 }
193  
194 sub find_home {
195 for my $d (@_) {
196 return $d if (defined $d && -d $d && -w _);
197 }
198  
199 my $is_windows = detect_windows;
200 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
201  
202 my $env_home = $ENV{HOME};
203 return $env_home if (defined $env_home && -d $env_home);
204  
205 my $username = $ENV{USER} // $ENV{USERNAME};
206 if ($is_windows) {
207 my $home_drive = $ENV{HOMEDRIVE} // 'C:';
208 my $home_path = $ENV{HOMEPATH};
209 if (defined $home_path) {
210 $env_home = $home_drive . $home_path;
211 }
212 elsif (defined $username) {
213 $env_home = $home_drive . '\\Users\\' . $username;
214 }
215 return $env_home if (defined $env_home && -d $env_home);
216 } else {
217 my @row = getpwuid($<);
218 if (@row >= 9) {
219 my $home_dir = $row[7];
220 return $home_dir if (defined $home_dir && -d $home_dir);
221 }
222 return '/root' if (-d '/root' && -w '/root');
223 }
224  
225 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'cannot find home directory', $is_windows, 1);
226 return undef;
227 }
228  
229 sub find_temp {
230 my $is_windows = detect_windows;
231 my $dir_sep_tok = '/';
232 my $home_dir = find_home;
233  
234 push(@_, $ENV{TEMPDIR}) if (defined($ENV{TEMPDIR}));
235 push(@_, $ENV{TEMP}) if (defined($ENV{TEMP}));
236 push(@_, $ENV{TMP}) if (defined($ENV{TMP})); # set in Windows sometimes
237  
238 if ($is_windows) {
239 $dir_sep_tok = '\\';
240 push(@_, 'C:\\Windows\\Temp');
241 push(@_, 'D:\\Windows\\Temp');
242 foreach my $vol (qw(C D E F G W X Y Z)) {
243 push(@_, "$vol:\\Temp");
244 }
245 }
246 # might be CygWin, so adding these regardless of OS:
247 push(@_, qw (/var/tmp /tmp));
248  
249 push(@_, map {join($dir_sep_tok,("$home_dir",$_))} qw(.tmp .temp tmp temp), $home_dir) if (defined($home_dir));
250 push(@_, map {join($dir_sep_tok,("$ENV{PWD}", $_))} qw(.tmp .temp tmp temp), $ENV{PWD} ) if (defined($ENV{PWD} ));
251 push(@_, '/dev/shm') unless ($is_windows); # Lowest priority, since this is typically a ramdisk.
252 foreach my $d (@_) {
253 next unless (-d $d);
254 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
255 return $d if (-w _);
256 }
257 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no appropriate temporary directory found', '', 0);
258 return undef;
259 }
260  
261 sub find_bin {
262 return find_bin_win32 (@_) if ($Config::Config{osname} =~ /MSWin/);
263 my ($bin_name, @bin_dirs) = @_;
264 my $home_dir = find_home;
265 push(@bin_dirs, split(/\:/, $ENV{PATH})) if (defined($ENV{PATH}));
266 push(@bin_dirs, "$home_dir/bin") if (defined($home_dir));
267 push(@bin_dirs, ('/usr/local/sbin', '/usr/local/bin', '/sbin', '/bin', '/usr/sbin', '/usr/bin'));
268 my %been_there = ();
269 foreach my $d (@bin_dirs) {
270 next if (defined($been_there{$d}));
271 $been_there{$d} = 1;
272 my $f = "$d/$bin_name";
273 next unless (-x $f);
274 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
275 return $f;
276 }
277 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no executable found', '', 0);
278 return undef;
279 }
280  
281 sub find_bin_win32 {
282 my ($bin_name, @bin_dirs) = @_;
283 push(@bin_dirs, split(/\;/, $ENV{PATH})) if (defined($ENV{PATH}));
284 push(@bin_dirs, ('C:\\WINDOWS\\system32', 'C:\\WINDOWS'));
285 my %been_there = ();
286 foreach my $d (@bin_dirs) {
287 next if (defined($been_there{$d}));
288 $been_there{$d} = 1;
289 my $f = "$d\\$bin_name";
290 next unless (-x $f);
291 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
292 return $f;
293 }
294 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'no executable found', '', 0);
295 return undef;
296 }
297  
298 # returns 1 on great success, 0 on miserable failure
299 sub lockafile {
300 my ($f, %opt) = @_;
301 $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.
302 $opt{msg} = "programmer is lame" unless (defined($opt{msg}) && $opt{msg} ne ''); # Helpful message for the human to understand wtf this lock is about
303 $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.
304 $opt{sleep_duration} = 0.25 unless (defined($opt{sleep_duration}) && $opt{sleep_duration} > 0.0);
305 my $lockfile_name = $opt{lockfile_name} || "$f.lock";
306 my $tm_start = time();
307 my $lockfile_fh;
308  
309 # TODO - This is fast and simple, but fails to handle expired lockfiles and extending lockfile durations.
310 if ($LOCKS_HASH{$f}) {
311 $LOCKS_HASH{$f}++;
312 return 1;
313 }
314  
315 while (!sysopen($lockfile_fh, $lockfile_name, &O_RDWR | &O_CREAT | &O_EXCL)) {
316 if (-e $lockfile_name) {
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 my $mtime = (stat(_))[9];
319 my $txt = File::Valet::rd_f($lockfile_name);
320  
321 if (!defined($txt)) { # handling potential race condition or naughty unreadable lockfile
322 if ((time() - $tm_start) > $opt{limit}) {
323 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('WARNING', 'lockfile racy or unreadable', $lockfile_name, 0);
324 return 0;
325 }
326 select(undef, undef, undef, $opt{sleep_duration});
327 next;
328 }
329  
330 chomp($txt);
331 if ($txt =~ /^\d+\t/) {
332 my ($pid, $lock_duration, $message, $whence) = split(/\t/, $txt);
333 $lock_duration = 30 unless (defined($lock_duration));
334 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 unlink($lockfile_name) if ((time() > $mtime + $lock_duration) || ($locking_process_still_lives < 1));
338 }
339 }
340  
341 if ((time() - $tm_start) > $opt{limit}) {
342 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0); # Not an error; simply unable to acquire lock within specified duration
343 return 0;
344 }
345  
346 select(undef, undef, undef, $opt{sleep_duration});
347 }
348 my $msg = sprintf("\%d\t\%d\t%s\t%s\n", $$, $opt{nsec}, $opt{msg}, $0); # populating lockfile with information about locking process
349 syswrite($lockfile_fh, $msg);
350 close($lockfile_fh);
351 $LOCKS_HASH{$f} = 1;
352 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
353 return 1;
354 }
355  
356 sub unlockafile {
357 my ($f, %opt) = @_;
358 my $lockfile_fh;
359 my $dgram;
360 my $lockfile_name = $opt{lockfile_name} || "$f.lock";
361 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('OK', '', '', 0);
362  
363 # TODO - This is fast and simple, but fails to handle expired lockfiles
364 if ($LOCKS_HASH{$f}) {
365 $LOCKS_HASH{$f}--;
366 return 1 if ($LOCKS_HASH{$f} > 0);
367 }
368 $! = 0;
369 unless (sysopen($lockfile_fh, $lockfile_name, &O_RDONLY)) {
370 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'open failure', $!, 0+$!);
371 return 0;
372 }
373 unless(my $result = sysread($lockfile_fh, $dgram, 4095)) {
374 if (defined($result)) {
375 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read zero bytes from lockfile', '', 0+$!);
376 } else {
377 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'read error', $!, 0+$!);
378 }
379 return 0;
380 }
381 chomp($dgram);
382 my ($lpid, $nsec, $msg, $whence) = split(/\t/, $dgram);
383 close($lockfile_fh);
384 $LOCKS_HASH{$f} = 0;
385 if (defined($lpid) && ($lpid ne $$)) {
386 # oops! not ours anymore
387 ($OK, $ERROR, $ERRNO, $ERRNUM) = ('ERROR', 'lost lock', $dgram, 0+$!);
388 return 0;
389 }
390 unlink($lockfile_name);
391 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 my $n_locks = 0;
400 my $n_files = 0;
401 my $n_errors = 0;
402 foreach my $f (keys %LOCKS_HASH) {
403 next unless ($LOCKS_HASH{$f});
404 $n_locks += $LOCKS_HASH{$f};
405 $LOCKS_HASH{$f} = 0;
406 unlockafile($f);
407 $n_errors++ unless ($OK eq 'OK');
408 $n_files++;
409 }
410 return ($n_errors, $n_locks, $n_files);
411 }
412  
413 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