Fixed recursive directories removal: 0600 -> 0700
[nethome.git] / bin / cvsutil
1 #! /usr/bin/perl
2 #
3 #       $Id$
4 #
5 #       Recommended aliases:
6 #               alias cvsfiles='cvsutil --files --print'
7 #               alias cvsignores='cvsutil --ignores --print'
8 #               alias cvsignoresall='cvsutil --ignores --workings --print'
9 #               alias cvsignoresrm='cvsutil --ignores --rm'
10 #               alias cvsignoresrmall='cvsutil --ignores --workings --rm'
11 #               alias cvsignoresallrm='cvsutil --ignores --workings --rm'
12
13 use strict;
14 use warnings;
15
16 use Getopt::Long;
17 use Cwd qw(chdir fastgetcwd);
18 use Errno qw(ENOENT);
19 use Carp qw(confess cluck croak carp);
20 BEGIN {
21         if (!eval q{ use File::Remove qw(remove); 1; }) {{
22                 sub main::remove(@)
23                 {
24                         my $r="";
25                         if ("SCALAR" eq ref $_[0]) {
26                                 $r="-r" if ${$_[0]};
27                                 shift;
28                                 }
29                         my $cmd="rm -f $r ".join(" ",map({s/'/'\\''/g;"'$_'";} @_));
30                         my $err=system($cmd) and confess("$cmd: $cmd");
31                         return @_;
32                 }
33                 }}
34 }
35
36 use constant ENTRIES  =>"CVS/Entries";
37 use constant CVSIGNORE=>".cvsignore";
38 use constant ROOT     =>"CVS/Root";
39
40 my($opt_files,$opt_ignores,$opt_workings,$opt_dirs);
41 my($opt_rm,$opt_print);
42 our($opt_root); # undefined=>no rooting, ""=>print roots or first phase for root checking, ne ""=>set roots
43 my($opt_verbose,$opt_fatal);
44 my($opt_force); # set roots without their checking
45 my(@opt_ignore);
46
47 $Getopt::Long::ignorecase=0;
48 $Getopt::Long::bundling=1;
49 die if !GetOptions_shortfilter(
50                 "f|files!"   ,\$opt_files,
51                 "i|ignores!" ,\$opt_ignores,
52                 "w|workings!",\$opt_workings,
53                 "d|dirs!"    ,\$opt_dirs,
54                 "r|rm!"      ,\$opt_rm,
55                 "p|print!"   ,\$opt_print,
56                   "root:s"   ,\$opt_root,
57                 "v|verbose!" ,\$opt_verbose,
58                   "fatal!"   ,\$opt_fatal,
59                   "force!"   ,\$opt_force,
60                 "I|ignore=s" ,\@opt_ignore,
61                 );
62
63 die "--root possible only with (optional and ignored) -d|--dirs"
64                 if defined $opt_root && ($opt_files || $opt_ignores || $opt_workings || $opt_dirs || $opt_rm || $opt_print);
65 die "-d|--dirs forbidden with -r|--rm"
66                 if $opt_dirs && $opt_rm;
67 die "Nothing to do (no -r|--rm, no -p|--print)"
68                 if !$opt_rm && !$opt_print && !defined $opt_root;
69 die "Nothing to process (no -f|--files, no -i|--ignores, no -w|--workings, no -d|--dirs)"
70                 if !defined $opt_root && (!$opt_files && !$opt_ignores && !$opt_workings && !$opt_dirs);
71
72 my @all_ignore=("CVS");
73 for (@opt_ignore) {
74         push @all_ignore,$_;
75         @all_ignore=() if $_ eq "!";
76         }
77
78 my($root_contents,@root_dirs);
79
80 @ARGV=(".") if !@ARGV;
81 our(@dir_dirs)=@ARGV;
82 our(@dir_files,@dir_ignores,@dir_workings,@dir_victims);
83 our($dir_dirname)="";
84 unless ((defined $opt_root && $opt_root ne "") && $opt_force) {
85         local($opt_root)=$opt_root;
86         $opt_root="" if defined $opt_root && $opt_root ne "";
87         localdircore();
88         }
89
90 actionrootfinal() if defined $opt_root && $opt_root eq "";
91 if (defined $opt_root && $opt_root ne "") {
92         die "Non-matching \"".ROOT."\" contents, use --force for override" if !@root_dirs && !$opt_force;
93         localdircore();
94         }
95
96 exit 0;
97
98 sub mayfatal
99 {
100 my($msg,%opts)=@_;
101
102         my $errstr=$!;
103         $msg.=" in \"".fastgetcwd."\" (CVS \"$dir_dirname\")".($opts{"noerrno"} ? "" : ": $errstr");
104         croak $msg if $opt_fatal;
105         carp $msg;
106 }
107
108 sub fordirs
109 {
110 my($func,@dirs)=@_;
111
112         my $origdir=fastgetcwd;
113         for (@dirs) {
114                 if (!chdir $_) {
115                         mayfatal "Unable to process directory \"$_\"";
116                         next;
117                         }
118                 &$func($_);
119                 chdir $origdir;
120                 }
121 }
122
123 sub localdir
124 {
125 my($localdirname)=@_;
126
127         verbose("localdir(\"$localdirname\") entry");
128
129         local(@dir_dirs,@dir_files,@dir_ignores,@dir_workings,@dir_victims);
130         local($dir_dirname)=$dir_dirname.$localdirname."/";
131
132         localreaddir() or return;
133
134         localdircore();
135
136         verbose("localdir(\"$localdirname\") exit");
137 }
138
139 sub localdircore
140 {
141         localvictims() if !defined $opt_root;
142         localaction();
143
144         fordirs \&localdir,@dir_dirs;
145 }
146
147 sub filterout
148 {
149 my($from,@what)=@_;
150
151         my %hash=map { $_=>1; } @$from;
152         for (@what) {
153                 delete $hash{$_};
154                 }
155         return keys %hash;
156 }
157
158 sub localreaddir
159 {
160         local *E;
161         if (!open E,ENTRIES) {
162                 mayfatal "File \"".ENTRIES."\" cannot be opened";
163                 return 0;
164                 }
165         while (<E>) {
166                 chomp;
167                 next if /^D$/;
168                 do { push @dir_dirs ,$1; next; } if m#^D/([^/]*)/#;
169                 next if m#^/[^/]*/-#;   # deleted file: /filename/-1.1/dummy timestamp//
170                 next if m#^/[^/]*/0/#;  # new file: /filename/0/dummy timestamp//
171                 do { push @dir_files,$1; next; } if m#^/([^/]*)/# ;
172                 mayfatal "File ".ENTRIES." contains invalid line \"$_\"",("noerrno"=>1);
173                 }
174         close E;
175
176         return 1 if defined $opt_root;
177
178         local *I;
179         if (open I,CVSIGNORE) {
180                 while (<I>) {
181                         while (/\S+/g) {
182                                 for (<$&>) {
183                                         push @dir_ignores,$_ if -e $_;
184                                         }
185                                 }
186                         }
187                 close I;
188                 @dir_ignores=filterout \@dir_ignores,@dir_dirs,@dir_files;
189                 }
190         else {
191                 mayfatal "File \"".CVSIGNORE."\" cannot be opened" if !$!{ENOENT};
192                 }
193
194         local *D;
195         if (!opendir D,".") {
196                 mayfatal "Cannot read directory \".\"";
197                 return 0;
198                 }
199         @dir_workings=filterout [readdir D],@dir_dirs,@dir_files,@dir_ignores,@all_ignore,".","..";
200         closedir D;
201         return 1;
202 }
203
204 sub localvictims
205 {
206         push @dir_victims,@dir_files    if $opt_files;
207         push @dir_victims,@dir_ignores  if $opt_ignores;
208         push @dir_victims,@dir_workings if $opt_workings;
209         push @dir_victims,@dir_dirs     if $opt_dirs;
210 }
211
212 sub localactionprint
213 {
214 my($filename)=@_;
215
216         mayfatal "File \"$filename\" does not exist",("noerrno"=>1) if !-e $filename;
217         print "${dir_dirname}$filename\n";
218 }
219
220 sub localactionrm
221 {
222 my($filename)=@_;
223
224         chmod 0700,$filename or do {
225                 mayfatal "File \"$_\" cannot be chmod(2)ed" if !$!{ENOENT};
226                 };
227         # '\1' for '-r':
228         remove \1,$filename or do {
229                 mayfatal "File \"$_\" cannot be removed" if !$!{ENOENT};
230                 };
231 }
232
233 sub localactionrootset
234 {
235         local *R;
236         if (!open R,'+<',ROOT) {
237                 mayfatal "File \"".ROOT."\" cannot be written";
238                 return;
239                 }
240         print R "$opt_root\n";
241         truncate R,tell R;
242         close R;
243 }
244
245 sub localactionrootprint
246 {
247         local *R;
248         if (!open R,ROOT) {
249                 mayfatal "File \"".ROOT."\" cannot be opened";
250                 return;
251                 }
252         local $/=undef;
253         localactionrootprintcheck(<R>);
254         close R;
255 }
256
257 sub localactionrootprintcheck
258 {
259 my($contents)=@_;
260
261         if (!defined $root_contents) {
262                 push @root_dirs,$dir_dirname;
263                 $root_contents=$contents;
264                 return;
265                 }
266         if (@root_dirs && $root_contents eq $contents) {
267                 push @root_dirs,$dir_dirname;
268                 return;
269                 }
270         if (@root_dirs) {
271                 print map "$_: $root_contents",@root_dirs;
272                 @root_dirs=();
273                 }
274         print "$dir_dirname: $contents";
275 }
276
277 sub actionrootfinal
278 {
279         return if !@root_dirs;
280         print $root_contents;
281 }
282
283 sub localaction
284 {
285         if ("" ne $dir_dirname && defined $opt_root) {
286                 localactionrootset()   if defined $opt_root && "" ne $opt_root;
287                 localactionrootprint() if defined $opt_root && "" eq $opt_root;
288                 return;
289                 }
290         for (@dir_victims) {
291                 localactionprint $_ if $opt_print;
292                 localactionrm    $_ if $opt_rm;
293                 }
294 }
295
296 sub verbose
297 {
298 my($msg)=@_;
299
300         return if !$opt_verbose;
301         print fastgetcwd.": $msg\n";
302 }
303
304 sub GetOptions_shortfilter
305 {
306         my @r;
307         while ($_=shift) {
308                 if (/^(\w)\|/) {
309                         my $ref=shift;
310                         push @r,$1,$ref,$',$ref;
311                         next;
312                         }
313                 push @r,$_;
314                 }
315         return GetOptions @r;
316 }