30 package org.sleuthkit.autopsy.examples;
32 import java.util.HashMap;
33 import java.util.logging.Level;
56 class SampleFileIngestModule
implements FileIngestModule {
58 private static final HashMap<Long, Long> artifactCountsForIngestJobs =
new HashMap<>();
59 private static int attrId = -1;
60 private final boolean skipKnownFiles;
61 private IngestJobContext context = null;
62 private static final IngestModuleReferenceCounter refCounter =
new IngestModuleReferenceCounter();
64 SampleFileIngestModule(SampleModuleIngestJobSettings settings) {
65 this.skipKnownFiles = settings.skipKnownFiles();
69 public void startUp(IngestJobContext context)
throws IngestModuleException {
70 this.context = context;
71 refCounter.incrementAndGet(context.getJobId());
73 synchronized (SampleFileIngestModule.class) {
80 Case autopsyCase = Case.getCurrentCase();
81 SleuthkitCase sleuthkitCase = autopsyCase.getSleuthkitCase();
84 attrId = sleuthkitCase.getAttrTypeID(
"ATTR_SAMPLE");
86 attrId = sleuthkitCase.addAttrType(
"ATTR_SAMPLE",
"Sample Attribute");
88 }
catch (TskCoreException ex) {
89 IngestServices ingestServices = IngestServices.getInstance();
90 Logger logger = ingestServices.getLogger(SampleIngestModuleFactory.getModuleName());
91 logger.log(Level.SEVERE,
"Failed to create blackboard attribute", ex);
93 throw new IngestModuleException(ex.getLocalizedMessage());
100 public IngestModule.ProcessResult process(AbstractFile file) {
102 return IngestModule.ProcessResult.ERROR;
106 if ((file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
107 || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS)
108 || (file.isFile() ==
false)) {
109 return IngestModule.ProcessResult.OK;
113 if (skipKnownFiles && file.getKnown() == TskData.FileKnown.KNOWN) {
114 return IngestModule.ProcessResult.OK;
121 byte buffer[] =
new byte[1024];
122 int len = file.read(buffer, 0, 1024);
124 for (
int i = 0; i < len; i++) {
125 if (buffer[i] == 0x00) {
132 BlackboardAttribute attr =
new BlackboardAttribute(attrId, SampleIngestModuleFactory.getModuleName(), count);
137 BlackboardArtifact art = file.getGenInfoArtifact();
138 art.addAttribute(attr);
142 addToBlackboardPostCount(context.getJobId(), 1L);
145 ModuleDataEvent
event =
new ModuleDataEvent(SampleIngestModuleFactory.getModuleName(), ARTIFACT_TYPE.TSK_GEN_INFO);
146 IngestServices.getInstance().fireModuleDataEvent(event);
148 return IngestModule.ProcessResult.OK;
150 }
catch (TskCoreException ex) {
151 IngestServices ingestServices = IngestServices.getInstance();
152 Logger logger = ingestServices.getLogger(SampleIngestModuleFactory.getModuleName());
153 logger.log(Level.SEVERE,
"Error processing file (id = " + file.getId() +
")", ex);
154 return IngestModule.ProcessResult.ERROR;
159 public void shutDown() {
162 reportBlackboardPostCount(context.getJobId());
165 synchronized static void addToBlackboardPostCount(
long ingestJobId,
long countToAdd) {
166 Long fileCount = artifactCountsForIngestJobs.get(ingestJobId);
169 if (fileCount == null) {
171 artifactCountsForIngestJobs.put(ingestJobId, fileCount);
174 fileCount += countToAdd;
175 artifactCountsForIngestJobs.put(ingestJobId, fileCount);
178 synchronized static void reportBlackboardPostCount(
long ingestJobId) {
179 Long refCount = refCounter.decrementAndGet(ingestJobId);
181 Long filesCount = artifactCountsForIngestJobs.remove(ingestJobId);
182 String msgText = String.format(
"Posted %d times to the blackboard", filesCount);
183 IngestMessage message = IngestMessage.createMessage(
184 IngestMessage.MessageType.INFO,
185 SampleIngestModuleFactory.getModuleName(),
187 IngestServices.getInstance().postMessage(message);