Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

file read timeout

by 0xbeef (Hermit)
on Aug 06, 2006 at 19:02 UTC ( #565863=perlquestion: print w/replies, xml ) Need Help??

0xbeef has asked for the wisdom of the Perl Monks concerning the following question:

The following code snippet provides a crude timer mechanism for file read failures, somewhat along the lines of what Re: Redirecting stdout/stderr to pipe achieves for commands. The real code will run on many systems and I deem the timeout as necessary, although I know read failures are uncommon.

I eliminated the use of sysread+select due to the fact that I need @$outref populated line-by-line (whereas sysread uses buffers). I also do not have any real ideas on encoding. Can anyone make any improvement suggestions or is this ok?


UPDATE: fixed an endless loop in the example.

#!/usr/bin/perl -w use strict; use IO::Select; use Symbol qw(gensym); my $g_verbose = 1; my $g_pid; my $g_stop; my $g_timeout = 2; #for test purposes my $g_did_run; my $g_max_cmd_size = 2_000_000; my $g_max_file_size = 2_000_000; my %g_pids; sub read_stat($$$) { my ($file,$rcref,$statref) = @_; @$statref = stat($file); if ( $#$statref < 0 ) { print "Stat of file '$file' failed: $!\n"; $$rcref = 1; } else { $$rcref = 0; } return $$rcref; } sub read_file($$$$$) { my ($file,$encoding,$rcref,$outref,$statref) = @_; @$outref = (); my $outbytes = 0; $g_stop = 0; # die if timeout occurs! local $SIG{ALRM} = sub { print "File \'$file\' collection reached deadline after $g_timeo +ut secs.\n"; die "Exiting - a read operation timed out!\n"; $g_stop = 1; }; alarm($g_timeout); if ( !defined($encoding) ) { $encoding = 'text/plain'; } if ( $encoding ne 'text/plain' ) { print "Error: Can't collect file '$file' - '$encoding' encoding +is not supported.\n"; $$rcref = -2; return $$rcref; } if ( $g_verbose ) { print "Collecting '$file'.\n"; } read_stat($file,$rcref,$statref); if ( $$rcref != 0 ) { return undef; } if ( !open(IFILE,"<$file") ) { print "Open of file '$file' failed: $!\n"; return undef; } # read data into @$outref (line by line). alarm prevents timeout. while (!$g_stop) { last if (eof(IFILE)); my $line = <IFILE>; chomp $line; push @$outref, $line; # for test purposes - make it timeout! sleep 5; $outbytes += length($line); if ($outbytes >= $g_max_file_size) { print "Maximum file size reached after $outbytes bytes\n"; last; alarm(0); close(IFILE); if (!$g_stop) { $$rcref = 0; } else { $$rcref = -1; } return $$rcref; } #MAIN my $file = '/etc/hosts'; my $encoding = 'text/plain'; my $rc; my (@cmdout,@stat); $rc = read_file($file,$encoding,\$rc,\@cmdout,\@stat); print "Collected $file (rc=$rc)\n";

Replies are listed 'Best First'.
Re: file read timeout
by cdarke (Prior) on Aug 07, 2006 at 15:27 UTC
    You seem to be missing at least two close curly brackets, one after the while loop and one after the if statement before the alarm.

    You return undef on error, so the final statement might be better written as:
    print "Collected $file (rc=". (defined $rc?$rc:'undef') . ")\n";
    You are mixing stdout (bare print) and stderr (die) messages. I suggest you only print error messages to stderr, use print STDERR, or warn, or incorporate into the die statement that follows.

    Your while loop could be simplified in a number of ways. For example:
    while (my $line = <FILE>) { chomp $line; push @$outref, $line; # for test purposes - make it timeout! sleep 5; $outbytes += length($line); if ($outbytes >= $g_max_file_size) { warn "Maximum file size reached after $outbytes bytes"; last; } }

    Assuming I put the close braces in the right place.
    Another approach might be to avoid the loop altogether by testing the file size before you start reading:
    my $file_size = -s IFILE; die "$file too large at $file_size bytes" if $file_size > $g_max_fi +le_size; @$outref = <IFILE>;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://565863]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-05-24 19:13 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (84 votes). Check out past polls.