bin/rrr-server

filter: Statementstmt 26.2% 32 / 122 Branchbran 2.6% 2 / 76 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 60.0% 9 / 15 Podpod n/a totaltotal 18.0% 43 / 238 CC 39 SCAR 54.9
CC
39
Cov
16%
handle_event
506.0
newdir
132.0
my_inotify
42.0
CRAP 242.8
↑ Summary blib/lib/File/Rsync/Mirror/Recent.pm »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 rrr-server - watch a tree and continuously update indexfiles
6  
7 =head1 SYNOPSIS
8  
9 rrr-server [options] principalfile
10  
11 =head1 OPTIONS
12  
13 =over 8
14  
15 =cut
16  
17 1 my @opt = <<'=back' =~ /B<--(\S+)>/g;
18  
19 =item B<--help|h>
20  
21 Prints a brief message and exits.
22  
23 =item B<--verbose|v+>
24  
25 More feedback.
26  
27 =back
28  
29 =head1 DESCRIPTION
30  
31 After you have setup a tree watch it with inotify and keep it
32 uptodate. Depends on inotify which probably only exists on linux.
33  
34 =head1 PREREQUISITE
35  
36 Linux::Inotify2.
37  
38 It is not declared as prerequisites of the F:R:M:Recent package
39 because server side handling is optional. XXX Todo: make server side
40 handling a separate package so we can declare Inotify2 as prereq.
41  
42 =cut
43  
44 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use File::Rsync::Mirror::Recent;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 1 our %Opt;
56 1 ▶GetOptions(\%Opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%main::Opt, @opt) or pod2usage(1) 1 0 1
57 @opt,
58 ) or pod2usage(1);
59  
60 1 ▶if ($Opt{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"help"}) 1 0 1
61 1 pod2usage(0);
62 }
63  
64 0 ▶if (@ARGV != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV != 1) 0 0 0
65 0 pod2usage(1);
66 }
67  
68 0 ▶sub my_inotify {
Subroutine my_inotify: not called CC=6 SCAR=37.4
69 0 my($inotify, $directory) = @_;
70 0 ▶ unless ($inotify->watch
Branch: 1 branch
BranchTrueFalseTotal
unless ($inotify->watch($directory, IN_CLOSE_WRITE() | IN_MOVED_FROM() | IN_MOVED_TO() | IN_CREATE() | IN_DELETE() | IN_DELETE_SELF() | IN_MOVE_SELF())) 0 0 0
71 (
72 $directory,
73 IN_CLOSE_WRITE()
74 |IN_MOVED_FROM()
75 |IN_MOVED_TO()
76 |IN_CREATE()
77 |IN_DELETE()
78 |IN_DELETE_SELF()
79 |IN_MOVE_SELF()
80 )) {
81 # or die "watch creation failed: $!";
82 0 ▶ if ($!{ENOSPC}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'ENOSPC'}) { } 0 0 0
83 0 die "Alert: ENOSPC reached, probably your system needs to increase the amount of inotify watches allowed per user via '/proc/sys/fs/inotify/max_user_watches'\n";
84 } else {
85 0 for my $err (qw(ENOENT EBADF EEXIST)) {
86 0 ▶ if ($!{$err}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{$err}) 0 0 0
87 0 ▶ warn "$err encountered on '$directory'. Giving up on this watch, trying to continue.\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
88 0 return;
89 }
90 }
91 0 die "Alert: $!";
92 }
93 }
94 }
95  
96 0 ▶sub handle_file {
Subroutine handle_file: not called CC=1 SCAR=6.9
97 0 my($rf,$fullname,$type,$batch) = @_;
98 0 push @$batch, {path => $fullname, type => $type};
99 }
100  
101 0 ▶sub newdir {
Subroutine newdir: not called CC=11 SCAR=48.8
102 0 my($inotify,$rf,$fullname,$batch) = @_;
103 0 ▶ return if -l $fullname;
Branch: 1 branch
BranchTrueFalseTotal
if -l $fullname 0 0 0
104 0 my_inotify($inotify, $fullname);
105 # immediately inspect it, we certainly have missed the first
106 # events in this directory
107 0 ▶ opendir my $dh, $fullname or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $fullname or return 0 0 0
108 0 for my $dirent (readdir $dh) {
109 0 ▶ next if $dirent eq "." || $dirent eq "..";
Branch: 1 branch
BranchTrueFalseTotal
if $dirent eq "." or $dirent eq ".." 0 0 0
Condition0%0/3
$dirent eq "." or $dirent eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$dirent eq "." or $dirent eq ".."
$dirent eq "." $dirent eq ".."
Truth table0%0/3
A or B
A= $dirent eq "."B= $dirent eq ".."
ABresult
1X1
011
000
110 0 my $abs = File::Spec->catfile($fullname,$dirent);
111 0 ▶ if (-l $abs || -f _) {
Branch: 2 branches
BranchTrueFalseTotal
if (-l $abs or -f _) { } 0 0 0
elsif (-d $abs) { } 0 0 0
Condition0%0/3
-l $abs or -f _
l!l&&r!l&&!r
000
MC/DC0%0/2
-l $abs or -f _
-l $abs -f _
Truth table0%0/3
A or B
A= -l $absB= -f _
ABresult
1X1
011
000
112 0 ▶ warn "[..:..:..] Readdir_F $abs\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
113 0 handle_file($rf,$abs,"new",$batch);
114 } elsif (-d $abs) {
115 0 ▶ warn "[..:..:..] Readdir_D $abs\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
116 0 newdir($inotify,$rf,$abs,$batch);
117 }
118 }
119 }
120  
121 0 ▶sub handle_event {
Subroutine handle_event: not called CC=22 SCAR=62.3
122 0 my($inotify,$rf,$ev,$batch) = @_;
123 0 my @stringifiedmask;
124 0 for my $watch (
125 "IN_CREATE", "IN_CLOSE_WRITE", "IN_MOVED_TO", # new
126 "IN_DELETE", "IN_MOVED_FROM", # delete
127 "IN_DELETE_SELF", "IN_MOVE_SELF", # self
128 ) {
129 0 ▶ if ($ev->$watch()){
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->$watch) 0 0 0
130 0 push @stringifiedmask, $watch;
131 # new directories must be added to the watches, deleted
132 # directories deleted; moved directories both
133 }
134 }
135 0 my $rootdir = $rf->localroot;
136 # warn sprintf "rootdir[$rootdir]time[%s]ev.w.name[%s]ev.name[%s]ev.fullname[%s]mask[%s]\n", time, $ev->w->name, $ev->name, $ev->fullname, join("|",@stringifiedmask);
137 0 my $ignore = 0;
138 0 ▶ if ($ev->w->name eq $rootdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->w->name eq $rootdir) 0 0 0
139 0 my $meta = $rf->meta_data;
140 0 my $ignore_rx = qr((?x: ^ \Q$meta->{filenameroot}\E - [0-9]*[smhdWMQYZ] \Q$meta->{serializer_suffix}\E ));
141 0 ▶ if ($ev->name =~ $ignore_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->name =~ /$ignore_rx/) 0 0 0
142 # warn sprintf "==> Ignoring object in rootdir looking like internal file: %s", $ev->name;
143 0 $ignore++;
144 }
145 }
146 0 ▶ unless ($ignore) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore) 0 0 0
147 0 my $fullname = $ev->fullname;
148 0 my($reportname) = $fullname =~ m{^\Q$rootdir\E/(.*)};
149 0 my $time = sprintf "%02d:%02d:%02d", (localtime)[2,1,0];
150 0 ▶ if (0) {
Branch: 6 branches
BranchTrueFalseTotal
if (do { $ev->IN_Q_OVERFLOW }) { } 0 0 0
elsif ($ev->IN_DELETE or $ev->IN_MOVED_FROM) { } 0 0 0
elsif ($ev->IN_DELETE_SELF or $ev->IN_MOVE_SELF) { } 0 0 0
elsif (-l $fullname) { } 0 0 0
elsif ($ev->IN_ISDIR) { } 0 0 0
elsif (-f _) { } 0 0 0
151 } elsif ($ev->IN_Q_OVERFLOW) {
152 0 $rf->_requires_fsck(1);
153 ▶ } elsif ($ev->IN_DELETE || $ev->IN_MOVED_FROM) {
Condition0%0/3
$ev->IN_DELETE or $ev->IN_MOVED_FROM
l!l&&r!l&&!r
000
MC/DC0%0/2
$ev->IN_DELETE or $ev->IN_MOVED_FROM
$ev->IN_DELETE $ev->IN_MOVED_FROM
Truth table0%0/3
A or B
A= $ev->IN_DELETEB= $ev->IN_MOVED_FROM
ABresult
1X1
011
000
154 # we don't know whether it was a directory, we simply pass
155 # it to $rf. $rf must be robust enough to swallow bogus
156 # deletes. Alternatively we could look into $rf whether
157 # this object is known, couldn't we?
158 0 handle_file($rf,$fullname,"delete",$batch);
159 0 ▶ warn "[$time] Deleteobj $reportname (@stringifiedmask)\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
160 ▶ } elsif ($ev->IN_DELETE_SELF || $ev->IN_MOVE_SELF) {
Condition0%0/3
$ev->IN_DELETE_SELF or $ev->IN_MOVE_SELF
l!l&&r!l&&!r
000
MC/DC0%0/2
$ev->IN_DELETE_SELF or $ev->IN_MOVE_SELF
$ev->IN_DELETE_SELF $ev->IN_MOVE_SELF
Truth table0%0/3
A or B
A= $ev->IN_DELETE_SELFB= $ev->IN_MOVE_SELF
ABresult
1X1
011
000
161 0 $ev->w->cancel;
162 0 ▶ warn "[$time] Delwatcher $reportname (@stringifiedmask)\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
163 } elsif (-l $fullname) {
164 0 handle_file($rf,$fullname,"new",$batch);
165 0 ▶ warn "[$time] Updatelink $reportname (@stringifiedmask)\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
166 } elsif ($ev->IN_ISDIR) {
167 0 newdir($inotify,$rf,$fullname,$batch);
168 0 ▶ warn "[$time] Newwatcher $reportname (@stringifiedmask)\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
169 } elsif (-f _) {
170 0 ▶ if ($ev->IN_CLOSE_WRITE || $ev->IN_MOVED_TO) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev->IN_CLOSE_WRITE or $ev->IN_MOVED_TO) 0 0 0
Condition0%0/3
$ev->IN_CLOSE_WRITE or $ev->IN_MOVED_TO
l!l&&r!l&&!r
000
MC/DC0%0/2
$ev->IN_CLOSE_WRITE or $ev->IN_MOVED_TO
$ev->IN_CLOSE_WRITE $ev->IN_MOVED_TO
Truth table0%0/3
A or B
A= $ev->IN_CLOSE_WRITEB= $ev->IN_MOVED_TO
ABresult
1X1
011
000
171 0 handle_file($rf,$fullname,"new",$batch);
172 0 ▶ warn "[$time] Updatefile $reportname (@stringifiedmask)\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
173 }
174 } else {
175 0 ▶ warn "[$time] Ignore $reportname (@stringifiedmask)\n" if $Opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::Opt{"verbose"} 0 0 0
176 }
177 }
178 }
179  
180 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
181 0 my($inotify, $rootdir) = @_;
182 0 foreach my $directory ( File::Find::Rule->new->directory->not( File::Find::Rule->new->symlink )->in($rootdir) ) {
183 0 my_inotify($inotify, $directory);
184 }
185  
186 }
187  
188 # Need also to verify that we watch all directories we encounter.
189 0 ▶sub fsck {
Subroutine fsck: not called CC=2 SCAR=17.9
190 0 my($rf) = @_;
191 0 ▶ 0 == system $^X, "-I$FindBin::Bin/../lib", "$FindBin::Bin/rrr-fsck", $rf->rfile, "--verbose"
Branch: 1 branch
BranchTrueFalseTotal
0 == system($^X, "-I$FindBin::Bin/../lib", "$FindBin::Bin/rrr-fsck", $rf->rfile, "--verbose") or die "Could not run rrr-fsck" 0 0 0
192 or die "Could not run rrr-fsck";
193 }
194  
195 MAIN: {
196 0 my($principal) = @ARGV;
197 0 $principal = File::Spec->rel2abs($principal);
198 0 my $recc = File::Rsync::Mirror::Recent->new
199 (local => $principal);
200 0 my($rf) = $recc->principal_recentfile;
201 0 my $rootdir = $rf->localroot;
202 0 for my $req (qw(Linux::Inotify2 File::Find::Rule)) {
203 0 eval qq{ require $req; 1 };
204 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
205 0 die "Failing on 'require $req': $@"
206 } else {
207 0 $req->import;
208 }
209 }
210  
211 0 ▶ my $inotify = new Linux::Inotify2
Branch: 1 branch
BranchTrueFalseTotal
my $inotify = "Linux::Inotify2"->new or die "Unable to create new inotify object: $!" 0 0 0
212 or die "Unable to create new inotify object: $!";
213  
214 0 init($inotify, $rootdir);
215 0 fsck($rf);
216 0 my $last_aggregate_call = 0;
217  
218 0 while () {
219 0 my @events = $inotify->read;
220 0 ▶ unless ( @events > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@events > 0) 0 0 0
221 0 print "Alert: inotify read error: $!";
222 0 last;
223 }
224 0 my @batch;
225 0 foreach my $event (@events) {
226 0 handle_event($inotify,$rf,$event,\@batch);
227 }
228 0 ▶ $rf->batch_update(\@batch) if @batch;
Branch: 1 branch
BranchTrueFalseTotal
if @batch 0 0 0
229 0 ▶ if (time > $last_aggregate_call + 60) { # arbitrary
Branch: 1 branch
BranchTrueFalseTotal
if (&main::time() > $last_aggregate_call + 60) 0 0 0
230 0 $rf->aggregate;
231 0 $last_aggregate_call = time;
232 }
233 0 ▶ if ($rf->_requires_fsck) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rf->_requires_fsck) 0 0 0
234 0 fsck($rf);
235 0 $rf->_requires_fsck(0);
236 }
237 }
238 }
239  
240 __END__
↑ Summary blib/lib/File/Rsync/Mirror/Recent.pm »