Autopsy  4.18.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.Host;
36 import org.sleuthkit.datamodel.Image;
37 import org.sleuthkit.datamodel.SleuthkitCase;
38 import org.sleuthkit.datamodel.TskCoreException;
39 import org.sleuthkit.datamodel.TskFileRange;
40 
41 /*
42  * A runnable that adds a raw data source to a case database.
43  */
44 final class AddRawImageTask implements Runnable {
45 
46  private static final Logger logger = Logger.getLogger(AddRawImageTask.class.getName());
47  private final String deviceId;
48  private final String imageFilePath;
49  private final String timeZone;
50  private final long chunkSize;
51  private final Host host;
52  private final DataSourceProcessorProgressMonitor progressMonitor;
53  private final DataSourceProcessorCallback callback;
54  private boolean criticalErrorOccurred;
55  private static final long TWO_GB = 2000000000L;
56 
73  AddRawImageTask(String deviceId, String imageFilePath, String timeZone, long chunkSize, Host host, DataSourceProcessorProgressMonitor progressMonitor, DataSourceProcessorCallback callback) {
74  this.deviceId = deviceId;
75  this.imageFilePath = imageFilePath;
76  this.timeZone = timeZone;
77  this.chunkSize = chunkSize;
78  this.host = host;
79  this.callback = callback;
80  this.progressMonitor = progressMonitor;
81  }
82 
86  @Override
87  public void run() {
88  /*
89  * Process the input image file.
90  */
91  progressMonitor.setIndeterminate(true);
92  progressMonitor.setProgress(0);
93  List<Content> newDataSources = new ArrayList<>();
94  List<String> errorMessages = new ArrayList<>();
95  addImageToCase(newDataSources, errorMessages);
96 
97  progressMonitor.setProgress(100);
98 
102  DataSourceProcessorCallback.DataSourceProcessorResult result;
103  if (criticalErrorOccurred) {
104  result = DataSourceProcessorCallback.DataSourceProcessorResult.CRITICAL_ERRORS;
105  } else if (!errorMessages.isEmpty()) {
106  result = DataSourceProcessorCallback.DataSourceProcessorResult.NONCRITICAL_ERRORS;
107  } else {
108  result = DataSourceProcessorCallback.DataSourceProcessorResult.NO_ERRORS;
109  }
110  callback.done(result, errorMessages, newDataSources);
111  criticalErrorOccurred = false;
112  }
113 
124  @Messages({"AddRawImageTask.progress.add.text=Adding raw image: ",
125  "AddRawImageTask.image.critical.error.adding=Critical error adding ",
126  "AddRawImageTask.for.device=for device ",
127  "AddRawImageTask.image.notExisting=is not existing.",
128  "AddRawImageTask.image.noncritical.error.adding=Non-critical error adding ",
129  "AddRawImageTask.noOpenCase.errMsg=No open case available."})
130  private void addImageToCase(List<Content> dataSources, List<String> errorMessages) {
131  SleuthkitCase caseDatabase;
132  try {
133  caseDatabase = Case.getCurrentCaseThrows().getSleuthkitCase();
134  } catch (NoCurrentCaseException ex) {
135  errorMessages.add(Bundle.AddRawImageTask_noOpenCase_errMsg());
136  logger.log(Level.SEVERE, Bundle.AddRawImageTask_noOpenCase_errMsg(), ex);
137  criticalErrorOccurred = true;
138  return;
139  }
140  progressMonitor.setProgressText(Bundle.AddRawImageTask_progress_add_text() + imageFilePath);
141  List<String> imageFilePaths = new ArrayList<>();
142  File imageFile = Paths.get(imageFilePath).toFile();
143  if (!imageFile.exists()) {
144  String errorMessage = Bundle.AddRawImageTask_image_critical_error_adding() + imageFilePath + Bundle.AddRawImageTask_for_device()
145  + deviceId + Bundle.AddRawImageTask_image_notExisting();
146  errorMessages.add(errorMessage);
147  logger.log(Level.SEVERE, errorMessage);
148  criticalErrorOccurred = true;
149  return;
150  }
151  imageFilePaths.add(imageFilePath);
152  try {
153  /*
154  * Get Image that will be added to case
155  */
156  Image dataSource = caseDatabase.addImageInfo(0, imageFilePaths, timeZone, host); //TODO: change hard coded deviceId.
157  dataSources.add(dataSource);
158  List<TskFileRange> fileRanges = new ArrayList<>();
159 
160  /*
161  * Verify the size of the new image. Note that it may not be what is
162  * expected, but at least part of it was added to the case.
163  */
164  String verificationError = dataSource.verifyImageSize();
165  if (!verificationError.isEmpty()) {
166  errorMessages.add(Bundle.AddRawImageTask_image_noncritical_error_adding() + imageFilePaths + Bundle.AddRawImageTask_for_device() + deviceId + ":" + verificationError);
167  }
168 
169  long imageSize = dataSource.getSize();
170  int sequence = 0;
171  //start byte and end byte
172  long start = 0;
173  if (chunkSize > 0 && imageSize >= TWO_GB) {
174  for (double size = TWO_GB; size < dataSource.getSize(); size += TWO_GB) {
175  fileRanges.add(new TskFileRange(start, TWO_GB, sequence));
176  start += TWO_GB;
177  sequence++;
178  }
179 
180  }
181  double leftoverSize = imageSize - sequence * TWO_GB;
182  fileRanges.add(new TskFileRange(start, (long)leftoverSize, sequence));
183 
184 
185  caseDatabase.addLayoutFiles(dataSource, fileRanges);
186 
187  } catch (TskCoreException ex) {
188  String errorMessage = Bundle.AddRawImageTask_image_critical_error_adding() + imageFilePaths + Bundle.AddRawImageTask_for_device() + deviceId + ":" + ex.getLocalizedMessage();
189  errorMessages.add(errorMessage);
190  logger.log(Level.SEVERE, errorMessage, ex);
191  criticalErrorOccurred = true;
192  }
193  }
194 }

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