Skip to content

Commit

Permalink
rmdir: stat file first (#568)
Browse files Browse the repository at this point in the history
* Avoid calling rmdir() on file arguments unless stat (via -d ARG) indicates the file exists and is a directory
* Technically rmdir() would fail anyway, but this seems more defensive
  • Loading branch information
mknos committed Apr 19, 2024
1 parent 7f078cb commit c7aa8b1
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions bin/rmdir
Expand Up @@ -23,16 +23,21 @@ use constant EX_FAILURE => 1;
my ($VERSION) = '1.2';
my $Program = basename($0);

my $warnings = 0;
my $rc = EX_SUCCESS;
my %opt;
if (!getopts('p', \%opt) || scalar(@ARGV) == 0) {
warn "usage: $Program [-p] directory ...\n";
exit EX_FAILURE;
}
foreach my $directory (@ARGV) {
unless (-d $directory) {
warn "$Program: '$directory': not a directory or does not exist\n";
$rc = EX_FAILURE;
next;
}
unless (rmdir $directory) {
warn "$Program: failed to remove '$directory': $!\n";
$warnings = 1;
$rc = EX_FAILURE;
next;
};
if ($opt{'p'}) {
Expand All @@ -43,13 +48,13 @@ foreach my $directory (@ARGV) {

unless (rmdir $directory) {
warn "$Program: failed to remove '$directory': $!\n";
$warnings = 1;
$rc = EX_FAILURE;
last;
}
}
}
}
exit $warnings;
exit $rc;

__END__
Expand Down

0 comments on commit c7aa8b1

Please sign in to comment.