4 # Copyright (C) 2001 Heath Kehoe <hakehoe@avalon.net>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 Usage: $0 [-r] [-v] directory [directory ...]
28 Scans each directory and:
29 1) removes orphaned .AppleDouble files (from <directory>/.AppleDouble)
30 2) fixes permissions on .AppleDouble files to match corresponding data file (minus x bits)
31 3) fixes owner/group of .AppleDouble files to match corresponding data file (root only)
34 -r Recursively check all subdirectories of each directory
35 -R Like -r but follows symbolic links to directories (warning: no loop-checking is done)
36 -p Preview: no deletions or changes are actually made
46 return (($mode & 0170000) == 0040000);
50 return (($mode & 0170000) == 0100000);
54 return (($mode & 0170000) == 0120000);
60 my($f, $havead, @dirlist, $mode, $uid, $gid, $fn);
63 print STDERR "Scanning $dir ...\n" if($opt_v);
65 $havead = -d "$dir/.AppleDouble";
67 # there's nothing more to do if we're not recursive and there's no .AppleDouble
68 return if(!$havead && !$opt_r);
70 opendir DIR, $dir or do {
71 warn "Can't opendir $dir: $!\n";
74 while(defined($f = readdir DIR)) {
77 next if($f eq ".AppleDouble");
78 next if($f eq ".AppleDesktop");
80 (undef, undef, $mode, undef, $uid, $gid) = lstat "$dir/$f";
81 next if(!defined($mode));
84 (undef, undef, $mode, undef, $uid, $gid) = stat "$dir/$f";
85 next if(!defined($mode));
86 next if(S_ISDIR($mode) && !$opt_R);
89 push @dirlist, $f if($opt_r);
90 } elsif(S_ISREG($mode)) {
92 $dm{$f} = $mode & 0666;
97 warn "Ignoring special file: $dir/$f\n";
103 if(opendir DIR, "$dir/.AppleDouble") {
104 while(defined($f = readdir DIR)) {
107 next if($f eq ".Parent");
109 $fn = "$dir/.AppleDouble/$f";
110 (undef, undef, $mode, undef, $uid, $gid) = stat $fn;
111 next if(!defined($mode));
114 warn "Found subdirectory $f in $dir/.AppleDouble\n";
117 unless(exists $dm{$f}) {
118 print STDERR "Deleting $fn ...\n" if($opt_v);
122 unlink "$fn" or warn "Can't unlink $fn: $!\n";
126 $mode = $mode & 07777;
127 if($mode != $dm{$f}) {
128 printf STDERR "Changing permissions from %o to %o on $fn\n", $mode, $dm{$f} if($opt_v);
130 printf "chmod %o '$fn'\n", $dm{$f};
132 chmod $dm{$f}, $fn or warn "Can't chmod $fn: $!\n";
135 if($isroot && ($uid != $du{$f} || $gid != $dg{$f})) {
136 print STDERR "Changing owner from $uid:$gid to $du{$f}:$dg{$f} on $fn\n" if($opt_v);
138 print "chown $du{$f}:$dg{$f} '$fn'\n";
140 chown $du{$f}, $dg{$f}, $fn or warn "Can't chown $fn: $!\n";
146 warn "Can't opendir $dir/.AppleDouble: $!\n";
151 foreach $f ( @dirlist ) {
157 usage unless(getopts 'prRv');
158 usage if(@ARGV == 0);
164 foreach $d ( @ARGV ) {