bring the two inotify->watch calls into one sub and add some error conditions that...
[rersyncrecent.git] / bin / rrr-server
blobf81ae05c689bbaa48bd6d897e39ffdb6fd30c57a
1 #!/usr/bin/perl
3 =head1 NAME
5 rrr-server - watch a tree and continuously update indexfiles
7 =head1 SYNOPSIS
9 rrr-server [options] principalfile
11 =head1 OPTIONS
13 =over 8
15 =cut
17 my @opt = <<'=back' =~ /B<--(\S+)>/g;
19 =item B<--help|h>
21 Prints a brief message and exists.
23 =item B<--verbose|v+>
25 More feedback.
27 =back
29 =head1 DESCRIPTION
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.
34 =head1 PREREQUISITE
36 Linux::Inotify2.
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.
42 =cut
44 use strict;
45 use warnings;
47 use File::Find qw(find);
48 use lib "/home/k/sources/rersyncrecent/lib";
49 use File::Rsync::Mirror::Recent;
50 use File::Spec;
51 use Getopt::Long;
52 use Pod::Usage qw(pod2usage);
53 use Time::HiRes qw(time);
55 our %Opt;
56 GetOptions(\%Opt,
57 @opt,
58 ) or pod2usage(1);
60 if ($Opt{help}) {
61 pod2usage(0);
64 if (@ARGV != 1) {
65 pod2usage(1);
68 my($principal) = @ARGV;
69 my $recc = File::Rsync::Mirror::Recent->new
70 (local => $principal);
71 my($rf) = $recc->principal_recentfile;
72 my $rootdir = $rf->localroot;
73 $rootdir =~ s|/$||;
74 for my $req (qw(Linux::Inotify2 File::Find::Rule)) {
75 eval qq{ require $req; 1 };
76 if ($@) {
77 die "Failing on 'require $req': $@"
78 } else {
79 $req->import;
83 my $inotify = new Linux::Inotify2
84 or die "Unable to create new inotify object: $!";
86 sub my_inotify {
87 my($directory) = @_;
88 unless ($inotify->watch
90 $directory,
91 IN_CLOSE_WRITE()
92 |IN_MOVED_FROM()
93 |IN_MOVED_TO()
94 |IN_CREATE()
95 |IN_DELETE()
96 |IN_DELETE_SELF()
97 |IN_MOVE_SELF()
98 )) {
99 # or die "watch creation failed: $!";
100 if ($!{ENOSPC}) {
101 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";
102 } else {
103 for my $err (qw(ENOENT EBADF EEXIST)) {
104 if ($!{$err}) {
105 warn "$err encountered on '$directory'. Giving up on this watch, trying to continue.\n" if $Opt{verbose};
106 return;
109 die "Alert: $!";
113 foreach my $directory ( File::Find::Rule->new->directory->in($rootdir) ) {
114 my_inotify($directory);
117 sub handle_file {
118 my($rf,$fullname,$type,$batch) = @_;
119 push @$batch, {path => $fullname, type => $type};
122 sub newdir {
123 my($inotify,$rf,$fullname,$batch) = @_;
124 my_inotify($fullname);
125 # immediately inspect it, we certainly have missed the first
126 # events in this directory
127 opendir my $dh, $fullname or return;
128 for my $dirent (readdir $dh) {
129 next if $dirent eq "." || $dirent eq "..";
130 my $abs = File::Spec->catfile($fullname,$dirent);
131 if (-l $abs || -f _) {
132 warn "[..:..:..] Readdir_F $abs\n";
133 handle_file($rf,$abs,"new",$batch);
134 } elsif (-d $abs) {
135 warn "[..:..:..] Readdir_D $abs\n";
136 newdir($inotify,$rf,$abs,$batch);
141 my $requires_fsck = 1;
142 sub handle_event {
143 my($ev,$batch) = @_;
144 my @stringifiedmask;
145 for my $watch (
146 "IN_CREATE", "IN_CLOSE_WRITE", "IN_MOVED_TO", # new
147 "IN_DELETE", "IN_MOVED_FROM", # delete
148 "IN_DELETE_SELF", "IN_MOVE_SELF", # self
150 if ($ev->$watch()){
151 push @stringifiedmask, $watch;
152 # new directories must be added to the watches, deleted
153 # directories deleted; moved directories both
156 # 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);
157 my $ignore = 0;
158 if ($ev->w->name eq $rootdir) {
159 my $meta = $rf->meta_data;
160 my $ignore_rx = qr((?x: ^ \Q$meta->{filenameroot}\E - [0-9]*[smhdWMQYZ] \Q$meta->{serializer_suffix}\E ));
161 if ($ev->name =~ $ignore_rx) {
162 # warn sprintf "==> Ignoring object in rootdir looking like internal file: %s", $ev->name;
163 $ignore++;
166 unless ($ignore) {
167 my $fullname = $ev->fullname;
168 my($reportname) = $fullname =~ m{^\Q$rootdir\E/(.*)};
169 my $time = sprintf "%02d:%02d:%02d", (localtime)[2,1,0];
170 if (0) {
171 } elsif ($ev->IN_Q_OVERFLOW) {
172 $requires_fsck = 1;
173 } elsif ($ev->IN_DELETE || $ev->IN_MOVED_FROM) {
174 # we don't know whether it was a directory, we simply pass
175 # it to $rf. $rf must be robust enough to swallow bogus
176 # deletes. Alternatively we could look into $rf whether
177 # this object is known, couldn't we?
178 handle_file($rf,$fullname,"delete",$batch);
179 warn "[$time] Deleteobj $reportname (@stringifiedmask)\n";
180 } elsif ($ev->IN_DELETE_SELF || $ev->IN_MOVE_SELF) {
181 $ev->w->cancel;
182 warn "[$time] Delwatcher $reportname (@stringifiedmask)\n";
183 } elsif ($ev->IN_ISDIR) {
184 newdir($inotify,$rf,$fullname,$batch);
185 warn "[$time] Newwatcher $reportname (@stringifiedmask)\n";
186 } elsif (-l $fullname) {
187 handle_file($rf,$fullname,"new",$batch);
188 warn "[$time] Updatelink $reportname (@stringifiedmask)\n";
189 } elsif (-f _) {
190 if ($ev->IN_CLOSE_WRITE || $ev->IN_MOVED_TO) {
191 handle_file($rf,$fullname,"new",$batch);
192 warn "[$time] Updatefile $reportname (@stringifiedmask)\n";
194 } else {
195 warn "[$time] Ignore $reportname (@stringifiedmask)\n";
200 my $last_aggregate_call = 0;
201 my $have_warned_fsck = 0;
203 while () {
204 my @events = $inotify->read;
205 unless ( @events > 0 ) {
206 print "Alert: inotify read error: $!";
207 last;
209 my @batch;
210 foreach my $event (@events) {
211 handle_event($event,\@batch);
213 $rf->batch_update(\@batch);
214 if (time > $last_aggregate_call + 30) {
215 $rf->aggregate;
216 $last_aggregate_call = time;
218 if ($requires_fsck) {
219 if (time > $have_warned_fsck + 3600) {
220 warn "REMINDER: TODO: HANDLE FSCK";
221 $have_warned_fsck = time;
226 __END__
229 # Local Variables:
230 # mode: cperl
231 # coding: utf-8
232 # cperl-indent-level: 4
233 # End: