From 575180ba5e1aa3013ea7232fcbdf43f7d4faeaf5 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 3 Jun 2016 11:35:28 -0400 Subject: [PATCH] introduce new function that recursively deletes empty directories. Call it on each monitor dir --- scripts/zmaudit.pl.in | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index 7f2df08b9..656612220 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -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 +