Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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";

In reply to file read timeout by 0xbeef

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-07-01 13:13 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (98 votes). Check out past polls.