24 package org.sleuthkit.autopsy.casemodule.services;
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;
42 import org.
sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
49 import org.apache.commons.lang3.StringUtils;
85 public synchronized List<AbstractFile>
findFilesByMimeType(Collection<String> mimeTypes)
throws TskCoreException {
87 throw new TskCoreException(
"File manager has been closed");
104 public synchronized List<AbstractFile>
findFilesByParentPath(
long dataSourceObjectID, String parentPath)
throws TskCoreException {
105 if (null == caseDb) {
106 throw new TskCoreException(
"File manager has been closed");
123 public synchronized List<AbstractFile>
findFilesByMimeType(Content dataSource, Collection<String> mimeTypes)
throws TskCoreException {
124 if (null == caseDb) {
125 throw new TskCoreException(
"File manager has been closed");
127 return caseDb.findAllFilesWhere(
"data_source_obj_id = " + dataSource.getId() +
" AND " +
createFileTypeInCondition(mimeTypes));
140 public synchronized List<AbstractFile>
findFilesExactName(
long parentId, String name)
throws TskCoreException{
141 if (null == caseDb) {
142 throw new TskCoreException(
"File manager has been closed");
144 String whereClause =
"name = '%s'";
145 return caseDb.findAllFilesInFolderWhere(parentId, String.format(whereClause, name));
156 String types = StringUtils.join(mimeTypes,
"', '");
157 return "mime_type IN ('" + types +
"')";
170 return "data_source_obj_id = " + dataSourceObjectID +
" AND parent_path LIKE '" + parentPath +
"%'";
185 public synchronized List<AbstractFile>
findFiles(String fileName)
throws TskCoreException {
186 if (null == caseDb) {
187 throw new TskCoreException(
"File manager has been closed");
189 List<AbstractFile> result =
new ArrayList<>();
190 List<Content> dataSources = caseDb.getRootObjects();
191 for (Content dataSource : dataSources) {
192 result.addAll(
findFiles(dataSource, fileName));
213 public synchronized List<AbstractFile>
findFiles(String fileName, String parentSubString)
throws TskCoreException {
214 if (null == caseDb) {
215 throw new TskCoreException(
"File manager has been closed");
217 List<AbstractFile> result =
new ArrayList<>();
218 List<Content> dataSources = caseDb.getRootObjects();
219 for (Content dataSource : dataSources) {
220 result.addAll(
findFiles(dataSource, fileName, parentSubString));
239 public synchronized List<AbstractFile>
findFiles(String fileName, AbstractFile parent)
throws TskCoreException {
240 if (null == caseDb) {
241 throw new TskCoreException(
"File manager has been closed");
243 List<AbstractFile> result =
new ArrayList<>();
244 List<Content> dataSources = caseDb.getRootObjects();
245 for (Content dataSource : dataSources) {
246 result.addAll(
findFiles(dataSource, fileName, parent));
265 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName)
throws TskCoreException {
266 if (null == caseDb) {
267 throw new TskCoreException(
"File manager has been closed");
269 return caseDb.findFiles(dataSource, fileName);
290 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName, String parentSubString)
throws TskCoreException {
291 if (null == caseDb) {
292 throw new TskCoreException(
"File manager has been closed");
294 return caseDb.findFiles(dataSource, fileName, parentSubString);
313 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName, AbstractFile parent)
throws TskCoreException {
314 if (null == caseDb) {
315 throw new TskCoreException(
"File manager has been closed");
317 return findFiles(dataSource, fileName, parent.getName());
336 public synchronized List<AbstractFile>
openFiles(Content dataSource, String filePath)
throws TskCoreException {
337 if (null == caseDb) {
338 throw new TskCoreException(
"File manager has been closed");
340 return caseDb.openFiles(dataSource, filePath);
375 long ctime,
long crtime,
long atime,
long mtime,
378 String rederiveDetails, String toolName, String toolVersion, String otherDetails,
379 TskData.EncodingType encodingType) throws TskCoreException {
380 if (null == caseDb) {
381 throw new TskCoreException(
"File manager has been closed");
383 return caseDb.addDerivedFile(fileName, localPath, size,
384 ctime, crtime, atime, mtime,
385 isFile, parentObj, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
420 long ctime,
long crtime,
long atime,
long mtime,
421 boolean isFile, String mimeType,
422 String rederiveDetails, String toolName, String toolVersion, String otherDetails,
423 TskData.EncodingType encodingType) throws TskCoreException {
424 if (null == caseDb) {
425 throw new TskCoreException(
"File manager has been closed");
427 return caseDb.updateDerivedFile(derivedFile, localPath, size,
428 ctime, crtime, atime, mtime,
429 isFile, mimeType, rederiveDetails, toolName, toolVersion, otherDetails, encodingType);
443 public synchronized List<LayoutFile>
addCarvedFiles(CarvingResult carvingResult)
throws TskCoreException {
444 if (null == caseDb) {
445 throw new TskCoreException(
"File manager has been closed");
447 return caseDb.addCarvedFiles(carvingResult);
492 public synchronized LocalFilesDataSource
addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List<String> localFilePaths,
FileAddProgressUpdater progressUpdater)
throws TskCoreException, TskDataException {
493 if (null == caseDb) {
494 throw new TskCoreException(
"File manager has been closed");
497 CaseDbTransaction trans = null;
499 String rootDirectoryName = rootVirtualDirectoryName;
500 if (rootDirectoryName.isEmpty()) {
508 trans = caseDb.beginTransaction();
509 LocalFilesDataSource dataSource = caseDb.addLocalFilesDataSource(deviceId, rootDirectoryName, timeZone, trans);
510 List<AbstractFile> filesAdded =
new ArrayList<>();
511 for (java.io.File localFile : localFiles) {
512 AbstractFile fileAdded =
addLocalFile(trans, dataSource, localFile, TskData.EncodingType.NONE, progressUpdater);
513 if (null != fileAdded) {
514 filesAdded.add(fileAdded);
516 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
525 for (AbstractFile fileAdded : filesAdded) {
535 }
catch (TskCoreException ex) {
536 LOGGER.log(Level.SEVERE,
"Failed to rollback transaction after exception", ex);
556 int localFileDataSourcesCounter = 0;
558 List<VirtualDirectory> localFileDataSources = caseDb.getVirtualDirectoryRoots();
559 for (VirtualDirectory vd : localFileDataSources) {
561 ++localFileDataSourcesCounter;
565 }
catch (TskCoreException ex) {
566 throw new TskCoreException(
"Error querying for existing local file data sources with defualt names", ex);
583 List<java.io.File> localFiles =
new ArrayList<>();
584 for (String path : localFilePaths) {
585 java.io.File localFile =
new java.io.File(path);
586 if (!localFile.exists() || !localFile.canRead()) {
587 throw new TskDataException(String.format(
"File at %s does not exist or cannot be read", localFile.getAbsolutePath()));
589 localFiles.add(localFile);
611 private AbstractFile
addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile,
613 if (localFile.isDirectory()) {
617 LocalDirectory localDirectory = caseDb.addLocalDirectory(parentDirectory.getId(), localFile.getName(), trans);
618 progressUpdater.fileAdded(localDirectory);
623 final java.io.File[] childFiles = localFile.listFiles();
624 if (childFiles != null && childFiles.length > 0) {
625 for (java.io.File childFile : childFiles) {
626 addLocalFile(trans, localDirectory, childFile, progressUpdater);
630 return localDirectory;
632 return caseDb.addLocalFile(localFile.getName(), localFile.getAbsolutePath(), localFile.length(),
634 localFile.isFile(), encodingType, parentDirectory, trans);
646 public synchronized void close() throws IOException {
673 if (null == caseDb) {
674 throw new TskCoreException(
"File manager has been closed");
678 }
catch (TskDataException ex) {
679 throw new TskCoreException(ex.getLocalizedMessage(), ex);
702 public synchronized LayoutFile
addCarvedFile(String fileName,
long fileSize,
long parentObjId, List<TskFileRange> layout)
throws TskCoreException {
703 if (null == caseDb) {
704 throw new TskCoreException(
"File manager has been closed");
706 Content parent = caseDb.getContentById(parentObjId);
707 List<CarvingResult.CarvedFile> carvedFiles =
new ArrayList<>();
708 carvedFiles.add(
new CarvingResult.CarvedFile(fileName, fileSize, layout));
709 List<LayoutFile> layoutFiles = caseDb.addCarvedFiles(
new CarvingResult(parent, carvedFiles));
710 return layoutFiles.get(0);
729 public synchronized List<LayoutFile>
addCarvedFiles(List<org.sleuthkit.datamodel.CarvedFileContainer> filesToAdd)
throws TskCoreException {
730 if (null == caseDb) {
731 throw new TskCoreException(
"File manager has been closed");
733 return caseDb.addCarvedFiles(filesToAdd);
770 long ctime,
long crtime,
long atime,
long mtime,
772 AbstractFile parentFile,
773 String rederiveDetails, String toolName, String toolVersion, String otherDetails)
throws TskCoreException {
774 return addDerivedFile(fileName, localPath, size, ctime, crtime, atime, mtime, isFile, parentFile,
775 rederiveDetails, toolName, toolVersion, otherDetails, TskData.EncodingType.NONE);
798 private AbstractFile
addLocalFile(CaseDbTransaction trans, SpecialDirectory parentDirectory, java.io.File localFile,
FileAddProgressUpdater progressUpdater)
throws TskCoreException {
799 return addLocalFile(trans, parentDirectory, localFile, TskData.EncodingType.NONE, progressUpdater);
synchronized void close()
static String createFileTypeInCondition(Collection< String > mimeTypes)
synchronized List< AbstractFile > findFilesExactName(long parentId, String name)
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()