bin/rrr-fsck

filter: Statementstmt 30.5% 43 / 141 Branchbran 3.4% 2 / 58 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 85.7% 12 / 14 Podpod n/a totaltotal 24.5% 57 / 232 CC 7 SCAR 26.2
CC
7
Cov
21%
__ANON__
42.0
__ANON__
6.0
BEGIN
1.0
CRAP 13.8
« blib/lib/File/Rsync/Mirror/Recentfile.pm ↑ Summary blib/lib/File/Rsync/Mirror/Recentfile/Done.pm »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 rrr-fsck -
6  
7 =head1 SYNOPSIS
8  
9 rrr-fsck [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<--dry-run|n>
20  
21 Does nothing, only prints what it would do.
22  
23 =item B<--help|h>
24  
25 Prints a brief message and exits.
26  
27 =item B<--remoteroot=s>
28  
29 If provided fsck will try to mirror missing files from this location.
30 For remote locations requiring authentication you may need to set the
31 environment variables USER and RSYNC_PASSWORD as well.
32  
33 =item B<--verbose|v+>
34  
35 More feedback.
36  
37 =item B<--yes|y>
38  
39 Consider all answers to asked questions to be I<yes>.
40  
41 =back
42  
43 =head1 DESCRIPTION
44  
45 Compares disk contents with index contents and gathers files missing
46 on local disk and files missing in local index.
47  
48 If remoteroot is given missing files are fetched from remote.
49  
50 Files on the local disk that have no counterpart in the index are
51 considered obsolete and the user is asked for each file if the file
52 should be deleted. And if the user confirms it will be deleted.
53  
54 =head1 BUGS
55  
56 There is a race condition when the tree or the index is manipulated
57 while we are running. This implies that the result is only then 100%
58 correct when disk and index are not changed while we are running.
59  
60 There should be an option to declare the files on disk authoritative
61 so that they are added to the index.
62  
63 =cut
64  
65  
66 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use ExtUtils::MakeMaker qw(prompt);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use File::Rsync::Mirror::Recent;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 1 our %Opt;
81 1 lock_keys %Opt, map { /([^=|]+)/ } @opt;
82 1 ▶GetOptions(\%Opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%main::Opt, @opt) or pod2usage(1) 1 0 1
83 @opt,
84 ) or pod2usage(1);
85  
86 1 ▶if ($Opt{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"help"}) 1 0 1
87 1 pod2usage(0);
88 }
89  
90 0 ▶if (@ARGV == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV == 1) { } 0 0 0
91 } else {
92 0 pod2usage(1);
93 }
94  
95 0 my($principal) = @ARGV;
96 0 my $recc = File::Rsync::Mirror::Recent->new
97 (
98 local => $principal,
99 localroot => dirname $principal,
100 );
101 0 for my $passthrough (qw(remoteroot verbose)) {
102 0 ▶ if (my $opt = $Opt{$passthrough}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt = $main::Opt{$passthrough}) 0 0 0
103 0 $recc->$passthrough($opt);
104 }
105 }
106 0 my $root = $recc->localroot;
107 0 ▶die "Alert: Root not defined, giving up" unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 0 0
108 0 my $prf = $recc->principal_recentfile;
109 0 my $filenameroot = $prf->filenameroot;
110 0 my $serializer_suffix = $prf->serializer_suffix;
111 0 my $ignore_rx = qr((?x:
112 ^ \Q$filenameroot\E (?: - [0-9]*[smhdWMQYZ] \Q$serializer_suffix\E (?: \.lock (?: /.* )? )? | \.recent ) \z
113 ));
114  
115 0 my %diskfiles;
116 my $i;
117 0 my $last_verbosity = 0;
118 0 $|=1;
119 0 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 0 0
120 0 print "\n";
121 }
122 find({
123 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
124 0 my @lstat = lstat $_;
125 0 ▶ return unless -l _ or -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -l _ or -f _ 0 0 0
Condition0%0/3
-l _ or -f _
l!l&&r!l&&!r
000
MC/DC0%0/2
-l _ or -f _
-l _ -f _
Truth table0%0/3
A or B
A= -l _B= -f _
ABresult
1X1
011
000
126 0 my($reportname) = $File::Find::name =~ m{^\Q$root\E/*(.*)};
127 0 ▶ return if $reportname =~ $ignore_rx;
Branch: 1 branch
BranchTrueFalseTotal
if $reportname =~ /$ignore_rx/ 0 0 0
128 0 $i++;
129 0 ▶ if ($Opt{verbose} && time - $last_verbosity > 0.166666) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"} and &main::time() - $last_verbosity > "0.166666") 0 0 0
Condition0%0/3
$main::Opt{"verbose"} and &main::time() - $last_verbosity > "0.166666"
!ll&&!rl&&r
000
MC/DC0%0/2
$main::Opt{"verbose"} and &main::time() - $last_verbosity > "0.166666"
$main::Opt{"verbose"} &main::time() - $last_verbosity > "0.166666"
Truth table0%0/3
A and B
A= $main::Opt{"verbose"}B= &main::time() - $last_verbosity > "0.166666"
ABresult
0X0
100
111
130 0 printf "\r%8d files and symlinks checked on disk ", $i;
131 0 $last_verbosity = time;
132 }
133 0 $diskfiles{$File::Find::name} = $lstat[9];
134 },
135 0 no_chdir => 1,
136 },
137 $root
138 );
139 0 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 0 0
140 0 printf "\r%8d files and symlinks checked on disk\n", $i;
141 }
142 0 $i = 0;
143  
144 0 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 0 0
145 0 print "\rChecking index";
146 }
147 0 my @newsargs = ();
148 0 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 0 0
149 @newsargs =
150 0 ▶ (callback => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
151 0 $i = scalar @{shift;};
152 0 ▶ if (time - $last_verbosity > 0.166666) {
Branch: 1 branch
BranchTrueFalseTotal
if (&main::time() - $last_verbosity > "0.166666") 0 0 0
153 0 printf "\r%8d entries read from index ", $i;
154 0 $last_verbosity = time;
155 }
156 0 });
157 }
158 0 my $indexfiles = $recc->news(@newsargs);
159 0 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 0 0
160 0 printf "\r%8d entries read from index\n", scalar @$indexfiles;
161 }
162 0 my %seen;
163 my %indexfiles = map {
164 ("$root/$_->{path}"=>$_->{epoch})
165 0 } grep {
166 0 defined $_->{path} &&
167 !$seen{$_->{path}}++
168 0 ▶ && $_->{type} eq "new"
Condition0%0/6
defined $_->{'path'} && !$seen{$_->{'path'}}++ && $_->{'type'} eq 'new'
!ll&&!rl&&r
000
defined $_->{"path"} and not $seen{$_->{"path"}}++
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_->{'path'} && !$seen{$_->{'path'}}++ && $_->{'type'} eq 'new'
defined $_->{"path"} not $seen{$_->{"path"}}++ $_->{'type'} eq 'new'
Truth table0%0/4
A and not(B) && C
A= defined $_->{"path"}B= not $seen{$_->{"path"}}++C= $_->{'type'} eq 'new'
ABCresult
0XX0
10X0
1100
1111
169 } @$indexfiles;
170  
171 0 my @missing_rfrfiles;
172 0 for my $rf (@{$recc->recentfiles}) {
173 0 my $rfrfile = $rf->rfile;
174 0 ▶ unless (-e $rfrfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $rfrfile) 0 0 0
175 0 push @missing_rfrfiles, $rfrfile;
176 }
177 }
178 0 ▶if (@missing_rfrfiles) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing_rfrfiles) 0 0 0
179 0 warn "Warning: missing index files @missing_rfrfiles\n";
180 }
181 0 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 0 0
182 0 printf "\r%8d file objects found in index\n", scalar keys %indexfiles;
183 }
184 0 my $sprintfd = length(max scalar @$indexfiles, scalar keys %diskfiles);
185 0 warn sprintf(
186 "diskfiles: %*d\n".
187 "indexfiles: %*d\n",
188 $sprintfd, scalar keys %diskfiles,
189 $sprintfd, scalar keys %indexfiles,
190 );
191 0 my @diskmisses = sort { $indexfiles{$b} <=> $indexfiles{$a} } grep { ! exists $diskfiles{$_} } keys %indexfiles;
192 0 my @indexmisses = sort { $diskfiles{$a} <=> $diskfiles{$b} } grep { ! exists $indexfiles{$_} } keys %diskfiles;
193 0 warn sprintf(
194 "missing on disk: %*d\n".
195 "missing in index: %*d\n",
196 $sprintfd, scalar @diskmisses,
197 $sprintfd, scalar @indexmisses,
198 );
199 0 $DB::single++;
200 0 my $rf = $recc->principal_recentfile;
201 0 my $last_aggregate_call = time;
202 0 my @batch;
203 0 for my $dm (@diskmisses) {
204 0 ▶ if (0) {
Branch: 2 branches
BranchTrueFalseTotal
if (do { $main::Opt{'dry-run'} }) { } 0 0 0
elsif ($main::Opt{'remoteroot'}) { } 0 0 0
205 } elsif ($Opt{"dry-run"}) {
206 0 ▶ if ($Opt{remoteroot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{'remoteroot'}) { } 0 0 0
207 0 warn "Would fetch $dm\n";
208 } else {
209 0 warn "Would remove from indexfile $dm\n";
210 }
211 } elsif ($Opt{remoteroot}) {
212 0 my $relative = substr $dm, 1 + length $root;
213 0 $rf->get_remotefile($relative);
214 } else {
215 0 warn "Removing from indexfile: $dm\n";
216 0 push @batch, {path => $dm, type => "delete"};
217 }
218 }
219 0 for my $im (@indexmisses) {
220 0 ▶ if ($Opt{"dry-run"}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($main::Opt{'dry-run'}) { } 0 0 0
elsif ($main::Opt{'remoteroot'}) { } 0 0 0
221 0 ▶ if ($Opt{remoteroot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{'remoteroot'}) { } 0 0 0
222 0 warn "Would remove $im\n";
223 } else {
224 0 warn "Would add to indexfile $im\n";
225 }
226 } elsif ($Opt{remoteroot}) {
227 0 my $ans;
228 0 ▶ if ($Opt{yes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{'yes'}) { } 0 0 0
229 0 warn "Going to unlink '$im'\n";
230 0 $ans = "y";
231 } else {
232 0 $ans = prompt "Unlink '$im'?", "y";
233 }
234 0 ▶ if ($ans =~ /^y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ans =~ /^y/i) 0 0 0
235 0 ▶ unlink $im or die "Could not unlink '$im': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $im or die "Could not unlink '${im}': $!" 0 0 0
236 }
237 } else {
238 0 warn "Adding to indexfile: $im\n";
239 0 ▶ my @stat = lstat $im or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = lstat $im or next 0 0 0
240 0 push @batch, {epoch => $stat[9], path => $im, type => "new"};
241 }
242 }
243 0 ▶unless ($Opt{"dry-run"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($main::Opt{"dry-run"}) 0 0 0
244 0 ▶ if (@batch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@batch) 0 0 0
245 0 $rf->batch_update(\@batch);
246 }
247 }
248  
249 __END__
« blib/lib/File/Rsync/Mirror/Recentfile.pm ↑ Summary blib/lib/File/Rsync/Mirror/Recentfile/Done.pm »