Any/SCP/Putter.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 86 MC/DCmcdc 0.0% 0 / 47 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 390 CC 32 SCAR 40.8
CC
32
Cov
0%
run
210.0
_send_file
42.0
_remote_open
20.0
CRAP 59.1
1 package Net::SSH::Any::SCP::Putter;
2  
3 use strict;
4 use warnings;
5  
6 use Carp;
7 use Fcntl ();
8  
9 use Net::SSH::Any::Constants qw(SSHA_SCP_ERROR SSHA_REMOTE_CMD_ERROR);
10 use Net::SSH::Any::Util qw($debug _debug _debugf _debug_dump _debug_hexdump
11 _first_defined _inc_numbered _gen_wanted
12 _scp_escape_name _scp_unescape_name);
13  
14 require Net::SSH::Any::SCP::Base;
15 our @ISA = qw(Net::SSH::Any::SCP::Base);
16  
17 sub _new {
18 my ($class, $any, $opts, $target) = @_;
19 my $p = $class->SUPER::_new($any, $opts);
20 $p->{target} = $target;
21 $p->{recursive} = delete $opts->{recursive};
22 $p->{send_time} = delete $opts->{send_time};
23 $p;
24 }
25  
26 sub read_dir {}
27 sub _read_dir {
28 my ($p, $action) = @_;
29 $p->read_dir($action ? ($action, $action->{_handle}): ());
30 }
31  
32 sub open_dir {}
33 sub open_file {}
34 sub _open {
35 my ($p, $action) = @_;
36 my $method = "open_$action->{type}";
37 my $handle = $p->$method($action);
38 if (defined $handle) {
39 $action->{_handle} = $handle;
40 return 1;
41 }
42 else {
43 $p->set_local_error($action, "unable to open directory or file for $action->{path}");
44 return
45 }
46 }
47  
48 sub close_dir {}
49 sub close_file {}
50 sub _close {
51 my ($p, $action) = @_;
52 my $method = "close_$action->{type}";
53 $p->$method($action, delete $action->{_handle}) and return 1;
54 $p->set_local_error($action, "unable to close directory or file $action->{path}");
55 return
56 }
57  
58 sub _read_file {
59 my ($p, $action, $len) = @_;
60 $debug and $debug & 4096 and _debug_dump "_read_file action", $action;
61 $p->read_file($action, $action->{_handle}, $len);
62 }
63  
64 sub _send_line_and_get_response {
65 my ($p, $dpipe, $action, $line) = @_;
66 $debug and $debug & 4096 and
67 _debug_hexdump("writting line", $line);
68 my ($fatal, $error) = ( $dpipe->print($line)
69 ? $p->_read_response($dpipe)
70 : (2, "broken dpipe"));
71 if ($fatal) {
72 $p->set_remote_error($action, $error);
73 $fatal > 1 and $p->abort;
74 return;
75 }
76 return 1;
77 }
78  
79 sub _remote_open {
80 my ($p, $dpipe, $action) = @_;
81 my ($type, $perm, $size, $name) = @{$action}{qw(type perm size name)};
82 my $cmd = ($type eq 'dir' ? 'D' :
83 $type eq 'file' ? 'C' :
84 croak "bad action type $action->{type}");
85 $perm = (defined $perm ? $perm : 0777) & 0777;
86 $debug and $debug & 4096 and
87 _debugf("remote_open type: %s, perm: 0%o, size: %d, name: %s", $type, $perm, $size, $name);
88 _scp_escape_name($name);
89 $p->_send_line_and_get_response($dpipe, $action, sprintf("%s%04o %d %s\x0A", $cmd, $perm, $size, $name));
90 }
91  
92 sub _clean_actions {
93 my $p = shift;
94 while (my $action = $p->_pop_action(undef, 1)) {
95 $p->_close($action, 2, "broken dpipe");
96 }
97 }
98  
99 sub do_stat { 1 }
100  
101 sub _do_stat {
102 my ($p, $action) = @_;
103 unless ($p->do_stat($action)) {
104 $p->set_local_error($action, "unable to retrieve file system properties for $action->{path}");
105 return;
106 }
107 unless (defined $action->{type}) {
108 $action->{type} = (Fcntl::S_ISDIR($action->{perm} || 0) ? 'dir' : 'file');
109 }
110 1;
111 }
112  
113 sub _link_check {
114 my ($p, $action) = @_;
115 if (not $p->{follow_links} and Fcntl::S_ISLNK($action->{perm} || 0)) {
116 $p->set_local_error($action, "not a regular file");
117 return;
118 }
119 1;
120 }
121  
122 sub _dir_check {
123 my ($p, $action) = @_;
124 if (not $p->{recursive} and $action->{type} eq 'dir') {
125 $p->set_local_err
126 }
127 }
128  
129 sub on_end_of_put { 1 }
130  
131 sub _send_time {
132 my ($p, $dpipe, $action) = @_;
133 return 1 unless $p->{send_time};
134 my ($mtime, $atime) = @{$action}{'mtime', 'atime'};
135 $p->_send_line_and_get_response($dpipe, $action,
136 sprintf("T%d %d %d %d\x0A", $mtime, 0, $atime, 0));
137 }
138  
139 sub _send_file {
140 my ($p, $dpipe, $action) = @_;
141 my $failed = 0;
142 my $remaining = $action->{size} || 0;
143 while ($remaining > 0) {
144 my $data;
145 my $len = ($remaining > 16384 ? 16386 : $remaining);
146 if ($failed) {
147 $data = "\0" x $len;
148 }
149 else {
150 $data = $p->_read_file($action, $len);
151 unless (defined $data and length $data) {
152 $failed = 1;
153 $debug and $debug & 4096 and _debug "no data from putter";
154 redo;
155 }
156 if (length($data) > $remaining) {
157 $debug and $debug & 4096 and _debug("too much data, discarding excess");
158 substr($data, $remaining) = '';
159 $failed = 1;
160 }
161 }
162 $debug and $debug & 4096 and _debug_hexdump("sending data (failed: $failed)", $data);
163 $dpipe->print($data) or last OUT;
164 $remaining -= length $data;
165 }
166 $p->_close($action) or $failed = 1;
167 $p->_send_line_and_get_response($dpipe, $action, ($failed ? "\x01failed\x0A" : "\x00"));
168 }
169  
170 sub run {
171 my ($p, $opts) = @_;
172 my $any = $p->{any};
173 my $dpipe = $any->dpipe({ %$opts, quote_args => 1 },
174 # 'strace', '-fo', '/tmp/scp.strace',
175 $p->{scp_cmd},
176 '-t',
177 ($p->{send_time} ? '-p' : ()),
178 ($p->{recursive} ? '-r' : ()),
179 ($p->{double_dash} ? '--' : ()),
180 $p->{target} );
181 $any->error and return;
182  
183 local $SIG{PIPE} = 'IGNORE';
184  
185 my ($error_level, $error_msg) = $p->_read_response($dpipe);
186 if ($error_level) {
187 $any->_or_set_error(SSHA_SCP_ERROR, "remote SCP refused transfer", $error_msg);
188 return;
189 }
190  
191 OUT: while (not $p->{aborted}) {
192 my $line;
193 my $current_dir_action = $p->{actions}[-1];
194 if (my $action = $p->_read_dir($current_dir_action)) {
195 my $type = $action->{type};
196 $action = $p->_push_action(%$action);
197  
198 $debug and $debug & 4096 and _debug_dump("next action", $action);
199  
200 # local_error actions are just pushed into the log
201 unless (defined $type and $type eq 'local_error') {
202 if ($p->_do_stat($action)) {
203 $type = $action->{type};
204 if ($type eq 'dir' and not $p->{recursive}) {
205 $debug and $debug & 4096 and _debug "discarding directory $action->{path}";
206 $p->set_local_error($action, "not a regular file");
207 }
208 else {
209 if ($p->_open($action)) {
210 if ($p->_send_time($dpipe, $action)) {
211 if ($p->_remote_open($dpipe, $action)) {
212 if ($type eq 'dir') {
213 # do not pop the action from the actions stack;
214 next;
215 }
216 elsif ($type eq 'file') {
217 $p->_send_file($dpipe, $action);
218 }
219 }
220 }
221 else {
222 $p->_close($action);
223 }
224 }
225 }
226 }
227 }
228 $p->_pop_action;
229 }
230 else { # close dir
231 my $action = $p->_pop_action('dir', 1) or last;
232 $p->_close($action);
233 $p->_send_line_and_get_response($dpipe, $action, "E\x0A");
234 }
235 }
236  
237 $dpipe->close;
238  
239 $p->_clean_actions;
240  
241 $p->on_end_of_put or
242 $p->_or_set_error(SSHA_SCP_ERROR, "SCP transfer not completely successful");
243  
244 not $any->error
245 }
246  
247 1;