Autopsy  4.0
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 2011-2016 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;
35 import org.sleuthkit.datamodel.AbstractFile;
36 import org.sleuthkit.datamodel.Content;
37 import org.sleuthkit.datamodel.DerivedFile;
38 import org.sleuthkit.datamodel.LayoutFile;
39 import org.sleuthkit.datamodel.LocalFile;
40 import org.sleuthkit.datamodel.SleuthkitCase;
41 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
42 import org.sleuthkit.datamodel.TskCoreException;
43 import org.sleuthkit.datamodel.TskFileRange;
44 import org.sleuthkit.datamodel.VirtualDirectory;
45 import org.sleuthkit.datamodel.CarvedFileContainer;
46 import org.sleuthkit.datamodel.LocalFilesDataSource;
47 import org.sleuthkit.datamodel.TskDataException;
48 
52 public class FileManager implements Closeable {
53 
54  private SleuthkitCase tskCase;
55  private static final Logger logger = Logger.getLogger(FileManager.class.getName());
56  private volatile int curNumFileSets; //current number of filesets (root virt dir objects)
57 
58  public FileManager(SleuthkitCase tskCase) {
59  this.tskCase = tskCase;
60  init();
61  }
62 
66  private synchronized void init() {
67  //get the number of local file sets in db
68  List<VirtualDirectory> virtRoots;
69  curNumFileSets = 0;
70  try {
71  virtRoots = tskCase.getVirtualDirectoryRoots();
72  for (VirtualDirectory vd : virtRoots) {
73  if (vd.getName().startsWith(VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX)) {
75  }
76  }
77  } catch (TskCoreException ex) {
78  logger.log(Level.SEVERE, "Error initializing FileManager and getting number of local file sets"); //NON-NLS
79  }
80 
81  }
82 
93  public synchronized List<AbstractFile> findFiles(String fileName) throws TskCoreException {
94  List<AbstractFile> result = new ArrayList<>();
95 
96  if (tskCase == null) {
97  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles.exception.msg"));
98  }
99  List<Content> dataSources = tskCase.getRootObjects();
100  for (Content dataSource : dataSources) {
101  result.addAll(findFiles(dataSource, fileName));
102  }
103  return result;
104  }
105 
119  public synchronized List<AbstractFile> findFiles(String fileName, String dirName) throws TskCoreException {
120  List<AbstractFile> result = new ArrayList<>();
121 
122  if (tskCase == null) {
123  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles2.exception.msg"));
124  }
125  List<Content> dataSources = tskCase.getRootObjects();
126  for (Content dataSource : dataSources) {
127  result.addAll(findFiles(dataSource, fileName, dirName));
128  }
129  return result;
130  }
131 
144  public synchronized List<AbstractFile> findFiles(String fileName, AbstractFile parentFile) throws TskCoreException {
145  List<AbstractFile> result = new ArrayList<>();
146 
147  if (tskCase == null) {
148  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles3.exception.msg"));
149  }
150  List<Content> dataSources = tskCase.getRootObjects();
151  for (Content dataSource : dataSources) {
152  result.addAll(findFiles(dataSource, fileName, parentFile));
153  }
154  return result;
155  }
156 
168  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName) throws TskCoreException {
169  if (tskCase == null) {
170  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles.exception.msg"));
171  }
172  return tskCase.findFiles(dataSource, fileName);
173  }
174 
189  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName, String dirName) throws TskCoreException {
190  if (tskCase == null) {
191  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles2.exception.msg"));
192  }
193  return tskCase.findFiles(dataSource, fileName, dirName);
194  }
195 
209  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName, AbstractFile parentFile) throws TskCoreException {
210  if (tskCase == null) {
211  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.findFiles3.exception.msg"));
212  }
213  return findFiles(dataSource, fileName, parentFile.getName());
214  }
215 
224  public synchronized List<AbstractFile> openFiles(Content dataSource, String filePath) throws TskCoreException {
225  if (tskCase == null) {
226  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.openFiles.exception.msg"));
227  }
228  return tskCase.openFiles(dataSource, filePath);
229  }
230 
261  public synchronized DerivedFile addDerivedFile(String fileName, String localPath, long size,
262  long ctime, long crtime, long atime, long mtime,
263  boolean isFile, AbstractFile parentFile,
264  String rederiveDetails, String toolName, String toolVersion, String otherDetails) throws TskCoreException {
265 
266  if (tskCase == null) {
267  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addDerivedFile.exception.msg"));
268  }
269 
270  return tskCase.addDerivedFile(fileName, localPath, size,
271  ctime, crtime, atime, mtime,
272  isFile, parentFile, rederiveDetails, toolName, toolVersion, otherDetails);
273  }
274 
289  public synchronized LayoutFile addCarvedFile(String carvedFileName, long carvedFileSize,
290  long systemId, List<TskFileRange> sectors) throws TskCoreException {
291 
292  if (tskCase == null) {
293  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addCarvedFile.exception.msg"));
294  }
295 
296  return tskCase.addCarvedFile(carvedFileName, carvedFileSize, systemId, sectors);
297  }
298 
312  public List<LayoutFile> addCarvedFiles(List<CarvedFileContainer> filesToAdd) throws TskCoreException {
313  if (tskCase == null) {
314  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addCarvedFile.exception.msg"));
315  } else {
316  return tskCase.addCarvedFiles(filesToAdd);
317  }
318  }
319 
325  public interface FileAddProgressUpdater {
326 
332  public void fileAdded(AbstractFile newFile);
333  }
334 
353  public synchronized VirtualDirectory addLocalFilesDirs(List<String> localAbsPaths, FileAddProgressUpdater addProgressUpdater) throws TskCoreException {
354  List<java.io.File> rootsToAdd;
355  try {
356  rootsToAdd = getFilesAndDirectories(localAbsPaths);
357  } catch (TskDataException ex) {
358  throw new TskCoreException(ex.getLocalizedMessage(), ex);
359  }
360 
361  CaseDbTransaction trans = tskCase.beginTransaction();
362  // make a virtual top-level directory for this set of localFiles/dirs
363  final VirtualDirectory fileSetRootDir = addLocalFileSetRootDir(trans);
364 
365  try {
366  // recursively add each item in the set
367  for (java.io.File localRootToAdd : rootsToAdd) {
368  AbstractFile localFileAdded = addLocalDirInt(trans, fileSetRootDir, localRootToAdd, addProgressUpdater);
369 
370  if (localFileAdded == null) {
371  String msg = NbBundle
372  .getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.cantAdd.msg",
373  localRootToAdd.getAbsolutePath());
374  logger.log(Level.SEVERE, msg);
375  throw new TskCoreException(msg);
376  } else {
377  //added.add(localFileAdded);
378  //send new content event
379  //for now reusing ingest events, in future this will be replaced by datamodel / observer sending out events
380  // @@@ Is this the right place for this? A directory tree refresh will be triggered, so this may be creating a race condition
381  // since the transaction is not yet committed.
383  }
384  }
385 
386  trans.commit();
387  } catch (TskCoreException ex) {
388  trans.rollback();
389  }
390  return fileSetRootDir;
391  }
392 
421  public synchronized LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException, TskDataException {
422  List<java.io.File> localFiles = getFilesAndDirectories(localFilePaths);
423  CaseDbTransaction trans = null;
424  try {
425  String rootDirectoryName = rootVirtualDirectoryName;
426  int newLocalFilesSetCount = curNumFileSets + 1;
427  if (rootVirtualDirectoryName.isEmpty()) {
428  rootDirectoryName = VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX + newLocalFilesSetCount;
429  }
430  trans = tskCase.beginTransaction();
431  LocalFilesDataSource dataSource = tskCase.addLocalFilesDataSource(deviceId, rootDirectoryName, timeZone, trans);
432  VirtualDirectory rootDirectory = dataSource.getRootDirectory();
433  List<AbstractFile> filesAdded = new ArrayList<>();
434  for (java.io.File localFile : localFiles) {
435  AbstractFile fileAdded = addLocalDirInt(trans, rootDirectory, localFile, progressUpdater);
436  if (null != fileAdded) {
437  filesAdded.add(fileAdded);
438  } else {
439  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
440  }
441  }
442  trans.commit();
443  if (rootVirtualDirectoryName.isEmpty()) {
444  curNumFileSets = newLocalFilesSetCount;
445  }
446  for (AbstractFile fileAdded : filesAdded) {
448  }
449  return dataSource;
450  } catch (TskCoreException ex) {
451  if (null != trans) {
452  trans.rollback();
453  }
454  throw ex;
455  }
456  }
457 
470  private List<java.io.File> getFilesAndDirectories(List<String> localFilePaths) throws TskDataException {
471  List<java.io.File> localFiles = new ArrayList<>();
472  for (String path : localFilePaths) {
473  java.io.File localFile = new java.io.File(path);
474  if (!localFile.exists() || !localFile.canRead()) {
475  throw new TskDataException(NbBundle.getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.notReadable.msg", localFile.getAbsolutePath()));
476  }
477  localFiles.add(localFile);
478  }
479  return localFiles;
480  }
481 
490  private VirtualDirectory addLocalFileSetRootDir(CaseDbTransaction trans) throws TskCoreException {
491 
492  VirtualDirectory created = null;
493 
494  int newFileSetCount = curNumFileSets + 1;
495  final String fileSetName = VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX + newFileSetCount;
496 
497  try {
498  created = tskCase.addVirtualDirectory(0, fileSetName, trans);
499  curNumFileSets = newFileSetCount;
500  } catch (TskCoreException ex) {
501  String msg = NbBundle
502  .getMessage(this.getClass(), "FileManager.addLocalFileSetRootDir.exception.errCreateDir.msg",
503  fileSetName);
504  logger.log(Level.SEVERE, msg, ex);
505  throw new TskCoreException(msg, ex);
506  }
507 
508  return created;
509  }
510 
525  private AbstractFile addLocalDirInt(CaseDbTransaction trans, VirtualDirectory parentVd,
526  java.io.File localFile, FileAddProgressUpdater addProgressUpdater) throws TskCoreException {
527 
528  if (tskCase == null) {
529  throw new TskCoreException(
530  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt.exception.closed.msg"));
531  }
532 
533  //final String localName = localDir.getName();
534  if (!localFile.exists()) {
535  throw new TskCoreException(
536  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt.exception.doesntExist.msg",
537  localFile.getAbsolutePath()));
538  }
539  if (!localFile.canRead()) {
540  throw new TskCoreException(
541  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt.exception.notReadable.msg",
542  localFile.getAbsolutePath()));
543  }
544 
545  if (localFile.isDirectory()) {
546  //create virtual folder (we don't have a notion of a 'local folder')
547  final VirtualDirectory childVd = tskCase.addVirtualDirectory(parentVd.getId(), localFile.getName(), trans);
548  if (childVd != null && addProgressUpdater != null) {
549  addProgressUpdater.fileAdded(childVd);
550  }
551  //add children recursively
552  final java.io.File[] childrenFiles = localFile.listFiles();
553  if (childrenFiles != null) {
554  for (java.io.File childFile : childrenFiles) {
555  addLocalDirInt(trans, childVd, childFile, addProgressUpdater);
556  }
557  }
558  return childVd;
559  } else {
560  //add leaf file, base case
561  return this.addLocalFileInt(parentVd, localFile, trans);
562  }
563  }
564 
581  private synchronized LocalFile addLocalFileInt(AbstractFile parentFile, java.io.File localFile, CaseDbTransaction trans) throws TskCoreException {
582 
583  if (tskCase == null) {
584  throw new TskCoreException(
585  NbBundle.getMessage(this.getClass(), "FileManager.addLocalDirInt2.exception.closed.msg"));
586  }
587 
588  long size = localFile.length();
589  boolean isFile = localFile.isFile();
590 
591  long ctime = 0;
592  long crtime = 0;
593  long atime = 0;
594  long mtime = 0;
595 
596  String fileName = localFile.getName();
597 
598  LocalFile lf = tskCase.addLocalFile(fileName, localFile.getAbsolutePath(), size,
599  ctime, crtime, atime, mtime,
600  isFile, parentFile, trans);
601 
602  return lf;
603  }
604 
605  @Override
606  public synchronized void close() throws IOException {
607  tskCase = null;
608  }
609 }
synchronized List< AbstractFile > findFiles(String fileName, AbstractFile parentFile)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, AbstractFile parentFile)
VirtualDirectory addLocalFileSetRootDir(CaseDbTransaction trans)
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)
synchronized LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, String dirName)
void fireModuleContentEvent(ModuleContentEvent moduleContentEvent)
synchronized List< AbstractFile > findFiles(String fileName)
synchronized LayoutFile addCarvedFile(String carvedFileName, long carvedFileSize, long systemId, List< TskFileRange > sectors)
synchronized static Logger getLogger(String name)
Definition: Logger.java:166
List< java.io.File > getFilesAndDirectories(List< String > localFilePaths)
synchronized List< AbstractFile > findFiles(String fileName, String dirName)
static synchronized IngestServices getInstance()

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.