Fix extraction action for VirtDir and simplify. Remove no longer needed visitor.

This commit is contained in:
adam-m 2013-05-09 17:13:48 -04:00
parent 556c5cde26
commit 35828bae1b

View File

@ -38,6 +38,7 @@ import org.sleuthkit.datamodel.LayoutFile;
import org.sleuthkit.datamodel.LocalFile; import org.sleuthkit.datamodel.LocalFile;
import org.sleuthkit.datamodel.ReadContentInputStream; import org.sleuthkit.datamodel.ReadContentInputStream;
import org.sleuthkit.datamodel.TskException; import org.sleuthkit.datamodel.TskException;
import org.sleuthkit.datamodel.VirtualDirectory;
/** /**
* Static class of utility methods for Content objects * Static class of utility methods for Content objects
@ -107,8 +108,7 @@ public final class ContentUtils {
final Image image = c.getImage(); final Image image = c.getImage();
if (image != null) { if (image != null) {
return TimeZone.getTimeZone(image.getTimeZone()); return TimeZone.getTimeZone(image.getTimeZone());
} } else {
else {
//case such as top level VirtualDirectory //case such as top level VirtualDirectory
return TimeZone.getDefault(); return TimeZone.getDefault();
} }
@ -135,7 +135,8 @@ public final class ContentUtils {
private static final int TO_FILE_BUFFER_SIZE = 8192; private static final int TO_FILE_BUFFER_SIZE = 8192;
/** /**
* Reads all the data from any content object and writes (extracts) it to a file. * Reads all the data from any content object and writes (extracts) it to a
* file.
* *
* @param content Any content object. * @param content Any content object.
* @param outputFile Will be created if it doesn't exist, and overwritten if * @param outputFile Will be created if it doesn't exist, and overwritten if
@ -194,7 +195,7 @@ public final class ContentUtils {
/** /**
* Helper to ignore the '.' and '..' directories * Helper to ignore the '.' and '..' directories
*/ */
public static boolean isDotDirectory(Directory dir) { public static boolean isDotDirectory(AbstractFile dir) {
String name = dir.getName(); String name = dir.getName();
return name.equals(".") || name.equals(".."); return name.equals(".") || name.equals("..");
} }
@ -292,6 +293,21 @@ public final class ContentUtils {
@Override @Override
public Void visit(Directory dir) { public Void visit(Directory dir) {
return visitDir(dir);
}
@Override
public Void visit(VirtualDirectory dir) {
return visitDir(dir);
}
private java.io.File getFsContentDest(Content fsc) {
String path = dest.getAbsolutePath() + java.io.File.separator
+ fsc.getName();
return new java.io.File(path);
}
public Void visitDir(AbstractFile dir) {
// don't extract . and .. directories // don't extract . and .. directories
if (isDotDirectory(dir)) { if (isDotDirectory(dir)) {
@ -300,14 +316,13 @@ public final class ContentUtils {
dest.mkdir(); dest.mkdir();
// member visitor to generate destination files for children
DestFileContentVisitor destFileCV = new DestFileContentVisitor();
try { try {
int numProcessed = 0; int numProcessed = 0;
// recurse on children // recurse on children
for (Content child : dir.getChildren()) { for (Content child : dir.getChildren()) {
java.io.File childFile = child.accept(destFileCV); java.io.File childFile = getFsContentDest(child);
ExtractFscContentVisitor childVisitor = ExtractFscContentVisitor childVisitor =
new ExtractFscContentVisitor(childFile, progress, worker, false); new ExtractFscContentVisitor(childFile, progress, worker, false);
// If this is the source directory of an extract it // If this is the source directory of an extract it
@ -335,52 +350,5 @@ public final class ContentUtils {
throw new UnsupportedOperationException("Can't extract a " throw new UnsupportedOperationException("Can't extract a "
+ cntnt.getClass().getSimpleName()); + cntnt.getClass().getSimpleName());
} }
/**
* Helper visitor to get the destination file for a child Content object
*/
private class DestFileContentVisitor extends ContentVisitor.Default<java.io.File> {
/**
* Get destination file by adding File/Directory name to the path of
* parent
*/
private java.io.File getFsContentDest(AbstractFile fsc) {
String path = dest.getAbsolutePath() + java.io.File.separator
+ fsc.getName();
return new java.io.File(path);
}
@Override
public java.io.File visit(File f) {
return getFsContentDest(f);
}
@Override
public java.io.File visit(LayoutFile lf) {
return getFsContentDest(lf);
}
@Override
public java.io.File visit(DerivedFile df) {
return getFsContentDest(df);
}
@Override
public java.io.File visit(LocalFile lf) {
return getFsContentDest(lf);
}
@Override
public java.io.File visit(Directory dir) {
return getFsContentDest(dir);
}
@Override
protected java.io.File defaultVisit(Content cntnt) {
throw new UnsupportedOperationException("Can't get destination file for a "
+ cntnt.getClass().getSimpleName());
}
}
} }
} }