Skip to content

Commit

Permalink
fix #1975: create temp File before opening File with bad ext externally
Browse files Browse the repository at this point in the history
  • Loading branch information
lfcnassif committed Nov 11, 2023
1 parent 6a3192f commit 34d1721
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 1 addition & 5 deletions iped-app/src/main/java/iped/app/ui/ExternalFileOpen.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public void run() {
try {
if (IOUtil.isToOpenExternally(item.getName(), item.getType())) {
LOGGER.info("Externally Opening file " + item.getPath()); //$NON-NLS-1$
File file = Util.getFileRenamedToExt(item.getTempFile(), item.getType());
file.setReadOnly();
if (IOUtil.isTemporaryFile(file)) {
file.deleteOnExit();
}
File file = Util.getFileWithRightExt(item);
open(file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

import iped.data.IItem;
import iped.data.IItemReader;
import iped.parsers.standard.RawStringParser;
import iped.parsers.standard.StandardParser;
Expand Down Expand Up @@ -460,5 +461,27 @@ public static File getFileRenamedToExt(File file, String ext) {
return file;
}

public static File getFileWithRightExt(IItem item) throws IOException {
File file = item.getTempFile();
String ext = item.getType();
boolean isTmpFile = IOUtil.isTemporaryFile(file);
boolean badExt = !ext.isEmpty() && !file.getName().endsWith("." + ext);
if (!isTmpFile && badExt) {
File tmp = File.createTempFile("iped", "." + ext);
tmp.deleteOnExit();
IOUtil.copyFile(file, tmp);
return tmp;
} else {
if (isTmpFile) {
file.deleteOnExit();
if (badExt) {
file = Util.getFileRenamedToExt(file, ext);
}
} else {
file.setReadOnly();
}
return file;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ public void open(final String luceneQuery) {
}
File file = null;
try {
file = Util.getFileRenamedToExt(item.getTempFile(), item.getType());
if (IOUtil.isTemporaryFile(file)) {
file.deleteOnExit();
}
file = Util.getFileWithRightExt(item);
} catch (IOException e1) {
e1.printStackTrace();
}
Expand Down

0 comments on commit 34d1721

Please sign in to comment.