line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2008-2011 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package IPC::PerlSSH::Library::FS; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
9
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
4
|
use IPC::PerlSSH::Library; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
331
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.17'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
C - a library of filesystem functions for |
18
|
|
|
|
|
|
|
C |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use IPC::PerlSSH; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $ips = IPC::PerlSSH->new( Host => "over.there" ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$ips->use_library( "FS", qw( mkdir chmod writefile ) ); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$ips->call( "mkdir", "/tmp/testing" ); |
29
|
|
|
|
|
|
|
$ips->call( "chmod", 0600, "/tmp/testing" ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$ips->call( "writefile", "/tmp/testing/secret", <
|
32
|
|
|
|
|
|
|
Some secret contents of my file here |
33
|
|
|
|
|
|
|
EOF |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This module provides a library of functions for interating with the remote |
38
|
|
|
|
|
|
|
filesystem. It provides wrappers for most of the perl filesystem functions, |
39
|
|
|
|
|
|
|
and some useful new functions that are more convenient to call remotely. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Because of the large number of functions defined by this library, it is |
42
|
|
|
|
|
|
|
recommended to only load the ones being used by the program, to avoid sending |
43
|
|
|
|
|
|
|
unnecessary data when setting up SSH connections across slow links. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 FUNCTIONS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 Simple Functions |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
The following perl functions have trivial wrappers that take arguments and |
52
|
|
|
|
|
|
|
return values in the same way as perl's. They throw exceptions via the |
53
|
|
|
|
|
|
|
C call when they fail, rather than returning undef, because |
54
|
|
|
|
|
|
|
otherwise C<$!> would be difficult to obtain. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
chown chmod lstat mkdir readlink rename rmdir stat symlink unlink utime |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
The following functions are imported from L with the following |
59
|
|
|
|
|
|
|
API adjustments: |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
mkpath( $path, %opts ) # %opts supports mode, user, group |
62
|
|
|
|
|
|
|
rmtree( $path, %opts ) # %opts supports safe, keep_root |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
init q{ |
67
|
|
|
|
|
|
|
use File::Path qw( mkpath rmtree ); |
68
|
|
|
|
|
|
|
}; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
func chown => |
71
|
|
|
|
|
|
|
q{my $uid = shift; my $gid = shift; |
72
|
|
|
|
|
|
|
chown $uid, $gid, $_ or die "Cannot chown($uid, $gid, '$_') - $!" for @_;}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
func chmod => |
75
|
|
|
|
|
|
|
q{my $mode = shift; |
76
|
|
|
|
|
|
|
chmod $mode, $_ or die "Cannot chmod($mode, '$_') - $!" for @_;}; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
func lstat => |
79
|
|
|
|
|
|
|
q{my @s = lstat $_[0]; @s or die "Cannot lstat('$_[0]') - $!"; @s}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
func mkdir => |
82
|
|
|
|
|
|
|
q{mkdir $_[0] or die "Cannot mkdir('$_[0]') - $!"}; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
func mkpath => |
85
|
|
|
|
|
|
|
q{my ( $path, %opts ) = @_; |
86
|
|
|
|
|
|
|
mkpath $path, \%opts or die "Cannot mkpath('$path') - $!"}; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
func readlink => |
89
|
|
|
|
|
|
|
q{my $l = readlink $_[0]; defined $l or die "Cannot readlink('$_[0]') - $!"; $l}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
func rename => |
92
|
|
|
|
|
|
|
q{rename $_[0], $_[1] or die "Cannot rename('$_[0]','$_[1]') - $!"}; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
func rmdir => |
95
|
|
|
|
|
|
|
q{rmdir $_[0] or die "Cannot rmdir('$_[0]') - $!"}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
func rmtree => |
98
|
|
|
|
|
|
|
q{my ( $path, %opts ) = @_; |
99
|
|
|
|
|
|
|
rmtree $path, \%opts or die "Cannot rmtree('$_[0]') - $!"}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
func stat => |
102
|
|
|
|
|
|
|
q{my @s = stat $_[0]; @s or die "Cannot stat('$_[0]') - $!"; @s}; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
func symlink => |
105
|
|
|
|
|
|
|
q{symlink $_[0], $_[1] or die "Cannot symlink('$_[0]','$_[1]') - $!"}; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
func unlink => |
108
|
|
|
|
|
|
|
q{unlink $_[0] or die "Cannot unlink('$_[0]') - $!"}; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
func utime => |
111
|
|
|
|
|
|
|
q{my $atime = shift; my $mtime = shift; |
112
|
|
|
|
|
|
|
utime $atime, $mtime, $_ or die "Cannot utime($atime, $mtime, '$_') - $!" for @_}; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 Variations on C |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
The following functions each returns just one element from the C list |
117
|
|
|
|
|
|
|
for efficiency when only one is required. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
stat_dev stat_ino stat_mode stat_nlink stat_uid stat_gid stat_rdev |
120
|
|
|
|
|
|
|
stat_size stat_atime stat_mtime stat_ctime stat_blksize stat_blocks |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my %statfields = ( |
125
|
|
|
|
|
|
|
dev => 0, |
126
|
|
|
|
|
|
|
ino => 1, |
127
|
|
|
|
|
|
|
mode => 2, |
128
|
|
|
|
|
|
|
nlink => 3, |
129
|
|
|
|
|
|
|
uid => 4, |
130
|
|
|
|
|
|
|
gid => 5, |
131
|
|
|
|
|
|
|
rdev => 6, |
132
|
|
|
|
|
|
|
# size is 7 but we do that a different way |
133
|
|
|
|
|
|
|
atime => 8, |
134
|
|
|
|
|
|
|
mtime => 9, |
135
|
|
|
|
|
|
|
ctime => 10, |
136
|
|
|
|
|
|
|
blksize => 11, |
137
|
|
|
|
|
|
|
blocks => 12, |
138
|
|
|
|
|
|
|
); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
func( "stat_$_", "(stat(\$_[0]))[$statfields{$_}]" ) for keys %statfields; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=pod |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
The following stored functions wrap the perl -X file tests (documented here in |
145
|
|
|
|
|
|
|
the same order as in F) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
stat_readable stat_writable stat_executable stat_owned |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
stat_real_readable stat_real_writable stat_real_executable |
150
|
|
|
|
|
|
|
stat_real_owned |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
stat_exists stat_isempty stat_size |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
stat_isfile stat_isdir stat_islink stat_ispipe stat_issocket |
155
|
|
|
|
|
|
|
stat_isblock stat_ischar |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
stat_issetuid stat_issetgid stat_issticky |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
stat_istext stat_isbinary |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
stat_mtime_days stat_atime_days stat_ctime_days |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=cut |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# We can cheat with the filetests |
166
|
|
|
|
|
|
|
my %filetests = ( |
167
|
|
|
|
|
|
|
readable => 'r', |
168
|
|
|
|
|
|
|
writable => 'w', |
169
|
|
|
|
|
|
|
executable => 'x', |
170
|
|
|
|
|
|
|
owned => 'o', |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
real_readable => 'R', |
173
|
|
|
|
|
|
|
real_writable => 'W', |
174
|
|
|
|
|
|
|
real_executable => 'X', |
175
|
|
|
|
|
|
|
real_owned => 'O', |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
'exists' => 'e', |
178
|
|
|
|
|
|
|
isempty => 'z', |
179
|
|
|
|
|
|
|
size => 's', |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
isfile => 'f', |
182
|
|
|
|
|
|
|
isdir => 'd', |
183
|
|
|
|
|
|
|
islink => 'l', |
184
|
|
|
|
|
|
|
ispipe => 'p', |
185
|
|
|
|
|
|
|
issocket => 's', |
186
|
|
|
|
|
|
|
isblock => 'b', |
187
|
|
|
|
|
|
|
ischar => 'c', |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
issetuid => 'u', |
190
|
|
|
|
|
|
|
issetgid => 'g', |
191
|
|
|
|
|
|
|
issticky => 'k', |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
istext => 'T', |
194
|
|
|
|
|
|
|
isbinary => 'B', |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
mtime_days => 'M', |
197
|
|
|
|
|
|
|
atime_days => 'A', |
198
|
|
|
|
|
|
|
ctime_days => 'C', |
199
|
|
|
|
|
|
|
); |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
func( "stat_$_", "-$filetests{$_} \$_[0]" ) for keys %filetests; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head2 Variation Functions |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
The following functions are defined as variations on the perl function of the |
206
|
|
|
|
|
|
|
same name |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
my @ents = $ips->call( "readdir", $dirpath, $hidden ); |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Return a list of the directory entries. Hidden files are skipped if $hidden is |
211
|
|
|
|
|
|
|
true. F<.> and F<..> are always skipped. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
func readdir => |
216
|
|
|
|
|
|
|
q{opendir( my $dirh, $_[0] ) or die "Cannot opendir('$_[0]') - $!"; |
217
|
|
|
|
|
|
|
my @ents = readdir( $dirh ); |
218
|
|
|
|
|
|
|
grep { $_[1] ? $_ !~ m/^\.\.?$/ : $_ !~ m/^\./ } @ents}; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=pod |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
$ips->call( "remove", $path ); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Calls C if C<$path> is a directory, or C if not |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=cut |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
func remove => |
229
|
|
|
|
|
|
|
q{(lstat $_[0] && -d _) ? rmdir $_[0] : unlink $_[0] or die "Cannot remove('$_[0]') - $!"}; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head2 New Functions |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
The following functions are newly defined to wrap common perl idoms |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
my $content = $ips->call( "readfile", $filepath ); |
236
|
|
|
|
|
|
|
$ips->call( "writefile", $filepath, $newcontent ); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
To open a remote filehandle and interact with it over a sequence of multiple |
239
|
|
|
|
|
|
|
calls, see also L. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
func readfile => |
244
|
|
|
|
|
|
|
q{open( my $fileh, "<", $_[0] ) or die "Cannot open('$_[0]') for reading - $!"; |
245
|
|
|
|
|
|
|
local $/; <$fileh>}; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
func writefile => |
248
|
|
|
|
|
|
|
q{open( my $fileh, ">", $_[0] ) or die "Cannot open('$_[0]') for writing - $!"; |
249
|
|
|
|
|
|
|
print $fileh $_[1] or die "Cannot print to '$_[0]' - $!"}; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head1 AUTHOR |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Paul Evans |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
0x55AA; |