Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ContentUtils.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.datamodel;
20 
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.text.SimpleDateFormat;
25 import java.util.TimeZone;
26 import java.util.concurrent.Future;
27 import java.util.logging.Level;
28 import java.util.prefs.PreferenceChangeEvent;
29 import java.util.prefs.PreferenceChangeListener;
30 import javax.swing.SwingWorker;
31 import org.netbeans.api.progress.ProgressHandle;
32 import org.openide.util.NbBundle;
35 import org.sleuthkit.datamodel.AbstractFile;
36 import org.sleuthkit.datamodel.Content;
37 import org.sleuthkit.datamodel.ContentVisitor;
38 import org.sleuthkit.datamodel.DerivedFile;
39 import org.sleuthkit.datamodel.Directory;
40 import org.sleuthkit.datamodel.File;
41 import org.sleuthkit.datamodel.Image;
42 import org.sleuthkit.datamodel.LayoutFile;
43 import org.sleuthkit.datamodel.LocalFile;
44 import org.sleuthkit.datamodel.ReadContentInputStream;
45 import org.sleuthkit.datamodel.TskException;
46 import org.sleuthkit.datamodel.VirtualDirectory;
47 
51 public final class ContentUtils {
52 
53  private final static Logger logger = Logger.getLogger(ContentUtils.class.getName());
55  private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
56  private static final SimpleDateFormat dateFormatterISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
57 
58  static {
59  UserPreferences.addChangeListener(new PreferenceChangeListener() {
60  @Override
61  public void preferenceChange(PreferenceChangeEvent evt) {
62  if (evt.getKey().equals(UserPreferences.DISPLAY_TIMES_IN_LOCAL_TIME)) {
63  displayTimesInLocalTime = UserPreferences.displayTimesInLocalTime();
64  }
65  }
66  });
67  }
68 
69  // don't instantiate
70  private ContentUtils() {
71  throw new AssertionError();
72  }
73 
82  public static String getStringTime(long epochSeconds, TimeZone tzone) {
83  String time = "0000-00-00 00:00:00";
84  if (epochSeconds != 0) {
85  synchronized (dateFormatter) {
86  dateFormatter.setTimeZone(tzone);
87  time = dateFormatter.format(new java.util.Date(epochSeconds * 1000));
88  }
89  }
90  return time;
91  }
92 
93  public static String getStringTimeISO8601(long epochSeconds, TimeZone tzone) {
94  String time = "0000-00-00T00:00:00Z"; //NON-NLS
95  if (epochSeconds != 0) {
96  synchronized (dateFormatterISO8601) {
97  dateFormatterISO8601.setTimeZone(tzone);
98  time = dateFormatterISO8601.format(new java.util.Date(epochSeconds * 1000));
99  }
100  }
101 
102  return time;
103  }
104 
113  public static String getStringTime(long epochSeconds, Content c) {
114  return getStringTime(epochSeconds, getTimeZone(c));
115  }
116 
126  public static String getStringTimeISO8601(long epochSeconds, Content c) {
127  return getStringTimeISO8601(epochSeconds, getTimeZone(c));
128  }
129 
130  public static TimeZone getTimeZone(Content c) {
131 
132  try {
134  return TimeZone.getTimeZone("GMT");
135  } else {
136  final Content dataSource = c.getDataSource();
137  if ((dataSource != null) && (dataSource instanceof Image)) {
138  Image image = (Image) dataSource;
139  return TimeZone.getTimeZone(image.getTimeZone());
140  } else {
141  //case such as top level VirtualDirectory
142  return TimeZone.getDefault();
143  }
144  }
145  } catch (TskException ex) {
146  return TimeZone.getDefault();
147  }
148  }
149  private static final SystemNameVisitor systemName = new SystemNameVisitor();
150 
151  public static String getSystemName(Content content) {
152  return content.accept(systemName);
153  }
154 
155  private static class SystemNameVisitor extends ContentVisitor.Default<String> {
156 
158  }
159 
160  @Override
161  protected String defaultVisit(Content cntnt) {
162  return cntnt.getName() + ":" + Long.toString(cntnt.getId());
163  }
164  }
165  private static final int TO_FILE_BUFFER_SIZE = 8192;
166 
185  public static <T> long writeToFile(Content content, java.io.File outputFile,
186  ProgressHandle progress, Future<T> worker, boolean source) throws IOException {
187 
188  InputStream in = new ReadContentInputStream(content);
189 
190  boolean append = false;
191  FileOutputStream out = new FileOutputStream(outputFile, append);
192 
193  // Get the unit size for a progress bar
194  int unit = (int) (content.getSize() / 100);
195  long totalRead = 0;
196 
197  try {
198  byte[] buffer = new byte[TO_FILE_BUFFER_SIZE];
199  int len = in.read(buffer);
200  while (len != -1) {
201  // If there is a worker, check for a cancelation
202  if (worker != null && worker.isCancelled()) {
203  break;
204  }
205  out.write(buffer, 0, len);
206  len = in.read(buffer);
207  totalRead += len;
208  // If there is a progress bar and this is the source file,
209  // report any progress
210  if (progress != null && source && totalRead >= TO_FILE_BUFFER_SIZE) {
211  int totalProgress = (int) (totalRead / unit);
212  progress.progress(content.getName(), totalProgress);
213  // If it's not the source, just update the file being processed
214  } else if (progress != null && !source) {
215  progress.progress(content.getName());
216  }
217  }
218  } finally {
219  out.close();
220  }
221  return totalRead;
222  }
223 
224  public static void writeToFile(Content content, java.io.File outputFile) throws IOException {
225  writeToFile(content, outputFile, null, null, false);
226  }
227 
231  public static boolean isDotDirectory(AbstractFile dir) {
232  String name = dir.getName();
233  return name.equals(".") || name.equals("..");
234  }
235 
241  public static class ExtractFscContentVisitor<T, V> extends ContentVisitor.Default<Void> {
242 
243  java.io.File dest;
244  ProgressHandle progress;
245  SwingWorker<T, V> worker;
246  boolean source = false;
247 
260  public ExtractFscContentVisitor(java.io.File dest,
261  ProgressHandle progress, SwingWorker<T, V> worker, boolean source) {
262  this.dest = dest;
263  this.progress = progress;
264  this.worker = worker;
265  this.source = source;
266  }
267 
268  public ExtractFscContentVisitor(java.io.File dest) {
269  this.dest = dest;
270  }
271 
276  public static <T, V> void extract(Content cntnt, java.io.File dest, ProgressHandle progress, SwingWorker<T, V> worker) {
277  cntnt.accept(new ExtractFscContentVisitor<>(dest, progress, worker, true));
278  }
279 
280  @Override
281  public Void visit(File f) {
282  try {
283  ContentUtils.writeToFile(f, dest, progress, worker, source);
284  } catch (IOException ex) {
285  logger.log(Level.SEVERE,
286  "Trouble extracting file to " + dest.getAbsolutePath(), //NON-NLS
287  ex);
288  }
289  return null;
290  }
291 
292  @Override
293  public Void visit(LayoutFile f) {
294  try {
295  ContentUtils.writeToFile(f, dest, progress, worker, source);
296  } catch (IOException ex) {
297  logger.log(Level.SEVERE,
298  "Trouble extracting unallocated content file to " + dest.getAbsolutePath(), //NON-NLS
299  ex);
300  }
301  return null;
302  }
303 
304  @Override
305  public Void visit(DerivedFile df) {
306  try {
307  ContentUtils.writeToFile(df, dest, progress, worker, source);
308  } catch (IOException ex) {
309  logger.log(Level.SEVERE,
310  "Error extracting derived file to " + dest.getAbsolutePath(), //NON-NLS
311  ex);
312  }
313  return null;
314  }
315 
316  @Override
317  public Void visit(LocalFile lf) {
318  try {
319  ContentUtils.writeToFile(lf, dest, progress, worker, source);
320  } catch (IOException ex) {
321  logger.log(Level.SEVERE,
322  "Error extracting local file to " + dest.getAbsolutePath(), //NON-NLS
323  ex);
324  }
325  return null;
326  }
327 
328  @Override
329  public Void visit(Directory dir) {
330  return visitDir(dir);
331  }
332 
333  @Override
334  public Void visit(VirtualDirectory dir) {
335  return visitDir(dir);
336  }
337 
338  private java.io.File getFsContentDest(Content fsc) {
339  String path = dest.getAbsolutePath() + java.io.File.separator
340  + fsc.getName();
341  return new java.io.File(path);
342  }
343 
344  public Void visitDir(AbstractFile dir) {
345 
346  // don't extract . and .. directories
347  if (isDotDirectory(dir)) {
348  return null;
349  }
350 
351  dest.mkdir();
352 
353  try {
354  int numProcessed = 0;
355  // recurse on children
356  for (Content child : dir.getChildren()) {
357  java.io.File childFile = getFsContentDest(child);
358  ExtractFscContentVisitor<T, V> childVisitor
359  = new ExtractFscContentVisitor<>(childFile, progress, worker, false);
360  // If this is the source directory of an extract it
361  // will have a progress and worker, and will keep track
362  // of the progress bar's progress
363  if (worker != null && worker.isCancelled()) {
364  break;
365  }
366  if (progress != null && source) {
367  progress.progress(child.getName(), numProcessed);
368  }
369  child.accept(childVisitor);
370  numProcessed++;
371  }
372  } catch (TskException ex) {
373  logger.log(Level.SEVERE,
374  "Trouble fetching children to extract.", ex); //NON-NLS
375  }
376 
377  return null;
378  }
379 
380  @Override
381  protected Void defaultVisit(Content cntnt) {
382  throw new UnsupportedOperationException(NbBundle.getMessage(this.getClass(),
383  "ContentUtils.exception.msg",
384  cntnt.getClass().getSimpleName()));
385  }
386  }
387 
393  public static boolean shouldDisplayTimesInLocalTime() {
395  }
396 }
static String getStringTime(long epochSeconds, TimeZone tzone)
static final SimpleDateFormat dateFormatter
ExtractFscContentVisitor(java.io.File dest, ProgressHandle progress, SwingWorker< T, V > worker, boolean source)
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
static String getSystemName(Content content)
static< T, V > void extract(Content cntnt, java.io.File dest, ProgressHandle progress, SwingWorker< T, V > worker)
static String getStringTimeISO8601(long epochSeconds, TimeZone tzone)
static final SimpleDateFormat dateFormatterISO8601
static final SystemNameVisitor systemName
static String getStringTime(long epochSeconds, Content c)
synchronized static Logger getLogger(String name)
Definition: Logger.java:166
static String getStringTimeISO8601(long epochSeconds, Content c)
static void addChangeListener(PreferenceChangeListener listener)
static void writeToFile(Content content, java.io.File outputFile)
static boolean isDotDirectory(AbstractFile dir)

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.