24 package org.sleuthkit.autopsy.casemodule.services;
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;
45 import org.
sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
52 import org.apache.commons.lang3.StringUtils;
104 public List<AbstractFile>
findFilesByParentPath(
long dataSourceObjectID, String parentPath)
throws TskCoreException {
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));
135 return caseDb.getFileManager().findFilesExactName(parentId, name);
146 String types = StringUtils.join(mimeTypes,
"', '");
147 return "mime_type IN ('" + types +
"')";
160 return "data_source_obj_id = " + dataSourceObjectID +
" AND parent_path LIKE '" + parentPath +
"%'";
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));
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));
223 public List<AbstractFile>
findFiles(String fileName, AbstractFile parent)
throws TskCoreException {
224 return caseDb.findFilesInFolder(fileName, parent);
241 public List<AbstractFile>
findFiles(Content dataSource, String fileName)
throws TskCoreException {
242 return caseDb.findFiles(dataSource, fileName);
263 public List<AbstractFile>
findFiles(Content dataSource, String fileName, String parentSubString)
throws TskCoreException {
264 return caseDb.findFiles(dataSource, fileName, parentSubString);
284 public List<AbstractFile>
openFiles(Content dataSource, String filePath)
throws TskCoreException {
285 return caseDb.openFiles(dataSource, filePath);
320 long ctime,
long crtime,
long atime,
long mtime,
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);
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);
382 public List<LayoutFile>
addCarvedFiles(CarvingResult carvingResult)
throws TskCoreException {
383 return caseDb.addCarvedFiles(carvingResult);
429 return addLocalFilesDataSource(deviceId, rootVirtualDirectoryName, timeZone, null, localFilePaths,
false,
false,
false, progressUpdater);
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);
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 {
499 CaseDbTransaction trans = null;
501 String rootDirectoryName = rootVirtualDirectoryName;
502 if (rootDirectoryName.isEmpty()) {
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);
518 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
527 for (AbstractFile fileAdded : filesAdded) {
537 }
catch (TskCoreException ex) {
538 LOGGER.log(Level.SEVERE,
"Failed to rollback transaction after exception", ex);
558 int localFileDataSourcesCounter = 0;
560 List<VirtualDirectory> localFileDataSources = caseDb.getVirtualDirectoryRoots();
561 for (VirtualDirectory vd : localFileDataSources) {
563 ++localFileDataSourcesCounter;
567 }
catch (TskCoreException ex) {
568 throw new TskCoreException(
"Error querying for existing local file data sources with defualt names", ex);
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()));
591 localFiles.add(localFile);
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()) {
622 LocalDirectory localDirectory = caseDb.addLocalDirectory(parentDirectory.getId(), localFile.getName(), trans);
623 progressUpdater.fileAdded(localDirectory);
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);
635 return localDirectory;
637 long createTimestamp = 0;
638 long modifiedTimestamp = 0;
639 long accessTimestamp = 0;
641 BasicFileAttributes attrs;
642 attrs = Files.readAttributes(localFile.toPath(), BasicFileAttributes.class);
644 createTimestamp = (attrs.creationTime().toMillis()/1000);
647 modifiedTimestamp = (attrs.lastModifiedTime().toMillis()/1000);
650 accessTimestamp = (attrs.lastAccessTime().toMillis()/1000);
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(),
658 localFile.isFile(), encodingType, parentDirectory, trans);
671 public void close() throws IOException {
700 }
catch (TskDataException ex) {
701 throw new TskCoreException(ex.getLocalizedMessage(), ex);
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);
748 public List<LayoutFile>
addCarvedFiles(List<org.sleuthkit.datamodel.CarvedFileContainer> filesToAdd)
throws TskCoreException {
749 return caseDb.addCarvedFiles(filesToAdd);
786 long ctime,
long crtime,
long atime,
long mtime,
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);
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);
837 public List<AbstractFile>
findFiles(Content dataSource, String fileName, AbstractFile parent)
throws TskCoreException {
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)
static final Logger LOGGER
LayoutFile addCarvedFile(String fileName, long fileSize, long parentObjId, List< TskFileRange > layout)
void fileAdded(AbstractFile newFile)
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)
final SleuthkitCase caseDb
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)
FileManager(SleuthkitCase caseDb)
List< AbstractFile > findFilesByParentPath(long dataSourceObjectID, String parentPath)
static final String LOGICAL_FILE_SET_PREFIX
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()