Delete linkpath before deleting empty directories. add closedir and some more debugging. Also add popping off that last part of the dir at each loop

This commit is contained in:
Isaac Connor 2019-10-15 20:44:15 -04:00
parent 04c22988ad
commit b63fc0b0e5
1 changed files with 28 additions and 22 deletions

View File

@ -459,28 +459,6 @@ sub delete_files {
Error('No event path in delete files. ' . $event->to_string());
} # end if event_path
# Now check for empty directories and delete them.
my @path_parts = split('/', $event_path);
pop @path_parts;
# Guaranteed the first part is the monitor id
while ( @path_parts > 1 ) {
my $path = join('/', $storage_path, @path_parts);
my $dh;
if ( !opendir($dh, $path) ) {
Warning("Fail to open $path");
last;
}
if ( scalar(grep { $_ ne '.' and $_ ne '..' } readdir($dh)) == 0 ) {
Debug("Removing empty dir at $path");
if ( !rmdir $path ) {
Warning("Fail to rmdir $path: $!");
last;
}
} else {
last;
}
} # end while path_parts
if ( $event->Scheme() eq 'Deep' ) {
my $link_path = $event->LinkPath();
Debug("Deleting link for Event $$event{Id} from $storage_path/$link_path.");
@ -489,6 +467,34 @@ sub delete_files {
unlink($storage_path.'/'.$link_path) or Error("Unable to unlink '$storage_path/$link_path': $!");
}
} # end if Scheme eq Deep
# Now check for empty directories and delete them.
my @path_parts = split('/', $event_path);
pop @path_parts;
# Guaranteed the first part is the monitor id
Debug("Initial path_parts: @path_parts");
while ( @path_parts > 1 ) {
my $path = join('/', $storage_path, @path_parts);
my $dh;
if ( !opendir($dh, $path) ) {
Warning("Fail to open $path");
last;
}
my @dir = readdir($dh);
closedir($dh);
if ( scalar(grep { $_ ne '.' and $_ ne '..' } @dir) == 0 ) {
Debug("Removing empty dir at $path");
if ( !rmdir $path ) {
Warning("Fail to rmdir $path: $!");
last;
}
} else {
Debug("Dir $path is not empty @dir");
last;
}
pop @path_parts;
} # end while path_parts
} # end foreach Storage
} # end sub delete_files