| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# |
|
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Rex::FS::File - File Class |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
This is the File Class used by I and I. |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Rex::Interface::File; |
|
16
|
|
|
|
|
|
|
my $fh = Rex::Interface::File->create('Local'); |
|
17
|
|
|
|
|
|
|
$fh->open( '<', 'filename' ); |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $file = Rex::FS::File->new(fh => $fh); |
|
20
|
|
|
|
|
|
|
$file->read($len); |
|
21
|
|
|
|
|
|
|
$file->read_all; |
|
22
|
|
|
|
|
|
|
$file->write($buf); |
|
23
|
|
|
|
|
|
|
$file->close; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 CLASS METHODS |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
package Rex::FS::File; |
|
30
|
|
|
|
|
|
|
|
|
31
|
45
|
|
|
45
|
|
595
|
use v5.12.5; |
|
|
45
|
|
|
|
|
170
|
|
|
32
|
45
|
|
|
45
|
|
239
|
use warnings; |
|
|
45
|
|
|
|
|
83
|
|
|
|
45
|
|
|
|
|
1247
|
|
|
33
|
45
|
|
|
45
|
|
1917
|
use Rex::Interface::File; |
|
|
45
|
|
|
|
|
105
|
|
|
|
45
|
|
|
|
|
374
|
|
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $VERSION = '1.14.2.3'; # TRIAL VERSION |
|
36
|
|
|
|
|
|
|
|
|
37
|
45
|
|
|
45
|
|
2174
|
use constant DEFAULT_READ_LEN => 64; |
|
|
45
|
|
|
|
|
161
|
|
|
|
45
|
|
|
|
|
32892
|
|
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 new |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
This is the constructor. You need to set the filehandle which the object should work on |
|
42
|
|
|
|
|
|
|
or pass a filename. If you pass a filehandle, it has to be a C |
|
43
|
|
|
|
|
|
|
object |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $fh = Rex::Interface::File->create('Local'); |
|
46
|
|
|
|
|
|
|
$fh->open( '<', 'filename' ); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $file = Rex::FS::File->new(fh => $fh); |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Create a C object with a filename |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# open a local file in read mode |
|
53
|
|
|
|
|
|
|
my $file = Rex::FS::File->new( |
|
54
|
|
|
|
|
|
|
filename => 'filename', |
|
55
|
|
|
|
|
|
|
mode => 'r', # or '<' |
|
56
|
|
|
|
|
|
|
type => 'Local', |
|
57
|
|
|
|
|
|
|
); |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# or shorter |
|
60
|
|
|
|
|
|
|
my $file = Rex::FS::File->new( filename => 'filename' ); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# open a local file in write mode |
|
63
|
|
|
|
|
|
|
my $file = Rex::FS::File->new( |
|
64
|
|
|
|
|
|
|
filename => 'filename', |
|
65
|
|
|
|
|
|
|
mode => 'w', # or '>' |
|
66
|
|
|
|
|
|
|
); |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Allowed modes: |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
< read |
|
71
|
|
|
|
|
|
|
r read |
|
72
|
|
|
|
|
|
|
> write |
|
73
|
|
|
|
|
|
|
w write |
|
74
|
|
|
|
|
|
|
>> append |
|
75
|
|
|
|
|
|
|
a append |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
For allowed C see documentation of L. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new { |
|
82
|
189
|
|
|
189
|
1
|
904
|
my $that = shift; |
|
83
|
189
|
|
33
|
|
|
1900
|
my $proto = ref($that) || $that; |
|
84
|
189
|
|
|
|
|
1325
|
my $self = {@_}; |
|
85
|
|
|
|
|
|
|
|
|
86
|
189
|
|
|
|
|
5682
|
my %modes = ( |
|
87
|
|
|
|
|
|
|
'w' => '>', |
|
88
|
|
|
|
|
|
|
'r' => '<', |
|
89
|
|
|
|
|
|
|
'a' => '>>', |
|
90
|
|
|
|
|
|
|
'<' => '<', |
|
91
|
|
|
|
|
|
|
'>' => '>', |
|
92
|
|
|
|
|
|
|
'>>' => '>>', |
|
93
|
|
|
|
|
|
|
); |
|
94
|
|
|
|
|
|
|
|
|
95
|
189
|
100
|
|
|
|
1038
|
if ( $self->{filename} ) { |
|
96
|
5
|
|
100
|
|
|
48
|
$self->{mode} ||= '<'; |
|
97
|
|
|
|
|
|
|
|
|
98
|
5
|
|
50
|
|
|
33
|
my $mode = $modes{ $self->{mode} } || '<'; |
|
99
|
5
|
|
50
|
|
|
95
|
$self->{fh} = Rex::Interface::File->create( $self->{type} || 'Local' ); |
|
100
|
5
|
|
|
|
|
30
|
$self->{fh}->open( $mode, $self->{filename} ); |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
189
|
|
|
|
|
914
|
bless( $self, $proto ); |
|
104
|
|
|
|
|
|
|
|
|
105
|
189
|
50
|
|
|
|
3180
|
if ( ref $self->{fh} !~ m{Rex::Interface::File} ) { |
|
106
|
0
|
|
|
|
|
0
|
die "Need an Rex::Interface::File object"; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
189
|
|
|
|
|
1357
|
return $self; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub DESTROY { |
|
113
|
189
|
|
|
189
|
|
9739
|
my ($self) = @_; |
|
114
|
189
|
50
|
|
|
|
2870
|
if ( ref $self->{'fh'} =~ m/^Rex::Interface::File/ ) { |
|
115
|
0
|
0
|
|
|
|
0
|
$self->close if ( $self->{'fh'}->{'fh'} ); |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
else { |
|
118
|
189
|
50
|
|
|
|
1195
|
$self->close if ( $self->{'fh'} ); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 write($buf) |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Write $buf into the filehandle. |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
$file->write("Hello World"); |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub write { |
|
131
|
|
|
|
|
|
|
|
|
132
|
247
|
|
|
247
|
1
|
2198
|
my ( $self, @buf ) = @_; |
|
133
|
247
|
|
|
|
|
445
|
my $fh = $self->{fh}; |
|
134
|
|
|
|
|
|
|
|
|
135
|
247
|
100
|
|
|
|
644
|
if ( scalar(@buf) > 1 ) { |
|
136
|
3
|
|
|
|
|
23
|
for my $line (@buf) { |
|
137
|
12
|
|
|
|
|
41
|
$fh->write($line); |
|
138
|
12
|
|
|
|
|
30
|
$fh->write($/); |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
else { |
|
142
|
244
|
|
|
|
|
599
|
$fh->write( $buf[0] ); |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 seek($offset) |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Seek to the file position $offset. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Set the file pointer to the 5th byte. |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$file->seek(5); |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub seek { |
|
157
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $offset ) = @_; |
|
158
|
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
0
|
my $fh = $self->{'fh'}; |
|
160
|
0
|
|
|
|
|
0
|
$fh->seek($offset); |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 read($len) |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Read $len bytes out of the filehandle. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my $content = $file->read(1024); |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub read { |
|
172
|
757
|
|
|
757
|
1
|
1638
|
my ( $self, $len ) = @_; |
|
173
|
757
|
50
|
|
|
|
1982
|
$len = DEFAULT_READ_LEN if ( !$len ); |
|
174
|
|
|
|
|
|
|
|
|
175
|
757
|
|
|
|
|
1319
|
my $fh = $self->{'fh'}; |
|
176
|
757
|
|
|
|
|
2045
|
return $fh->read($len); |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 read_all |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Read everything out of the filehandle. |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
my $content = $file->read_all; |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub read_all { |
|
188
|
143
|
|
|
143
|
1
|
3410
|
my ($self) = @_; |
|
189
|
|
|
|
|
|
|
|
|
190
|
143
|
|
|
|
|
790
|
my $all = ''; |
|
191
|
143
|
|
|
|
|
1297
|
while ( my $in = $self->read() ) { |
|
192
|
614
|
|
|
|
|
2015
|
$all .= $in; |
|
193
|
|
|
|
|
|
|
} |
|
194
|
143
|
100
|
|
|
|
997
|
if (wantarray) { |
|
195
|
6
|
|
|
|
|
63
|
return split( /\n/, $all ); |
|
196
|
|
|
|
|
|
|
} |
|
197
|
137
|
|
|
|
|
866
|
return $all; |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head2 close |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Close the file. |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
$file->close; |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub close { |
|
209
|
372
|
|
|
372
|
1
|
1536
|
my ($self) = @_; |
|
210
|
372
|
|
|
|
|
795
|
my $fh = $self->{'fh'}; |
|
211
|
372
|
|
|
|
|
2035
|
$fh->close; |
|
212
|
|
|
|
|
|
|
} |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
1; |