Began update towards object serialization

This commit is contained in:
Oliver Spohngellert 2016-02-29 15:32:11 -05:00
parent 1fe62f18f6
commit b277ce16c3
2 changed files with 226 additions and 149 deletions

View File

@ -38,14 +38,20 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport; import java.beans.PropertyChangeSupport;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.logging.Level; import java.util.logging.Level;
import javax.persistence.PersistenceException;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.netbeans.api.progress.ProgressHandle; import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory; import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.util.io.NbObjectInputStream;
import org.openide.util.io.NbObjectOutputStream;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.Content; import org.sleuthkit.datamodel.Content;
@ -72,11 +78,13 @@ public class HashDbManager implements PropertyChangeListener {
private static final String PATH_ELEMENT = "hash_set_path"; //NON-NLS private static final String PATH_ELEMENT = "hash_set_path"; //NON-NLS
private static final String LEGACY_PATH_NUMBER_ATTRIBUTE = "number"; //NON-NLS private static final String LEGACY_PATH_NUMBER_ATTRIBUTE = "number"; //NON-NLS
private static final String CONFIG_FILE_NAME = "hashsets.xml"; //NON-NLS private static final String CONFIG_FILE_NAME = "hashsets.xml"; //NON-NLS
private static final String DB_SERIALIZATION_FILE_NAME = "hashDbs.settings";
private static final String XSD_FILE_NAME = "HashsetsSchema.xsd"; //NON-NLS private static final String XSD_FILE_NAME = "HashsetsSchema.xsd"; //NON-NLS
private static final String ENCODING = "UTF-8"; //NON-NLS private static final String ENCODING = "UTF-8"; //NON-NLS
private static final String HASH_DATABASE_FILE_EXTENSON = "kdb"; //NON-NLS private static final String HASH_DATABASE_FILE_EXTENSON = "kdb"; //NON-NLS
private static HashDbManager instance = null; private static HashDbManager instance = null;
private final String configFilePath = PlatformUtil.getUserConfigDirectory() + File.separator + CONFIG_FILE_NAME; private final String configFilePath = PlatformUtil.getUserConfigDirectory() + File.separator + CONFIG_FILE_NAME;
private final String DB_SERIALIZATION_FILE_PATH = PlatformUtil.getUserConfigDirectory() + File.separator + DB_SERIALIZATION_FILE_NAME;
private List<HashDb> knownHashSets = new ArrayList<>(); private List<HashDb> knownHashSets = new ArrayList<>();
private List<HashDb> knownBadHashSets = new ArrayList<>(); private List<HashDb> knownBadHashSets = new ArrayList<>();
private Set<String> hashSetNames = new HashSet<>(); private Set<String> hashSetNames = new HashSet<>();
@ -109,10 +117,8 @@ public class HashDbManager implements PropertyChangeListener {
} }
private HashDbManager() { private HashDbManager() {
if (hashSetsConfigurationFileExists()) {
readHashSetsConfigurationFromDisk(); readHashSetsConfigurationFromDisk();
} }
}
/** /**
* Gets the extension, without the dot separator, that the SleuthKit * Gets the extension, without the dot separator, that the SleuthKit
@ -490,10 +496,8 @@ public class HashDbManager implements PropertyChangeListener {
hashSetNames.clear(); hashSetNames.clear();
hashSetPaths.clear(); hashSetPaths.clear();
if (hashSetsConfigurationFileExists()) {
readHashSetsConfigurationFromDisk(); readHashSetsConfigurationFromDisk();
} }
}
private void closeHashDatabases(List<HashDb> hashDatabases) { private void closeHashDatabases(List<HashDb> hashDatabases) {
for (HashDb database : hashDatabases) { for (HashDb database : hashDatabases) {
@ -507,22 +511,17 @@ public class HashDbManager implements PropertyChangeListener {
} }
private boolean writeHashSetConfigurationToDisk() { private boolean writeHashSetConfigurationToDisk() {
boolean success = false; HashDbSerializationSettings settings = new HashDbSerializationSettings(this.knownHashSets, this.knownBadHashSets);
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DB_SERIALIZATION_FILE_PATH))) {
try { out.writeObject(settings);
DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); File xmlFile = new File(configFilePath);
Document doc = docBuilder.newDocument(); if (xmlFile.exists()) {
Element rootEl = doc.createElement(ROOT_ELEMENT); xmlFile.delete();
doc.appendChild(rootEl); }
return true;
writeHashDbsToDisk(doc, rootEl, knownHashSets); } catch (IOException ex) {
writeHashDbsToDisk(doc, rootEl, knownBadHashSets); throw new PersistenceException(String.format("Failed to write settings to %s", DB_SERIALIZATION_FILE_PATH), ex);
success = XMLUtil.saveDoc(HashDbManager.class, configFilePath, ENCODING, doc);
} catch (ParserConfigurationException ex) {
Logger.getLogger(HashDbManager.class.getName()).log(Level.SEVERE, "Error saving hash databases", ex); //NON-NLS
} }
return success;
} }
private static void writeHashDbsToDisk(Document doc, Element rootEl, List<HashDb> hashDbs) { private static void writeHashDbsToDisk(Document doc, Element rootEl, List<HashDb> hashDbs) {
@ -559,6 +558,7 @@ public class HashDbManager implements PropertyChangeListener {
} }
private boolean readHashSetsConfigurationFromDisk() { private boolean readHashSetsConfigurationFromDisk() {
if (hashSetsConfigurationFileExists()) {
boolean updatedSchema = false; boolean updatedSchema = false;
// Open the XML document that implements the configuration file. // Open the XML document that implements the configuration file.
@ -698,6 +698,42 @@ public class HashDbManager implements PropertyChangeListener {
} }
return true; return true;
} else {
File fileSetFile = new File(DB_SERIALIZATION_FILE_PATH);
if (fileSetFile.exists()) {
try {
try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(DB_SERIALIZATION_FILE_PATH))) {
HashDbSerializationSettings filesSetsSettings = (HashDbSerializationSettings) in.readObject();
this.setFields(filesSetsSettings);
return true;
}
} catch (IOException | ClassNotFoundException ex) {
throw new PersistenceException(String.format("Failed to read settings from %s", DB_SERIALIZATION_FILE_PATH), ex);
}
} else {
this.setFields(new HashDbSerializationSettings(new ArrayList<>(), new ArrayList<>()));
return true;
}
}
}
private void setFields(HashDbSerializationSettings settings) throws TskCoreException {
this.knownHashSets = settings.getKnownHashSets();
this.knownBadHashSets = settings.getKnownBadHashSets();
this.hashSetNames = new HashSet<>();
this.hashSetPaths = new HashSet<>();
for (HashDbManager.HashDb hashDb : knownHashSets) {
String hashSetName = hashDb.getHashSetName();
if (hashSetNames.contains(hashSetName)) {
int suffix = 0;
String newHashSetName;
do {
++suffix;
newHashSetName = hashSetName + suffix;
} while (hashSetNames.contains(newHashSetName));
}
this.hashSetPaths.add(hashDb.getDatabasePath());
}
} }
private String getValidFilePath(String hashSetName, String configuredPath) { private String getValidFilePath(String hashSetName, String configuredPath) {
@ -750,7 +786,7 @@ public class HashDbManager implements PropertyChangeListener {
* Instances of this class represent hash databases used to classify files * Instances of this class represent hash databases used to classify files
* as known or know bad. * as known or know bad.
*/ */
public static class HashDb { public static class HashDb implements Serializable {
/** /**
* Indicates how files with hashes stored in a particular hash database * Indicates how files with hashes stored in a particular hash database
@ -778,6 +814,7 @@ public class HashDbManager implements PropertyChangeListener {
INDEXING_DONE INDEXING_DONE
} }
private static final long serialVersionUID = 1L;
private int handle; private int handle;
private String hashSetName; private String hashSetName;
private boolean searchDuringIngest; private boolean searchDuringIngest;

View File

@ -0,0 +1,40 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.sleuthkit.autopsy.modules.hashdatabase;
import java.io.Serializable;
import java.util.List;
/**
* Class to represent the settings to be serialized for hash databases
*/
class HashDbSerializationSettings implements Serializable {
private static final long serialVersionUID = 1L;
private final List<HashDbManager.HashDb> knownHashSets;
private final List<HashDbManager.HashDb> knownBadHashSets;
/**
* Constructs a settings object to be serialized for hash dbs
* @param knownHashSets
* @param knownBadHashSets
*/
HashDbSerializationSettings(List<HashDbManager.HashDb> knownHashSets, List<HashDbManager.HashDb> knownBadHashSets) {
this.knownHashSets = knownHashSets;
this.knownBadHashSets = knownBadHashSets;
}
List<HashDbManager.HashDb> getKnownHashSets() {
return this.knownHashSets;
}
/**
* @return the knownBadHashSets
*/
public List<HashDbManager.HashDb> getKnownBadHashSets() {
return knownBadHashSets;
}
}