Fix deleting empty directories

This commit is contained in:
Isaac Connor 2018-09-09 13:28:24 -04:00
parent 9e8a876101
commit 882e47c9ab
1 changed files with 12 additions and 8 deletions

View File

@ -382,7 +382,7 @@ MAIN: while( $loop ) {
} # if USE_DEEP_STORAGE
Debug( 'Got '.int(keys(%$fs_events))." filesystem events for monitor $monitor_dir\n" );
#delete_empty_directories( $monitor_dir );
delete_empty_directories($monitor_dir);
} # end foreach monitor
if ( $cleaned ) {
@ -879,26 +879,30 @@ sub deleteSwapImage {
sub delete_empty_directories {
my $DIR;
Debug("delete_empty_directories $_[0]");
if ( !opendir($DIR, $_[0]) ) {
Error("delete_empty_directories: Can't open directory '".getcwd()."/$_[0]': $!" );
return;
}
my @contents = map { $_ eq '.' or $_ eq '..' ? () : $_ } readdir( $DIR );
my @contents = map { ( $_ eq '.' or $_ eq '..' ) ? () : $_ } readdir( $DIR );
Debug("delete_empty_directories $_[0] has " . @contents .' entries:' . ( @contents < 2 ? join(',',@contents) : '' ));
my @dirs = map { -d $_[0].'/'.$_ ? $_ : () } @contents;
if ( @dirs ) {
Debug("Have " . @dirs . " dirs");
foreach ( @dirs ) {
delete_empty_directories( $_[0].'/'.$_ );
}
#Reload, since we may now be empty
rewinddir $DIR;
@contents = map { $_ eq '.' or $_ eq '..' ? () : $_ } readdir( $DIR );
}
if ( ! @contents ) {
( my $dir ) = ( $_[0] =~ /^(.*)$/ );
unlink $dir;
@contents = map { ($_ eq '.' or $_ eq '..') ? () : $_ } readdir( $DIR );
}
closedir($DIR);
if ( ! @contents ) {
( my $dir ) = ( $_[0] =~ /^(.*)$/ );
Debug("Unlinking $dir because it's empty");
if ( ! rmdir $dir ) {
Error("Unable to unlink $dir: $!");
}
}
} # end sub delete_empty_directories
1;