Debian Codegraph @

lib/Test2/Harness/Util/File.pm

raw
1 package Test2::Harness::Util::File;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use IO::Handle;
8
9 use Test2::Harness::Util();
10
11 use Carp qw/croak confess/;
12 use Fcntl qw/SEEK_SET SEEK_CUR/;
13
14 use Test2::Harness::Util::HashBase qw{ -name -_fh -_init_fh done -line_pos <skip_bad_decode };
15
16 sub exists { -e $_[0]->{+NAME} }
17
18 sub decode { shift; $_[0] }
19 sub encode { shift; $_[0] }
20
21 sub init {
22 my $self = shift;
23
24 croak "'name' is a required attribute" unless $self->{+NAME};
25
26 $self->{+_INIT_FH} = delete $self->{fh};
27 }
28
29 sub open_file {
30 my $self = shift;
31 return Test2::Harness::Util::open_file($self->{+NAME}, @_)
32 }
33
34 sub maybe_read {
35 my $self = shift;
36 return undef unless -e $self->{+NAME};
37 return $self->read;
38 }
39
40 sub read {
41 my $self = shift;
42 my $out = Test2::Harness::Util::read_file($self->{+NAME});
43
44 eval { $out = $self->decode($out); 1 } or confess "$self->{+NAME}: $@";
45 return $out;
46 }
47
48 sub rewrite {
49 my $self = shift;
50 return Test2::Harness::Util::write_file($self->{+NAME}, $self->encode(@_));
51 }
52
53 sub write {
54 my $self = shift;
55 return Test2::Harness::Util::write_file_atomic($self->{+NAME}, $self->encode(@_));
56 }
57
58 sub reset {
59 my $self = shift;
60 delete $self->{+_FH};
61 delete $self->{+DONE};
62 delete $self->{+LINE_POS};
63 return;
64 }
65
66 sub fh {
67 my $self = shift;
68 return $self->{+_FH}->{$$} if $self->{+_FH}->{$$};
69
70 # Remove any other PID handles
71 $self->{+_FH} = {};
72
73 if (my $fh = $self->{+_INIT_FH}) {
74 $self->{+_FH}->{$$} = $fh;
75 }
76 else {
77 $self->{+_FH}->{$$} = Test2::Harness::Util::maybe_open_file($self->{+NAME}) or return undef;
78 }
79
80 $self->{+_FH}->{$$}->blocking(0);
81 return $self->{+_FH}->{$$};
82 }
83
84 sub read_line {
85 my $self = shift;
86 my %params = @_;
87
88 my $pos = $params{from};
89 $pos = $self->{+LINE_POS} ||= 0 unless defined $pos;
90
91 my $fh = $self->{+_FH}->{$$} || $self->fh or return undef;
92 seek($fh,$pos,SEEK_SET) or die "Could not seek: $!"
93 if eof($fh) || tell($fh) != $pos;
94
95 my $line = <$fh>;
96
97 # No line, nothing to do
98 return unless defined $line && length($line);
99
100 # Partial line, hold off unless done
101 return unless $self->{+DONE} || substr($line, -1, 1) eq "\n";
102
103 my $new_pos = tell($fh);
104 die "Failed to 'tell': $!" if $new_pos == -1;
105
106 my $err = 0;
107 local $@;
108 unless (eval { $line = $self->decode($line); 1 }) {
109 $err = $@ // 'error';
110 confess "$self->{+NAME} ($pos -> $new_pos): $err" unless $self->{+SKIP_BAD_DECODE};
111 warn "Skipping line that failed to decode: $err\n" if $self->{+SKIP_BAD_DECODE} > 1;
112 $line = undef;
113 }
114
115 $self->{+LINE_POS} = $new_pos unless defined $params{peek} || defined $params{from};
116 return $line unless wantarray;
117 return ($pos, $new_pos, $line, $err);
118 }
119
120 1;
121
122 __END__
123
124 =pod
125
126 =encoding UTF-8
127
128 =head1 NAME
129
130 Test2::Harness::Util::File - Utility class for manipulating a file.
131
132 =head1 DESCRIPTION
133
134 This is a utility class for file operations. This also serves as a base class
135 for several file helpers.
136
137 =head1 SYNOPSIS
138
139 use Test2::Harness::Util::File;
140
141 my $f = Test2::Harness::Util::File->new(name => '/path/to/file');
142
143 $f->write($content);
144
145 my $fh = $f->open_file('<');
146
147 # Read, throw exception if it cannot read
148 my $content = $f->read();
149
150 # Try to read, but do not throw an exception if it cannot be read.
151 my $content_or_undef = $f->maybe_read();
152
153 my $line1 = $f->read_line();
154 my $line2 = $f->read_line();
155 ...
156
157 =head1 ATTRIBUTES
158
159 =over 4
160
161 =item $filename = $f->name;
162
163 Get the filename. Must also be provided during construction.
164
165 =item $bool = $f->done;
166
167 True if read_line() has read every line.
168
169 =back
170
171 =head1 METHODS
172
173 =over 4
174
175 =item $decoded = $f->decode($encoded)
176
177 This is a no-op, it returns the argument unchanged. This is called by C<read>
178 and C<read_line>. Subclasses can override this if the file contains encoded
179 data.
180
181 =item $encoded = $f->encode($decoded)
182
183 This is a no-op, it returns the argument unchanged. This is called by C<write>.
184 Subclasses can override this if the file contains encoded data.
185
186 =item $bool = $f->exists()
187
188 Check if the file exists
189
190 =item $content = $f->maybe_read()
191
192 This will read the file if it can and return the content (all lines joined
193 together as a single string). If the file cannot be read, or does not exist
194 this will return undef.
195
196 =item $fh = $f->open_file()
197
198 =item $fh = $f->open_file($mode)
199
200 Open a handle to the file. If no $mode is provided C<< '<' >> is used.
201
202 =item $content = $f->read()
203
204 This will read the file if it can and return the content (all lines joined
205 together as a single string). If the file cannot be read, or does not exist
206 this will throw an exception.
207
208 =item $line = $f->read_line()
209
210 Read a single line from the file, subsequent calls will read the next line and
211 so on until the end of the file is reached. Reset with the C<reset()> method.
212
213 =item $f->reset()
214
215 Reset the internal line iterator used by C<read_line()>.
216
217 =item $f->write($content)
218
219 This is an atomic-write. First $content will be written to a temporary file
220 using C<< '>' >> mode. Then the temporary file will be renamed to the desired
221 file name. Under the hood this uses C<write_file_atomic()> from
222 L<Test2::Harness::Util>.
223
224 =back
225
226 =head1 SOURCE
227
228 The source code repository for Test2-Harness can be found at
229 F<http://github.com/Test-More/Test2-Harness/>.
230
231 =head1 MAINTAINERS
232
233 =over 4
234
235 =item Chad Granum E<lt>[email protected]<gt>
236
237 =back
238
239 =head1 AUTHORS
240
241 =over 4
242
243 =item Chad Granum E<lt>[email protected]<gt>
244
245 =back
246
247 =head1 COPYRIGHT
248
249 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
250
251 This program is free software; you can redistribute it and/or
252 modify it under the same terms as Perl itself.
253
254 See F<http://dev.perl.org/licenses/>
255
256 =cut