blib/lib/Tail/Tool/File.pm

filter: Statementstmt 53.7% 64 / 119 Branchbran 18.5% 10 / 54 Conditioncond 16.1% 5 / 31 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 56.2% 9 / 16 Podpod 100% 3 / 3 totaltotal 38.0% 91 / 239 CC 39 SCAR 44.7
CC
39
Cov
39%
get_line
182.0
_get_file_handle
127.8
watch
27.0
CRAP 87.7
« blib/lib/Tail/Tool.pm ↑ Summary blib/lib/Tail/Tool/Plugin/GroupLines.pm »
1 package Tail::Tool::File;
2  
3 # Created on: 2010-10-25 11:11:38
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Scalar::Util qw/openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our $VERSION = version->new('0.4.5');
18  
19 3 has name => (
20 is => 'rw',
21 isa => 'Str',
22 documentation => 'The name of a file to be watched',
23 );
24 3 has remote => (
25 is => 'rw',
26 isa => 'Bool',
27 default => 0,
28 init_arg => undef,
29 documentation => 'Flags that the file is located on a remote server',
30 );
31 3 has cmd => (
32 is => 'rw',
33 isa => 'Str',
34 documentation => '',
35 );
36 3 has pid => (
37 is => 'rw',
38 isa => 'Str',
39 documentation => '',
40 );
41 3 has handle => (
42 is => 'rw',
43 isa => 'FileHandle',
44 clearer => 'clear_handle',
45 documentation => 'The opened filehandle of name',
46 );
47 3 has size => (
48 is => 'rw',
49 isa => 'Int',
50 init_arg => undef,
51 default => 0,
52 documentation => 'The size of file when last read',
53 );
54 3 has pause => (
55 is => 'rw',
56 isa => 'Bool',
57 documentation => 'Flags not to display any lines from the file',
58 );
59 3 has auto_unpause => (
60 is => 'rw',
61 isa => 'Bool',
62 default => 0,
63 init_arg => undef,
64 documentation => 'If a file was missing moved or deleted this flags that tailing should be restarted when the file reappears',
65 );
66 3 has no_inotify => (
67 is => 'ro',
68 isa => 'Bool',
69 documentation => 'Flags not to use the INotify the file (can be useful when a file is on a network file system like sshfs)',
70 );
71 3 has watcher => (
72 is => 'rw',
73 init_arg => undef,
74 clearer => 'clear_watcher',
75 documentation => 'This is the event watcher ojbect handle',
76 );
77 3 has runner => (
78 is => 'rw',
79 isa => 'CodeRef',
80 documentation => 'This is the subroutine reference that should be run with each file change',
81 );
82 3 has started => (
83 is => 'rw',
84 isa => 'Bool',
85 default => 0,
86 init_arg => undef,
87 documentation => 'Flags that tailing has started and not to limit the number of lines any more',
88 );
89 3 has stat_time => (
90 is => 'rw',
91 isa => 'Int',
92 default => time,
93 init_arg => undef,
94 documentation => 'The last time a file was stat()ed',
95 );
96 3 has stat_period => (
97 is => 'rw',
98 isa => 'Int',
99 default => 1,
100 documentation => 'The time period between checks if a file has been moved or deleted',
101 );
102 3 has tailer => (
103 is => 'rw',
104 isa => 'Tail::Tool',
105 documentation => 'The object that this file belongs to',
106 );
107 3 has restart => (
108 is => 'ro',
109 default => 0,
110 );
111  
112 3 my $inotify;
113 my $watcher;
114 sub watch {
115 1 ▶ my ($self, $lines) = @_;
Subroutine watch: called CC=11 SCAR=33.0
Pod documented
116  
117 1 ▶ return 0 if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 1 1
118 1 ▶ return $self->watcher if $self->watcher;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watcher 0 1 1
119  
120 1 $self->_get_file_handle();
121  
122 1 ▶ if ( !defined $inotify ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inotify) 1 0 1
123 1 eval { require Linux::Inotify2 };
124 1 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Tail::Tool::File::EVAL_ERROR) { } 0 1 1
125 0 $inotify = 0;
126 }
127 else {
128 1 $inotify = Linux::Inotify2->new;
129 }
130 }
131  
132 1 my $w;
133 1 ▶ if ( $self->name ne '-' && !$self->remote && $inotify && !$self->no_inotify ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->name ne '-' and not $self->remote and $inotify and not $self->no_inotify) { } 1 0 1
elsif ($self->name eq '-') { } 0 0 0
Condition33%3/9
$self->name ne "-" and not $self->remote and $inotify and not $self->no_inotify
!ll&&!rl&&r
001
$self->name ne "-" and not $self->remote and $inotify
!ll&&!rl&&r
001
$self->name ne "-" and not $self->remote
!ll&&!rl&&r
001
MC/DC0%0/4
$self->name ne "-" and not $self->remote and $inotify and not $self->no_inotify
$self->name ne "-" not $self->remote $inotify not $self->no_inotify
Truth table20%1/5
A and not(B) and C and not(D)
A= $self->name ne "-"B= not $self->remoteC= $inotifyD= not $self->no_inotify
ABCDresult
0XXX0
10XX0
110X0
11100
11111
134 # IN_MODIFY | IN_ALL_EVENTS & ~IN_ACCESS
135 1 ▶ $w = $inotify->watch( $self->name, Linux::Inotify2::IN_ALL_EVENTS(), sub { $self->run } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
136 1 ▶ if ( !$watcher ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($watcher) 1 0 1
137 1 ▶ $watcher = AE::io $inotify->fileno, 0, sub { $inotify->poll };
Subroutine __ANON__: not called CC=1 SCAR=1.2
138 }
139 }
140 elsif ( $self->name eq '-' ) {
141 0 $self->started(1);
142 $w = AE::io \*STDIN, 0, sub {
143 0 ▶ if ( !defined fileno \*STDIN ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless (defined fileno \*STDIN) 0 0 0
144 0 close STDIN;
145 }
146 $self->run
147 0 };
148 # TODO work out how to end if STDIN closed
149 }
150 else {
151 0 ▶ $w = AE::timer 0, 1, sub { $self->run };
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 }
153  
154 1 $self->watcher($w);
155  
156 1 return $self->watcher;
157 }
158  
159 sub run {
160 0 ▶ my ($self, $first) = @_;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
161 0 $self->runner->($self, $first);
162 }
163  
164 sub get_line {
165 0 ▶ my ($self) = @_;
Subroutine get_line: not called CC=13 SCAR=52.0
Pod documented
166 0 my $fh = $self->_get_file_handle;
167  
168 0 ▶ return if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 0 0
169  
170 0 ▶ if ( !$self->remote ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->remote) 0 0 0
171 0 ▶ my $size = -s $self->name || 0;
Condition0%0/2
-s $self->name || 0
l!l
00
MC/DC0%0/1
-s $self->name || 0
-s $self->name
Truth table0%0/2
A
A= -s $self->name
Aresult
11
00
172 0 ▶ if ( $size < $self->size ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size < $self->size) { } 0 0 0
elsif ($self->restart) { } 0 0 0
173 0 warn $self->name . " was truncated!\n";
174 0 close $fh;
175 0 $self->clear_handle;
176 0 $fh = $self->_get_file_handle;
177 }
178 elsif ($self->restart) {
179 # reset file handle
180 0 seek $fh, 0, 1;
181 }
182 else {
183 0 $self->clear_watcher;
184 }
185 0 ▶ $self->size($size || 0);
Condition0%0/2
$size || 0
l!l
00
MC/DC0%0/1
$size || 0
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
186 }
187  
188 0 my @lines = <$fh>;
189  
190 # re-check the stat time of the file to make sure that the file has not been rotated
191 0 ▶ if ( !$self->remote && !@lines && time > $self->stat_time + $self->stat_period * 60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->remote || @lines and time > $self->stat_time + $self->stat_period * 60) 0 0 0
Condition0%0/6
not $self->remote || @lines and time > $self->stat_time + $self->stat_period * 60
!ll&&!rl&&r
000
$self->remote || @lines
l!l&&r!l&&!r
000
MC/DC0%0/3
not $self->remote || @lines and time > $self->stat_time + $self->stat_period * 60
$self->remote @lines time > $self->stat_time + $self->stat_period * 60
Truth table0%0/4
not(A || B) and C
A= $self->remoteB= @linesC= time > $self->stat_time + $self->stat_period * 60
ABCresult
1XX0
01X0
0000
0011
192 0 $self->stat_time(time);
193 # TODO why is this being run if the file has finished? Should not be run for STDIN reading
194 0 my @stat_file = stat $self->name;
195 0 my @stat_handle = stat $fh;
196 # check if the file handle's modified time is not the same as files'
197 0 ▶ if ( !defined $stat_handle[1] || !defined $stat_file[1] || $stat_handle[1] != $stat_file[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stat_handle[1] && defined $stat_file[1] or $stat_handle[1] != $stat_file[1]) 0 0 0
Condition0%0/6
not defined $stat_handle[1] && defined $stat_file[1] or $stat_handle[1] != $stat_file[1]
l!l&&r!l&&!r
000
defined $stat_handle[1] && defined $stat_file[1]
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $stat_handle[1] && defined $stat_file[1] or $stat_handle[1] != $stat_file[1]
defined $stat_handle[1] defined $stat_file[1] $stat_handle[1] != $stat_file[1]
Truth table0%0/4
not(A && B) or C
A= defined $stat_handle[1]B= defined $stat_file[1]C= $stat_handle[1] != $stat_file[1]
ABCresult
0XX1
10X1
1111
1100
198 # close and reopen file incase the file has been rotated
199 0 close $fh;
200 0 $self->_get_file_handle();
201 }
202 }
203 0 return @lines;
204 }
205  
206 sub _get_file_handle {
207 1 ▶ my ($self) = @_;
Subroutine _get_file_handle: called CC=16 SCAR=48.5
208  
209 1 my $fh = $self->handle;
210 1 ▶ if ( $self->name eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name eq "-") 0 1 1
211 0 ▶ if ( !$fh ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $fh) { } 0 0 0
elsif (not &openhandle($fh)) { } 0 0 0
212 0 $self->handle(\*STDIN);
213 }
214 elsif ( !openhandle($fh) ) {
215 0 $self->clear_watcher;
216 }
217 0 return $self->handle;
218 }
219  
220 1 ▶ if ( $self->remote || $self->name =~ m{^ssh://}xms ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->remote or $self->name =~ m[^ssh://]msx) { } 0 1 1
elsif (not $fh or tell $fh == -1) { } 1 0 1
Condition33%1/3
$self->remote or $self->name =~ m[^ssh://]msx
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->remote or $self->name =~ m[^ssh://]msx
$self->remote $self->name =~ m[^ssh://]msx
Truth table33%1/3
A or B
A= $self->remoteB= $self->name =~ m[^ssh://]msx
ABresult
1X1
011
000
221 0 $self->remote(1);
222 0 ▶ return if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 0 0
223  
224 0 my ($user, $host, $port, $file) = $self->name =~ m{^ssh://(?: ([^@]+) [@] )? ( [\w.-]+ ) (?: [:] (\d+) )? / (.*)$}xms;
225 0 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
226 0 ▶ my $cmd = sprintf "ssh %s$host %s 'tail -f -n %d %s'",
Branch: 3 branches
BranchTrueFalseTotal
$user ? : 0 0 0
$port ? : 0 0 0
$self->tailer ? : 0 0 0
227 ( $user ? "$user\@" : '' ),
228 ( $port ? "-P $port" : '' ),
229 ( $self->tailer ? $self->tailer->lines : 10 ),
230 _shell_quote($file);
231  
232 0 ▶ if ( my $pid = open $fh, '-|', $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = open($fh, '-|', $cmd)) { } 0 0 0
233 0 $fh->blocking(0);
234 0 $self->pid($pid);
235 0 $self->handle($fh);
236 }
237 else {
238 0 $self->pause(1);
239 0 warn "Could not tail remote file (" . $self->name . "): $!";
240 }
241 }
242 }
243 ▶ elsif ( !$fh || tell $fh == -1 ) {
Condition33%1/3
not $fh or tell $fh == -1
l!l&&r!l&&!r
100
MC/DC0%0/2
not $fh or tell $fh == -1
not $fh tell $fh == -1
Truth table33%1/3
not(A) or B
A= not $fhB= tell $fh == -1
ABresult
1X1
011
000
244 1 ▶ if ( open $fh, '<', $self->name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, '<', $self->name) { } 1 0 1
245 1 $self->handle($fh);
246 1 $self->size(-s $self->name);
247 }
248 else {
249 0 ▶ warn "Could not open '".$self->name."': $!\n" if !$self->auto_unpause;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_unpause 0 0 0
250 0 $self->pause(1);
251 0 $self->auto_unpause(1);
252 }
253 }
254  
255 1 return $fh;
256 }
257  
258 sub _shell_quote {
259 0 ▶ my ($file) = @_;
Subroutine _shell_quote: not called CC=1 SCAR=6.9
260 0 $file =~ s{ ( [^\w\-./?*] ) }{\\$1}gxms;
261  
262 0 return $file;
263 }
264  
265 3 1;
266  
267 __END__
268  
269 =head1 NAME
270  
271 Tail::Tool::File - Looks after individual files
272  
273 =head1 VERSION
274  
275 This documentation refers to Tail::Tool::File version 0.4.5.
276  
277 =head1 SYNOPSIS
278  
279 use Tail::Tool::File;
280  
281 # Create a new Tail::Tool::File object
282 my $file = Tail::Tool::File->new( '/my/file' );
283  
284 =head1 DESCRIPTION
285  
286 =head1 SUBROUTINES/METHODS
287  
288 =head2 C<watch ()>
289  
290 Return: AnyEvent watcher or Linux::Inotify2 watcher
291  
292 Description: Creates the watcher for the file if the file exists and is not
293 paused.
294  
295 =head2 C<run ($first)>
296  
297 Param: C<$first> - bool - Specifies that this is the first time run has been
298 called.
299  
300 Description: Runs the the file event.
301  
302 =head2 C<get_line ()>
303  
304 Description: Gets any unread lines from the file.
305  
306 =head1 DIAGNOSTICS
307  
308 =head1 DEPENDENCIES
309  
310 =head1 INCOMPATIBILITIES
311  
312 =head1 BUGS AND LIMITATIONS
313  
314 There are no known bugs in this module.
315  
316 Please report problems to Ivan Wills (ivan.wills@gamil.com).
317  
318 Patches are welcome.
319  
320 =head1 AUTHOR
321  
322 Ivan Wills - (ivan.wills@gamil.com)
323 <Author name(s)> (<contact address>)
324  
325 =head1 LICENSE AND COPYRIGHT
326  
327 Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW, Australia, 2077).
328 All rights reserved.
329  
330 This module is free software; you can redistribute it and/or modify it under
331 the same terms as Perl itself. See L<perlartistic>. This program is
332 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
333 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
334 PARTICULAR PURPOSE.
335  
336 =cut
« blib/lib/Tail/Tool.pm ↑ Summary blib/lib/Tail/Tool/Plugin/GroupLines.pm »