19 package org.sleuthkit.autopsy.datasourcesummary.datamodel;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.ArrayList;
24 import java.util.Comparator;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.SortedMap;
28 import java.util.TreeMap;
31 import org.apache.commons.lang.StringUtils;
36 import org.
sleuthkit.datamodel.TskData.TSK_DB_FILES_TYPE_ENUM;
37 import org.
sleuthkit.datamodel.TskData.TSK_FS_META_FLAG_ENUM;
38 import org.
sleuthkit.datamodel.TskData.TSK_FS_META_TYPE_ENUM;
44 final class DataSourceInfoUtilities {
58 static Long getCountOfTskFiles(SleuthkitCase skCase, DataSource currentDataSource, String additionalWhere)
59 throws TskCoreException, SQLException {
60 if (currentDataSource != null) {
61 return skCase.countFilesWhere(
62 "data_source_obj_id=" + currentDataSource.getId()
63 + (StringUtils.isBlank(additionalWhere) ?
"" : (
" AND " + additionalWhere)));
80 static Long getCountOfRegularFiles(SleuthkitCase skCase, DataSource currentDataSource, String additionalWhere)
81 throws TskCoreException, SQLException {
82 String whereClause =
"meta_type=" + TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_REG.getValue();
84 if (StringUtils.isNotBlank(additionalWhere)) {
85 whereClause +=
" AND " + additionalWhere;
88 return getCountOfTskFiles(skCase, currentDataSource, whereClause);
103 static Long getCountOfRegNonSlackFiles(SleuthkitCase skCase, DataSource currentDataSource, String additionalWhere)
104 throws TskCoreException, SQLException {
105 String whereClause =
"meta_type=" + TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_REG.getValue()
106 +
" AND type<>" + TSK_DB_FILES_TYPE_ENUM.SLACK.getFileType();
108 if (StringUtils.isNotBlank(additionalWhere)) {
109 whereClause +=
" AND " + additionalWhere;
112 return getCountOfTskFiles(skCase, currentDataSource, whereClause);
118 interface ResultSetHandler<T> {
120 T process(ResultSet resultset)
throws SQLException;
136 static <T> T getBaseQueryResult(SleuthkitCase skCase, String query, ResultSetHandler<T> processor)
137 throws TskCoreException, SQLException {
138 try (SleuthkitCase.CaseDbQuery dbQuery = skCase.executeQuery(query)) {
139 ResultSet resultSet = dbQuery.getResultSet();
140 return processor.process(resultSet);
152 static String getMetaFlagsContainsStatement(TSK_FS_META_FLAG_ENUM flag) {
153 return "meta_flags & " + flag.getValue() +
" > 0";
184 static List<BlackboardArtifact> getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder)
throws TskCoreException {
185 return getArtifacts(skCase, artifactType, dataSource, attributeType, sortOrder, 0);
210 static List<BlackboardArtifact> getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder,
int maxCount)
throws TskCoreException {
212 throw new IllegalArgumentException(
"Invalid maxCount passed to getArtifacts, value must be equal to or greater than 0");
215 return createListFromMap(getArtifactMap(skCase, artifactType, dataSource, attributeType, sortOrder), maxCount);
221 private DataSourceInfoUtilities() {
240 static private SortedMap<BlackboardAttribute, List<BlackboardArtifact>> getArtifactMap(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder)
throws TskCoreException {
241 SortedMap<BlackboardAttribute, List<BlackboardArtifact>> sortedMap =
new TreeMap<>(
new AttributeComparator(sortOrder));
242 List<BlackboardArtifact> artifactList = skCase.getBlackboard().getArtifacts(artifactType.getTypeID(), dataSource.getId());
244 for (BlackboardArtifact artifact : artifactList) {
245 BlackboardAttribute attribute = artifact.getAttribute(attributeType);
246 if (attribute == null) {
250 List<BlackboardArtifact> mapArtifactList = sortedMap.get(attribute);
251 if (mapArtifactList == null) {
252 mapArtifactList =
new ArrayList<>();
253 sortedMap.put(attribute, mapArtifactList);
256 mapArtifactList.add(artifact);
270 static private List<BlackboardArtifact> createListFromMap(SortedMap<BlackboardAttribute, List<BlackboardArtifact>> sortedMap,
int maxCount) {
271 List<BlackboardArtifact> artifactList =
new ArrayList<>();
273 for (List<BlackboardArtifact> mapArtifactList : sortedMap.values()) {
275 if (maxCount == 0 || (artifactList.size() + mapArtifactList.size()) <= maxCount) {
276 artifactList.addAll(mapArtifactList);
280 if (maxCount == artifactList.size()) {
284 for (BlackboardArtifact artifact : mapArtifactList) {
285 if (artifactList.size() < maxCount) {
286 artifactList.add(artifact);
313 public int compare(BlackboardAttribute attribute1, BlackboardAttribute attribute2) {
314 if (!attribute1.getAttributeType().equals(attribute2.getAttributeType())) {
315 throw new IllegalArgumentException(
"Unable to compare attributes of different types");
318 int result =
compare(attribute1.getAttributeType(), attribute1, attribute2);
320 if (direction == SortOrder.DESCENDING) {
338 private int compare(BlackboardAttribute.Type type, BlackboardAttribute attribute1, BlackboardAttribute attribute2) {
339 switch (type.getValueType()) {
341 return attribute1.getValueString().compareToIgnoreCase(attribute2.getValueString());
343 return Integer.compare(attribute1.getValueInt(), attribute2.getValueInt());
346 return Long.compare(attribute1.getValueLong(), attribute2.getValueLong());
348 return Double.compare(attribute1.getValueDouble(), attribute2.getValueDouble());
352 throw new IllegalArgumentException(
"Unable to compare attributes of type " + attribute1.getAttributeType().getTypeName());
366 private static BlackboardAttribute getAttributeOrNull(BlackboardArtifact artifact, Type attributeType) {
368 return artifact.getAttribute(attributeType);
369 }
catch (TskCoreException ex) {
383 static String getStringOrNull(BlackboardArtifact artifact, Type attributeType) {
384 BlackboardAttribute attr = getAttributeOrNull(artifact, attributeType);
385 return (attr == null) ? null : attr.getValueString();
397 static Long getLongOrNull(BlackboardArtifact artifact, Type attributeType) {
398 BlackboardAttribute attr = getAttributeOrNull(artifact, attributeType);
399 return (attr == null) ? null : attr.getValueLong();
411 static Integer getIntOrNull(BlackboardArtifact artifact, Type attributeType) {
412 BlackboardAttribute attr = getAttributeOrNull(artifact, attributeType);
413 return (attr == null) ? null : attr.getValueInt();
426 static Date getDateOrNull(BlackboardArtifact artifact, Type attributeType) {
427 Long longVal = getLongOrNull(artifact, attributeType);
428 return (longVal == null || longVal == 0) ? null :
new Date(longVal * 1000);
int compare(BlackboardAttribute attribute1, BlackboardAttribute attribute2)
final SortOrder direction
int compare(BlackboardAttribute.Type type, BlackboardAttribute attribute1, BlackboardAttribute attribute2)