23 package org.sleuthkit.autopsy.recentactivity;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.nio.MappedByteBuffer;
30 import java.nio.channels.FileChannel;
31 import java.nio.charset.Charset;
32 import java.sql.ResultSet;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.logging.Level;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
53 private static Logger logger = Logger.getLogger(Util.class.getName());
56 private static final long FILETIME_EPOCH_DIFF = 11644473600000L;
59 private static final long FILETIME_ONE_MILLISECOND = 10 * 1000;
64 public static boolean pathexists(String path) {
65 File file =
new File(path);
66 boolean exists = file.exists();
70 public static String utcConvert(String utc) {
71 SimpleDateFormat formatter =
new SimpleDateFormat(
"MM-dd-yyyy HH:mm");
72 String tempconvert = formatter.format(
new Date(Long.parseLong(utc)));
76 public static String readFile(String path)
throws IOException {
77 FileInputStream stream =
new FileInputStream(
new File(path));
79 FileChannel fc = stream.getChannel();
80 MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
84 return Charset.defaultCharset().decode(bb).toString();
90 public static String getFileName(String value) {
92 String filematch =
"^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$";
94 Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
95 Matcher m = p.matcher(value);
97 filename = m.group(1);
100 int lastPos = value.lastIndexOf(
'\\');
101 filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
102 return filename.toString();
105 public static String getPath(String txt) {
109 String drive =
"([a-z]:\\\\\\S.+)";
110 Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
111 Matcher m = p.matcher(txt);
117 String network =
"(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)";
119 Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
120 Matcher m2 = p2.matcher(txt);
128 public static long findID(Content dataSource, String path) {
129 String parent_path = path.replace(
'\\',
'/');
130 if (parent_path.length() > 2 && parent_path.charAt(1) ==
':') {
131 parent_path = parent_path.substring(2);
133 int index = parent_path.lastIndexOf(
'/');
134 String name = parent_path.substring(++index);
135 parent_path = parent_path.substring(0, index);
136 List<AbstractFile> files = null;
138 FileManager fileManager = Case.getCurrentCaseThrows().getServices().getFileManager();
139 files = fileManager.findFiles(dataSource, name, parent_path);
140 }
catch (TskCoreException | NoCurrentCaseException ex) {
141 logger.log(Level.WARNING,
"Error fetching 'index.data' files for Internet Explorer history.");
144 if (files == null || files.isEmpty()) {
147 return files.get(0).getId();
150 public static boolean checkColumn(String column, String tablename, String connection) {
151 String query =
"PRAGMA table_info(" + tablename +
")";
152 boolean found =
false;
154 SQLiteDBConnect tempdbconnect = null;
156 tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
157 temprs = tempdbconnect.executeQry(query);
158 while (temprs.next()) {
159 if (temprs.getString(
"name") == null ? column == null : temprs.getString(
"name").equals(column)) {
163 }
catch (Exception ex) {
164 logger.log(Level.WARNING,
"Error while trying to get columns from sqlite db." + connection, ex);
167 if (tempdbconnect != null) {
168 tempdbconnect.closeConnection();
174 public static ResultSet runQuery(String query, String connection) {
175 ResultSet results = null;
177 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
178 results = tempdbconnect.executeQry(query);
179 tempdbconnect.closeConnection();
180 }
catch (Exception ex) {
181 logger.log(Level.WARNING,
"Error while trying to run sql query: " + query +
" : " + connection, ex);
193 static long filetimeToMillis(
final long filetime) {
194 return (filetime / FILETIME_ONE_MILLISECOND) - FILETIME_EPOCH_DIFF;