19 package org.sleuthkit.autopsy.commandlineingest;
22 import java.io.FilenameFilter;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.logging.Level;
37 class CommandLineManager {
39 private static final String LOG_DIR_NAME =
"Command Output";
40 private static final Logger LOGGER = Logger.getLogger(CommandLineOpenCaseManager.class.getName());
50 Case openCase(String casePath)
throws CaseActionException {
52 String metadataFilePath;
53 if (casePath.endsWith(
".aut") && (
new File(casePath)).isFile()) {
54 LOGGER.log(Level.INFO,
"Opening case {0}", casePath);
55 metadataFilePath = casePath;
57 LOGGER.log(Level.INFO,
"Opening case in directory {0}", casePath);
58 metadataFilePath = findAutFile(casePath);
60 Case.openAsCurrentCase(metadataFilePath);
62 Case newCase = Case.getCurrentCase();
63 LOGGER.log(Level.INFO,
"Opened case {0}", newCase.getName());
77 private String findAutFile(String caseDirectory)
throws CaseActionException {
78 File caseFolder = Paths.get(caseDirectory).toFile();
79 if (caseFolder.exists()) {
83 File[] fileArray = caseFolder.listFiles();
84 if (fileArray == null) {
85 throw new CaseActionException(
"No files found in case directory");
87 String autFilePath = null;
88 for (File file : fileArray) {
89 String name = file.getName().toLowerCase();
90 if (autFilePath == null && name.endsWith(getFileExtension())) {
91 return file.getAbsolutePath();
94 throw new CaseActionException(
"No .aut files found in case directory");
96 throw new CaseActionException(
"Case directory was not found");
110 Case createCase(String baseCaseName, String rootOutputDirectory, Case.CaseType caseType) throws CaseActionException {
112 LOGGER.log(Level.INFO,
"Creating new case {0} in directory {1}",
new Object[]{baseCaseName, rootOutputDirectory});
113 Path caseDirectoryPath = findCaseDirectory(Paths.get(rootOutputDirectory), baseCaseName);
114 if (null != caseDirectoryPath) {
116 LOGGER.log(Level.SEVERE,
"Case {0} already exists. Case name must be unique. Exiting", baseCaseName);
117 throw new CaseActionException(
"Case " + baseCaseName +
" already exists. Case name must be unique. Exiting");
119 caseDirectoryPath = createCaseFolderPath(Paths.get(rootOutputDirectory), baseCaseName);
122 Case.createCaseDirectory(caseDirectoryPath.toString(), caseType);
124 CaseDetails caseDetails =
new CaseDetails(baseCaseName);
125 Case.createAsCurrentCase(caseType, caseDirectoryPath.toString(), caseDetails);
128 Case caseForJob = Case.getCurrentCase();
129 LOGGER.log(Level.INFO,
"Created case {0}", caseForJob.getName());
142 private Path createCaseFolderPath(Path caseFoldersPath, String caseName) {
143 String folderName = caseName +
"_" + TimeStampUtils.createTimeStamp();
144 return Paths.get(caseFoldersPath.toString(), folderName);
156 Case openExistingCase(String caseName, String rootOutputDirectory)
throws CaseActionException {
157 LOGGER.log(Level.INFO,
"Opening case {0} in directory {1}",
new Object[]{caseName, rootOutputDirectory});
158 Path caseDirectoryPath = findCaseDirectory(Paths.get(rootOutputDirectory), caseName);
159 if (null != caseDirectoryPath) {
161 Path metadataFilePath = caseDirectoryPath.resolve(caseName + CaseMetadata.getFileExtension());
162 Case.openAsCurrentCase(metadataFilePath.toString());
165 LOGGER.log(Level.SEVERE,
"Case {0} doesn't exist. Exiting", caseName);
166 throw new CaseActionException(
"Case " + caseName +
" doesn't exist. Exiting");
169 Case caseForJob = Case.getCurrentCase();
170 LOGGER.log(Level.INFO,
"Opened case {0}", caseForJob.getName());
184 private Path findCaseDirectory(Path folderToSearch, String caseName) {
185 File searchFolder =
new File(folderToSearch.toString());
186 if (!searchFolder.isDirectory()) {
189 Path caseFolderPath = null;
190 String[] candidateFolders = searchFolder.list(
new CaseFolderFilter(caseName));
191 long mostRecentModified = 0;
192 for (String candidateFolder : candidateFolders) {
193 File file =
new File(candidateFolder);
194 if (file.lastModified() >= mostRecentModified) {
195 mostRecentModified = file.lastModified();
196 caseFolderPath = Paths.get(folderToSearch.toString(), file.getPath());
199 return caseFolderPath;
209 String getOutputDirPath(Case caseForJob) {
210 return caseForJob.getCaseDirectory() + File.separator + LOG_DIR_NAME;
223 public boolean accept(File folder, String fileName) {
224 File file =
new File(folder, fileName);
227 if (null != caseName) {
229 if (fileNamePrefix.equals(caseName)) {
249 for (File file : folder.listFiles()) {
250 if (file.getName().toLowerCase().endsWith(CASE_METADATA_EXT) && file.isFile()) {
static boolean hasCaseMetadataFile(File folder)
static boolean endsWithTimeStamp(String inputString)
static int getTimeStampLength()
boolean accept(File folder, String fileName)
static final String CASE_METADATA_EXT