File Coverage

lib/App/MtAws/Command/Retrieve.pm
Criterion Covered Total %
statement 32 54 59.2
branch 0 10 0.0
condition 3 3 100.0
subroutine 9 12 75.0
pod 0 3 0.0
total 44 82 53.6


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::Command::Retrieve;
22              
23             our $VERSION = '1.114_2';
24              
25 2     2   1695 use strict;
  2         2  
  2         54  
26 2     2   6 use warnings;
  2         4  
  2         46  
27 2     2   7 use utf8;
  2         3  
  2         9  
28 2     2   30 use Carp;
  2         2  
  2         145  
29 2     2   437 use App::MtAws::ForkEngine qw/with_forks fork_engine/;
  2         3  
  2         96  
30 2     2   8 use App::MtAws::Utils;
  2         3  
  2         260  
31              
32 2     2   1054 use App::MtAws::QueueJob::Retrieve;
  2         5  
  2         42  
33 2     2   417 use App::MtAws::QueueJob::Iterator;
  2         4  
  2         806  
34              
35             sub next_retrieve
36             {
37 0     0 0 0 my ($filelistref) = @_;
38 0 0       0 if (my $rec = shift @{ $filelistref }) {
  0         0  
39 0         0 return App::MtAws::QueueJob::Retrieve->new(map { $_ => $rec->{$_}} qw/archive_id filename relfilename/ );
  0         0  
40             } else {
41 0         0 return;
42             }
43             }
44              
45             sub run
46             {
47 0     0 0 0 my ($options, $j) = @_;
48 0 0       0 confess unless $j->{use_active_retrievals};
49             with_forks !$options->{'dry-run'}, $options, sub {
50 0     0   0 $j->read_journal(should_exist => 1);
51              
52 0         0 my @filelist = get_file_list($options, $j);
53              
54 0 0       0 if (@filelist) {
55 0 0       0 if ($options->{'dry-run'}) {
56 0         0 for (@filelist) {
57 0         0 print "Will RETRIEVE archive $_->{archive_id} (filename $_->{relfilename})\n"
58             }
59             } else {
60 0         0 my $ft = App::MtAws::QueueJob::Iterator->new(iterator => sub { next_retrieve(\@filelist) });
  0         0  
61 0         0 $j->open_for_write();
62 0         0 my ($R) = fork_engine->{parent_worker}->process_task($ft, $j);
63 0 0       0 die unless $R;
64 0         0 $j->close_for_write();
65             }
66             } else {
67 0         0 print "Nothing to restore\n";
68             }
69             }
70 0         0 }
71              
72             sub get_file_list # TODO: optimize as lazy code
73             {
74 12     12 0 136 my ($options, $j) = @_;
75 12         13 my $files = $j->{journal_h};
76             # TODO: refactor
77             my @filelist =
78 91   100     2263 grep { !$j->{active_retrievals}{$_->{archive_id}} && ! -f binaryfilename $_->{filename} }
79 91         140 map { {archive_id => $_->{archive_id}, relfilename => $_->{relfilename}, filename=> $j->absfilename($_->{relfilename}) } }
80 91         116 map { $j->latest($_) } # TODO: two maps is not effective
81 12         11 keys %{$files};
  12         26  
82 12         551 @filelist = splice(@filelist, 0, $options->{'max-number-of-files'});
83             }
84              
85             1;
86              
87             __END__