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 java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import javax.persistence.PersistenceException;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.FileUtils;
import org.netbeans.api.progress.ProgressHandle;
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.datamodel.AbstractFile;
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 LEGACY_PATH_NUMBER_ATTRIBUTE = "number"; //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 ENCODING = "UTF-8"; //NON-NLS
private static final String HASH_DATABASE_FILE_EXTENSON = "kdb"; //NON-NLS
private static HashDbManager instance = null;
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> knownBadHashSets = new ArrayList<>();
private Set<String> hashSetNames = new HashSet<>();
@ -109,10 +117,8 @@ public class HashDbManager implements PropertyChangeListener {
}
private HashDbManager() {
if (hashSetsConfigurationFileExists()) {
readHashSetsConfigurationFromDisk();
}
}
/**
* Gets the extension, without the dot separator, that the SleuthKit
@ -490,10 +496,8 @@ public class HashDbManager implements PropertyChangeListener {
hashSetNames.clear();
hashSetPaths.clear();
if (hashSetsConfigurationFileExists()) {
readHashSetsConfigurationFromDisk();
}
}
private void closeHashDatabases(List<HashDb> hashDatabases) {
for (HashDb database : hashDatabases) {
@ -507,22 +511,17 @@ public class HashDbManager implements PropertyChangeListener {
}
private boolean writeHashSetConfigurationToDisk() {
boolean success = false;
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootEl = doc.createElement(ROOT_ELEMENT);
doc.appendChild(rootEl);
writeHashDbsToDisk(doc, rootEl, knownHashSets);
writeHashDbsToDisk(doc, rootEl, knownBadHashSets);
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
HashDbSerializationSettings settings = new HashDbSerializationSettings(this.knownHashSets, this.knownBadHashSets);
try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DB_SERIALIZATION_FILE_PATH))) {
out.writeObject(settings);
File xmlFile = new File(configFilePath);
if (xmlFile.exists()) {
xmlFile.delete();
}
return true;
} catch (IOException ex) {
throw new PersistenceException(String.format("Failed to write settings to %s", DB_SERIALIZATION_FILE_PATH), ex);
}
return success;
}
private static void writeHashDbsToDisk(Document doc, Element rootEl, List<HashDb> hashDbs) {
@ -559,6 +558,7 @@ public class HashDbManager implements PropertyChangeListener {
}
private boolean readHashSetsConfigurationFromDisk() {
if (hashSetsConfigurationFileExists()) {
boolean updatedSchema = false;
// Open the XML document that implements the configuration file.
@ -698,6 +698,42 @@ public class HashDbManager implements PropertyChangeListener {
}
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) {
@ -750,7 +786,7 @@ public class HashDbManager implements PropertyChangeListener {
* Instances of this class represent hash databases used to classify files
* 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
@ -778,6 +814,7 @@ public class HashDbManager implements PropertyChangeListener {
INDEXING_DONE
}
private static final long serialVersionUID = 1L;
private int handle;
private String hashSetName;
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;
}
}