19 package org.sleuthkit.autopsy.ingest;
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.logging.Level;
26 import org.openide.util.NbBundle;
32 import org.
sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
40 "FileIngestPipeline_SaveResults_Activity=Saving Results"
42 final class FileIngestPipeline extends IngestTaskPipeline<FileIngestTask> {
44 private static final int FILE_BATCH_SIZE = 500;
45 private static final String SAVE_RESULTS_ACTIVITY = Bundle.FileIngestPipeline_SaveResults_Activity();
46 private static final Logger logger = Logger.getLogger(FileIngestPipeline.class.getName());
47 private static final IngestManager ingestManager = IngestManager.getInstance();
48 private final IngestJobPipeline ingestJobPipeline;
49 private final List<AbstractFile> fileBatch;
59 FileIngestPipeline(IngestJobPipeline ingestJobPipeline, List<IngestModuleTemplate> moduleTemplates) {
60 super(ingestJobPipeline, moduleTemplates);
61 this.ingestJobPipeline = ingestJobPipeline;
62 fileBatch =
new ArrayList<>();
66 Optional<IngestTaskPipeline.PipelineModule<FileIngestTask>> acceptModuleTemplate(IngestModuleTemplate
template) {
67 Optional<IngestTaskPipeline.PipelineModule<FileIngestTask>> module = Optional.empty();
68 if (
template.isFileIngestModuleTemplate()) {
69 FileIngestModule ingestModule =
template.createFileIngestModule();
70 module = Optional.of(
new FileIngestPipelineModule(ingestModule,
template.getModuleName()));
76 void prepareForTask(FileIngestTask task)
throws IngestTaskPipelineException {
80 void cleanUpAfterTask(FileIngestTask task)
throws IngestTaskPipelineException {
82 ingestManager.setIngestTaskProgress(task, SAVE_RESULTS_ACTIVITY);
83 AbstractFile file = task.getFile();
85 cacheFileForBatchUpdate(file);
86 }
catch (TskCoreException ex) {
87 throw new IngestTaskPipelineException(String.format(
"Failed to get file (file objId = %d)", task.getFileId()), ex);
89 ingestManager.setIngestTaskProgressCompleted(task);
94 List<IngestModuleError> shutDown() {
95 List<IngestModuleError> errors =
new ArrayList<>();
96 Date start =
new Date();
99 }
catch (IngestTaskPipelineException ex) {
100 errors.add(
new IngestModuleError(SAVE_RESULTS_ACTIVITY, ex));
102 Date finish =
new Date();
103 ingestManager.incrementModuleRunTime(SAVE_RESULTS_ACTIVITY, finish.getTime() - start.getTime());
104 errors.addAll(super.shutDown());
118 private void cacheFileForBatchUpdate(AbstractFile file)
throws IngestTaskPipelineException {
125 synchronized (fileBatch) {
127 if (fileBatch.size() >= FILE_BATCH_SIZE) {
128 updateBatchedFiles();
139 private void updateBatchedFiles() throws IngestTaskPipelineException {
146 synchronized (fileBatch) {
147 CaseDbTransaction transaction = null;
149 if (!ingestJobPipeline.isCancelled()) {
150 Case currentCase = Case.getCurrentCaseThrows();
151 SleuthkitCase caseDb = currentCase.getSleuthkitCase();
152 transaction = caseDb.beginTransaction();
153 for (AbstractFile file : fileBatch) {
154 file.save(transaction);
156 transaction.commit();
157 for (AbstractFile file : fileBatch) {
158 IngestManager.getInstance().fireFileIngestDone(file);
161 }
catch (NoCurrentCaseException | TskCoreException ex) {
162 if (transaction != null) {
164 transaction.rollback();
165 }
catch (TskCoreException ex1) {
166 logger.log(Level.SEVERE,
"Error rolling back transaction after failure to save updated properties for cached files from tasks", ex1);
169 throw new IngestTaskPipelineException(
"Failed to save updated properties for cached files from tasks", ex);
180 static final class FileIngestPipelineModule
extends IngestTaskPipeline.PipelineModule<FileIngestTask> {
182 private final FileIngestModule module;
192 FileIngestPipelineModule(FileIngestModule module, String displayName) {
193 super(module, displayName);
194 this.module = module;
198 void executeTask(IngestJobPipeline ingestJobPipeline, FileIngestTask task)
throws IngestModuleException {
199 AbstractFile file = null;
201 file = task.getFile();
202 }
catch (TskCoreException ex) {
203 throw new IngestModuleException(String.format(
"Failed to get file (file objId = %d)", task.getFileId()), ex);
205 ingestManager.setIngestTaskProgress(task, getDisplayName());
206 module.process(file);