| 1 | # SMB-Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org | ||
| 2 | # | ||
| 3 | # This program is free software: you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation, either version 3 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | |||
| 16 | package SMB::OpenFile; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use parent 'SMB'; | ||
| 22 | |||
| 23 | use Fcntl 'SEEK_SET'; | ||
| 24 | use SMB::File; | ||
| 25 | |||
| 26 | sub new ($$$$%) { | ||
| 27 | my $class = shift; | ||
| 28 | my $file = shift || die "No file\n"; | ||
| 29 | my $handle = shift || 0; | ||
| 30 | my $action = shift || SMB::File::ACTION_NONE; | ||
| 31 | my %options = @_; | ||
| 32 | |||
| 33 | my $self = $class->SUPER::new( | ||
| 34 | file => $file, | ||
| 35 | handle => $handle, | ||
| 36 | action => $action, | ||
| 37 | last_index => 0, | ||
| 38 | %options, | ||
| 39 | ); | ||
| 40 | |||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub close ($) { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | $self->file->delete_openfile($self); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub read ($%) { | ||
| 51 | my $self = shift; | ||
| 52 | my %params = @_; # length offset minlen remain | ||
| 53 | |||
| 54 | my $fh = $self->{handle} or return; | ||
| 55 | sysseek($fh, $params{offset} || 0, SEEK_SET) or return; | ||
| 56 | |||
| 57 | my $length = $params{length} // return; | ||
| 58 | my $minlen = $params{minlen} || 0; | ||
| 59 | |||
| 60 | my $buffer; | ||
| 61 | sysread($fh, $buffer, $length) // return; | ||
| 62 | return unless length($buffer) < $minlen; | ||
| 63 | |||
| 64 | return $buffer; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |