Autopsy  4.19.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 2012-2021 Basis Technology Corp.
6  * Copyright 2012 42six Solutions.
7  * Contact: aebadirad <at> 42six <dot> com
8  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * TODO (AUT-2158): This class should not extend Closeable.
23  */
24 package org.sleuthkit.autopsy.casemodule.services;
25 
26 import java.io.Closeable;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.logging.Level;
32 import org.openide.util.NbBundle;
36 import org.sleuthkit.datamodel.AbstractFile;
37 import org.sleuthkit.datamodel.Content;
38 import org.sleuthkit.datamodel.DerivedFile;
39 import org.sleuthkit.datamodel.Host;
40 import org.sleuthkit.datamodel.LayoutFile;
41 import org.sleuthkit.datamodel.LocalDirectory;
42 import org.sleuthkit.datamodel.SleuthkitCase;
43 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
44 import org.sleuthkit.datamodel.SpecialDirectory;
45 import org.sleuthkit.datamodel.TskCoreException;
46 import org.sleuthkit.datamodel.TskFileRange;
47 import org.sleuthkit.datamodel.VirtualDirectory;
48 import org.sleuthkit.datamodel.LocalFilesDataSource;
49 import org.sleuthkit.datamodel.TskDataException;
50 import org.apache.commons.lang3.StringUtils;
52 import org.sleuthkit.datamodel.CarvingResult;
53 import org.sleuthkit.datamodel.TskData;
54 
60 public class FileManager implements Closeable {
61 
62  private static final Logger LOGGER = Logger.getLogger(FileManager.class.getName());
63  private final SleuthkitCase caseDb;
64 
72  public FileManager(SleuthkitCase caseDb) {
73  this.caseDb = caseDb;
74  }
75 
86  public List<AbstractFile> findFilesByMimeType(Collection<String> mimeTypes) throws TskCoreException {
87  return caseDb.findAllFilesWhere(createFileTypeInCondition(mimeTypes));
88  }
89 
102  public List<AbstractFile> findFilesByParentPath(long dataSourceObjectID, String parentPath) throws TskCoreException {
103  return caseDb.findAllFilesWhere(createParentPathCondition(dataSourceObjectID, parentPath));
104  }
105 
118  public List<AbstractFile> findFilesByMimeType(Content dataSource, Collection<String> mimeTypes) throws TskCoreException {
119  return caseDb.findAllFilesWhere("data_source_obj_id = " + dataSource.getId() + " AND " + createFileTypeInCondition(mimeTypes));
120  }
121 
132  public List<AbstractFile> findFilesExactName(long parentId, String name) throws TskCoreException{
133  return caseDb.getFileManager().findFilesExactName(parentId, name);
134  }
135 
143  private static String createFileTypeInCondition(Collection<String> mimeTypes) {
144  String types = StringUtils.join(mimeTypes, "', '");
145  return "mime_type IN ('" + types + "')";
146  }
147 
157  private static String createParentPathCondition(long dataSourceObjectID, String parentPath) {
158  return "data_source_obj_id = " + dataSourceObjectID + " AND parent_path LIKE '" + parentPath + "%'";
159  }
160 
173  public List<AbstractFile> findFiles(String fileName) throws TskCoreException {
174  List<AbstractFile> result = new ArrayList<>();
175  List<Content> dataSources = caseDb.getRootObjects();
176  for (Content dataSource : dataSources) {
177  result.addAll(findFiles(dataSource, fileName));
178  }
179  return result;
180  }
181 
198  public List<AbstractFile> findFiles(String fileName, String parentSubString) throws TskCoreException {
199  List<AbstractFile> result = new ArrayList<>();
200  List<Content> dataSources = caseDb.getRootObjects();
201  for (Content dataSource : dataSources) {
202  result.addAll(findFiles(dataSource, fileName, parentSubString));
203  }
204  return result;
205  }
206 
221  public List<AbstractFile> findFiles(String fileName, AbstractFile parent) throws TskCoreException {
222  return caseDb.findFilesInFolder(fileName, parent);
223  }
224 
239  public List<AbstractFile> findFiles(Content dataSource, String fileName) throws TskCoreException {
240  return caseDb.findFiles(dataSource, fileName);
241  }
242 
261  public List<AbstractFile> findFiles(Content dataSource, String fileName, String parentSubString) throws TskCoreException {
262  return caseDb.findFiles(dataSource, fileName, parentSubString);
263  }
264 
265 
282  public List<AbstractFile> openFiles(Content dataSource, String filePath) throws TskCoreException {
283  return caseDb.openFiles(dataSource, filePath);
284  }
285 
315  public DerivedFile addDerivedFile(String fileName,
316  String localPath,
317  long size,
318  long ctime, long crtime, long atime, long mtime,
319  boolean isFile,
320  Content parentObj,
321  String rederiveDetails, String toolName, String toolVersion, String otherDetails,
322  TskData.EncodingType encodingType) throws TskCoreException {
323  return caseDb.addDerivedFile(fileName, localPath, size,
324  ctime, crtime, atime, mtime,
325  isFile, parentObj, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
326  }
327 
358  public DerivedFile updateDerivedFile(DerivedFile derivedFile, String localPath,
359  long size,
360  long ctime, long crtime, long atime, long mtime,
361  boolean isFile, String mimeType,
362  String rederiveDetails, String toolName, String toolVersion, String otherDetails,
363  TskData.EncodingType encodingType) throws TskCoreException {
364  return caseDb.updateDerivedFile(derivedFile, localPath, size,
365  ctime, crtime, atime, mtime,
366  isFile, mimeType, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
367  }
368 
380  public List<LayoutFile> addCarvedFiles(CarvingResult carvingResult) throws TskCoreException {
381  return caseDb.addCarvedFiles(carvingResult);
382  }
383 
388  public interface FileAddProgressUpdater {
389 
395  void fileAdded(AbstractFile newFile);
396  }
397 
426  public LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException, TskDataException {
427  return addLocalFilesDataSource(deviceId, rootVirtualDirectoryName, timeZone, null, localFilePaths, progressUpdater);
428  }
429 
459  public LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, Host host,
460  List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException, TskDataException {
461  List<java.io.File> localFiles = getFilesAndDirectories(localFilePaths);
462  CaseDbTransaction trans = null;
463  try {
464  String rootDirectoryName = rootVirtualDirectoryName;
465  if (rootDirectoryName.isEmpty()) {
466  rootDirectoryName = generateFilesDataSourceName(caseDb);
467  }
468 
469  /*
470  * Add the root virtual directory and its local/logical file
471  * children to the case database.
472  */
473  trans = caseDb.beginTransaction();
474  LocalFilesDataSource dataSource = caseDb.addLocalFilesDataSource(deviceId, rootDirectoryName, timeZone, host, trans);
475  List<AbstractFile> filesAdded = new ArrayList<>();
476  for (java.io.File localFile : localFiles) {
477  AbstractFile fileAdded = addLocalFile(trans, dataSource, localFile, TskData.EncodingType.NONE, progressUpdater);
478  if (null != fileAdded) {
479  filesAdded.add(fileAdded);
480  } else {
481  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
482  }
483  }
484  trans.commit();
485  trans = null;
486 
487  /*
488  * Publish content added events for the added files and directories.
489  */
490  for (AbstractFile fileAdded : filesAdded) {
492  }
493 
494  return dataSource;
495 
496  } finally {
497  if (null != trans) {
498  try {
499  trans.rollback();
500  } catch (TskCoreException ex) {
501  LOGGER.log(Level.SEVERE, "Failed to rollback transaction after exception", ex);
502  }
503  }
504  }
505  }
506 
520  private static String generateFilesDataSourceName(SleuthkitCase caseDb) throws TskCoreException {
521  int localFileDataSourcesCounter = 0;
522  try {
523  List<VirtualDirectory> localFileDataSources = caseDb.getVirtualDirectoryRoots();
524  for (VirtualDirectory vd : localFileDataSources) {
525  if (vd.getName().startsWith(VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX)) {
526  ++localFileDataSourcesCounter;
527  }
528  }
529  return VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX + (localFileDataSourcesCounter + 1);
530  } catch (TskCoreException ex) {
531  throw new TskCoreException("Error querying for existing local file data sources with defualt names", ex);
532  }
533  }
534 
547  private List<java.io.File> getFilesAndDirectories(List<String> localFilePaths) throws TskDataException {
548  List<java.io.File> localFiles = new ArrayList<>();
549  for (String path : localFilePaths) {
550  java.io.File localFile = new java.io.File(path);
551  if (!localFile.exists() || !localFile.canRead()) {
552  throw new TskDataException(String.format("File at %s does not exist or cannot be read", localFile.getAbsolutePath()));
553  }
554  localFiles.add(localFile);
555  }
556  return localFiles;
557  }
558 
576  private AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile,
577  TskData.EncodingType encodingType, FileAddProgressUpdater progressUpdater) throws TskCoreException {
578  if (localFile.isDirectory()) {
579  /*
580  * Add the directory as a local directory.
581  */
582  LocalDirectory localDirectory = caseDb.addLocalDirectory(parentDirectory.getId(), localFile.getName(), trans);
583  progressUpdater.fileAdded(localDirectory);
584 
585  /*
586  * Add its children, if any.
587  */
588  final java.io.File[] childFiles = localFile.listFiles();
589  if (childFiles != null && childFiles.length > 0) {
590  for (java.io.File childFile : childFiles) {
591  addLocalFile(trans, localDirectory, childFile, progressUpdater);
592  }
593  }
594 
595  return localDirectory;
596  } else {
597  return caseDb.addLocalFile(localFile.getName(), localFile.getAbsolutePath(), localFile.length(),
598  0, 0, 0, 0,
599  localFile.isFile(), encodingType, parentDirectory, trans);
600  }
601  }
602 
609  @Deprecated
610  @Override
611  public void close() throws IOException {
612  /*
613  * No-op maintained for backwards compatibility. Clients should not
614  * attempt to close case services.
615  */
616  }
617 
636  @Deprecated
637  public VirtualDirectory addLocalFilesDirs(List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException {
638  try {
639  return addLocalFilesDataSource("", "", "", localFilePaths, progressUpdater).getRootDirectory();
640  } catch (TskDataException ex) {
641  throw new TskCoreException(ex.getLocalizedMessage(), ex);
642  }
643  }
644 
663  @Deprecated
664  public LayoutFile addCarvedFile(String fileName, long fileSize, long parentObjId, List<TskFileRange> layout) throws TskCoreException {
665  Content parent = caseDb.getContentById(parentObjId);
666  List<CarvingResult.CarvedFile> carvedFiles = new ArrayList<>();
667  carvedFiles.add(new CarvingResult.CarvedFile(fileName, fileSize, layout));
668  List<LayoutFile> layoutFiles = caseDb.addCarvedFiles(new CarvingResult(parent, carvedFiles));
669  return layoutFiles.get(0);
670  }
671 
687  @Deprecated
688  public List<LayoutFile> addCarvedFiles(List<org.sleuthkit.datamodel.CarvedFileContainer> filesToAdd) throws TskCoreException {
689  return caseDb.addCarvedFiles(filesToAdd);
690  }
691 
722  @Deprecated
723  public DerivedFile addDerivedFile(String fileName,
724  String localPath,
725  long size,
726  long ctime, long crtime, long atime, long mtime,
727  boolean isFile,
728  AbstractFile parentFile,
729  String rederiveDetails, String toolName, String toolVersion, String otherDetails) throws TskCoreException {
730  return addDerivedFile(fileName, localPath, size, ctime, crtime, atime, mtime, isFile, parentFile,
731  rederiveDetails, toolName, toolVersion, otherDetails, TskData.EncodingType.NONE);
732  }
733 
753  @Deprecated
754  private AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, FileAddProgressUpdater progressUpdater) throws TskCoreException {
755  return addLocalFile(trans, parentDirectory, localFile, TskData.EncodingType.NONE, progressUpdater);
756  }
757 
776  @Deprecated
777  public List<AbstractFile> findFiles(Content dataSource, String fileName, AbstractFile parent) throws TskCoreException {
778  return findFiles(fileName, parent);
779  }
780 
781 }
static String createFileTypeInCondition(Collection< String > mimeTypes)
List< LayoutFile > addCarvedFiles(CarvingResult carvingResult)
List< AbstractFile > openFiles(Content dataSource, String filePath)
List< AbstractFile > findFilesByMimeType(Collection< String > mimeTypes)
List< AbstractFile > findFiles(String fileName)
LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
List< LayoutFile > addCarvedFiles(List< org.sleuthkit.datamodel.CarvedFileContainer > filesToAdd)
LayoutFile addCarvedFile(String fileName, long fileSize, long parentObjId, List< TskFileRange > layout)
List< AbstractFile > findFiles(Content dataSource, String fileName, String parentSubString)
VirtualDirectory addLocalFilesDirs(List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
List< AbstractFile > findFilesByMimeType(Content dataSource, Collection< String > mimeTypes)
AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, FileAddProgressUpdater progressUpdater)
List< AbstractFile > findFiles(Content dataSource, String fileName, AbstractFile parent)
LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, Host host, List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
static String createParentPathCondition(long dataSourceObjectID, String parentPath)
DerivedFile updateDerivedFile(DerivedFile derivedFile, String localPath, long size, long ctime, long crtime, long atime, long mtime, boolean isFile, String mimeType, String rederiveDetails, String toolName, String toolVersion, String otherDetails, TskData.EncodingType encodingType)
List< AbstractFile > findFiles(String fileName, String parentSubString)
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)
static String generateFilesDataSourceName(SleuthkitCase caseDb)
void fireModuleContentEvent(ModuleContentEvent moduleContentEvent)
List< AbstractFile > findFiles(Content dataSource, String fileName)
List< AbstractFile > findFilesExactName(long parentId, String name)
List< AbstractFile > findFiles(String fileName, AbstractFile parent)
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
List< AbstractFile > findFilesByParentPath(long dataSourceObjectID, String parentPath)
List< java.io.File > getFilesAndDirectories(List< String > localFilePaths)
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)
static synchronized IngestServices getInstance()

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.