Autopsy  3.1
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.logging.Level;
27 import java.util.prefs.PreferenceChangeEvent;
28 import java.util.prefs.PreferenceChangeListener;
29 import org.openide.util.NbBundle;
31 import javax.swing.SwingWorker;
32 import org.netbeans.api.progress.ProgressHandle;
46 
50 public final class ContentUtils {
51 
52  private final static Logger logger = Logger.getLogger(ContentUtils.class.getName());
54  private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
55  private static final SimpleDateFormat dateFormatterISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
56 
57  static {
58  UserPreferences.addChangeListener(new PreferenceChangeListener() {
59  @Override
60  public void preferenceChange(PreferenceChangeEvent evt) {
61  if (evt.getKey().equals(UserPreferences.DISPLAY_TIMES_IN_LOCAL_TIME)) {
62  displayTimesInLocalTime = UserPreferences.displayTimesInLocalTime();
63  }
64  }
65  });
66  }
67 
68  // don't instantiate
69  private ContentUtils() {
70  throw new AssertionError();
71  }
72 
80  public static String getStringTime(long epochSeconds, TimeZone tzone) {
81  String time = "0000-00-00 00:00:00";
82  if (epochSeconds != 0) {
83  synchronized (dateFormatter) {
84  dateFormatter.setTimeZone(tzone);
85  time = dateFormatter.format(new java.util.Date(epochSeconds * 1000));
86  }
87  }
88  return time;
89  }
90 
91  public static String getStringTimeISO8601(long epochSeconds, TimeZone tzone) {
92  String time = "0000-00-00T00:00:00Z"; //NON-NLS
93  if (epochSeconds != 0) {
94  synchronized (dateFormatterISO8601) {
95  dateFormatterISO8601.setTimeZone(tzone);
96  time = dateFormatterISO8601.format(new java.util.Date(epochSeconds * 1000));
97  }
98  }
99 
100  return time;
101  }
102 
110  public static String getStringTime(long epochSeconds, Content c) {
111  return getStringTime(epochSeconds, getTimeZone(c));
112  }
113 
122  public static String getStringTimeISO8601(long epochSeconds, Content c) {
123  return getStringTimeISO8601(epochSeconds, getTimeZone(c));
124  }
125 
126  public static TimeZone getTimeZone(Content c) {
127 
128  try {
130  return TimeZone.getTimeZone("GMT");
131  }
132  else {
133  final Content dataSource = c.getDataSource();
134  if ((dataSource != null) && (dataSource instanceof Image)) {
135  Image image = (Image)dataSource;
136  return TimeZone.getTimeZone(image.getTimeZone());
137  } else {
138  //case such as top level VirtualDirectory
139  return TimeZone.getDefault();
140  }
141  }
142  } catch (TskException ex) {
143  return TimeZone.getDefault();
144  }
145  }
146  private static final SystemNameVisitor systemName = new SystemNameVisitor();
147 
148  public static String getSystemName(Content content) {
149  return content.accept(systemName);
150  }
151 
152  private static class SystemNameVisitor extends ContentVisitor.Default<String> {
153 
155  }
156 
157  @Override
158  protected String defaultVisit(Content cntnt) {
159  return cntnt.getName() + ":" + Long.toString(cntnt.getId());
160  }
161  }
162  private static final int TO_FILE_BUFFER_SIZE = 8192;
163 
179  public static <T,V> long writeToFile(Content content, java.io.File outputFile,
180  ProgressHandle progress, SwingWorker<T,V> worker, boolean source) throws IOException {
181 
182  InputStream in = new ReadContentInputStream(content);
183 
184  boolean append = false;
185  FileOutputStream out = new FileOutputStream(outputFile, append);
186 
187  // Get the unit size for a progress bar
188  int unit = (int) (content.getSize() / 100);
189  long totalRead = 0;
190 
191  try {
192  byte[] buffer = new byte[TO_FILE_BUFFER_SIZE];
193  int len = in.read(buffer);
194  while (len != -1) {
195  // If there is a worker, check for a cancelation
196  if (worker != null && worker.isCancelled()) {
197  break;
198  }
199  out.write(buffer, 0, len);
200  len = in.read(buffer);
201  totalRead += len;
202  // If there is a progress bar and this is the source file,
203  // report any progress
204  if (progress != null && source && totalRead >= TO_FILE_BUFFER_SIZE) {
205  int totalProgress = (int) (totalRead / unit);
206  progress.progress(content.getName(), totalProgress);
207  // If it's not the source, just update the file being processed
208  } else if (progress != null && !source) {
209  progress.progress(content.getName());
210  }
211  }
212  } finally {
213  out.close();
214  }
215  return totalRead;
216  }
217 
218  public static void writeToFile(Content content, java.io.File outputFile) throws IOException {
219  writeToFile(content, outputFile, null, null, false);
220  }
221 
225  public static boolean isDotDirectory(AbstractFile dir) {
226  String name = dir.getName();
227  return name.equals(".") || name.equals("..");
228  }
229 
235  public static class ExtractFscContentVisitor<T,V> extends ContentVisitor.Default<Void> {
236 
237  java.io.File dest;
238  ProgressHandle progress;
239  SwingWorker<T,V> worker;
240  boolean source = false;
241 
253  public ExtractFscContentVisitor(java.io.File dest,
254  ProgressHandle progress, SwingWorker<T,V> worker, boolean source) {
255  this.dest = dest;
256  this.progress = progress;
257  this.worker = worker;
258  this.source = source;
259  }
260 
261  public ExtractFscContentVisitor(java.io.File dest) {
262  this.dest = dest;
263  }
264 
269  public static <T,V> void extract(Content cntnt, java.io.File dest, ProgressHandle progress, SwingWorker<T,V> worker) {
270  cntnt.accept(new ExtractFscContentVisitor<>(dest, progress, worker, true));
271  }
272 
273  @Override
274  public Void visit(File f) {
275  try {
276  ContentUtils.writeToFile(f, dest, progress, worker, source);
277  } catch (IOException ex) {
278  logger.log(Level.SEVERE,
279  "Trouble extracting file to " + dest.getAbsolutePath(), //NON-NLS
280  ex);
281  }
282  return null;
283  }
284 
285  @Override
286  public Void visit(LayoutFile f) {
287  try {
288  ContentUtils.writeToFile(f, dest, progress, worker, source);
289  } catch (IOException ex) {
290  logger.log(Level.SEVERE,
291  "Trouble extracting unallocated content file to " + dest.getAbsolutePath(), //NON-NLS
292  ex);
293  }
294  return null;
295  }
296 
297  @Override
298  public Void visit(DerivedFile df) {
299  try {
300  ContentUtils.writeToFile(df, dest, progress, worker, source);
301  } catch (IOException ex) {
302  logger.log(Level.SEVERE,
303  "Error extracting derived file to " + dest.getAbsolutePath(), //NON-NLS
304  ex);
305  }
306  return null;
307  }
308 
309  @Override
310  public Void visit(LocalFile lf) {
311  try {
312  ContentUtils.writeToFile(lf, dest, progress, worker, source);
313  } catch (IOException ex) {
314  logger.log(Level.SEVERE,
315  "Error extracting local file to " + dest.getAbsolutePath(), //NON-NLS
316  ex);
317  }
318  return null;
319  }
320 
321  @Override
322  public Void visit(Directory dir) {
323  return visitDir(dir);
324  }
325 
326  @Override
327  public Void visit(VirtualDirectory dir) {
328  return visitDir(dir);
329  }
330 
331  private java.io.File getFsContentDest(Content fsc) {
332  String path = dest.getAbsolutePath() + java.io.File.separator
333  + fsc.getName();
334  return new java.io.File(path);
335  }
336 
337  public Void visitDir(AbstractFile dir) {
338 
339  // don't extract . and .. directories
340  if (isDotDirectory(dir)) {
341  return null;
342  }
343 
344  dest.mkdir();
345 
346 
347 
348  try {
349  int numProcessed = 0;
350  // recurse on children
351  for (Content child : dir.getChildren()) {
352  java.io.File childFile = getFsContentDest(child);
353  ExtractFscContentVisitor<T,V> childVisitor =
354  new ExtractFscContentVisitor<>(childFile, progress, worker, false);
355  // If this is the source directory of an extract it
356  // will have a progress and worker, and will keep track
357  // of the progress bar's progress
358  if (worker != null && worker.isCancelled()) {
359  break;
360  }
361  if (progress != null && source) {
362  progress.progress(child.getName(), numProcessed);
363  }
364  child.accept(childVisitor);
365  numProcessed++;
366  }
367  } catch (TskException ex) {
368  logger.log(Level.SEVERE,
369  "Trouble fetching children to extract.", ex); //NON-NLS
370  }
371 
372  return null;
373  }
374 
375  @Override
376  protected Void defaultVisit(Content cntnt) {
377  throw new UnsupportedOperationException(NbBundle.getMessage(this.getClass(),
378  "ContentUtils.exception.msg",
379  cntnt.getClass().getSimpleName()));
380  }
381  }
382 
388  public static boolean shouldDisplayTimesInLocalTime(){
390  }
391 }
static String getStringTime(long epochSeconds, TimeZone tzone)
static< T, V > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, SwingWorker< T, V > worker, boolean source)
File(SleuthkitCase db, long objId, long fsObjId, TSK_FS_ATTR_TYPE_ENUM attrType, short attrId, String name, long metaAddr, int metaSeq, TSK_FS_NAME_TYPE_ENUM dirType, TSK_FS_META_TYPE_ENUM metaType, TSK_FS_NAME_FLAG_ENUM dirFlag, short metaFlags, long size, long ctime, long crtime, long atime, long mtime, short modes, int uid, int gid, String md5Hash, FileKnown knownState, String parentPath)
static final SimpleDateFormat dateFormatter
ExtractFscContentVisitor(java.io.File dest, ProgressHandle progress, SwingWorker< T, V > 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)
static String getStringTimeISO8601(long epochSeconds, Content c)
public< T > T accept(ContentVisitor< T > v)
static void addChangeListener(PreferenceChangeListener listener)
static void writeToFile(Content content, java.io.File outputFile)
static boolean isDotDirectory(AbstractFile dir)
static Logger getLogger(String name)
Definition: Logger.java:131

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