Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6152 allow cancelling via taskmonitor in streamcopy

dev747368b19c04f72930
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSUtilities.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSUtilities.java
index ba20f54a75..24374fac99 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSUtilities.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSUtilities.java
@@ -38,6 +38,7 @@ import ghidra.formats.gfilesystem.fileinfo.FileType;
import ghidra.util.*;
import ghidra.util.exception.CancelledException;
import ghidra.util.exception.CryptoException;
+import ghidra.util.task.CancelledListener;
import ghidra.util.task.TaskMonitor;
import util.CollectionUtils;
import utilities.util.FileUtilities;
@@ -373,14 +374,21 @@ public class FSUtilities {
byte buffer[] = new byte[FileUtilities.IO_BUFFER_SIZE];
int bytesRead;
long totalBytesCopied = 0;
- while ((bytesRead = is.read(buffer)) > 0) {
- os.write(buffer, 0, bytesRead);
- totalBytesCopied += bytesRead;
- monitor.setProgress(totalBytesCopied);
- monitor.checkCancelled();
- }
- os.flush();
- return totalBytesCopied;
+ CancelledListener l = () -> FSUtilities.uncheckedClose(is, null);
+ monitor.addCancelledListener(l);
+ try {
+ while ((bytesRead = is.read(buffer)) > 0) {
+ os.write(buffer, 0, bytesRead);
+ totalBytesCopied += bytesRead;
+ monitor.setProgress(totalBytesCopied);
+ monitor.checkCancelled();
+ }
+ os.flush();
+ return totalBytesCopied;
+ }
+ finally {
+ monitor.removeCancelledListener(l);
+ }
}
/**