diff options
author | Eric Wong <e@80x24.org> | 2023-11-02 09:35:38 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2023-11-03 06:39:44 +0000 |
commit | 8d86e5b49d7f5d6e0b1768d0bd55fd21df36f86e (patch) | |
tree | 74c292ea35d4963525327cc5f7e64dc975156fee /lib/PublicInbox/IO.pm | |
parent | 3aa444b4c4eef1c40a49e5db191eb844c6624b58 (diff) | |
download | public-inbox-8d86e5b49d7f5d6e0b1768d0bd55fd21df36f86e.tar.gz |
The IO package seems like a better home for I/O subs than the Git package. We lose the 60 second read timeout for `git cat-file --batch-*' processes since it's probably not necessary given how reliable the code has proven and things would fall over hard in other ways if the storage device were completely hosed.
Diffstat (limited to 'lib/PublicInbox/IO.pm')
-rw-r--r-- | lib/PublicInbox/IO.pm | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/PublicInbox/IO.pm b/lib/PublicInbox/IO.pm index 4c92566d..0d303500 100644 --- a/lib/PublicInbox/IO.pm +++ b/lib/PublicInbox/IO.pm @@ -6,10 +6,9 @@ package PublicInbox::IO; use v5.12; use parent qw(IO::Handle Exporter); use PublicInbox::DS qw(awaitpid); -our @EXPORT_OK = qw(write_file); - -# TODO: this can probably be the new home for read_all, try_cat -# and maybe even buffered read/readline... +our @EXPORT_OK = qw(poll_in read_all try_cat write_file); +use Carp qw(croak); +use IO::Poll qw(POLLIN); sub waitcb { # awaitpid callback my ($pid, $errref, $cb, @args) = @_; @@ -59,4 +58,23 @@ sub write_file ($$@) { # mode, filename, LIST (for print) defined(wantarray) && !wantarray ? $fh : close $fh; } +sub poll_in ($;$) { + IO::Poll::_poll($_[1] // -1, fileno($_[0]), my $ev = POLLIN); +} + +sub read_all ($;$$) { + use autodie qw(read); + my ($io, $len, $bref) = @_; + $bref //= \(my $buf); + my $r = read($io, $$bref, $len //= -s $io); + croak("read($io) ($r != $len)") if $len != $r; + $$bref; +} + +sub try_cat ($) { + my ($path) = @_; + open(my $fh, '<', $path) or return ''; + read_all $fh; +} + 1; |