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

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