| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=3 sw=3 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::Endpoint::HTTP::Interface::File::Base; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $that = shift; | ||
| 14 | my $proto = ref($that) || $that; | ||
| 15 | my $self = { @_ }; | ||
| 16 | |||
| 17 | bless($self, $proto); | ||
| 18 | |||
| 19 | return $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub open { | ||
| 23 | my ($self, $mode, $path) = @_; | ||
| 24 | |||
| 25 | CORE::open(my $fh, $mode, $path) or die($!); | ||
| 26 | CORE::close($fh); | ||
| 27 | |||
| 28 | return 1; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub read { | ||
| 32 | my ($self, $file, $start, $len) = @_; | ||
| 33 | |||
| 34 | CORE::open(my $fh, "<", $file) or die($!); | ||
| 35 | CORE::seek($fh, $start, 0); | ||
| 36 | my $buf; | ||
| 37 | sysread($fh, $buf, $len); | ||
| 38 | CORE::close($fh); | ||
| 39 | |||
| 40 | return $buf; | ||
| 41 | } | ||
| 42 | |||
| 43 | # this seems odd, but "write" is not allowed as an action | ||
| 44 | sub write_fh { | ||
| 45 | my ($self, $file, $start, $buf) = @_; | ||
| 46 | |||
| 47 | CORE::open(my $fh, "+<", $file) or die($!); | ||
| 48 | CORE::seek($fh, $start, 0); | ||
| 49 | my $bytes_written = syswrite($fh, $buf); | ||
| 50 | CORE::close($fh); | ||
| 51 | |||
| 52 | if(! defined $bytes_written) { die("Error writing to filehandle."); } | ||
| 53 | |||
| 54 | return $bytes_written; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub seek { | ||
| 58 | my $self = shift; | ||
| 59 | return 1; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub close { | ||
| 63 | my $self = shift; | ||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | 1; |