Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileManager.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2012 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.sleuthkit.autopsy.casemodule.services;
24 
25 import java.io.Closeable;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.logging.Level;
30 import org.openide.util.NbBundle;
46 
50 public class FileManager implements Closeable {
51 
53  private static final Logger logger = Logger.getLogger(FileManager.class.getName());
54  private volatile int curNumFileSets; //current number of filesets (root virt dir objects)
55 
56  public FileManager(SleuthkitCase tskCase) {
57  this.tskCase = tskCase;
58  init();
59  }
60 
64  private synchronized void init() {
65  //get the number of local file sets in db
66  List<VirtualDirectory> virtRoots;
67  curNumFileSets = 0;
68  try {
69  virtRoots = tskCase.getVirtualDirectoryRoots();
70  for (VirtualDirectory vd : virtRoots) {
71  if (vd.getName().startsWith(VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX)) {
73  }
74  }
75  } catch (TskCoreException ex) {
76  logger.log(Level.SEVERE, "Error initializing FileManager and getting number of local file sets"); //NON-NLS
77  }
78 
79 
80  }
81 
93  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName) throws TskCoreException {
94  if (tskCase == null) {
95  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles.exception.msg"));
96  }
97  return tskCase.findFiles(dataSource, fileName);
98  }
99 
114  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName, String dirName) throws TskCoreException {
115  if (tskCase == null) {
116  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles2.exception.msg"));
117  }
118  return tskCase.findFiles(dataSource, fileName, dirName);
119  }
120 
134  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName, AbstractFile parentFile) throws TskCoreException {
135  if (tskCase == null) {
136  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles3.exception.msg"));
137  }
138  return findFiles(dataSource, fileName, parentFile.getName());
139  }
140 
149  public synchronized List<AbstractFile> openFiles(Content dataSource, String filePath) throws TskCoreException {
150  if (tskCase == null) {
151  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.openFiles.exception.msg"));
152  }
153  return tskCase.openFiles(dataSource, filePath);
154  }
155 
186  public synchronized DerivedFile addDerivedFile(String fileName, String localPath, long size,
187  long ctime, long crtime, long atime, long mtime,
188  boolean isFile, AbstractFile parentFile,
189  String rederiveDetails, String toolName, String toolVersion, String otherDetails) throws TskCoreException {
190 
191  if (tskCase == null) {
192  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addDerivedFile.exception.msg"));
193  }
194 
195  return tskCase.addDerivedFile(fileName, localPath, size,
196  ctime, crtime, atime, mtime,
197  isFile, parentFile, rederiveDetails, toolName, toolVersion, otherDetails);
198  }
199 
213  public synchronized LayoutFile addCarvedFile(String carvedFileName, long carvedFileSize,
214  long systemId, List<TskFileRange> sectors) throws TskCoreException {
215 
216  if (tskCase == null) {
217  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addCarvedFile.exception.msg"));
218  }
219 
220  return tskCase.addCarvedFile(carvedFileName, carvedFileSize, systemId, sectors);
221  }
222 
231  public List<LayoutFile> addCarvedFiles(List<CarvedFileContainer> filesToAdd) throws TskCoreException {
232  if (tskCase == null) {
233  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addCarvedFile.exception.msg"));
234  }
235  else {
236  return tskCase.addCarvedFiles(filesToAdd);
237  }
238  }
239 
245  public interface FileAddProgressUpdater {
246 
252  public void fileAdded(AbstractFile newFile);
253  }
254 
272  public synchronized VirtualDirectory addLocalFilesDirs(List<String> localAbsPaths, FileAddProgressUpdater addProgressUpdater) throws TskCoreException {
273  final List<java.io.File> rootsToAdd = new ArrayList<>();
274  //first validate all the inputs before any additions
275  for (String absPath : localAbsPaths) {
276  java.io.File localFile = new java.io.File(absPath);
277  if (!localFile.exists() || !localFile.canRead()) {
278  String msg = NbBundle
279  .getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.notReadable.msg",
280  localFile.getAbsolutePath());
281  logger.log(Level.SEVERE, msg);
282  throw new TskCoreException(msg);
283  }
284  rootsToAdd.add(localFile);
285  }
286 
287  CaseDbTransaction trans = tskCase.beginTransaction();
288  // make a virtual top-level directory for this set of files/dirs
289  final VirtualDirectory fileSetRootDir = addLocalFileSetRootDir(trans);
290 
291  try {
292  // recursively add each item in the set
293  for (java.io.File localRootToAdd : rootsToAdd) {
294  AbstractFile localFileAdded = addLocalDirInt(trans, fileSetRootDir, localRootToAdd, addProgressUpdater);
295 
296  if (localFileAdded == null) {
297  String msg = NbBundle
298  .getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.cantAdd.msg",
299  localRootToAdd.getAbsolutePath());
300  logger.log(Level.SEVERE, msg);
301  throw new TskCoreException(msg);
302  } else {
303  //added.add(localFileAdded);
304  //send new content event
305  //for now reusing ingest events, in future this will be replaced by datamodel / observer sending out events
306  // @@@ Is this the right place for this? A directory tree refresh will be triggered, so this may be creating a race condition
307  // since the transaction is not yet committed.
309  }
310  }
311 
312  trans.commit();
313  } catch (TskCoreException ex) {
314  trans.rollback();
315  }
316  return fileSetRootDir;
317  }
318 
328 
329  VirtualDirectory created = null;
330 
331  int newFileSetCount = curNumFileSets + 1;
332  final String fileSetName = VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX + newFileSetCount;
333 
334  try {
335  created = tskCase.addVirtualDirectory(0, fileSetName, trans);
336  curNumFileSets = newFileSetCount;
337  } catch (TskCoreException ex) {
338  String msg = NbBundle
339  .getMessage(this.getClass(), "FileManager.addLocalFileSetRootDir.exception.errCreateDir.msg",
340  fileSetName);
341  logger.log(Level.SEVERE, msg, ex);
342  throw new TskCoreException(msg, ex);
343  }
344 
345  return created;
346  }
347 
362  java.io.File localFile, FileAddProgressUpdater addProgressUpdater) throws TskCoreException {
363 
364  if (tskCase == null) {
365  throw new TskCoreException(
366  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt.exception.closed.msg"));
367  }
368 
369  //final String localName = localDir.getName();
370  if (!localFile.exists()) {
371  throw new TskCoreException(
372  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt.exception.doesntExist.msg",
373  localFile.getAbsolutePath()));
374  }
375  if (!localFile.canRead()) {
376  throw new TskCoreException(
377  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt.exception.notReadable.msg",
378  localFile.getAbsolutePath()));
379  }
380 
381 
382  if (localFile.isDirectory()) {
383  //create virtual folder (we don't have a notion of a 'local folder')
384  final VirtualDirectory childVd = tskCase.addVirtualDirectory(parentVd.getId(), localFile.getName(), trans);
385  if (childVd != null && addProgressUpdater != null) {
386  addProgressUpdater.fileAdded(childVd);
387  }
388  //add children recursively
389  final java.io.File[] childrenFiles = localFile.listFiles();
390  if (childrenFiles != null) {
391  for (java.io.File childFile : childrenFiles) {
392  addLocalDirInt(trans, childVd, childFile, addProgressUpdater);
393  }
394  }
395  return childVd;
396  } else {
397  //add leaf file, base case
398  return this.addLocalFileInt(parentVd, localFile, trans);
399  }
400  }
401 
417  private synchronized LocalFile addLocalFileInt(AbstractFile parentFile, java.io.File localFile, CaseDbTransaction trans) throws TskCoreException {
418 
419  if (tskCase == null) {
420  throw new TskCoreException(
421  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt2.exception.closed.msg"));
422  }
423 
424  long size = localFile.length();
425  boolean isFile = localFile.isFile();
426 
427  long ctime = 0;
428  long crtime = 0;
429  long atime = 0;
430  long mtime = 0;
431 
432  String fileName = localFile.getName();
433 
434  LocalFile lf = tskCase.addLocalFile(fileName, localFile.getAbsolutePath(), size,
435  ctime, crtime, atime, mtime,
436  isFile, parentFile, trans);
437 
438  return lf;
439  }
440 
441  @Override
442  public synchronized void close() throws IOException {
443  tskCase = null;
444  }
445 }
DerivedFile addDerivedFile(String fileName, String localPath, long size, long ctime, long crtime, long atime, long mtime, boolean isFile, AbstractFile parentFile, String rederiveDetails, String toolName, String toolVersion, String otherDetails)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, AbstractFile parentFile)
LocalFile addLocalFile(String fileName, String localPath, long size, long ctime, long crtime, long atime, long mtime, boolean isFile, AbstractFile parent)
VirtualDirectory addLocalFileSetRootDir(CaseDbTransaction trans)
List< AbstractFile > openFiles(Content dataSource, String filePath)
List< VirtualDirectory > getVirtualDirectoryRoots()
synchronized VirtualDirectory addLocalFilesDirs(List< String > localAbsPaths, FileAddProgressUpdater addProgressUpdater)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName)
List< LayoutFile > addCarvedFiles(List< CarvedFileContainer > filesToAdd)
synchronized DerivedFile addDerivedFile(String fileName, String localPath, long size, long ctime, long crtime, long atime, long mtime, boolean isFile, AbstractFile parentFile, String rederiveDetails, String toolName, String toolVersion, String otherDetails)
AbstractFile addLocalDirInt(CaseDbTransaction trans, VirtualDirectory parentVd, java.io.File localFile, FileAddProgressUpdater addProgressUpdater)
synchronized LocalFile addLocalFileInt(AbstractFile parentFile, java.io.File localFile, CaseDbTransaction trans)
synchronized List< AbstractFile > openFiles(Content dataSource, String filePath)
List< AbstractFile > findFiles(Content dataSource, String fileName)
List< LayoutFile > addCarvedFiles(List< CarvedFileContainer > filesToAdd)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, String dirName)
LayoutFile addCarvedFile(String carvedFileName, long carvedFileSize, long containerId, List< TskFileRange > data)
void fireModuleContentEvent(ModuleContentEvent moduleContentEvent)
synchronized LayoutFile addCarvedFile(String carvedFileName, long carvedFileSize, long systemId, List< TskFileRange > sectors)
VirtualDirectory addVirtualDirectory(long parentId, String directoryName)
static Logger getLogger(String name)
Definition: Logger.java:131
static synchronized IngestServices getInstance()

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.