Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: for #6374 to delete non-empty directories #6375

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stream returned by Files.walk should be closed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... most streams don't need to be closed like this; as documented in the Stream javadoc. Hadn't thought about this possibly being tied to an IO channel. Turns out this isn't uncommon: https://sourcegraph.com/search?q=context:global+content:%22try+%28Stream%3CPath%3E+paths+%3D+Files.walk%28%22+lang:java&patternType=standard&sm=1&groupBy=repo

Fixed with #6382

Thanks!

.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());
}
}
Loading