diff --git a/cleandir/cleandir b/cleandir/cleandir index 77bc6d5..8bf4b7f 100755 --- a/cleandir/cleandir +++ b/cleandir/cleandir @@ -28,18 +28,17 @@ sub cleandir { if ($verbose > 2) { print STDERR "$0:", " " x $level, " checking $dir/$i\n"; } - my $st = lstat("$dir/$i"); + my $st = lstat("$i"); if ($verbose > 3) { print STDERR "$0:", " " x $level, " mtime=", $st->mtime, " atime=", $st->atime, "\n"; } if (-d _) { my $cwd = getcwd(); - if (!chdir($i)) { + if (chdir($i)) { + my $remaining = -1; my $st1 = lstat("."); if ($st->dev == $st1->dev && $st->ino == $st1->ino) { - if (cleandir("$dir/$i", $since, $level+1) == 0 && $st->mtime < $since) { - if (rmdir("$dir/$i")) {next} - } + $remaining = cleandir("$dir/$i", $since, $level+1); } else { print STDERR "$0:", " " x $level, " $dir/$i changed dev/inode from ", @@ -58,6 +57,13 @@ sub cleandir { $std1->dev, "/", $std1->ino, "\n"; } + if ($remaining == 0 && $st->mtime < $since) { + if ($verbose > 0) { + print STDERR "$0:", " " x $level, "rmdir $i\n"; + } + if (rmdir("$i")) {next} + print STDERR "$0:", " " x $level, "rmdir $i failed: $!\n"; + } } else { print STDERR "$0:", " " x $level, " chdir $dir/$i failed: $!\n"; } @@ -69,7 +75,7 @@ sub cleandir { if ($verbose > 0) { print STDERR "$0:", " " x $level, " removing $dir/$i\n"; } - if (unlink("$dir/$i")) {next} + if (unlink("$i")) {next} print STDERR "$0:", " " x $level, " removing $dir/$i failed: $!\n"; } @@ -105,3 +111,21 @@ sub main { } main(); + +# $Log: cleandir,v $ +# Revision 1.4 2000-11-20 21:10:08 hjp +# Checks introduced in last version prevented deletion of unused subdirs. +# Fixed. +# +# revision 1.3 +# date: 2000/09/10 16:16:41; author: hjp; state: Exp; lines: +37 -6 +# Added checks to detect directory/symlink switching attacks. +# ---------------------------- +# revision 1.2 +# date: 1999/08/21 12:37:53; author: hjp; state: Exp; lines: +25 -13 +# More levels of verbosity. +# ---------------------------- +# revision 1.1 +# date: 1999/07/09 21:05:26; author: hjp; state: Exp; +# Added cleandir +#