23 package org.sleuthkit.autopsy.recentactivity;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
31 import java.nio.MappedByteBuffer;
32 import java.nio.channels.FileChannel;
33 import java.nio.charset.Charset;
34 import java.sql.ResultSet;
35 import java.text.SimpleDateFormat;
36 import java.util.Date;
37 import java.util.List;
38 import java.util.StringTokenizer;
39 import java.util.logging.Level;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
56 private static Logger logger = Logger.getLogger(Util.class.getName());
61 public static boolean pathexists(String path) {
62 File file =
new File(path);
63 boolean exists = file.exists();
67 public static String utcConvert(String utc) {
68 SimpleDateFormat formatter =
new SimpleDateFormat(
"MM-dd-yyyy HH:mm");
69 String tempconvert = formatter.format(
new Date(Long.parseLong(utc)));
73 public static String readFile(String path)
throws IOException {
74 FileInputStream stream =
new FileInputStream(
new File(path));
76 FileChannel fc = stream.getChannel();
77 MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
81 return Charset.defaultCharset().decode(bb).toString();
87 public static String getFileName(String value) {
89 String filematch =
"^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$";
91 Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
92 Matcher m = p.matcher(value);
94 filename = m.group(1);
97 int lastPos = value.lastIndexOf(
'\\');
98 filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
99 return filename.toString();
102 public static String getPath(String txt) {
106 String drive =
"([a-z]:\\\\\\S.+)";
107 Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
108 Matcher m = p.matcher(txt);
114 String network =
"(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)";
116 Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
117 Matcher m2 = p2.matcher(txt);
125 public static long findID(Content dataSource, String path) {
126 String parent_path = path.replace(
'\\',
'/');
127 if (parent_path.length() > 2 && parent_path.charAt(1) ==
':') {
128 parent_path = parent_path.substring(2);
130 int index = parent_path.lastIndexOf(
'/');
131 String name = parent_path.substring(++index);
132 parent_path = parent_path.substring(0, index);
133 List<AbstractFile> files = null;
135 FileManager fileManager = Case.getCurrentCaseThrows().getServices().getFileManager();
136 files = fileManager.findFiles(dataSource, name, parent_path);
137 }
catch (TskCoreException | NoCurrentCaseException ex) {
138 logger.log(Level.WARNING,
"Error fetching 'index.data' files for Internet Explorer history.");
141 if (files == null || files.isEmpty()) {
144 return files.get(0).getId();
147 public static boolean checkColumn(String column, String tablename, String connection) {
148 String query =
"PRAGMA table_info(" + tablename +
")";
149 boolean found =
false;
152 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
153 temprs = tempdbconnect.executeQry(query);
154 while (temprs.next()) {
155 if (temprs.getString(
"name") == null ? column == null : temprs.getString(
"name").equals(column)) {
159 }
catch (Exception ex) {
160 logger.log(Level.WARNING,
"Error while trying to get columns from sqlite db." + connection, ex);
165 public static ResultSet runQuery(String query, String connection) {
166 ResultSet results = null;
168 SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + connection);
169 results = tempdbconnect.executeQry(query);
170 tempdbconnect.closeConnection();
171 }
catch (Exception ex) {
172 logger.log(Level.WARNING,
"Error while trying to run sql query: " + query +
" : " + connection, ex);