23 package org.sleuthkit.autopsy.casemodule.services;
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;
41 import org.
sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
48 import org.apache.commons.lang3.StringUtils;
84 public synchronized List<AbstractFile>
findFilesByMimeType(Collection<String> mimeTypes)
throws TskCoreException {
86 throw new TskCoreException(
"File manager has been closed");
102 public synchronized List<AbstractFile>
findFilesByParentPath(
long dataSourceObjectID, String parentPath)
throws TskCoreException {
103 if (null == caseDb) {
104 throw new TskCoreException(
"File manager has been closed");
121 public synchronized List<AbstractFile>
findFilesByMimeType(Content dataSource, Collection<String> mimeTypes)
throws TskCoreException {
122 if (null == caseDb) {
123 throw new TskCoreException(
"File manager has been closed");
125 return caseDb.findAllFilesWhere(
"data_source_obj_id = " + dataSource.getId() +
" AND " +
createFileTypeInCondition(mimeTypes));
136 String types = StringUtils.join(mimeTypes,
"', '");
137 return "mime_type IN ('" + types +
"')";
149 return "data_source_obj_id = " + dataSourceObjectID +
" AND parent_path LIKE '" + parentPath +
"%'";
165 public synchronized List<AbstractFile>
findFiles(String fileName)
throws TskCoreException {
166 if (null == caseDb) {
167 throw new TskCoreException(
"File manager has been closed");
169 List<AbstractFile> result =
new ArrayList<>();
170 List<Content> dataSources = caseDb.getRootObjects();
171 for (Content dataSource : dataSources) {
172 result.addAll(
findFiles(dataSource, fileName));
191 public synchronized List<AbstractFile>
findFiles(String fileName, String parentSubString)
throws TskCoreException {
192 if (null == caseDb) {
193 throw new TskCoreException(
"File manager has been closed");
195 List<AbstractFile> result =
new ArrayList<>();
196 List<Content> dataSources = caseDb.getRootObjects();
197 for (Content dataSource : dataSources) {
198 result.addAll(
findFiles(dataSource, fileName, parentSubString));
217 public synchronized List<AbstractFile>
findFiles(String fileName, AbstractFile parent)
throws TskCoreException {
218 if (null == caseDb) {
219 throw new TskCoreException(
"File manager has been closed");
221 List<AbstractFile> result =
new ArrayList<>();
222 List<Content> dataSources = caseDb.getRootObjects();
223 for (Content dataSource : dataSources) {
224 result.addAll(
findFiles(dataSource, fileName, parent));
243 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName)
throws TskCoreException {
244 if (null == caseDb) {
245 throw new TskCoreException(
"File manager has been closed");
247 return caseDb.findFiles(dataSource, fileName);
266 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName, String parentSubString)
throws TskCoreException {
267 if (null == caseDb) {
268 throw new TskCoreException(
"File manager has been closed");
270 return caseDb.findFiles(dataSource, fileName, parentSubString);
289 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName, AbstractFile parent)
throws TskCoreException {
290 if (null == caseDb) {
291 throw new TskCoreException(
"File manager has been closed");
293 return findFiles(dataSource, fileName, parent.getName());
312 public synchronized List<AbstractFile>
openFiles(Content dataSource, String filePath)
throws TskCoreException {
313 if (null == caseDb) {
314 throw new TskCoreException(
"File manager has been closed");
316 return caseDb.openFiles(dataSource, filePath);
351 long ctime,
long crtime,
long atime,
long mtime,
354 String rederiveDetails, String toolName, String toolVersion, String otherDetails,
355 TskData.EncodingType encodingType) throws TskCoreException {
356 if (null == caseDb) {
357 throw new TskCoreException(
"File manager has been closed");
359 return caseDb.addDerivedFile(fileName, localPath, size,
360 ctime, crtime, atime, mtime,
361 isFile, parentObj, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
395 long ctime,
long crtime,
long atime,
long mtime,
396 boolean isFile, String mimeType,
397 String rederiveDetails, String toolName, String toolVersion, String otherDetails,
398 TskData.EncodingType encodingType) throws TskCoreException {
399 if (null == caseDb) {
400 throw new TskCoreException(
"File manager has been closed");
402 return caseDb.updateDerivedFile(derivedFile, localPath, size,
403 ctime, crtime, atime, mtime,
404 isFile, mimeType, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
418 public synchronized List<LayoutFile>
addCarvedFiles(CarvingResult carvingResult)
throws TskCoreException {
419 if (null == caseDb) {
420 throw new TskCoreException(
"File manager has been closed");
422 return caseDb.addCarvedFiles(carvingResult);
467 public synchronized LocalFilesDataSource
addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List<String> localFilePaths,
FileAddProgressUpdater progressUpdater)
throws TskCoreException, TskDataException {
468 if (null == caseDb) {
469 throw new TskCoreException(
"File manager has been closed");
472 CaseDbTransaction trans = null;
474 String rootDirectoryName = rootVirtualDirectoryName;
475 if (rootDirectoryName.isEmpty()) {
483 trans = caseDb.beginTransaction();
484 LocalFilesDataSource dataSource = caseDb.addLocalFilesDataSource(deviceId, rootDirectoryName, timeZone, trans);
485 List<AbstractFile> filesAdded =
new ArrayList<>();
486 for (java.io.File localFile : localFiles) {
487 AbstractFile fileAdded =
addLocalFile(trans, dataSource, localFile, TskData.EncodingType.NONE, progressUpdater);
488 if (null != fileAdded) {
489 filesAdded.add(fileAdded);
491 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
499 for (AbstractFile fileAdded : filesAdded) {
505 }
catch (TskCoreException ex) {
509 }
catch (TskCoreException ex2) {
510 LOGGER.log(Level.SEVERE, String.format(
"Failed to rollback transaction after exception: %s", ex.getMessage()), ex2);
531 int localFileDataSourcesCounter = 0;
533 List<VirtualDirectory> localFileDataSources = caseDb.getVirtualDirectoryRoots();
534 for (VirtualDirectory vd : localFileDataSources) {
536 ++localFileDataSourcesCounter;
540 }
catch (TskCoreException ex) {
541 throw new TskCoreException(
"Error querying for existing local file data sources with defualt names", ex);
558 List<java.io.File> localFiles =
new ArrayList<>();
559 for (String path : localFilePaths) {
560 java.io.File localFile =
new java.io.File(path);
561 if (!localFile.exists() || !localFile.canRead()) {
562 throw new TskDataException(String.format(
"File at %s does not exist or cannot be read", localFile.getAbsolutePath()));
564 localFiles.add(localFile);
586 private AbstractFile
addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile,
588 if (localFile.isDirectory()) {
592 LocalDirectory localDirectory = caseDb.addLocalDirectory(parentDirectory.getId(), localFile.getName(), trans);
593 progressUpdater.fileAdded(localDirectory);
598 final java.io.File[] childFiles = localFile.listFiles();
599 if (childFiles != null && childFiles.length > 0) {
600 for (java.io.File childFile : childFiles) {
601 addLocalFile(trans, localDirectory, childFile, progressUpdater);
605 return localDirectory;
607 return caseDb.addLocalFile(localFile.getName(), localFile.getAbsolutePath(), localFile.length(),
609 localFile.isFile(), encodingType, parentDirectory, trans);
619 public synchronized void close() throws IOException {
643 if (null == caseDb) {
644 throw new TskCoreException(
"File manager has been closed");
648 }
catch (TskDataException ex) {
649 throw new TskCoreException(ex.getLocalizedMessage(), ex);
672 public synchronized LayoutFile
addCarvedFile(String fileName,
long fileSize,
long parentObjId, List<TskFileRange> layout)
throws TskCoreException {
673 if (null == caseDb) {
674 throw new TskCoreException(
"File manager has been closed");
676 Content parent = caseDb.getContentById(parentObjId);
677 List<CarvingResult.CarvedFile> carvedFiles =
new ArrayList<>();
678 carvedFiles.add(
new CarvingResult.CarvedFile(fileName, fileSize, layout));
679 List<LayoutFile> layoutFiles = caseDb.addCarvedFiles(
new CarvingResult(parent, carvedFiles));
680 return layoutFiles.get(0);
699 public synchronized List<LayoutFile>
addCarvedFiles(List<org.sleuthkit.datamodel.CarvedFileContainer> filesToAdd)
throws TskCoreException {
700 if (null == caseDb) {
701 throw new TskCoreException(
"File manager has been closed");
703 return caseDb.addCarvedFiles(filesToAdd);
740 long ctime,
long crtime,
long atime,
long mtime,
742 AbstractFile parentFile,
743 String rederiveDetails, String toolName, String toolVersion, String otherDetails)
throws TskCoreException {
744 return addDerivedFile(fileName, localPath, size, ctime, crtime, atime, mtime, isFile, parentFile,
745 rederiveDetails, toolName, toolVersion, otherDetails, TskData.EncodingType.NONE);
769 private AbstractFile
addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile,
FileAddProgressUpdater progressUpdater)
throws TskCoreException {
770 return addLocalFile(trans, parentDirectory, localFile, TskData.EncodingType.NONE, progressUpdater);
synchronized void close()
static String createFileTypeInCondition(Collection< String > mimeTypes)
synchronized VirtualDirectory addLocalFilesDirs(List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
static final Logger LOGGER
synchronized LayoutFile addCarvedFile(String fileName, long fileSize, long parentObjId, List< TskFileRange > layout)
synchronized List< AbstractFile > findFiles(String fileName, String parentSubString)
void fileAdded(AbstractFile newFile)
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)
static String createParentPathCondition(long dataSourceObjectID, String parentPath)
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)
synchronized List< AbstractFile > findFilesByParentPath(long dataSourceObjectID, String parentPath)
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)
synchronized List< LayoutFile > addCarvedFiles(CarvingResult carvingResult)
FileManager(SleuthkitCase caseDb)
synchronized 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)
static final String LOGICAL_FILE_SET_PREFIX
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, String parentSubString)
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)
static synchronized IngestServices getInstance()