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

Copyright © 2012-2019 Basis Technology. Generated on: Tue Jan 7 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.