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 IngestPipeline<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 IngestJobExecutor ingestJobExecutor;
49 private final List<AbstractFile> fileBatch;
61 FileIngestPipeline(IngestJobExecutor ingestJobExecutor, List<IngestModuleTemplate> moduleTemplates) {
62 super(ingestJobExecutor, moduleTemplates);
63 this.ingestJobExecutor = ingestJobExecutor;
64 fileBatch =
new ArrayList<>();
68 Optional<IngestPipeline.PipelineModule<FileIngestTask>> acceptModuleTemplate(IngestModuleTemplate
template) {
69 Optional<IngestPipeline.PipelineModule<FileIngestTask>> module = Optional.empty();
70 if (
template.isFileIngestModuleTemplate()) {
71 FileIngestModule ingestModule =
template.createFileIngestModule();
72 module = Optional.of(
new FileIngestPipelineModule(ingestModule,
template.getModuleName()));
78 void prepareForTask(FileIngestTask task)
throws IngestPipelineException {
82 void cleanUpAfterTask(FileIngestTask task)
throws IngestPipelineException {
84 ingestManager.setIngestTaskProgress(task, SAVE_RESULTS_ACTIVITY);
85 AbstractFile file = task.getFile();
87 cacheFileForBatchUpdate(file);
88 }
catch (TskCoreException ex) {
89 throw new IngestPipelineException(String.format(
"Failed to get file (file objId = %d)", task.getFileId()), ex);
91 ingestManager.setIngestTaskProgressCompleted(task);
96 List<IngestModuleError> shutDown() {
97 List<IngestModuleError> errors =
new ArrayList<>();
98 Date start =
new Date();
100 updateBatchedFiles();
101 }
catch (IngestPipelineException ex) {
102 errors.add(
new IngestModuleError(SAVE_RESULTS_ACTIVITY, ex));
104 Date finish =
new Date();
105 ingestManager.incrementModuleRunTime(SAVE_RESULTS_ACTIVITY, finish.getTime() - start.getTime());
106 errors.addAll(super.shutDown());
120 private void cacheFileForBatchUpdate(AbstractFile file)
throws IngestPipelineException {
127 synchronized (fileBatch) {
129 if (fileBatch.size() >= FILE_BATCH_SIZE) {
130 updateBatchedFiles();
141 private void updateBatchedFiles() throws IngestPipelineException {
148 synchronized (fileBatch) {
149 CaseDbTransaction transaction = null;
151 if (!ingestJobExecutor.isCancelled()) {
152 Case currentCase = Case.getCurrentCaseThrows();
153 SleuthkitCase caseDb = currentCase.getSleuthkitCase();
154 transaction = caseDb.beginTransaction();
155 for (AbstractFile file : fileBatch) {
156 file.save(transaction);
158 transaction.commit();
159 for (AbstractFile file : fileBatch) {
160 IngestManager.getInstance().fireFileIngestDone(file);
163 }
catch (NoCurrentCaseException | TskCoreException ex) {
164 if (transaction != null) {
166 transaction.rollback();
167 }
catch (TskCoreException ex1) {
168 logger.log(Level.SEVERE,
"Error rolling back transaction after failure to save updated properties for cached files from tasks", ex1);
171 throw new IngestPipelineException(
"Failed to save updated properties for cached files from tasks", ex);
182 static final class FileIngestPipelineModule
extends IngestPipeline.PipelineModule<FileIngestTask> {
184 private final FileIngestModule module;
194 FileIngestPipelineModule(FileIngestModule module, String displayName) {
195 super(module, displayName);
196 this.module = module;
200 void process(IngestJobExecutor ingestJobExecutor, FileIngestTask task)
throws IngestModuleException {
201 AbstractFile file = null;
203 file = task.getFile();
204 }
catch (TskCoreException ex) {
205 throw new IngestModuleException(String.format(
"Failed to get file (file objId = %d)", task.getFileId()), ex);
207 ingestManager.setIngestTaskProgress(task, getDisplayName());
208 module.process(file);