#!/usr/bin/perl -w use strict; use lib './'; use Fcntl qw( ); use Futils qw( n ); use vars qw( $a $b ); BEGIN { $| = 1 } @Futils::onflockfail = qw( --undef ); my($f) = Futils->new(); my($SL) = $f->SL(); my($fh) = undef; print( q[Creating temporary testbed directory... ] ); $f->make_dir(qq[.${\$SL}Futils-testbed]); print( q[Creating temp file for I/O testing...] . n . n ); my($tmpf) = qq[.${\$SL}Futils-testbed${\$SL}tmptst]; print( q[Printing output of Perl's built-in time() function to the file,] ); $f->write_file('file' => $tmpf, 'content' => time . n); print( q[ closed file...] . n ); print( q[Re-opening file to a random handle in append mode...] . n ); $fh = $f->open_to_FH( 'file' => $tmpf, 'mode' => 'append' ); print( q[Random handle: ] . $fh ); print( q[, handle's descriptor no: ] . ( fileno($fh) || q[error! filehandle no longer open] ) . n ); print( q[Printing 'hello world' to the new filehandle...] . n ); print( $fh 'hello world' . n ); ($f->can_flock) ? print( n . q[Testing flock() on the new filehandle... ] ) : print( n . q[Your system can't flock(). Skipping flock() tests.] ); # =%=%=% FORKING!! %=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% if ( $f->can_flock() ) { my($pid) = fork; $| = 1; die(qq[Can't fork: $!]) unless defined($pid); if (!$pid) { $f->ftruncate($tmpf); exit } else { waitpid( $pid, 0 ) } # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% DONE WITH THAT. %=%=% if (-s $tmpf) { print(<<__stat__) } SUCCEEDED! Victory! __stat__ else { print(<<__stat__) } FAILED. That sucks, man. Sorry :O( __stat__ } print( qq[Now closing file opened to handle $fh] . n ); close($fh); print( q[Re-opening file to a random handle in append mode...] . n ); my($fh2) = $f->open_to_FH( 'file' => $tmpf, 'mode' => 'append' ); print( qq[File opened to handle $fh2] ); print( q[ descriptor no. ] . ( fileno($fh2) || q[error! filehandle no longer open] ) . n ); print( q[Printing 'goodbye world' to the new filehandle...] . n ); print( $fh2 'goodbye world' . n ); print( q[Closing file opened to handle ] . $fh2 . n ); close($fh2); print( q[Appending output of Perl's built-in time() function to the file,] ); $f->write_file( 'file' => $tmpf, 'content' => time . n, 'mode' => 'append' ); print( q[ closed file...] . n ); print( n . q[tempfile contents (including newlines) between dashes: ] . n . q[--] . n . $f->load_file($tmpf) . q[--] . n . n ); print(q[tempfile is a...............] . $f->file_type($tmpf) . q[ file] . n); print(q[tempfile size...............] . $f->fsize($tmpf) . q[ bytes] . n ); print(q[tempfile contains...........] . $f->line_count($tmpf) . q[ lines] . n); print(q[tempfile last modified at...] . $f->last_mod( $tmpf, '--short' ) . n); print(q[tempfile created at.........] . $f->created( $tmpf, '--short' ) . n ); print(q[tempfile last accessed at...] . $f->last_access($tmpf, '--mdy') . n ); print( q[Testing Futils::ftruncate() on tempfile... ] ); $f->ftruncate($tmpf); (-s $tmpf) ? print( q[FAILED!] . n ) : print( q[SUCCEEDED.] . n ); print( n . q[Present working directory contains ]); my(@files) = $f->list_dir( $INC[3], '--files-only' ); print( scalar(@files) . q[ files, and ] ); @files = $f->list_dir( $INC[3], '--dirs-only', '--no-fsdots' ); print( scalar(@files) . q[ directories:] . n ); @files = $f->list_dir( $INC[3], '--sl-after-dirs', '--no-fsdots' ); my(@lines) = (); while (my(@six) = splice(@files,0,6)) { push(@lines, sprintf(' % -12.12s' x 6,@six)); } print(join(n,@lines) . n . n); my($newdir) = qq[.${\$SL}Futils-testbed${\$SL}tmp] . $SL . int(rand(time)) . $SL . int(rand(time)) . $SL . int(rand(time)) . $SL . int(rand(time)); print( q[Making a set of 4 nested subdirectories in the testbed...] . n ); $f->make_dir($newdir); print(q[deepest directory: ] . n ); print(q[ -system path: ] . $newdir . n ); print(q[ -path escaped: ] . $f->escape_filename($newdir) . n); print(q[ -path stripped: ] . $f->strip_path($newdir) . n); my(@dirs) = $f->list_dir( qq[.${\$SL}Futils-testbed${\$SL}tmp], '--follow' ); print( n . q[Removing test directories... ] . n ); foreach (reverse( sort({ length($a) <=> length($b) } @dirs)) ) { print(qq[ Removing $_] . n); rmdir($_) or die ($!); } print(q[ Removing temp file... ] ); unlink($tmpf) or die($!); print( q[ Removing testbed directory...] ); rmdir(qq[.${\$SL}Futils-testbed${\$SL}tmp]) or die($!); rmdir(qq[.${\$SL}Futils-testbed]) or die($!,$^E); print( ' done.', n . n ); exit;