Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
IndexFinder.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.keywordsearch;
20 
21 import java.io.File;
22 import java.nio.file.Paths;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27 import org.apache.commons.lang.math.NumberUtils;
31 
35 class IndexFinder {
36 
37  private static final Logger logger = Logger.getLogger(IndexFinder.class.getName());
38  private static final String KWS_OUTPUT_FOLDER_NAME = "keywordsearch";
39  private static final String KWS_DATA_FOLDER_NAME = "data";
40  private static final String INDEX_FOLDER_NAME = "index";
41  private static final String CURRENT_SOLR_VERSION = "4";
42  private static final String CURRENT_SOLR_SCHEMA_VERSION = "2.0";
43 
44  static String getCurrentSolrVersion() {
45  return CURRENT_SOLR_VERSION;
46  }
47 
48  static String getCurrentSchemaVersion() {
49  return CURRENT_SOLR_SCHEMA_VERSION;
50  }
51 
52  static Index findLatestVersionIndexDir(List<Index> allIndexes) {
53  for (Index index : allIndexes) {
54  if (index.getSolrVersion().equals(CURRENT_SOLR_VERSION) && index.getSchemaVersion().equals(CURRENT_SOLR_SCHEMA_VERSION)) {
55  return index;
56  }
57  }
58  return null;
59  }
60 
61  static Index createLatestVersionIndexDir(Case theCase) throws AutopsyService.AutopsyServiceException {
62  String indexFolderName = "solr" + CURRENT_SOLR_VERSION + "_schema" + CURRENT_SOLR_SCHEMA_VERSION;
63  // new index should be stored in "\ModuleOutput\keywordsearch\data\solrX_schemaY\index"
64  File targetDirPath = Paths.get(theCase.getModuleDirectory(), KWS_OUTPUT_FOLDER_NAME, KWS_DATA_FOLDER_NAME, indexFolderName, INDEX_FOLDER_NAME).toFile(); //NON-NLS
65  if (!targetDirPath.mkdirs()) {
66  throw new AutopsyService.AutopsyServiceException("Unable to create text index directory " + targetDirPath.getAbsolutePath());
67  }
68  return new Index(targetDirPath.getAbsolutePath(), CURRENT_SOLR_VERSION, CURRENT_SOLR_SCHEMA_VERSION, "", theCase.getName());
69  }
70 
71  static Index identifyIndexToUse(List<Index> allIndexes) {
72  /*
73  * NOTE: All of the following paths are valid multi-user index paths:
74  * (Solr 4, schema 1.8)
75  * X:\Case\ingest1\ModuleOutput\keywordsearch\data\index
76  * X:\Case\ingest4\ModuleOutput\keywordsearch\data\solr6_schema2.0\index
77  * X:\Case\ingest4\ModuleOutput\keywordsearch\data\solr6_schema1.8\index
78  * X:\Case\ingest4\ModuleOutput\keywordsearch\data\solr7_schema2.0\index
79  */
80  Index bestCandidateIndex = null;
81  double solrVerFound = 0.0;
82  double schemaVerFound = 0.0;
83  for (Index index : allIndexes) {
84  // higher Solr version takes priority because it may negate index upgrade
85  if (NumberUtils.toDouble(index.getSolrVersion()) >= solrVerFound) {
86  // if same solr version, pick the one with highest schema version
87  if (NumberUtils.toDouble(index.getSchemaVersion()) >= schemaVerFound) {
88  bestCandidateIndex = index;
89  solrVerFound = NumberUtils.toDouble(index.getSolrVersion());
90  schemaVerFound = NumberUtils.toDouble(index.getSchemaVersion());
91  }
92  }
93  }
94  return bestCandidateIndex;
95  }
96 
106  static Index findOldIndexDir(Case theCase) {
107  // first find all existing "/ModuleOutput/keywordsearch/data/" folders
108  if (theCase.getCaseType() == Case.CaseType.MULTI_USER_CASE) {
109  // multi user cases contain a subfolder for each node that participated in case ingest or review.
110  // Any one (but only one!) of those subfolders may contain the actual index.
111  /*
112  * NOTE: the following path is an example of valid Solr 4 Schema 1.8
113  * multi-user index path:
114  * X:\Case\ingest1\ModuleOutput\keywordsearch\data\index
115  */
116 
117  // get a list of all folder's contents
118  List<File> contents = getAllContentsInFolder(theCase.getCaseDirectory());
119  if (!contents.isEmpty()) {
120  // decipher "ModuleOutput" directory name from module output path
121  // (e.g. X:\Case\ingest4\ModuleOutput\) because there is no other way to get it...
122  String moduleOutDirName = new File(theCase.getModuleDirectory()).getName();
123 
124  // scan all topLevelOutputDir subfolders for presence of non-empty "/ModuleOutput/keywordsearch/data/" folder
125  for (File item : contents) {
126  File path = Paths.get(item.getAbsolutePath(), moduleOutDirName, KWS_OUTPUT_FOLDER_NAME, KWS_DATA_FOLDER_NAME, INDEX_FOLDER_NAME).toFile(); //NON-NLS
127  // must be a non-empty index directory
128  if (isNonEmptyIndexFolder(path)) {
129  return new Index(path.toString(), "4", "1.8", theCase.getTextIndexName(), theCase.getName());
130  }
131  }
132  }
133  } else {
134  // single user case
135  /*
136  * NOTE: the following path is valid single user Solr 4 Schema 1.8
137  * index path: X:\Case\ModuleOutput\keywordsearch\data\index
138  */
139  File path = Paths.get(theCase.getModuleDirectory(), KWS_OUTPUT_FOLDER_NAME, KWS_DATA_FOLDER_NAME, INDEX_FOLDER_NAME).toFile(); //NON-NLS
140  // must be a non-empty index directory
141  if (isNonEmptyIndexFolder(path)) {
142  return new Index(path.toString(), "4", "1.8", theCase.getTextIndexName(), theCase.getName());
143  }
144  }
145  return null;
146  }
147 
155  private static List<File> getAllContentsInFolder(String path) {
156  File directory = new File(path);
157  File[] contents = directory.listFiles();
158  if (contents == null) {
159  // the directory file is not really a directory..
160  return Collections.emptyList();
161  } else if (contents.length == 0) {
162  // Folder is empty
163  return Collections.emptyList();
164  } else {
165  // Folder has contents
166  return new ArrayList<>(Arrays.asList(contents));
167  }
168  }
169 
170  private static boolean isNonEmptyIndexFolder(File path) {
171  if (path.exists() && path.isDirectory() && path.getName().equals(INDEX_FOLDER_NAME) && path.listFiles().length > 0) {
172  return true;
173  }
174  return false;
175  }
176 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.