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
|
|
664
|
use v5.12.5; |
|
45
|
|
|
|
|
162
|
|
32
|
45
|
|
|
45
|
|
237
|
use warnings; |
|
45
|
|
|
|
|
90
|
|
|
45
|
|
|
|
|
1247
|
|
33
|
45
|
|
|
45
|
|
2292
|
use Rex::Interface::File; |
|
45
|
|
|
|
|
92
|
|
|
45
|
|
|
|
|
343
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $VERSION = '1.14.2.2'; # TRIAL VERSION |
36
|
|
|
|
|
|
|
|
37
|
45
|
|
|
45
|
|
2133
|
use constant DEFAULT_READ_LEN => 64; |
|
45
|
|
|
|
|
183
|
|
|
45
|
|
|
|
|
33034
|
|
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
|
1024
|
my $that = shift; |
83
|
189
|
|
33
|
|
|
1904
|
my $proto = ref($that) || $that; |
84
|
189
|
|
|
|
|
1112
|
my $self = {@_}; |
85
|
|
|
|
|
|
|
|
86
|
189
|
|
|
|
|
5992
|
my %modes = ( |
87
|
|
|
|
|
|
|
'w' => '>', |
88
|
|
|
|
|
|
|
'r' => '<', |
89
|
|
|
|
|
|
|
'a' => '>>', |
90
|
|
|
|
|
|
|
'<' => '<', |
91
|
|
|
|
|
|
|
'>' => '>', |
92
|
|
|
|
|
|
|
'>>' => '>>', |
93
|
|
|
|
|
|
|
); |
94
|
|
|
|
|
|
|
|
95
|
189
|
100
|
|
|
|
1077
|
if ( $self->{filename} ) { |
96
|
5
|
|
100
|
|
|
52
|
$self->{mode} ||= '<'; |
97
|
|
|
|
|
|
|
|
98
|
5
|
|
50
|
|
|
24
|
my $mode = $modes{ $self->{mode} } || '<'; |
99
|
5
|
|
50
|
|
|
163
|
$self->{fh} = Rex::Interface::File->create( $self->{type} || 'Local' ); |
100
|
5
|
|
|
|
|
30
|
$self->{fh}->open( $mode, $self->{filename} ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
189
|
|
|
|
|
839
|
bless( $self, $proto ); |
104
|
|
|
|
|
|
|
|
105
|
189
|
50
|
|
|
|
3109
|
if ( ref $self->{fh} !~ m{Rex::Interface::File} ) { |
106
|
0
|
|
|
|
|
0
|
die "Need an Rex::Interface::File object"; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
189
|
|
|
|
|
1424
|
return $self; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub DESTROY { |
113
|
189
|
|
|
189
|
|
10662
|
my ($self) = @_; |
114
|
189
|
50
|
|
|
|
3306
|
if ( ref $self->{'fh'} =~ m/^Rex::Interface::File/ ) { |
115
|
0
|
0
|
|
|
|
0
|
$self->close if ( $self->{'fh'}->{'fh'} ); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
else { |
118
|
189
|
50
|
|
|
|
1291
|
$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
|
2432
|
my ( $self, @buf ) = @_; |
133
|
247
|
|
|
|
|
479
|
my $fh = $self->{fh}; |
134
|
|
|
|
|
|
|
|
135
|
247
|
100
|
|
|
|
618
|
if ( scalar(@buf) > 1 ) { |
136
|
3
|
|
|
|
|
34
|
for my $line (@buf) { |
137
|
12
|
|
|
|
|
53
|
$fh->write($line); |
138
|
12
|
|
|
|
|
31
|
$fh->write($/); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
else { |
142
|
244
|
|
|
|
|
699
|
$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
|
1646
|
my ( $self, $len ) = @_; |
173
|
757
|
50
|
|
|
|
1725
|
$len = DEFAULT_READ_LEN if ( !$len ); |
174
|
|
|
|
|
|
|
|
175
|
757
|
|
|
|
|
1363
|
my $fh = $self->{'fh'}; |
176
|
757
|
|
|
|
|
1856
|
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
|
4638
|
my ($self) = @_; |
189
|
|
|
|
|
|
|
|
190
|
143
|
|
|
|
|
766
|
my $all = ''; |
191
|
143
|
|
|
|
|
1148
|
while ( my $in = $self->read() ) { |
192
|
614
|
|
|
|
|
2377
|
$all .= $in; |
193
|
|
|
|
|
|
|
} |
194
|
143
|
100
|
|
|
|
774
|
if (wantarray) { |
195
|
6
|
|
|
|
|
69
|
return split( /\n/, $all ); |
196
|
|
|
|
|
|
|
} |
197
|
137
|
|
|
|
|
590
|
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
|
1553
|
my ($self) = @_; |
210
|
372
|
|
|
|
|
1308
|
my $fh = $self->{'fh'}; |
211
|
372
|
|
|
|
|
1344
|
$fh->close; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
1; |