Autopsy  4.6.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DirectoryTreeFilterChildren.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 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.directorytree;
20 
21 import java.util.List;
22 import java.util.logging.Level;
24 import org.openide.nodes.Children;
26 import org.openide.nodes.FilterNode;
27 import org.openide.nodes.Node;
41 import org.sleuthkit.datamodel.AbstractFile;
42 import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
43 import org.sleuthkit.datamodel.Content;
44 import org.sleuthkit.datamodel.Directory;
45 import org.sleuthkit.datamodel.LayoutFile;
46 import org.sleuthkit.datamodel.TskException;
47 import org.sleuthkit.datamodel.VirtualDirectory;
48 import org.sleuthkit.datamodel.Volume;
49 
55 class DirectoryTreeFilterChildren extends FilterNode.Children {
56 
57  private final ShowItemVisitor showItemV = new ShowItemVisitor();
58  private final IsLeafItemVisitor isLeafItemV = new IsLeafItemVisitor();
59  private final static Logger logger = Logger.getLogger(DirectoryTreeFilterChildren.class.getName());
60 
64  public DirectoryTreeFilterChildren(Node arg) {
65  super(arg);
66  }
67 
68  @Override
69  protected Node copyNode(Node arg0) {
70  return new DirectoryTreeFilterNode(arg0, true);
71  }
72 
73  protected Node copyNode(Node arg0, boolean createChildren) {
74  return new DirectoryTreeFilterNode(arg0, createChildren);
75  }
76 
77  /*
78  * This method takes in a node as an argument and will create a new one if
79  * it should be displayed in the tree. If it is to be displayed, it also
80  * figures out if it is a leaf or not (i.e. should it have a + sign in the
81  * tree).
82  *
83  * It does NOT create children nodes
84  */
85  @Override
86  protected Node[] createNodes(Node origNode) {
87  if (origNode == null || !(origNode instanceof DisplayableItemNode)) {
88  return new Node[]{};
89  }
90 
91  // Shoudl this node be displayed in the tree or not
92  final DisplayableItemNode diNode = (DisplayableItemNode) origNode;
93  if (diNode.accept(showItemV) == false) {
94  //do not show
95  return new Node[]{};
96  }
97 
98  // If it is going to be displayed, then determine if it should
99  // have a '+' next to it based on if it has children of itself.
100  // We will filter out the "." and ".." directories
101  final boolean isLeaf = diNode.accept(isLeafItemV);
102 
103  return new Node[]{this.copyNode(origNode, !isLeaf)};
104  }
105 
114  private static boolean isLeafDirectory(DirectoryNode node) {
115  Directory dir = node.getLookup().lookup(Directory.class);
116  boolean ret = true;
117  try {
118  for (Content c : dir.getChildren()) {
119  if (c instanceof Directory && (!((Directory) c).getName().equals(".")
120  && !((Directory) c).getName().equals(".."))) {
121  ret = false;
122  break;
123  } else if(AbstractContentNode.contentHasVisibleContentChildren(c)){
124  //fie has children, such as derived files
125  ret = false;
126  break;
127  }
128  }
129  } catch (TskException ex) {
130  Logger.getLogger(DirectoryTreeFilterChildren.class.getName())
131  .log(Level.WARNING, "Error getting directory children", ex); //NON-NLS
132  return false;
133  }
134  return ret;
135  }
136 
137  private static boolean isLeafVolume(VolumeNode node) {
138  Volume vol = node.getLookup().lookup(Volume.class);
139  boolean ret = true;
140 
141  try {
142  for (Content c : vol.getChildren()) {
143  if (!(c instanceof LayoutFile
144  || c instanceof VirtualDirectory)) {
145  ret = false;
146  break;
147  }
148  }
149 
150  } catch (TskException ex) {
151  Logger.getLogger(DirectoryTreeFilterChildren.class.getName())
152  .log(Level.WARNING, "Error getting volume children", ex); //NON-NLS
153  return false;
154  }
155  return ret;
156  }
157 
161  private static boolean isDotDirectory(DirectoryNode dir) {
162  String name = dir.getDisplayName();
163  return name.equals(DirectoryNode.DOTDIR) || name.equals(DirectoryNode.DOTDOTDIR);
164  }
165 
175  public static Children createInstance(Node arg, boolean createChildren) {
176  if (createChildren) {
177  return new DirectoryTreeFilterChildren(arg);
178  } else {
179  return Children.LEAF;
180  }
181  }
182 
183  private static class IsLeafItemVisitor extends DisplayableItemNodeVisitor.Default<Boolean> {
184 
185  @Override
186  protected Boolean defaultVisit(DisplayableItemNode c) {
187  return c.isLeafTypeNode();
188  }
189 
190  @Override
191  public Boolean visit(DirectoryNode dn) {
192  return isLeafDirectory(dn);
193  }
194 
196  //is a leaf if has no children, or children are files not dirs
197  boolean hasChildren = node.hasContentChildren();
198  if (!hasChildren) {
199  return true;
200  }
201  List<Content> derivedChildren = node.getContentChildren();
202  //child of a file, must be a (derived) file too
203  for (Content childContent : derivedChildren) {
204  if ((childContent instanceof AbstractFile) && ((AbstractFile) childContent).isDir()) {
205  return false;
206  } else {
208  return false;
209  }
210  }
211  }
212  return true;
213  }
214 
215  @Override
216  public Boolean visit(FileNode fn) {
217  return visitDeep(fn);
218  }
219 
220  @Override
221  public Boolean visit(LocalFileNode lfn) {
222  return visitDeep(lfn);
223  }
224 
225  @Override
226  public Boolean visit(LayoutFileNode fn) {
227  return visitDeep(fn);
228  }
229 
230  @Override
231  public Boolean visit(SlackFileNode sfn) {
232  return visitDeep(sfn);
233  }
234 
235  @Override
236  public Boolean visit(VolumeNode vn) {
237  return isLeafVolume(vn);
238  }
239 
240  @Override
241  public Boolean visit(VirtualDirectoryNode vdn) {
242  return visitDeep(vdn);
243  }
244 
245  @Override
246  public Boolean visit(LocalDirectoryNode ldn) {
247  return visitDeep(ldn);
248  }
249 
250  @Override
251  public Boolean visit(FileTypesNode ft) {
252  return defaultVisit(ft);
253  }
254 
255  @Override
256  public Boolean visit(BlackboardArtifactNode bbafn) {
257  // Only show Message arttifacts with children
258  if ( (bbafn.getArtifact().getArtifactTypeID() == ARTIFACT_TYPE.TSK_EMAIL_MSG.getTypeID()) ||
259  (bbafn.getArtifact().getArtifactTypeID() == ARTIFACT_TYPE.TSK_MESSAGE.getTypeID()) ) {
260  return bbafn.hasContentChildren();
261  }
262 
263  return false;
264  }
265  }
266 
267  private static class ShowItemVisitor extends DisplayableItemNodeVisitor.Default<Boolean> {
268 
269  @Override
270  protected Boolean defaultVisit(DisplayableItemNode c) {
271  return true;
272  }
273 
274  @Override
275  public Boolean visit(DirectoryNode dn) {
276  if (isDotDirectory(dn)) {
277  return false;
278  }
279  return true;
280  }
281 
282  @Override
283  public Boolean visit(FileNode fn) {
284  return fn.hasVisibleContentChildren();
285  }
286 
287  @Override
288  public Boolean visit(LocalFileNode lfn) {
289  return lfn.hasVisibleContentChildren();
290  }
291 
292  @Override
293  public Boolean visit(LayoutFileNode ln) {
294  return ln.hasVisibleContentChildren();
295  }
296 
297  @Override
298  public Boolean visit(SlackFileNode sfn) {
299  return sfn.hasVisibleContentChildren();
300  }
301 
302  @Override
303  public Boolean visit(VirtualDirectoryNode vdn) {
304  return true;
305  //return vdn.hasContentChildren();
306  }
307 
308 
309  @Override
310  public Boolean visit(LocalDirectoryNode ldn) {
311  return true;
312  }
313 
314  @Override
315  public Boolean visit(FileTypesNode fileTypes) {
316  return defaultVisit(fileTypes);
317  }
318 
319  @Override
320  public Boolean visit(BlackboardArtifactNode bbafn) {
321 
322  // Only show Message arttifacts with children
323  if ( (bbafn.getArtifact().getArtifactTypeID() == ARTIFACT_TYPE.TSK_EMAIL_MSG.getTypeID()) ||
324  (bbafn.getArtifact().getArtifactTypeID() == ARTIFACT_TYPE.TSK_MESSAGE.getTypeID()) ) {
325  return bbafn.hasContentChildren();
326  }
327 
328  return false;
329  }
330 
331  }
332 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.