Skip to content
Open
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 @@ -56,6 +56,8 @@
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.NoSuchFileException;
import java.nio.file.FileSystemException;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -928,20 +930,54 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
deleteWithRetry(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.TERMINATE;
// Log the failure, decide if you want to terminate or continue
System.err.println("Failed to visit file: " + file + ", error: " + exc.getMessage());
return FileVisitResult.TERMINATE; // Or CONTINUE, depending on
// desired behavior
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
deleteWithRetry(dir);
return FileVisitResult.CONTINUE;
}

private void deleteWithRetry(Path targetPath) throws IOException {
final int MAX_RETRIES = 5;
final long RETRY_DELAY_MS = 100; // 100 milliseconds

for (int i = 0; i < MAX_RETRIES; i++) {
try {
Files.delete(targetPath);
return; // Success, exit retry loop

} catch (NoSuchFileException e) {
return;
} catch (FileSystemException e) {
if (e.getMessage().contains("Device or resource busy")
|| e.getMessage().contains("Directory not empty")) {
System.err.println("Attempt " + (i + 1) + " to delete " + targetPath + " failed: "
+ e.getMessage() + ". Retrying...");
try {
Thread.sleep(RETRY_DELAY_MS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new IOException("Deletion interrupted for " + targetPath, ie);
}
} else {
throw e; // Re-throw other FileSystemExceptions
// immediately
}
}
}
throw new IOException("Failed to delete " + targetPath + " after " + MAX_RETRIES + " retries.");
}
});
}

Expand Down