Autopsy  4.4
Graphical digital forensics platform for The Sleuth Kit and other tools.
DeletedContent.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-2015 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.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Observable;
27 import java.util.Observer;
28 import java.util.logging.Level;
29 import javax.swing.JOptionPane;
30 import javax.swing.SwingUtilities;
31 import org.openide.nodes.AbstractNode;
32 import org.openide.nodes.ChildFactory;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Node;
35 import org.openide.nodes.Sheet;
36 import org.openide.util.NbBundle;
37 import org.openide.util.lookup.Lookups;
38 import org.openide.windows.WindowManager;
42 import org.sleuthkit.datamodel.AbstractFile;
43 import org.sleuthkit.datamodel.Content;
44 import org.sleuthkit.datamodel.ContentVisitor;
45 import org.sleuthkit.datamodel.Directory;
46 import org.sleuthkit.datamodel.File;
47 import org.sleuthkit.datamodel.FsContent;
48 import org.sleuthkit.datamodel.LayoutFile;
49 import org.sleuthkit.datamodel.SleuthkitCase;
50 import org.sleuthkit.datamodel.TskCoreException;
51 import org.sleuthkit.datamodel.TskData;
52 
56 public class DeletedContent implements AutopsyVisitableItem {
57 
58  private SleuthkitCase skCase;
59 
60  public enum DeletedContentFilter implements AutopsyVisitableItem {
61 
62  FS_DELETED_FILTER(0,
63  "FS_DELETED_FILTER", //NON-NLS
64  NbBundle.getMessage(DeletedContent.class, "DeletedContent.fsDelFilter.text")),
65  ALL_DELETED_FILTER(1,
66  "ALL_DELETED_FILTER", //NON-NLS
67  NbBundle.getMessage(DeletedContent.class, "DeletedContent.allDelFilter.text"));
68  private int id;
69  private String name;
70  private String displayName;
71 
72  private DeletedContentFilter(int id, String name, String displayName) {
73  this.id = id;
74  this.name = name;
75  this.displayName = displayName;
76 
77  }
78 
79  public String getName() {
80  return this.name;
81  }
82 
83  public int getId() {
84  return this.id;
85  }
86 
87  public String getDisplayName() {
88  return this.displayName;
89  }
90 
91  @Override
92  public <T> T accept(AutopsyItemVisitor<T> v) {
93  return v.visit(this);
94  }
95  }
96 
97  public DeletedContent(SleuthkitCase skCase) {
98  this.skCase = skCase;
99  }
100 
101  @Override
102  public <T> T accept(AutopsyItemVisitor<T> v) {
103  return v.visit(this);
104  }
105 
106  public SleuthkitCase getSleuthkitCase() {
107  return this.skCase;
108  }
109 
110  public static class DeletedContentsNode extends DisplayableItemNode {
111 
112  private static final String NAME = NbBundle.getMessage(DeletedContent.class,
113  "DeletedContent.deletedContentsNode.name");
114  private SleuthkitCase skCase;
115 
116  DeletedContentsNode(SleuthkitCase skCase) {
117  super(Children.create(new DeletedContentsChildren(skCase), true), Lookups.singleton(NAME));
118  super.setName(NAME);
119  super.setDisplayName(NAME);
120  this.skCase = skCase;
121  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/file-icon-deleted.png"); //NON-NLS
122  }
123 
124  @Override
125  public boolean isLeafTypeNode() {
126  return false;
127  }
128 
129  @Override
130  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
131  return v.visit(this);
132  }
133 
134  @Override
135  protected Sheet createSheet() {
136  Sheet s = super.createSheet();
137  Sheet.Set ss = s.get(Sheet.PROPERTIES);
138  if (ss == null) {
139  ss = Sheet.createPropertiesSet();
140  s.put(ss);
141  }
142 
143  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "DeletedContent.createSheet.name.name"),
144  NbBundle.getMessage(this.getClass(), "DeletedContent.createSheet.name.displayName"),
145  NbBundle.getMessage(this.getClass(), "DeletedContent.createSheet.name.desc"),
146  NAME));
147  return s;
148  }
149 
150  @Override
151  public String getItemType() {
152  return getClass().getName();
153  }
154  }
155 
156  public static class DeletedContentsChildren extends ChildFactory<DeletedContent.DeletedContentFilter> {
157 
158  private SleuthkitCase skCase;
159  private Observable notifier;
160  // true if we have already told user that not all files will be shown
161  private static volatile boolean maxFilesDialogShown = false;
162 
163  public DeletedContentsChildren(SleuthkitCase skCase) {
164  this.skCase = skCase;
165  this.notifier = new DeletedContentsChildrenObservable();
166  }
167 
172  private final class DeletedContentsChildrenObservable extends Observable {
173 
178  }
179 
180  private void removeListeners() {
181  deleteObservers();
185  }
186 
187  private final PropertyChangeListener pcl = new PropertyChangeListener() {
188  @Override
189  public void propertyChange(PropertyChangeEvent evt) {
190  String eventType = evt.getPropertyName();
191  if (eventType.equals(IngestManager.IngestModuleEvent.CONTENT_CHANGED.toString())) {
200  try {
202  // new file was added
203  // @@@ COULD CHECK If the new file is deleted before notifying...
204  update();
205  } catch (IllegalStateException notUsed) {
209  }
210  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
211  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())
212  || eventType.equals(Case.Events.DATA_SOURCE_ADDED.toString())) {
219  try {
221  update();
222  } catch (IllegalStateException notUsed) {
226  }
227  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
228  // case was closed. Remove listeners so that we don't get called with a stale case handle
229  if (evt.getNewValue() == null) {
230  removeListeners();
231  }
232  maxFilesDialogShown = false;
233  }
234  }
235  };
236 
237  private void update() {
238  setChanged();
239  notifyObservers();
240  }
241  }
242 
243  @Override
244 
245  protected boolean createKeys(List<DeletedContent.DeletedContentFilter> list) {
246  list.addAll(Arrays.asList(DeletedContent.DeletedContentFilter.values()));
247  return true;
248  }
249 
250  @Override
252  return new DeletedContentNode(skCase, key, notifier);
253  }
254 
256 
258 
259  // Use version that has observer for updates
260  @Deprecated
262  super(Children.create(new DeletedContentChildren(filter, skCase, null), true), Lookups.singleton(filter.getDisplayName()));
263  this.filter = filter;
264  init();
265  }
266 
267  DeletedContentNode(SleuthkitCase skCase, DeletedContent.DeletedContentFilter filter, Observable o) {
268  super(Children.create(new DeletedContentChildren(filter, skCase, o), true), Lookups.singleton(filter.getDisplayName()));
269  this.filter = filter;
270  init();
271  o.addObserver(new DeletedContentNodeObserver());
272  }
273 
274  private void init() {
275  super.setName(filter.getName());
276 
277  String tooltip = filter.getDisplayName();
278  this.setShortDescription(tooltip);
279  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/file-icon-deleted.png"); //NON-NLS
281  }
282 
283  // update the display name when new events are fired
284  private class DeletedContentNodeObserver implements Observer {
285 
286  @Override
287  public void update(Observable o, Object arg) {
289  }
290  }
291 
292  private void updateDisplayName() {
293  //get count of children without preloading all children nodes
294  final long count = DeletedContentChildren.calculateItems(skCase, filter);
295  //final long count = getChildren().getNodesCount(true);
296  super.setDisplayName(filter.getDisplayName() + " (" + count + ")");
297  }
298 
299  @Override
300  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
301  return v.visit(this);
302  }
303 
304  @Override
305  protected Sheet createSheet() {
306  Sheet s = super.createSheet();
307  Sheet.Set ss = s.get(Sheet.PROPERTIES);
308  if (ss == null) {
309  ss = Sheet.createPropertiesSet();
310  s.put(ss);
311  }
312 
313  ss.put(new NodeProperty<>(
314  NbBundle.getMessage(this.getClass(), "DeletedContent.createSheet.filterType.name"),
315  NbBundle.getMessage(this.getClass(), "DeletedContent.createSheet.filterType.displayName"),
316  NbBundle.getMessage(this.getClass(), "DeletedContent.createSheet.filterType.desc"),
317  filter.getDisplayName()));
318 
319  return s;
320  }
321 
322  @Override
323  public boolean isLeafTypeNode() {
324  return true;
325  }
326 
327  @Override
328  public String getItemType() {
333  return DisplayableItemNode.FILE_PARENT_NODE_KEY;
334  }
335  }
336 
337  static class DeletedContentChildren extends ChildFactory.Detachable<AbstractFile> {
338 
339  private final SleuthkitCase skCase;
340  private final DeletedContent.DeletedContentFilter filter;
341  private static final Logger logger = Logger.getLogger(DeletedContentChildren.class.getName());
342  private static final int MAX_OBJECTS = 10001;
343  private final Observable notifier;
344 
345  DeletedContentChildren(DeletedContent.DeletedContentFilter filter, SleuthkitCase skCase, Observable o) {
346  this.skCase = skCase;
347  this.filter = filter;
348  this.notifier = o;
349  }
350 
351  private final Observer observer = new DeletedContentChildrenObserver();
352 
353  // Cause refresh of children if there are changes
354  private class DeletedContentChildrenObserver implements Observer {
355 
356  @Override
357  public void update(Observable o, Object arg) {
358  refresh(true);
359  }
360  }
361 
362  @Override
363  protected void addNotify() {
364  if (notifier != null) {
365  notifier.addObserver(observer);
366  }
367  }
368 
369  @Override
370  protected void removeNotify() {
371  if (notifier != null) {
372  notifier.deleteObserver(observer);
373  }
374  }
375 
376  @Override
377  protected boolean createKeys(List<AbstractFile> list) {
378  List<AbstractFile> queryList = runFsQuery();
379  if (queryList.size() == MAX_OBJECTS) {
380  queryList.remove(queryList.size() - 1);
381  // only show the dialog once - not each time we refresh
382  if (maxFilesDialogShown == false) {
383  maxFilesDialogShown = true;
384  SwingUtilities.invokeLater(new Runnable() {
385  @Override
386  public void run() {
387  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), NbBundle.getMessage(this.getClass(),
388  "DeletedContent.createKeys.maxObjects.msg",
389  MAX_OBJECTS - 1));
390  }
391  });
392  }
393  }
394  list.addAll(queryList);
395  return true;
396  }
397 
398  static private String makeQuery(DeletedContent.DeletedContentFilter filter) {
399  String query = "";
400  switch (filter) {
401  case FS_DELETED_FILTER:
402  query = "dir_flags = " + TskData.TSK_FS_NAME_FLAG_ENUM.UNALLOC.getValue() //NON-NLS
403  + " AND meta_flags != " + TskData.TSK_FS_META_FLAG_ENUM.ORPHAN.getValue() //NON-NLS
404  + " AND type = " + TskData.TSK_DB_FILES_TYPE_ENUM.FS.getFileType(); //NON-NLS
405 
406  break;
407  case ALL_DELETED_FILTER:
408  query = " ( "
409  + "( "
410  + "(dir_flags = " + TskData.TSK_FS_NAME_FLAG_ENUM.UNALLOC.getValue() //NON-NLS
411  + " OR " //NON-NLS
412  + "meta_flags = " + TskData.TSK_FS_META_FLAG_ENUM.ORPHAN.getValue() //NON-NLS
413  + ")"
414  + " AND type = " + TskData.TSK_DB_FILES_TYPE_ENUM.FS.getFileType() //NON-NLS
415  + " )"
416  + " OR type = " + TskData.TSK_DB_FILES_TYPE_ENUM.CARVED.getFileType() //NON-NLS
417  + " )";
418  //+ " AND type != " + TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS.getFileType()
419  //+ " AND type != " + TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS.getFileType()
420  //+ " AND type != " + TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS.getFileType()
421  //+ " AND type != " + TskData.TSK_DB_FILES_TYPE_ENUM.DERIVED.getFileType()
422  //+ " AND type != " + TskData.TSK_DB_FILES_TYPE_ENUM.LOCAL.getFileType()
423  //+ " AND type != " + TskData.TSK_DB_FILES_TYPE_ENUM.VIRTUAL_DIR.getFileType();
424  break;
425 
426  default:
427  logger.log(Level.SEVERE, "Unsupported filter type to get deleted content: {0}", filter); //NON-NLS
428 
429  }
430 
431  query += " LIMIT " + MAX_OBJECTS; //NON-NLS
432  return query;
433  }
434 
435  private List<AbstractFile> runFsQuery() {
436  List<AbstractFile> ret = new ArrayList<>();
437 
438  String query = makeQuery(filter);
439  try {
440  ret = skCase.findAllFilesWhere(query);
441  } catch (TskCoreException e) {
442  logger.log(Level.SEVERE, "Error getting files for the deleted content view using: " + query, e); //NON-NLS
443  }
444 
445  return ret;
446 
447  }
448 
454  static long calculateItems(SleuthkitCase sleuthkitCase, DeletedContent.DeletedContentFilter filter) {
455  try {
456  return sleuthkitCase.countFilesWhere(makeQuery(filter));
457  } catch (TskCoreException ex) {
458  logger.log(Level.SEVERE, "Error getting deleted files search view count", ex); //NON-NLS
459  return 0;
460  }
461  }
462 
463  @Override
464  protected Node createNodeForKey(AbstractFile key) {
465  return key.accept(new ContentVisitor.Default<AbstractNode>() {
466  public FileNode visit(AbstractFile f) {
467  return new FileNode(f, false);
468  }
469 
470  public FileNode visit(FsContent f) {
471  return new FileNode(f, false);
472  }
473 
474  @Override
475  public FileNode visit(LayoutFile f) {
476  return new FileNode(f, false);
477  }
478 
479  @Override
480  public FileNode visit(File f) {
481  return new FileNode(f, false);
482  }
483 
484  @Override
485  public FileNode visit(Directory f) {
486  return new FileNode(f, false);
487  }
488 
489  @Override
490  protected AbstractNode defaultVisit(Content di) {
491  throw new UnsupportedOperationException(NbBundle.getMessage(this.getClass(),
492  "DeletedContent.createNodeForKey.typeNotSupported.msg",
493  di.toString()));
494  }
495  });
496  }
497  }
498  }
499 }
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
static void removePropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:369
void removeIngestJobEventListener(final PropertyChangeListener listener)
boolean createKeys(List< DeletedContent.DeletedContentFilter > list)
void addIngestJobEventListener(final PropertyChangeListener listener)
static void addPropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:357
void addIngestModuleEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
Node createNodeForKey(DeletedContent.DeletedContentFilter key)

Copyright © 2012-2016 Basis Technology. Generated on: Tue Jun 13 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.