blib/lib/Net/FTP/Throttle.pm untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 325 CC 40 SCAR 56.9
CC
40
Cov
0%
_store_cmd
420.0
get
380.0
new
12.0
CRAP 295.8
1 package Net::FTP::Throttle;
2 use strict;
3 use warnings;
4 use Algorithm::TokenBucket;
5 use Carp;
6 use Fcntl qw(O_WRONLY O_RDONLY O_APPEND O_CREAT O_TRUNC);
7 use Net::FTP;
8 use Time::HiRes qw(sleep);
9 use base qw(Net::FTP);
10 our $VERSION = "0.32";
11  
12 # a lot of this code was stolen from Net::FTP
13  
14 BEGIN {
15  
16 # make a constant so code is fast'ish
17 my $is_os390 = $^O eq 'os390';
18 *trEBCDIC = sub () {$is_os390}
19 }
20  
21 sub new {
22 my $package = shift;
23 my $self = $package->SUPER::new(@_);
24  
25 return unless $self;
26  
27 my ( $peer, %arg );
28 if ( @_ % 2 ) {
29 $peer = shift;
30 %arg = @_;
31 } else {
32 %arg = @_;
33 $peer = delete $arg{Host};
34 }
35  
36 my $mbps = $arg{MegabitsPerSecond} || croak "No MegabitsPerSecond passed";
37 my $bps = $mbps * 1024 * 1024 / 8;
38  
39 my $bucket = new Algorithm::TokenBucket $bps, 10240;
40 ${*$self}{'tokenbucket'} = $bucket;
41 return $self;
42 }
43  
44 sub get {
45 my ( $ftp, $remote, $local, $where ) = @_;
46 my $bucket = ${*$ftp}{'tokenbucket'};
47  
48 my ( $loc, $len, $buf, $resp, $data );
49 local *FD;
50  
51 my $localfd = ref($local) || ref( \$local ) eq "GLOB";
52  
53 ( $local = $remote ) =~ s#^.*/##
54 unless ( defined $local );
55  
56 croak("Bad remote filename '$remote'\n")
57 if $remote =~ /[\r\n]/s;
58  
59 ${*$ftp}{'net_ftp_rest'} = $where if defined $where;
60 my $rest = ${*$ftp}{'net_ftp_rest'};
61  
62 delete ${*$ftp}{'net_ftp_port'};
63 delete ${*$ftp}{'net_ftp_pasv'};
64  
65 $data = $ftp->retr($remote)
66 or return undef;
67  
68 if ($localfd) {
69 $loc = $local;
70 } else {
71 $loc = \*FD;
72  
73 unless (
74 sysopen(
75 $loc, $local,
76 O_CREAT | O_WRONLY | ( $rest ? O_APPEND: O_TRUNC )
77 )
78 )
79 {
80 carp "Cannot open Local file $local: $!\n";
81 $data->abort;
82 return undef;
83 }
84 }
85  
86 if ( $ftp->type eq 'I' && !binmode($loc) ) {
87 carp "Cannot binmode Local file $local: $!\n";
88 $data->abort;
89 close($loc) unless $localfd;
90 return undef;
91 }
92  
93 $buf = '';
94 my ( $count, $hashh, $hashb, $ref ) = (0);
95  
96 ( $hashh, $hashb ) = @$ref
97 if ( $ref = ${*$ftp}{'net_ftp_hash'} );
98  
99 my $blksize = ${*$ftp}{'net_ftp_blksize'};
100 local $\; # Just in case
101  
102 while (1) {
103 last unless $len = $data->read( $buf, $blksize );
104  
105 sleep 0.01 until $bucket->conform($len);
106 $bucket->count($len);
107  
108 if ( trEBCDIC && $ftp->type ne 'I' ) {
109 $buf = $ftp->toebcdic($buf);
110 $len = length($buf);
111 }
112  
113 if ($hashh) {
114 $count += $len;
115 print $hashh "#" x ( int( $count / $hashb ) );
116 $count %= $hashb;
117 }
118 unless ( print $loc $buf ) {
119 carp "Cannot write to Local file $local: $!\n";
120 $data->abort;
121 close($loc)
122 unless $localfd;
123 return undef;
124 }
125 }
126  
127 print $hashh "\n" if $hashh;
128  
129 unless ($localfd) {
130 unless ( close($loc) ) {
131 carp "Cannot close file $local (perhaps disk space) $!\n";
132 return undef;
133 }
134 }
135  
136 unless ( $data->close() ) # implied $ftp->response
137 {
138 carp "Unable to close datastream";
139 return undef;
140 }
141  
142 return $local;
143 }
144  
145 sub _store_cmd {
146 my ( $ftp, $cmd, $local, $remote ) = @_;
147 my ( $loc, $sock, $len, $buf );
148 local *FD;
149 my $bucket = ${*$ftp}{'tokenbucket'};
150  
151 my $localfd = ref($local) || ref( \$local ) eq "GLOB";
152  
153 unless ( defined $remote ) {
154 croak 'Must specify remote filename with stream input'
155 if $localfd;
156  
157 require File::Basename;
158 $remote = File::Basename::basename($local);
159 }
160 if ( defined ${*$ftp}{'net_ftp_allo'} ) {
161 delete ${*$ftp}{'net_ftp_allo'};
162 } else {
163  
164 # if the user hasn't already invoked the alloc method since the last
165 # _store_cmd call, figure out if the local file is a regular file(not
166 # a pipe, or device) and if so get the file size from stat, and send
167 # an ALLO command before sending the STOR, STOU, or APPE command.
168 my $size
169 = do { local $^W; -f $local && -s _ }; # no ALLO if sending data from a pipe
170 $ftp->_ALLO($size) if $size;
171 }
172 croak("Bad remote filename '$remote'\n")
173 if $remote =~ /[\r\n]/s;
174  
175 if ($localfd) {
176 $loc = $local;
177 } else {
178 $loc = \*FD;
179  
180 unless ( sysopen( $loc, $local, O_RDONLY ) ) {
181 carp "Cannot open Local file $local: $!\n";
182 return undef;
183 }
184 }
185  
186 if ( $ftp->type eq 'I' && !binmode($loc) ) {
187 carp "Cannot binmode Local file $local: $!\n";
188 return undef;
189 }
190  
191 delete ${*$ftp}{'net_ftp_port'};
192 delete ${*$ftp}{'net_ftp_pasv'};
193  
194 $sock = $ftp->_data_cmd( $cmd, $remote )
195 or return undef;
196  
197 $remote = ( $ftp->message =~ /FILE:\s*(.*)/ )[0]
198 if 'STOU' eq uc $cmd;
199  
200 my $blksize = ${*$ftp}{'net_ftp_blksize'};
201  
202 my ( $count, $hashh, $hashb, $ref ) = (0);
203  
204 ( $hashh, $hashb ) = @$ref
205 if ( $ref = ${*$ftp}{'net_ftp_hash'} );
206  
207 while (1) {
208 last unless $len = read( $loc, $buf = "", $blksize );
209  
210 sleep 0.01 until $bucket->conform($len);
211 $bucket->count($len);
212  
213 if ( trEBCDIC && $ftp->type ne 'I' ) {
214 $buf = $ftp->toascii($buf);
215 $len = length($buf);
216 }
217  
218 if ($hashh) {
219 $count += $len;
220 print $hashh "#" x ( int( $count / $hashb ) );
221 $count %= $hashb;
222 }
223  
224 my $wlen;
225 unless ( defined( $wlen = $sock->write( $buf, $len ) )
226 && $wlen == $len )
227 {
228 $sock->abort;
229 close($loc)
230 unless $localfd;
231 print $hashh "\n" if $hashh;
232 return undef;
233 }
234 }
235  
236 print $hashh "\n" if $hashh;
237  
238 close($loc)
239 unless $localfd;
240  
241 $sock->close()
242 or return undef;
243  
244 if ( 'STOU' eq uc $cmd
245 and $ftp->message =~ m/unique\s+file\s*name\s*:\s*(.*)\)|"(.*)"/ )
246 {
247 require File::Basename;
248 $remote = File::Basename::basename($+);
249 }
250  
251 return $remote;
252 }
253  
254 1;
255  
256 __END__