Autopsy  4.20.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.nio.file.Files;
29 import java.nio.file.attribute.BasicFileAttributes;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.logging.Level;
34 import org.openide.util.NbBundle;
38 import org.sleuthkit.datamodel.AbstractFile;
39 import org.sleuthkit.datamodel.Content;
40 import org.sleuthkit.datamodel.DerivedFile;
41 import org.sleuthkit.datamodel.Host;
42 import org.sleuthkit.datamodel.LayoutFile;
43 import org.sleuthkit.datamodel.LocalDirectory;
44 import org.sleuthkit.datamodel.SleuthkitCase;
45 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
46 import org.sleuthkit.datamodel.SpecialDirectory;
47 import org.sleuthkit.datamodel.TskCoreException;
48 import org.sleuthkit.datamodel.TskFileRange;
49 import org.sleuthkit.datamodel.VirtualDirectory;
50 import org.sleuthkit.datamodel.LocalFilesDataSource;
51 import org.sleuthkit.datamodel.TskDataException;
52 import org.apache.commons.lang3.StringUtils;
54 import org.sleuthkit.datamodel.CarvingResult;
55 import org.sleuthkit.datamodel.TskData;
56 
62 public class FileManager implements Closeable {
63 
64  private static final Logger LOGGER = Logger.getLogger(FileManager.class.getName());
65  private final SleuthkitCase caseDb;
66 
74  public FileManager(SleuthkitCase caseDb) {
75  this.caseDb = caseDb;
76  }
77 
88  public List<AbstractFile> findFilesByMimeType(Collection<String> mimeTypes) throws TskCoreException {
89  return caseDb.findAllFilesWhere(createFileTypeInCondition(mimeTypes));
90  }
91 
104  public List<AbstractFile> findFilesByParentPath(long dataSourceObjectID, String parentPath) throws TskCoreException {
105  return caseDb.findAllFilesWhere(createParentPathCondition(dataSourceObjectID, parentPath));
106  }
107 
120  public List<AbstractFile> findFilesByMimeType(Content dataSource, Collection<String> mimeTypes) throws TskCoreException {
121  return caseDb.findAllFilesWhere("data_source_obj_id = " + dataSource.getId() + " AND " + createFileTypeInCondition(mimeTypes));
122  }
123 
134  public List<AbstractFile> findFilesExactName(long parentId, String name) throws TskCoreException{
135  return caseDb.getFileManager().findFilesExactName(parentId, name);
136  }
137 
145  private static String createFileTypeInCondition(Collection<String> mimeTypes) {
146  String types = StringUtils.join(mimeTypes, "', '");
147  return "mime_type IN ('" + types + "')";
148  }
149 
159  private static String createParentPathCondition(long dataSourceObjectID, String parentPath) {
160  return "data_source_obj_id = " + dataSourceObjectID + " AND parent_path LIKE '" + parentPath + "%'";
161  }
162 
175  public List<AbstractFile> findFiles(String fileName) throws TskCoreException {
176  List<AbstractFile> result = new ArrayList<>();
177  List<Content> dataSources = caseDb.getRootObjects();
178  for (Content dataSource : dataSources) {
179  result.addAll(findFiles(dataSource, fileName));
180  }
181  return result;
182  }
183 
200  public List<AbstractFile> findFiles(String fileName, String parentSubString) throws TskCoreException {
201  List<AbstractFile> result = new ArrayList<>();
202  List<Content> dataSources = caseDb.getRootObjects();
203  for (Content dataSource : dataSources) {
204  result.addAll(findFiles(dataSource, fileName, parentSubString));
205  }
206  return result;
207  }
208 
223  public List<AbstractFile> findFiles(String fileName, AbstractFile parent) throws TskCoreException {
224  return caseDb.findFilesInFolder(fileName, parent);
225  }
226 
241  public List<AbstractFile> findFiles(Content dataSource, String fileName) throws TskCoreException {
242  return caseDb.findFiles(dataSource, fileName);
243  }
244 
263  public List<AbstractFile> findFiles(Content dataSource, String fileName, String parentSubString) throws TskCoreException {
264  return caseDb.findFiles(dataSource, fileName, parentSubString);
265  }
266 
267 
284  public List<AbstractFile> openFiles(Content dataSource, String filePath) throws TskCoreException {
285  return caseDb.openFiles(dataSource, filePath);
286  }
287 
317  public 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  return caseDb.addDerivedFile(fileName, localPath, size,
326  ctime, crtime, atime, mtime,
327  isFile, parentObj, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
328  }
329 
360  public DerivedFile updateDerivedFile(DerivedFile derivedFile, String localPath,
361  long size,
362  long ctime, long crtime, long atime, long mtime,
363  boolean isFile, String mimeType,
364  String rederiveDetails, String toolName, String toolVersion, String otherDetails,
365  TskData.EncodingType encodingType) throws TskCoreException {
366  return caseDb.updateDerivedFile(derivedFile, localPath, size,
367  ctime, crtime, atime, mtime,
368  isFile, mimeType, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
369  }
370 
382  public List<LayoutFile> addCarvedFiles(CarvingResult carvingResult) throws TskCoreException {
383  return caseDb.addCarvedFiles(carvingResult);
384  }
385 
390  public interface FileAddProgressUpdater {
391 
397  void fileAdded(AbstractFile newFile);
398  }
399 
428  public LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException, TskDataException {
429  return addLocalFilesDataSource(deviceId, rootVirtualDirectoryName, timeZone, null, localFilePaths, false, false, false, progressUpdater);
430  }
460  public LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, Host host, List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException, TskDataException {
461  return addLocalFilesDataSource(deviceId, rootVirtualDirectoryName, timeZone, host, localFilePaths, false, false, false, progressUpdater);
462  }
463 
496  public LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, Host host,
497  List<String> localFilePaths, boolean createTimestamp, boolean accessTimestamp, boolean modifiedTimestamp, FileAddProgressUpdater progressUpdater) throws TskCoreException, TskDataException {
498  List<java.io.File> localFiles = getFilesAndDirectories(localFilePaths);
499  CaseDbTransaction trans = null;
500  try {
501  String rootDirectoryName = rootVirtualDirectoryName;
502  if (rootDirectoryName.isEmpty()) {
503  rootDirectoryName = generateFilesDataSourceName(caseDb);
504  }
505 
506  /*
507  * Add the root virtual directory and its local/logical file
508  * children to the case database.
509  */
510  trans = caseDb.beginTransaction();
511  LocalFilesDataSource dataSource = caseDb.addLocalFilesDataSource(deviceId, rootDirectoryName, timeZone, host, trans);
512  List<AbstractFile> filesAdded = new ArrayList<>();
513  for (java.io.File localFile : localFiles) {
514  AbstractFile fileAdded = addLocalFile(trans, dataSource, localFile, createTimestamp, accessTimestamp, modifiedTimestamp, TskData.EncodingType.NONE, progressUpdater);
515  if (null != fileAdded) {
516  filesAdded.add(fileAdded);
517  } else {
518  throw new TskCoreException(NbBundle.getMessage(this.getClass(), "FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
519  }
520  }
521  trans.commit();
522  trans = null;
523 
524  /*
525  * Publish content added events for the added files and directories.
526  */
527  for (AbstractFile fileAdded : filesAdded) {
529  }
530 
531  return dataSource;
532 
533  } finally {
534  if (null != trans) {
535  try {
536  trans.rollback();
537  } catch (TskCoreException ex) {
538  LOGGER.log(Level.SEVERE, "Failed to rollback transaction after exception", ex);
539  }
540  }
541  }
542  }
543 
557  private static String generateFilesDataSourceName(SleuthkitCase caseDb) throws TskCoreException {
558  int localFileDataSourcesCounter = 0;
559  try {
560  List<VirtualDirectory> localFileDataSources = caseDb.getVirtualDirectoryRoots();
561  for (VirtualDirectory vd : localFileDataSources) {
562  if (vd.getName().startsWith(VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX)) {
563  ++localFileDataSourcesCounter;
564  }
565  }
566  return VirtualDirectoryNode.LOGICAL_FILE_SET_PREFIX + (localFileDataSourcesCounter + 1);
567  } catch (TskCoreException ex) {
568  throw new TskCoreException("Error querying for existing local file data sources with defualt names", ex);
569  }
570  }
571 
584  private List<java.io.File> getFilesAndDirectories(List<String> localFilePaths) throws TskDataException {
585  List<java.io.File> localFiles = new ArrayList<>();
586  for (String path : localFilePaths) {
587  java.io.File localFile = new java.io.File(path);
588  if (!localFile.exists() || !localFile.canRead()) {
589  throw new TskDataException(String.format("File at %s does not exist or cannot be read", localFile.getAbsolutePath()));
590  }
591  localFiles.add(localFile);
592  }
593  return localFiles;
594  }
595 
616  private AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, boolean createTime,
617  boolean accessTime, boolean modifiedTime, TskData.EncodingType encodingType, FileAddProgressUpdater progressUpdater) throws TskCoreException {
618  if (localFile.isDirectory()) {
619  /*
620  * Add the directory as a local directory.
621  */
622  LocalDirectory localDirectory = caseDb.addLocalDirectory(parentDirectory.getId(), localFile.getName(), trans);
623  progressUpdater.fileAdded(localDirectory);
624 
625  /*
626  * Add its children, if any.
627  */
628  final java.io.File[] childFiles = localFile.listFiles();
629  if (childFiles != null && childFiles.length > 0) {
630  for (java.io.File childFile : childFiles) {
631  addLocalFile(trans, localDirectory, childFile, createTime, accessTime, modifiedTime, encodingType, progressUpdater);
632  }
633  }
634 
635  return localDirectory;
636  } else {
637  long createTimestamp = 0;
638  long modifiedTimestamp = 0;
639  long accessTimestamp = 0;
640  try {
641  BasicFileAttributes attrs;
642  attrs = Files.readAttributes(localFile.toPath(), BasicFileAttributes.class);
643  if (createTime) {
644  createTimestamp = (attrs.creationTime().toMillis()/1000);
645  }
646  if (modifiedTime) {
647  modifiedTimestamp = (attrs.lastModifiedTime().toMillis()/1000);
648  }
649  if (accessTime) {
650  accessTimestamp = (attrs.lastAccessTime().toMillis()/1000);
651  }
652  return caseDb.addLocalFile(localFile.getName(), localFile.getAbsolutePath(), localFile.length(),
653  0, createTimestamp, accessTimestamp, modifiedTimestamp,
654  localFile.isFile(), encodingType, parentDirectory, trans);
655  } catch (IOException ex) {
656  return caseDb.addLocalFile(localFile.getName(), localFile.getAbsolutePath(), localFile.length(),
657  0, 0, 0, 0,
658  localFile.isFile(), encodingType, parentDirectory, trans);
659  }
660  }
661  }
662 
669  @Deprecated
670  @Override
671  public void close() throws IOException {
672  /*
673  * No-op maintained for backwards compatibility. Clients should not
674  * attempt to close case services.
675  */
676  }
677 
696  @Deprecated
697  public VirtualDirectory addLocalFilesDirs(List<String> localFilePaths, FileAddProgressUpdater progressUpdater) throws TskCoreException {
698  try {
699  return addLocalFilesDataSource("", "", "", localFilePaths, progressUpdater).getRootDirectory();
700  } catch (TskDataException ex) {
701  throw new TskCoreException(ex.getLocalizedMessage(), ex);
702  }
703  }
704 
723  @Deprecated
724  public LayoutFile addCarvedFile(String fileName, long fileSize, long parentObjId, List<TskFileRange> layout) throws TskCoreException {
725  Content parent = caseDb.getContentById(parentObjId);
726  List<CarvingResult.CarvedFile> carvedFiles = new ArrayList<>();
727  carvedFiles.add(new CarvingResult.CarvedFile(fileName, fileSize, layout));
728  List<LayoutFile> layoutFiles = caseDb.addCarvedFiles(new CarvingResult(parent, carvedFiles));
729  return layoutFiles.get(0);
730  }
731 
747  @Deprecated
748  public List<LayoutFile> addCarvedFiles(List<org.sleuthkit.datamodel.CarvedFileContainer> filesToAdd) throws TskCoreException {
749  return caseDb.addCarvedFiles(filesToAdd);
750  }
751 
782  @Deprecated
783  public DerivedFile addDerivedFile(String fileName,
784  String localPath,
785  long size,
786  long ctime, long crtime, long atime, long mtime,
787  boolean isFile,
788  AbstractFile parentFile,
789  String rederiveDetails, String toolName, String toolVersion, String otherDetails) throws TskCoreException {
790  return addDerivedFile(fileName, localPath, size, ctime, crtime, atime, mtime, isFile, parentFile,
791  rederiveDetails, toolName, toolVersion, otherDetails, TskData.EncodingType.NONE);
792  }
793 
813  @Deprecated
814  private AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, FileAddProgressUpdater progressUpdater) throws TskCoreException {
815  return addLocalFile(trans, parentDirectory, localFile, false, false, false, TskData.EncodingType.NONE, progressUpdater);
816  }
817 
836  @Deprecated
837  public List<AbstractFile> findFiles(Content dataSource, String fileName, AbstractFile parent) throws TskCoreException {
838  return findFiles(fileName, parent);
839  }
840 
841 }
static String createFileTypeInCondition(Collection< String > mimeTypes)
List< LayoutFile > addCarvedFiles(CarvingResult carvingResult)
List< AbstractFile > openFiles(Content dataSource, String filePath)
List< AbstractFile > findFilesByMimeType(Collection< String > mimeTypes)
LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, Host host, List< String > localFilePaths, boolean createTimestamp, boolean accessTimestamp, boolean modifiedTimestamp, FileAddProgressUpdater progressUpdater)
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)
AbstractFile addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile, boolean createTime, boolean accessTime, boolean modifiedTime, TskData.EncodingType encodingType, FileAddProgressUpdater progressUpdater)
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)
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-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.