introduce new function that recursively deletes empty directories. Call it on each monitor dir

pull/1624/head
Isaac Connor 2016-06-03 11:35:28 -04:00
parent 2385575c56
commit 575180ba5e
1 changed files with 23 additions and 1 deletions

View File

@ -247,7 +247,8 @@ MAIN: while( $loop ) {
chdir( EVENT_PATH );
}
Debug( "Got ".int(keys(%$fs_events))." events\n" );
}
delete_empty_directories( $monitor_dir );
} # end foreach monitor directory
redo MAIN if ( $cleaned );
$cleaned = 0;
@ -637,3 +638,24 @@ sub deleteSwapImage
#unlink( $file );
}
}
sub delete_empty_directories {
if ( ! opendir( DIR, $_[0] ) ) {
Error( "Can't open directory '$_[0]': $!" );
return;
}
my @contents = map { $_ eq '.' or $_ eq '..' ? () : $_ } readdir( DIR );
my @dirs = map { -d $_ ? $_ : () } @contents;
if ( @dirs ) {
foreach ( @dirs ) {
delete_empty_directories( $_[0].'/'.$_ );
}
#Reload, since we may now be empty
@contents = map { $_ eq '.' or $_ eq '..' ? () : $_ } readdir( DIR );
}
if ( ! @contents ) {
unlink $_[0];
}
closedir( DIR );
} # end sub delete_empty_directories