Autopsy  4.5.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.Collection;
29 import java.util.List;
30 import java.util.logging.Level;
31 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.LocalDirectory;
40 import org.sleuthkit.datamodel.SleuthkitCase;
41 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
42 import org.sleuthkit.datamodel.SpecialDirectory;
43 import org.sleuthkit.datamodel.TskCoreException;
44 import org.sleuthkit.datamodel.TskFileRange;
45 import org.sleuthkit.datamodel.VirtualDirectory;
46 import org.sleuthkit.datamodel.LocalFilesDataSource;
47 import org.sleuthkit.datamodel.TskDataException;
48 import org.apache.commons.lang3.StringUtils;
50 import org.sleuthkit.datamodel.CarvingResult;
51 import org.sleuthkit.datamodel.TskData;
52 
58 public class FileManager implements Closeable {
59 
60  private static final Logger LOGGER = Logger.getLogger(FileManager.class.getName());
61  private SleuthkitCase caseDb;
62 
70  public FileManager(SleuthkitCase caseDb) {
71  this.caseDb = caseDb;
72  }
73 
84  public synchronized List<AbstractFile> findFilesByMimeType(Collection<String> mimeTypes) throws TskCoreException {
85  if (null == caseDb) {
86  throw new TskCoreException("File manager has been closed");
87  }
88  return caseDb.findAllFilesWhere(createFileTypeInCondition(mimeTypes));
89  }
90 
103  public synchronized List<AbstractFile> findFilesByMimeType(Content dataSource, Collection<String> mimeTypes) throws TskCoreException {
104  if (null == caseDb) {
105  throw new TskCoreException("File manager has been closed");
106  }
107  return caseDb.findAllFilesWhere("data_source_obj_id = " + dataSource.getId() + " AND " + createFileTypeInCondition(mimeTypes));
108  }
109 
117  private static String createFileTypeInCondition(Collection<String> mimeTypes) {
118  String types = StringUtils.join(mimeTypes, "', '");
119  return "mime_type IN ('" + types + "')";
120  }
121 
134  public synchronized List<AbstractFile> findFiles(String fileName) throws TskCoreException {
135  if (null == caseDb) {
136  throw new TskCoreException("File manager has been closed");
137  }
138  List<AbstractFile> result = new ArrayList<>();
139  List<Content> dataSources = caseDb.getRootObjects();
140  for (Content dataSource : dataSources) {
141  result.addAll(findFiles(dataSource, fileName));
142  }
143  return result;
144  }
145 
160  public synchronized List<AbstractFile> findFiles(String fileName, String parentName) throws TskCoreException {
161  if (null == caseDb) {
162  throw new TskCoreException("File manager has been closed");
163  }
164  List<AbstractFile> result = new ArrayList<>();
165  List<Content> dataSources = caseDb.getRootObjects();
166  for (Content dataSource : dataSources) {
167  result.addAll(findFiles(dataSource, fileName, parentName));
168  }
169  return result;
170  }
171 
186  public synchronized List<AbstractFile> findFiles(String fileName, AbstractFile parent) throws TskCoreException {
187  if (null == caseDb) {
188  throw new TskCoreException("File manager has been closed");
189  }
190  List<AbstractFile> result = new ArrayList<>();
191  List<Content> dataSources = caseDb.getRootObjects();
192  for (Content dataSource : dataSources) {
193  result.addAll(findFiles(dataSource, fileName, parent));
194  }
195  return result;
196  }
197 
212  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName) throws TskCoreException {
213  if (null == caseDb) {
214  throw new TskCoreException("File manager has been closed");
215  }
216  return caseDb.findFiles(dataSource, fileName);
217  }
218 
235  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName, String parentName) throws TskCoreException {
236  if (null == caseDb) {
237  throw new TskCoreException("File manager has been closed");
238  }
239  return caseDb.findFiles(dataSource, fileName, parentName);
240  }
241 
258  public synchronized List<AbstractFile> findFiles(Content dataSource, String fileName, AbstractFile parent) throws TskCoreException {
259  if (null == caseDb) {
260  throw new TskCoreException("File manager has been closed");
261  }
262  return findFiles(dataSource, fileName, parent.getName());
263  }
264 
281  public synchronized List<AbstractFile> openFiles(Content dataSource, String filePath) throws TskCoreException {
282  if (null == caseDb) {
283  throw new TskCoreException("File manager has been closed");
284  }
285  return caseDb.openFiles(dataSource, filePath);
286  }
287 
317  public synchronized DerivedFile addDerivedFile(String fileName,
318  String localPath,
319  long size,
320  long ctime, long crtime, long atime, long mtime,
321  boolean isFile,
322  Content parentObj,
323  String rederiveDetails, String toolName, String toolVersion, String otherDetails,
324  TskData.EncodingType encodingType) throws TskCoreException {
325  if (null == caseDb) {
326  throw new TskCoreException("File manager has been closed");
327  }
328  return caseDb.addDerivedFile(fileName, localPath, size,
329  ctime, crtime, atime, mtime,
330  isFile, parentObj, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
331  }
332 
344  public synchronized List<LayoutFile> addCarvedFiles(CarvingResult carvingResult) throws TskCoreException {
345  if (null == caseDb) {
346  throw new TskCoreException("File manager has been closed");
347  }
348  return caseDb.addCarvedFiles(carvingResult);
349  }
350 
355  public interface FileAddProgressUpdater {
356 
362  void fileAdded(AbstractFile newFile);
363  }
364 
393  public synchronized LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException, TskDataException {
394  if (null == caseDb) {
395  throw new TskCoreException("File manager has been closed");
396  }
397  List<java.io.File> localFiles = getFilesAndDirectories(localFilePaths);
398  CaseDbTransaction trans = null;
399  try {
400  String rootDirectoryName = rootVirtualDirectoryName;
401  if (rootDirectoryName.isEmpty()) {
402  rootDirectoryName = generateFilesDataSourceName(caseDb);
403  }
404 
405  /*
406  * Add the root virtual directory and its local/logical file
407  * children to the case database.
408  */
409  trans = caseDb.beginTransaction();
410  LocalFilesDataSource dataSource = caseDb.addLocalFilesDataSource(deviceId, rootDirectoryName, timeZone, trans);
411  List<AbstractFile> filesAdded = new ArrayList<>();
412  for (java.io.File localFile : localFiles) {
413  AbstractFile fileAdded = addLocalFile(trans, dataSource, localFile, TskData.EncodingType.NONE, progressUpdater);
414  if (null != fileAdded) {
415  filesAdded.add(fileAdded);
416  } else {
417  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
418  }
419  }
420  trans.commit();
421 
422  /*
423  * Publish content added events for the added files and directories.
424  */
425  for (AbstractFile fileAdded : filesAdded) {
427  }
428 
429  return dataSource;
430 
431  } catch (TskCoreException ex) {
432  if (null != trans) {
433  try {
434  trans.rollback();
435  } catch (TskCoreException ex2) {
436  LOGGER.log(Level.SEVERE, String.format("Failed to rollback transaction after exception: %s", ex.getMessage()), ex2);
437  }
438  }
439  throw ex;
440  }
441  }
442 
456  private static synchronized String generateFilesDataSourceName(SleuthkitCase caseDb) throws TskCoreException {
457  int localFileDataSourcesCounter = 0;
458  try {
459  List<VirtualDirectory> localFileDataSources = caseDb.getVirtualDirectoryRoots();
460  for (VirtualDirectory vd : localFileDataSources) {
461  if (vd.getName().startsWith(VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX)) {
462  ++localFileDataSourcesCounter;
463  }
464  }
465  return VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX + (localFileDataSourcesCounter + 1);
466  } catch (TskCoreException ex) {
467  throw new TskCoreException("Error querying for existing local file data sources with defualt names", ex);
468  }
469  }
470 
483  private List<java.io.File> getFilesAndDirectories(List<String> localFilePaths) throws TskDataException {
484  List<java.io.File> localFiles = new ArrayList<>();
485  for (String path : localFilePaths) {
486  java.io.File localFile = new java.io.File(path);
487  if (!localFile.exists() || !localFile.canRead()) {
488  throw new TskDataException(String.format("File at %s does not exist or cannot be read", localFile.getAbsolutePath()));
489  }
490  localFiles.add(localFile);
491  }
492  return localFiles;
493  }
494 
511  private AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile,
512  TskData.EncodingType encodingType, FileAddProgressUpdater progressUpdater) throws TskCoreException {
513  if (localFile.isDirectory()) {
514  /*
515  * Add the directory as a local directory.
516  */
517  LocalDirectory localDirectory = caseDb.addLocalDirectory(parentDirectory.getId(), localFile.getName(), trans);
518  progressUpdater.fileAdded(localDirectory);
519 
520  /*
521  * Add its children, if any.
522  */
523  final java.io.File[] childFiles = localFile.listFiles();
524  if (childFiles != null && childFiles.length > 0) {
525  for (java.io.File childFile : childFiles) {
526  addLocalFile(trans, localDirectory, childFile, progressUpdater);
527  }
528  }
529 
530  return localDirectory;
531  } else {
532  return caseDb.addLocalFile(localFile.getName(), localFile.getAbsolutePath(), localFile.length(),
533  0, 0, 0, 0,
534  localFile.isFile(), encodingType, parentDirectory, trans);
535  }
536  }
537 
543  @Override
544  public synchronized void close() throws IOException {
545  caseDb = null;
546  }
547 
566  @Deprecated
567  public synchronized VirtualDirectory addLocalFilesDirs(List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException {
568  if (null == caseDb) {
569  throw new TskCoreException("File manager has been closed");
570  }
571  try {
572  return addLocalFilesDataSource("", "", "", localFilePaths, progressUpdater).getRootDirectory();
573  } catch (TskDataException ex) {
574  throw new TskCoreException(ex.getLocalizedMessage(), ex);
575  }
576  }
577 
596  @Deprecated
597  public synchronized LayoutFile addCarvedFile(String fileName, long fileSize, long parentObjId, List<TskFileRange> layout) throws TskCoreException {
598  if (null == caseDb) {
599  throw new TskCoreException("File manager has been closed");
600  }
601  Content parent = caseDb.getContentById(parentObjId);
602  List<CarvingResult.CarvedFile> carvedFiles = new ArrayList<>();
603  carvedFiles.add(new CarvingResult.CarvedFile(fileName, fileSize, layout));
604  List<LayoutFile> layoutFiles = caseDb.addCarvedFiles(new CarvingResult(parent, carvedFiles));
605  return layoutFiles.get(0);
606  }
607 
623  @Deprecated
624  public synchronized List<LayoutFile> addCarvedFiles(List<org.sleuthkit.datamodel.CarvedFileContainer> filesToAdd) throws TskCoreException {
625  if (null == caseDb) {
626  throw new TskCoreException("File manager has been closed");
627  }
628  return caseDb.addCarvedFiles(filesToAdd);
629  }
630 
661  @Deprecated
662  public synchronized DerivedFile addDerivedFile(String fileName,
663  String localPath,
664  long size,
665  long ctime, long crtime, long atime, long mtime,
666  boolean isFile,
667  AbstractFile parentFile,
668  String rederiveDetails, String toolName, String toolVersion, String otherDetails) throws TskCoreException {
669  return addDerivedFile(fileName, localPath, size, ctime, crtime, atime, mtime, isFile, parentFile,
670  rederiveDetails, toolName, toolVersion, otherDetails, TskData.EncodingType.NONE);
671  }
672 
692  @Deprecated
693  private AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, FileAddProgressUpdater progressUpdater) throws TskCoreException {
694  return addLocalFile(trans, parentDirectory, localFile, TskData.EncodingType.NONE, progressUpdater);
695  }
696 
697 }
synchronized List< AbstractFile > findFiles(String fileName, String parentName)
static String createFileTypeInCondition(Collection< String > mimeTypes)
synchronized VirtualDirectory addLocalFilesDirs(List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
synchronized LayoutFile addCarvedFile(String fileName, long fileSize, long parentObjId, List< TskFileRange > layout)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName)
synchronized DerivedFile addDerivedFile(String fileName, String localPath, long size, long ctime, long crtime, long atime, long mtime, boolean isFile, Content parentObj, String rederiveDetails, String toolName, String toolVersion, String otherDetails, TskData.EncodingType encodingType)
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 addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, FileAddProgressUpdater progressUpdater)
synchronized List< AbstractFile > findFilesByMimeType(Collection< String > mimeTypes)
synchronized List< LayoutFile > addCarvedFiles(List< org.sleuthkit.datamodel.CarvedFileContainer > filesToAdd)
synchronized List< AbstractFile > openFiles(Content dataSource, String filePath)
synchronized LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
static synchronized String generateFilesDataSourceName(SleuthkitCase caseDb)
synchronized List< AbstractFile > findFilesByMimeType(Content dataSource, Collection< String > mimeTypes)
void fireModuleContentEvent(ModuleContentEvent moduleContentEvent)
synchronized List< AbstractFile > findFiles(String fileName)
AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, TskData.EncodingType encodingType, FileAddProgressUpdater progressUpdater)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
synchronized List< LayoutFile > addCarvedFiles(CarvingResult carvingResult)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, AbstractFile parent)
synchronized List< AbstractFile > findFiles(String fileName, AbstractFile parent)
List< java.io.File > getFilesAndDirectories(List< String > localFilePaths)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, String parentName)
static synchronized IngestServices getInstance()

Copyright © 2012-2016 Basis Technology. Generated on: Tue Feb 20 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.