File Coverage

lib/App/MtAws/QueueJob/MultipartPart.pm
Criterion Covered Total %
statement 66 66 100.0
branch 27 30 90.0
condition n/a
subroutine 15 15 100.0
pod 0 7 0.0
total 108 118 91.5


line stmt bran cond sub pod time code
1             # mt-aws-glacier - Amazon Glacier sync client
2             # Copyright (C) 2012-2014 Victor Efimov
3             # http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
4             # License: GPLv3
5             #
6             # This file is part of "mt-aws-glacier"
7             #
8             # mt-aws-glacier is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # mt-aws-glacier is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package App::MtAws::QueueJob::MultipartPart;
22              
23             our $VERSION = '1.114_2';
24              
25 14     14   86889 use strict;
  14         22  
  14         394  
26 14     14   61 use warnings;
  14         24  
  14         353  
27 14     14   57 use Carp;
  14         19  
  14         734  
28              
29 14     14   1748 use App::MtAws::QueueJobResult;
  14         26  
  14         887  
30 14     14   193 use App::MtAws::Exceptions;
  14         19  
  14         777  
31 14     14   4781 use App::MtAws::TreeHash;
  14         33  
  14         495  
32 14     14   71 use base 'App::MtAws::QueueJob';
  14         21  
  14         8624  
33              
34             sub init
35             {
36 47     47 0 68 my ($self) = @_;
37              
38 47 100       188 defined($self->{relfilename})||confess;
39 46 100       236 $self->{partsize}||confess;
40 44 100       179 defined($self->{mtime})||confess;
41 43 100       216 $self->{upload_id}||confess;
42 41 100       287 $self->{fh}||confess;
43 39 100       209 exists($self->{stdin})||confess;
44 37         59 $self->{all_raised} = 0;
45 37         50 $self->{position} = 0;
46 37         164 $self->{th} = App::MtAws::TreeHash->new();
47 37         73 $self->{uploadparts} = {};
48              
49 37         119 $self->enter('fist_part');
50             }
51              
52              
53             sub close_file
54             {
55 1     1 0 1938 my ($self) = @_;
56 1 50       24 close($self->{fh}) or confess;
57             }
58              
59             sub read_part
60             {
61 18     18 0 70401 my ($self) = @_;
62 18 100       236 if (my $r = read($self->{fh}, my $data, $self->{partsize})) {
63 11         52 my $part_th = App::MtAws::TreeHash->new(); #TODO: We calc sha twice for same data chunk here
64 11         2650 $part_th->eat_data(\$data);
65 11         2423 $part_th->calc_tree();
66              
67 11         2600 my $part_final_hash = $part_th->get_final_hash();
68 11         2281 my $start = $self->{position};
69             my $attachment = \$data,
70              
71 11         43 $self->{th}->eat_data(\$data);
72 11         2258 $self->{position} += $r;
73              
74 11         90 return (1, $start, $part_final_hash, $attachment);
75             } else {
76 7 100       40 die exception 'cannot_read_from_file' => "Cannot read from file errno=%errno%", 'ERRNO' unless defined $r;
77 6         22 return;
78             }
79              
80              
81             }
82              
83             sub get_part
84             {
85 451     451 0 493 my ($self) = @_;
86              
87 451         961 my ($ok, $start, $part_final_hash, $attachment) = $self->read_part;
88 451 100       2699 if ($ok) {
89 233         648 $self->{uploadparts}->{$start} = 1;
90             return task "upload_part",
91             {
92             start => $start,
93             upload_id => $self->{upload_id},
94             part_final_hash => $part_final_hash,
95             relfilename => $self->{relfilename},
96             mtime => $self->{mtime},
97             } => $attachment => sub {
98 233 50   233   736 delete $self->{uploadparts}->{$start} or confess;
99 233         1033 return;
100 233         1992 };
101             } else {
102 218         375 return;
103             }
104             }
105              
106             sub on_fist_part
107             {
108 31     31 0 41 my ($self) = @_;
109 31         80 my @res = $self->get_part();
110 31 50       74 confess "Unexpected: zero-size archive" unless @res;
111 31         78 return state("other_parts"), @res;
112             }
113              
114             sub on_other_parts
115             {
116 420     420 0 429 my ($self) = @_;
117 420         684 my @res = $self->get_part();
118 420 100       1201 return @res ? @res : (keys %{$self->{uploadparts}} ? JOB_WAIT : state('close'));
  218 100       1099  
119             }
120              
121             sub on_close
122             {
123 31     31 0 49 my ($self) = @_;
124 31 100       122 $self->{stdin} or $self->close_file; # close file after EOF found
125 31         90 state("done");
126             }
127              
128             1;