line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::FTP::File; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
29907
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
5
|
1
|
|
|
1
|
|
1006
|
use Net::FTP; |
|
1
|
|
|
|
|
51833
|
|
|
1
|
|
|
|
|
1772
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $pretty = 1; |
10
|
|
|
|
|
|
|
our $_fatal = 0; # not my() because you can't localize lexical variables |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my %cols = ( |
13
|
|
|
|
|
|
|
pretty => { |
14
|
|
|
|
|
|
|
0 => 'Permissions', |
15
|
|
|
|
|
|
|
1 => 'Number of Links', |
16
|
|
|
|
|
|
|
2 => 'Owner', |
17
|
|
|
|
|
|
|
3 => 'Group', |
18
|
|
|
|
|
|
|
4 => 'Bytes', |
19
|
|
|
|
|
|
|
5 => 'Last Modified Month', |
20
|
|
|
|
|
|
|
6 => 'Last Modified Day', |
21
|
|
|
|
|
|
|
7 => 'Last Modified Year/Time', |
22
|
|
|
|
|
|
|
8 => 'Path' |
23
|
|
|
|
|
|
|
}, |
24
|
|
|
|
|
|
|
utility => { |
25
|
|
|
|
|
|
|
0 => 'perms', |
26
|
|
|
|
|
|
|
1 => 'links', |
27
|
|
|
|
|
|
|
2 => 'owner', |
28
|
|
|
|
|
|
|
3 => 'group', |
29
|
|
|
|
|
|
|
4 => 'bytes', |
30
|
|
|
|
|
|
|
5 => 'month', |
31
|
|
|
|
|
|
|
6 => 'day', |
32
|
|
|
|
|
|
|
7 => 'yr_tm', |
33
|
|
|
|
|
|
|
8 => 'path' |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
our %DirProcHash = ( |
38
|
|
|
|
|
|
|
cols => $cols{pretty}, |
39
|
|
|
|
|
|
|
proc => sub { |
40
|
|
|
|
|
|
|
my $line = shift; |
41
|
|
|
|
|
|
|
my $hash = shift; |
42
|
|
|
|
|
|
|
if ( $line !~ m/^total/ ) { |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my @parts = split /\s+/, $line; |
45
|
|
|
|
|
|
|
my @lin = split /\s/, $line; |
46
|
|
|
|
|
|
|
my $path_re = join '\s+', map { quotemeta } @parts[ 8 .. $#parts ]; |
47
|
|
|
|
|
|
|
$path_re = '\s*' . $path_re . '\s*'; |
48
|
|
|
|
|
|
|
$path_re = qr($path_re); |
49
|
|
|
|
|
|
|
my ($path) = $line =~ m{($path_re)}; |
50
|
|
|
|
|
|
|
$path = substr( $path, 1 ); # remove first space that is there but is not part of the name |
51
|
|
|
|
|
|
|
my ( $file, $link ) = split / \-\> /, $path; |
52
|
|
|
|
|
|
|
$hash->{$file}->{'Link To'} = defined $link && $link ? $link : undef; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
for ( 0 .. 8 ) { |
55
|
|
|
|
|
|
|
my $label = exists $Net::FTP::File::DirProcHash{cols}->{$_} ? $Net::FTP::File::DirProcHash{cols}->{$_} : $_; |
56
|
|
|
|
|
|
|
$hash->{$file}->{$label} = $_ == 8 ? $file : $parts[$_]; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
}, |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub Net::FTP::pretty_dir { |
64
|
0
|
|
|
0
|
0
|
|
shift; |
65
|
0
|
|
|
|
|
|
my $newp = shift; |
66
|
0
|
0
|
|
|
|
|
if ( defined $newp ) { |
67
|
0
|
|
|
|
|
|
$pretty = $newp; |
68
|
0
|
0
|
|
|
|
|
$DirProcHash{cols} = $cols{pretty} if $pretty; |
69
|
0
|
0
|
|
|
|
|
$DirProcHash{cols} = $cols{utility} if !$pretty; |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
return $pretty; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $setmsg = sub { |
75
|
|
|
|
|
|
|
my $ftp = shift; |
76
|
|
|
|
|
|
|
my $msg = shift() . "\n"; |
77
|
|
|
|
|
|
|
$msg .= 'net_cmd_resp: ' . scalar $ftp->message if $ftp->message; |
78
|
|
|
|
|
|
|
${*$ftp}{'net_cmd_resp'} = [$msg]; |
79
|
|
|
|
|
|
|
$_fatal = 1; |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub Net::FTP::isfile { |
83
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
84
|
0
|
0
|
0
|
|
|
|
return 1 if $ftp->exists(@_) && !$ftp->isdir(@_); |
85
|
0
|
|
|
|
|
|
0; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub Net::FTP::isdir { |
89
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
90
|
0
|
|
|
|
|
|
local $_fatal; |
91
|
0
|
|
|
|
|
|
my $c = $ftp->pwd(); |
92
|
0
|
|
|
|
|
|
my $r = $ftp->cwd(@_); |
93
|
0
|
|
|
|
|
|
my $d = $ftp->cwd($c); |
94
|
0
|
|
|
|
|
|
my $e = $ftp->pwd(); |
95
|
0
|
0
|
0
|
|
|
|
$setmsg->( $ftp, "Could not CWD into original directory $c" ) if $c ne $e || !$d; |
96
|
0
|
0
|
|
|
|
|
return undef if $_fatal; |
97
|
0
|
0
|
|
|
|
|
return $r ? 1 : 0; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub Net::FTP::exists { |
101
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
102
|
0
|
0
|
|
|
|
|
if ( defined $ftp->size(@_) ) { return 1; } |
|
0
|
0
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
elsif ( $ftp->isdir(@_) ) { return 1; } |
104
|
0
|
|
|
|
|
|
else { return 0; } |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub Net::FTP::dir_hashref { |
108
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
109
|
0
|
|
|
|
|
|
my %dir; |
110
|
0
|
|
|
|
|
|
for my $ln ( $ftp->dir(@_) ) { |
111
|
0
|
|
|
|
|
|
$Net::FTP::File::DirProcHash{proc}->( $ln, \%dir ); |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
|
return \%dir; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub Net::FTP::copy { |
117
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
118
|
0
|
|
|
|
|
|
my ( $t, $f ); |
119
|
0
|
|
|
|
|
|
my $fd = $ftp->pwd; |
120
|
0
|
|
|
|
|
|
my ( $o, $n, $cd, $to ) = @_; |
121
|
0
|
0
|
|
|
|
|
if ( $ftp->isfile($o) ) { |
122
|
0
|
0
|
0
|
|
|
|
if ( !defined $to && defined $cd && $cd =~ m/^\d+$/ ) { $to = $cd; $cd = ''; } |
|
0
|
|
0
|
|
|
|
|
|
0
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $g = $ftp->retr($o); |
124
|
0
|
|
|
|
|
|
while ( $g->read( $f, 1024, $to ) ) { $t .= $f; } |
|
0
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
$g->close(); |
126
|
0
|
0
|
|
|
|
|
$ftp->cwd($cd) if $cd; |
127
|
0
|
|
|
|
|
|
my $p = $ftp->stor($n); |
128
|
0
|
|
|
|
|
|
$p->write( $t, length($t), $to ); |
129
|
0
|
|
|
|
|
|
$p->close(); |
130
|
0
|
0
|
|
|
|
|
$ftp->cwd($fd) if $cd; |
131
|
0
|
0
|
|
|
|
|
return 1 if $ftp->exists($n); |
132
|
0
|
|
|
|
|
|
return 0; |
133
|
|
|
|
|
|
|
} |
134
|
0
|
|
|
|
|
|
else { return undef; } |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub Net::FTP::move { |
138
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
139
|
0
|
0
|
|
|
|
|
if ( $_[0] eq $_[1] ) { |
140
|
0
|
|
|
|
|
|
$setmsg->( $ftp, "copy $_[0] to $_[1] failed: they are the same file" ); |
141
|
0
|
|
|
|
|
|
return; |
142
|
|
|
|
|
|
|
} |
143
|
0
|
|
|
|
|
|
my $cp = $ftp->copy(@_); |
144
|
0
|
|
|
|
|
|
local $_fatal = 0; |
145
|
0
|
0
|
|
|
|
|
$setmsg->( $ftp, "copy $_[0] to $_[1] failed: $_[0] does not exist" ) if !defined $cp; |
146
|
0
|
0
|
|
|
|
|
$setmsg->( $ftp, "copy $_[0] to $_[1] failed: $_[1] was not created" ) if !$cp; |
147
|
0
|
0
|
|
|
|
|
$ftp->delete( $_[0] ) or $setmsg->( $ftp, "Unable to delete original file $_[0] after copy" ); |
148
|
0
|
0
|
|
|
|
|
return 0 if $_fatal; |
149
|
0
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub Net::FTP::chmod { |
153
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
154
|
0
|
0
|
|
|
|
|
if ( $ftp->supported('SITE CHMOD') ) { |
155
|
0
|
|
|
|
|
|
my $chmod = $ftp->site( 'CHMOD', @_ ); |
156
|
0
|
0
|
|
|
|
|
return 1 if $chmod == 2; |
157
|
0
|
0
|
|
|
|
|
return 0 if $chmod == 5; |
158
|
0
|
|
|
|
|
|
return -1; |
159
|
|
|
|
|
|
|
} |
160
|
0
|
|
|
|
|
|
return undef; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub Net::FTP::touch { |
164
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
165
|
0
|
|
|
|
|
|
my $rfl = shift; |
166
|
0
|
0
|
0
|
|
|
|
if ( $ftp->isdir($rfl) ) { |
|
|
0
|
|
|
|
|
|
167
|
0
|
0
|
|
|
|
|
if ( shift() ) { |
168
|
0
|
0
|
|
|
|
|
$ftp->empty($rfl) or return; |
169
|
|
|
|
|
|
|
} |
170
|
0
|
|
|
|
|
|
else { return -1 } |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
elsif ( $ftp->isfile($rfl) && $ftp->size($rfl) > 0 ) { |
173
|
0
|
0
|
|
|
|
|
$ftp->copy( $rfl, $rfl ) or return; # becasue $ftp->append and $ftp->appe don't change $ftp->mdtm() if you append nothing, if you knwo of a better way please let me know, thx :) |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
else { |
176
|
0
|
0
|
|
|
|
|
$ftp->empty($rfl) or return; |
177
|
|
|
|
|
|
|
} |
178
|
0
|
|
|
|
|
|
return $ftp->mdtm($rfl); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub Net::FTP::empty { |
182
|
0
|
|
|
0
|
0
|
|
my $ftp = shift; |
183
|
0
|
|
|
|
|
|
my $zb = ''; |
184
|
0
|
0
|
|
|
|
|
open ZBF, '>', \$zb or return; |
185
|
0
|
0
|
|
|
|
|
$ftp->put( \*ZBF, shift() ) or return; |
186
|
0
|
|
|
|
|
|
close ZBF; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# not 'stat' since one day Net::FTP may want to support STAT and likely will call it stat() |
190
|
0
|
|
|
0
|
0
|
|
sub Net::FTP::fstat { die 'fstat() in not yet implemented' } |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
1; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
__END__ |