30 package org.sleuthkit.autopsy.examples;
32 import java.util.HashMap;
33 import java.util.logging.Level;
53 class SampleFileIngestModule
implements FileIngestModule {
55 private static final HashMap<Long, Long> artifactCountsForIngestJobs =
new HashMap<>();
56 private static final BlackboardAttribute.ATTRIBUTE_TYPE ATTR_TYPE = BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COUNT;
57 private final boolean skipKnownFiles;
58 private IngestJobContext context = null;
59 private static final IngestModuleReferenceCounter refCounter =
new IngestModuleReferenceCounter();
61 SampleFileIngestModule(SampleModuleIngestJobSettings settings) {
62 this.skipKnownFiles = settings.skipKnownFiles();
66 public void startUp(IngestJobContext context)
throws IngestModuleException {
67 this.context = context;
68 refCounter.incrementAndGet(context.getJobId());
72 public IngestModule.ProcessResult process(AbstractFile file) {
75 if ((file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
76 || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS)
77 || (file.isFile() ==
false)) {
78 return IngestModule.ProcessResult.OK;
82 if (skipKnownFiles && file.getKnown() == TskData.FileKnown.KNOWN) {
83 return IngestModule.ProcessResult.OK;
90 byte buffer[] =
new byte[1024];
91 int len = file.read(buffer, 0, 1024);
93 for (
int i = 0; i < len; i++) {
94 if (buffer[i] == 0x00) {
101 BlackboardAttribute attr =
new BlackboardAttribute(ATTR_TYPE, SampleIngestModuleFactory.getModuleName(), count);
106 BlackboardArtifact art = file.getGenInfoArtifact();
107 art.addAttribute(attr);
111 addToBlackboardPostCount(context.getJobId(), 1L);
120 file.getSleuthkitCase().getBlackboard().postArtifact(art, SampleIngestModuleFactory.getModuleName(), context.getJobId());
122 return IngestModule.ProcessResult.OK;
124 }
catch (TskCoreException | Blackboard.BlackboardException ex) {
125 IngestServices ingestServices = IngestServices.getInstance();
126 Logger logger = ingestServices.getLogger(SampleIngestModuleFactory.getModuleName());
127 logger.log(Level.SEVERE,
"Error processing file (id = " + file.getId() +
")", ex);
128 return IngestModule.ProcessResult.ERROR;
133 public void shutDown() {
136 reportBlackboardPostCount(context.getJobId());
139 synchronized static void addToBlackboardPostCount(
long ingestJobId,
long countToAdd) {
140 Long fileCount = artifactCountsForIngestJobs.get(ingestJobId);
143 if (fileCount == null) {
145 artifactCountsForIngestJobs.put(ingestJobId, fileCount);
148 fileCount += countToAdd;
149 artifactCountsForIngestJobs.put(ingestJobId, fileCount);
152 synchronized static void reportBlackboardPostCount(
long ingestJobId) {
153 Long refCount = refCounter.decrementAndGet(ingestJobId);
155 Long filesCount = artifactCountsForIngestJobs.remove(ingestJobId);
156 String msgText = String.format(
"Posted %d times to the blackboard", filesCount);
157 IngestMessage message = IngestMessage.createMessage(
158 IngestMessage.MessageType.INFO,
159 SampleIngestModuleFactory.getModuleName(),
161 IngestServices.getInstance().postMessage(message);