Autopsy  4.9.1
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;
32 import org.sleuthkit.datamodel.Content;
33 import org.sleuthkit.datamodel.Image;
34 import org.sleuthkit.datamodel.SleuthkitCase;
35 import org.sleuthkit.datamodel.TskCoreException;
36 import org.sleuthkit.datamodel.TskFileRange;
37 import org.openide.util.NbBundle.Messages;
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 
100  if (criticalErrorOccurred) {
102  } else if (!errorMessages.isEmpty()) {
104  } else {
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  caseDatabase.acquireSingleUserCaseWriteLock();
151  /*
152  * Get Image that will be added to case
153  */
154  Image dataSource = caseDatabase.addImageInfo(0, imageFilePaths, timeZone); //TODO: change hard coded deviceId.
155  dataSources.add(dataSource);
156  List<TskFileRange> fileRanges = new ArrayList<>();
157 
158  /*
159  * Verify the size of the new image. Note that it may not be what is
160  * expected, but at least part of it was added to the case.
161  */
162  String verificationError = dataSource.verifyImageSize();
163  if (!verificationError.isEmpty()) {
164  errorMessages.add(Bundle.AddRawImageTask_image_noncritical_error_adding() + imageFilePaths + Bundle.AddRawImageTask_for_device() + deviceId + ":" + verificationError);
165  }
166 
167  long imageSize = dataSource.getSize();
168  int sequence = 0;
169  //start byte and end byte
170  long start = 0;
171  if (chunkSize > 0 && imageSize >= TWO_GB) {
172  for (double size = TWO_GB; size < dataSource.getSize(); size += TWO_GB) {
173  fileRanges.add(new TskFileRange(start, TWO_GB, sequence));
174  start += TWO_GB;
175  sequence++;
176  }
177 
178  }
179  double leftoverSize = imageSize - sequence * TWO_GB;
180  fileRanges.add(new TskFileRange(start, (long)leftoverSize, sequence));
181 
182 
183  caseDatabase.addLayoutFiles(dataSource, fileRanges);
184 
185  } catch (TskCoreException ex) {
186  String errorMessage = Bundle.AddRawImageTask_image_critical_error_adding() + imageFilePaths + Bundle.AddRawImageTask_for_device() + deviceId + ":" + ex.getLocalizedMessage();
187  errorMessages.add(errorMessage);
188  logger.log(Level.SEVERE, errorMessage, ex);
189  criticalErrorOccurred = true;
190  } finally {
191  caseDatabase.releaseSingleUserCaseWriteLock();
192  }
193 
194  }
195 }
void done(DataSourceProcessorResult result, List< String > errList, List< Content > newDataSources)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.