Autopsy  4.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;
36 import org.sleuthkit.datamodel.AbstractFile;
37 import org.sleuthkit.datamodel.Content;
38 import org.sleuthkit.datamodel.Directory;
39 import org.sleuthkit.datamodel.LayoutFile;
40 import org.sleuthkit.datamodel.TskCoreException;
41 import org.sleuthkit.datamodel.TskException;
42 import org.sleuthkit.datamodel.VirtualDirectory;
43 import org.sleuthkit.datamodel.Volume;
44 
50 class DirectoryTreeFilterChildren extends FilterNode.Children {
51 
52  private final ShowItemVisitor showItemV = new ShowItemVisitor();
53  private final IsLeafItemVisitor isLeafItemV = new IsLeafItemVisitor();
54  private final static Logger logger = Logger.getLogger(DirectoryTreeFilterChildren.class.getName());
55 
59  public DirectoryTreeFilterChildren(Node arg) {
60  super(arg);
61  }
62 
63  @Override
64  protected Node copyNode(Node arg0) {
65  return new DirectoryTreeFilterNode(arg0, true);
66  }
67 
68  protected Node copyNode(Node arg0, boolean createChildren) {
69  return new DirectoryTreeFilterNode(arg0, createChildren);
70  }
71 
72  /*
73  * This method takes in a node as an argument and will create a new one if
74  * it should be displayed in the tree. If it is to be displayed, it also
75  * figures out if it is a leaf or not (i.e. should it have a + sign in the
76  * tree).
77  *
78  * It does NOT create children nodes
79  */
80  @Override
81  protected Node[] createNodes(Node origNode) {
82  if (origNode == null || !(origNode instanceof DisplayableItemNode)) {
83  return new Node[]{};
84  }
85 
86  // Shoudl this node be displayed in the tree or not
87  final DisplayableItemNode diNode = (DisplayableItemNode) origNode;
88  if (diNode.accept(showItemV) == false) {
89  //do not show
90  return new Node[]{};
91  }
92 
93  // If it is going to be displayed, then determine if it should
94  // have a '+' next to it based on if it has children of itself.
95  // We will filter out the "." and ".." directories
96  final boolean isLeaf = diNode.accept(isLeafItemV);
97 
98  return new Node[]{this.copyNode(origNode, !isLeaf)};
99  }
100 
109  private static boolean isLeafDirectory(DirectoryNode node) {
110  Directory dir = node.getLookup().lookup(Directory.class);
111  boolean ret = true;
112  try {
113  for (Content c : dir.getChildren()) {
114  if (c instanceof Directory && (!((Directory) c).getName().equals(".")
115  && !((Directory) c).getName().equals(".."))) {
116  ret = false;
117  break;
118  } else if (c.hasChildren()) {
119  //fie has children, such as derived files
120  ret = false;
121  break;
122  }
123  }
124  } catch (TskException ex) {
125  Logger.getLogger(DirectoryTreeFilterChildren.class.getName())
126  .log(Level.WARNING, "Error getting directory children", ex); //NON-NLS
127  return false;
128  }
129  return ret;
130  }
131 
132  private static boolean isLeafVolume(VolumeNode node) {
133  Volume vol = node.getLookup().lookup(Volume.class);
134  boolean ret = true;
135 
136  try {
137  for (Content c : vol.getChildren()) {
138  if (!(c instanceof LayoutFile
139  || c instanceof VirtualDirectory)) {
140  ret = false;
141  break;
142  }
143  }
144 
145  } catch (TskException ex) {
146  Logger.getLogger(DirectoryTreeFilterChildren.class.getName())
147  .log(Level.WARNING, "Error getting volume children", ex); //NON-NLS
148  return false;
149  }
150  return ret;
151  }
152 
156  private static boolean isDotDirectory(DirectoryNode dir) {
157  String name = dir.getDisplayName();
158  return name.equals(DirectoryNode.DOTDIR) || name.equals(DirectoryNode.DOTDOTDIR);
159  }
160 
170  public static Children createInstance(Node arg, boolean createChildren) {
171  if (createChildren) {
172  return new DirectoryTreeFilterChildren(arg);
173  } else {
174  return Children.LEAF;
175  }
176  }
177 
178  private static class IsLeafItemVisitor extends DisplayableItemNodeVisitor.Default<Boolean> {
179 
180  @Override
181  protected Boolean defaultVisit(DisplayableItemNode c) {
182  return c.isLeafTypeNode();
183  }
184 
185  @Override
186  public Boolean visit(DirectoryNode dn) {
187  return isLeafDirectory(dn);
188  }
189 
191  //is a leaf if has no children, or children are files not dirs
192  boolean hasChildren = node.hasContentChildren();
193  if (!hasChildren) {
194  return true;
195  }
196  List<Content> derivedChildren = node.getContentChildren();
197  //child of a file, must be a (derived) file too
198  for (Content childContent : derivedChildren) {
199  if (((AbstractFile) childContent).isDir()) {
200  return false;
201  } else {
202  try {
203  if (childContent.hasChildren()) {
204  return false;
205  }
206  } catch (TskCoreException e) {
207  logger.log(Level.SEVERE, "Error checking if file node is leaf.", e); //NON-NLS
208  }
209  }
210  }
211  return true;
212  }
213 
214  @Override
215  public Boolean visit(FileNode fn) {
216  return visitDeep(fn);
217  }
218 
219  @Override
220  public Boolean visit(LocalFileNode lfn) {
221  return visitDeep(lfn);
222  }
223 
224  @Override
225  public Boolean visit(LayoutFileNode fn) {
226  return visitDeep(fn);
227  }
228 
229  @Override
230  public Boolean visit(VolumeNode vn) {
231  return isLeafVolume(vn);
232  }
233 
234  @Override
235  public Boolean visit(VirtualDirectoryNode vdn) {
236  return visitDeep(vdn);
237  //return ! vdn.hasContentChildren();
238  }
239  }
240 
241  private static class ShowItemVisitor extends DisplayableItemNodeVisitor.Default<Boolean> {
242 
243  @Override
244  protected Boolean defaultVisit(DisplayableItemNode c) {
245  return true;
246  }
247 
248  @Override
249  public Boolean visit(DirectoryNode dn) {
250  if (isDotDirectory(dn)) {
251  return false;
252  }
253  return true;
254  }
255 
256  @Override
257  public Boolean visit(FileNode fn) {
258  return fn.hasContentChildren();
259  }
260 
261  @Override
262  public Boolean visit(LocalFileNode lfn) {
263  return lfn.hasContentChildren();
264  }
265 
266  @Override
267  public Boolean visit(LayoutFileNode ln) {
268  return ln.hasContentChildren();
269  }
270 
271  @Override
272  public Boolean visit(VirtualDirectoryNode vdn) {
273  return true;
274  //return vdn.hasContentChildren();
275  }
276  }
277 }

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.