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.List;
29 import java.util.logging.Level;
30 import org.openide.util.NbBundle;
41 import org.
sleuthkit.datamodel.SleuthkitCase.CaseDbTransaction;
66 private synchronized void init() {
68 List<VirtualDirectory> virtRoots;
71 virtRoots = tskCase.getVirtualDirectoryRoots();
72 for (VirtualDirectory vd : virtRoots) {
77 }
catch (TskCoreException ex) {
78 logger.log(Level.SEVERE,
"Error initializing FileManager and getting number of local file sets");
93 public synchronized List<AbstractFile>
findFiles(String fileName)
throws TskCoreException {
94 List<AbstractFile> result =
new ArrayList<>();
96 if (tskCase == null) {
97 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.findFiles.exception.msg"));
99 List<Content> dataSources = tskCase.getRootObjects();
100 for (Content dataSource : dataSources) {
101 result.addAll(
findFiles(dataSource, fileName));
119 public synchronized List<AbstractFile>
findFiles(String fileName, String dirName)
throws TskCoreException {
120 List<AbstractFile> result =
new ArrayList<>();
122 if (tskCase == null) {
123 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.findFiles2.exception.msg"));
125 List<Content> dataSources = tskCase.getRootObjects();
126 for (Content dataSource : dataSources) {
127 result.addAll(
findFiles(dataSource, fileName, dirName));
144 public synchronized List<AbstractFile>
findFiles(String fileName, AbstractFile parentFile)
throws TskCoreException {
145 List<AbstractFile> result =
new ArrayList<>();
147 if (tskCase == null) {
148 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.findFiles3.exception.msg"));
150 List<Content> dataSources = tskCase.getRootObjects();
151 for (Content dataSource : dataSources) {
152 result.addAll(
findFiles(dataSource, fileName, parentFile));
168 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName)
throws TskCoreException {
169 if (tskCase == null) {
170 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.findFiles.exception.msg"));
172 return tskCase.findFiles(dataSource, fileName);
189 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName, String dirName)
throws TskCoreException {
190 if (tskCase == null) {
191 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.findFiles2.exception.msg"));
193 return tskCase.findFiles(dataSource, fileName, dirName);
209 public synchronized List<AbstractFile>
findFiles(Content dataSource, String fileName, AbstractFile parentFile)
throws TskCoreException {
210 if (tskCase == null) {
211 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.findFiles3.exception.msg"));
213 return findFiles(dataSource, fileName, parentFile.getName());
224 public synchronized List<AbstractFile>
openFiles(Content dataSource, String filePath)
throws TskCoreException {
225 if (tskCase == null) {
226 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.openFiles.exception.msg"));
228 return tskCase.openFiles(dataSource, filePath);
261 public synchronized DerivedFile
addDerivedFile(String fileName, String localPath,
long size,
262 long ctime,
long crtime,
long atime,
long mtime,
263 boolean isFile, AbstractFile parentFile,
264 String rederiveDetails, String toolName, String toolVersion, String otherDetails)
throws TskCoreException {
266 if (tskCase == null) {
267 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.addDerivedFile.exception.msg"));
270 return tskCase.addDerivedFile(fileName, localPath, size,
271 ctime, crtime, atime, mtime,
272 isFile, parentFile, rederiveDetails, toolName, toolVersion, otherDetails);
289 public synchronized LayoutFile
addCarvedFile(String carvedFileName,
long carvedFileSize,
290 long systemId, List<TskFileRange> sectors)
throws TskCoreException {
292 if (tskCase == null) {
293 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.addCarvedFile.exception.msg"));
296 return tskCase.addCarvedFile(carvedFileName, carvedFileSize, systemId, sectors);
312 public List<LayoutFile>
addCarvedFiles(List<CarvedFileContainer> filesToAdd)
throws TskCoreException {
313 if (tskCase == null) {
314 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.addCarvedFile.exception.msg"));
316 return tskCase.addCarvedFiles(filesToAdd);
332 public void fileAdded(AbstractFile newFile);
354 List<java.io.File> rootsToAdd;
357 }
catch (TskDataException ex) {
358 throw new TskCoreException(ex.getLocalizedMessage(), ex);
361 CaseDbTransaction trans = tskCase.beginTransaction();
367 for (java.io.File localRootToAdd : rootsToAdd) {
368 AbstractFile localFileAdded =
addLocalDirInt(trans, fileSetRootDir, localRootToAdd, addProgressUpdater);
370 if (localFileAdded == null) {
371 String msg = NbBundle
372 .getMessage(this.getClass(),
"FileManager.addLocalFilesDirs.exception.cantAdd.msg",
373 localRootToAdd.getAbsolutePath());
374 logger.log(Level.SEVERE, msg);
375 throw new TskCoreException(msg);
387 }
catch (TskCoreException ex) {
390 return fileSetRootDir;
421 public synchronized LocalFilesDataSource
addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List<String> localFilePaths,
FileAddProgressUpdater progressUpdater)
throws TskCoreException, TskDataException {
423 CaseDbTransaction trans = null;
425 String rootDirectoryName = rootVirtualDirectoryName;
426 int newLocalFilesSetCount = curNumFileSets + 1;
427 if (rootVirtualDirectoryName.isEmpty()) {
430 trans = tskCase.beginTransaction();
431 LocalFilesDataSource dataSource = tskCase.addLocalFilesDataSource(deviceId, rootDirectoryName, timeZone, trans);
432 VirtualDirectory rootDirectory = dataSource.getRootDirectory();
433 List<AbstractFile> filesAdded =
new ArrayList<>();
434 for (java.io.File localFile : localFiles) {
435 AbstractFile fileAdded =
addLocalDirInt(trans, rootDirectory, localFile, progressUpdater);
436 if (null != fileAdded) {
437 filesAdded.add(fileAdded);
439 throw new TskCoreException(NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalFilesDirs.exception.cantAdd.msg", localFile.getAbsolutePath()));
443 if (rootVirtualDirectoryName.isEmpty()) {
444 curNumFileSets = newLocalFilesSetCount;
446 for (AbstractFile fileAdded : filesAdded) {
450 }
catch (TskCoreException ex) {
471 List<java.io.File> localFiles =
new ArrayList<>();
472 for (String path : localFilePaths) {
473 java.io.File localFile =
new java.io.File(path);
474 if (!localFile.exists() || !localFile.canRead()) {
475 throw new TskDataException(NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalFilesDirs.exception.notReadable.msg", localFile.getAbsolutePath()));
477 localFiles.add(localFile);
492 VirtualDirectory created = null;
494 int newFileSetCount = curNumFileSets + 1;
498 created = tskCase.addVirtualDirectory(0, fileSetName, trans);
499 curNumFileSets = newFileSetCount;
500 }
catch (TskCoreException ex) {
501 String msg = NbBundle
502 .getMessage(this.getClass(),
"FileManager.addLocalFileSetRootDir.exception.errCreateDir.msg",
504 logger.log(Level.SEVERE, msg, ex);
505 throw new TskCoreException(msg, ex);
525 private AbstractFile
addLocalDirInt(CaseDbTransaction trans, VirtualDirectory parentVd,
528 if (tskCase == null) {
529 throw new TskCoreException(
530 NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalDirInt.exception.closed.msg"));
534 if (!localFile.exists()) {
535 throw new TskCoreException(
536 NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalDirInt.exception.doesntExist.msg",
537 localFile.getAbsolutePath()));
539 if (!localFile.canRead()) {
540 throw new TskCoreException(
541 NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalDirInt.exception.notReadable.msg",
542 localFile.getAbsolutePath()));
545 if (localFile.isDirectory()) {
547 final VirtualDirectory childVd = tskCase.addVirtualDirectory(parentVd.getId(), localFile.getName(), trans);
548 if (childVd != null && addProgressUpdater != null) {
549 addProgressUpdater.fileAdded(childVd);
552 final java.io.File[] childrenFiles = localFile.listFiles();
553 if (childrenFiles != null) {
554 for (java.io.File childFile : childrenFiles) {
581 private synchronized LocalFile
addLocalFileInt(AbstractFile parentFile, java.io.File localFile, CaseDbTransaction trans)
throws TskCoreException {
583 if (tskCase == null) {
584 throw new TskCoreException(
585 NbBundle.getMessage(
this.getClass(),
"FileManager.addLocalDirInt2.exception.closed.msg"));
588 long size = localFile.length();
589 boolean isFile = localFile.isFile();
596 String fileName = localFile.getName();
598 LocalFile lf = tskCase.addLocalFile(fileName, localFile.getAbsolutePath(), size,
599 ctime, crtime, atime, mtime,
600 isFile, parentFile, trans);
606 public synchronized void close() throws IOException {
synchronized void close()
synchronized List< AbstractFile > findFiles(String fileName, AbstractFile parentFile)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, AbstractFile parentFile)
VirtualDirectory addLocalFileSetRootDir(CaseDbTransaction trans)
void fileAdded(AbstractFile newFile)
synchronized VirtualDirectory addLocalFilesDirs(List< String > localAbsPaths, FileAddProgressUpdater addProgressUpdater)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName)
List< LayoutFile > addCarvedFiles(List< CarvedFileContainer > filesToAdd)
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 addLocalDirInt(CaseDbTransaction trans, VirtualDirectory parentVd, java.io.File localFile, FileAddProgressUpdater addProgressUpdater)
synchronized LocalFile addLocalFileInt(AbstractFile parentFile, java.io.File localFile, CaseDbTransaction trans)
volatile int curNumFileSets
synchronized List< AbstractFile > openFiles(Content dataSource, String filePath)
synchronized LocalFilesDataSource addLocalFilesDataSource(String deviceId, String rootVirtualDirectoryName, String timeZone, List< String > localFilePaths, FileAddProgressUpdater progressUpdater)
synchronized List< AbstractFile > findFiles(Content dataSource, String fileName, String dirName)
static final Logger logger
void fireModuleContentEvent(ModuleContentEvent moduleContentEvent)
synchronized List< AbstractFile > findFiles(String fileName)
synchronized LayoutFile addCarvedFile(String carvedFileName, long carvedFileSize, long systemId, List< TskFileRange > sectors)
synchronized static Logger getLogger(String name)
FileManager(SleuthkitCase tskCase)
static final String LOGICAL_FILE_SET_PREFIX
List< java.io.File > getFilesAndDirectories(List< String > localFilePaths)
synchronized List< AbstractFile > findFiles(String fileName, String dirName)
static synchronized IngestServices getInstance()