Skip to content

Commit

Permalink
fix: for #6374 to delete non-empty directories (#6375)
Browse files Browse the repository at this point in the history
  • Loading branch information
sellersj authored Jan 9, 2024
1 parent 1450604 commit ce29982
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.UUID;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -91,7 +91,10 @@ public static boolean delete(@Nullable File file) {
}

try {
Files.delete(file.toPath());
Files.walk(file.toPath())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException ex) {
LOGGER.trace(ex.getMessage(), ex);
LOGGER.debug("Failed to delete file: {} (error message: {}); attempting to delete on exit.", file.getPath(), ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,21 @@ public void testDelete() throws Exception {
assertTrue("delete returned a failed status", status);
assertFalse("Temporary file exists after attempting deletion", file.exists());
}

/**
* Test of delete method with a non-empty directory, of class FileUtils.
*/
@Test
public void testDeleteWithSubDirectories() throws Exception {

File dir = new File(getSettings().getTempDirectory(), "delete-me");
dir.mkdirs();
File file = File.createTempFile("tmp", "deleteme", dir);
assertTrue("Unable to create a temporary file " + file.getAbsolutePath(), file.exists());

// delete the file
boolean status = FileUtils.delete(dir);
assertTrue("delete returned a failed status", status);
assertFalse("Temporary file exists after attempting deletion", file.exists());
}
}

0 comments on commit ce29982

Please sign in to comment.