Autopsy  4.15.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddRawImageTask.java
Go to the documentation of this file.
1 package org.sleuthkit.autopsy.datasourceprocessors;
2 
3 /*
4  * Autopsy Forensic Browser
5  *
6  * Copyright 2011-2018 Basis Technology Corp.
7  * Contact: carrier <at> sleuthkit <dot> org
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 
23 import java.io.File;
24 import java.nio.file.Paths;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.logging.Level;
28 import org.openide.util.NbBundle.Messages;
34 import org.sleuthkit.datamodel.Content;
35 import org.sleuthkit.datamodel.Image;
36 import org.sleuthkit.datamodel.SleuthkitCase;
37 import org.sleuthkit.datamodel.TskCoreException;
38 import org.sleuthkit.datamodel.TskFileRange;
39 
40 /*
41  * A runnable that adds a raw data source to a case database.
42  */
43 final class AddRawImageTask implements Runnable {
44 
45  private static final Logger logger = Logger.getLogger(AddRawImageTask.class.getName());
46  private final String deviceId;
47  private final String imageFilePath;
48  private final String timeZone;
49  private final long chunkSize;
50  private final DataSourceProcessorProgressMonitor progressMonitor;
51  private final DataSourceProcessorCallback callback;
52  private boolean criticalErrorOccurred;
53  private static final long TWO_GB = 2000000000L;
54 
71  AddRawImageTask(String deviceId, String imageFilePath, String timeZone, long chunkSize, DataSourceProcessorProgressMonitor progressMonitor, DataSourceProcessorCallback callback) {
72  this.deviceId = deviceId;
73  this.imageFilePath = imageFilePath;
74  this.timeZone = timeZone;
75  this.chunkSize = chunkSize;
76  this.callback = callback;
77  this.progressMonitor = progressMonitor;
78  }
79 
83  @Override
84  public void run() {
85  /*
86  * Process the input image file.
87  */
88  progressMonitor.setIndeterminate(true);
89  progressMonitor.setProgress(0);
90  List<Content> newDataSources = new ArrayList<>();
91  List<String> errorMessages = new ArrayList<>();
92  addImageToCase(newDataSources, errorMessages);
93 
94  progressMonitor.setProgress(100);
95 
99  DataSourceProcessorCallback.DataSourceProcessorResult result;
100  if (criticalErrorOccurred) {
101  result = DataSourceProcessorCallback.DataSourceProcessorResult.CRITICAL_ERRORS;
102  } else if (!errorMessages.isEmpty()) {
103  result = DataSourceProcessorCallback.DataSourceProcessorResult.NONCRITICAL_ERRORS;
104  } else {
105  result = DataSourceProcessorCallback.DataSourceProcessorResult.NO_ERRORS;
106  }
107  callback.done(result, errorMessages, newDataSources);
108  criticalErrorOccurred = false;
109  }
110 
121  @Messages({"AddRawImageTask.progress.add.text=Adding raw image: ",
122  "AddRawImageTask.image.critical.error.adding=Critical error adding ",
123  "AddRawImageTask.for.device=for device ",
124  "AddRawImageTask.image.notExisting=is not existing.",
125  "AddRawImageTask.image.noncritical.error.adding=Non-critical error adding ",
126  "AddRawImageTask.noOpenCase.errMsg=No open case available."})
127  private void addImageToCase(List<Content> dataSources, List<String> errorMessages) {
128  SleuthkitCase caseDatabase;
129  try {
130  caseDatabase = Case.getCurrentCaseThrows().getSleuthkitCase();
131  } catch (NoCurrentCaseException ex) {
132  errorMessages.add(Bundle.AddRawImageTask_noOpenCase_errMsg());
133  logger.log(Level.SEVERE, Bundle.AddRawImageTask_noOpenCase_errMsg(), ex);
134  criticalErrorOccurred = true;
135  return;
136  }
137  progressMonitor.setProgressText(Bundle.AddRawImageTask_progress_add_text() + imageFilePath);
138  List<String> imageFilePaths = new ArrayList<>();
139  File imageFile = Paths.get(imageFilePath).toFile();
140  if (!imageFile.exists()) {
141  String errorMessage = Bundle.AddRawImageTask_image_critical_error_adding() + imageFilePath + Bundle.AddRawImageTask_for_device()
142  + deviceId + Bundle.AddRawImageTask_image_notExisting();
143  errorMessages.add(errorMessage);
144  logger.log(Level.SEVERE, errorMessage);
145  criticalErrorOccurred = true;
146  return;
147  }
148  imageFilePaths.add(imageFilePath);
149  try {
150  /*
151  * Get Image that will be added to case
152  */
153  Image dataSource = caseDatabase.addImageInfo(0, imageFilePaths, timeZone); //TODO: change hard coded deviceId.
154  dataSources.add(dataSource);
155  List<TskFileRange> fileRanges = new ArrayList<>();
156 
157  /*
158  * Verify the size of the new image. Note that it may not be what is
159  * expected, but at least part of it was added to the case.
160  */
161  String verificationError = dataSource.verifyImageSize();
162  if (!verificationError.isEmpty()) {
163  errorMessages.add(Bundle.AddRawImageTask_image_noncritical_error_adding() + imageFilePaths + Bundle.AddRawImageTask_for_device() + deviceId + ":" + verificationError);
164  }
165 
166  long imageSize = dataSource.getSize();
167  int sequence = 0;
168  //start byte and end byte
169  long start = 0;
170  if (chunkSize > 0 && imageSize >= TWO_GB) {
171  for (double size = TWO_GB; size < dataSource.getSize(); size += TWO_GB) {
172  fileRanges.add(new TskFileRange(start, TWO_GB, sequence));
173  start += TWO_GB;
174  sequence++;
175  }
176 
177  }
178  double leftoverSize = imageSize - sequence * TWO_GB;
179  fileRanges.add(new TskFileRange(start, (long)leftoverSize, sequence));
180 
181 
182  caseDatabase.addLayoutFiles(dataSource, fileRanges);
183 
184  } catch (TskCoreException ex) {
185  String errorMessage = Bundle.AddRawImageTask_image_critical_error_adding() + imageFilePaths + Bundle.AddRawImageTask_for_device() + deviceId + ":" + ex.getLocalizedMessage();
186  errorMessages.add(errorMessage);
187  logger.log(Level.SEVERE, errorMessage, ex);
188  criticalErrorOccurred = true;
189  }
190  }
191 }

Copyright © 2012-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.