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             files = Case.getCurrentCaseThrows().getSleuthkitCase().getFileManager().findFilesExactNameExactPath(dataSource, name, parent_path);
 
  139         } 
catch (TskCoreException | NoCurrentCaseException ex) {
 
  140             logger.log(Level.WARNING, 
"Error fetching 'index.data' files for Internet Explorer history."); 
 
  143         if (files == null || files.isEmpty()) {
 
  146         return files.get(0).getId();
 
  149     public static boolean checkColumn(String column, String tablename, String connection) {
 
  150         String query = 
"PRAGMA table_info(" + tablename + 
")"; 
 
  151         boolean found = 
false;
 
  153         SQLiteDBConnect tempdbconnect = null;
 
  155             tempdbconnect = 
new SQLiteDBConnect(
"org.sqlite.JDBC", 
"jdbc:sqlite:" + connection); 
 
  156             temprs = tempdbconnect.executeQry(query);
 
  157             while (temprs.next()) {
 
  158                 if (temprs.getString(
"name") == null ? column == null : temprs.getString(
"name").equals(column)) { 
 
  162         } 
catch (Exception ex) {
 
  163             logger.log(Level.WARNING, 
"Error while trying to get columns from sqlite db." + connection, ex); 
 
  166             if (tempdbconnect != null) {
 
  167                 tempdbconnect.closeConnection();
 
  173     public static ResultSet runQuery(String query, String connection) {
 
  174         ResultSet results = null;
 
  176             SQLiteDBConnect tempdbconnect = 
new SQLiteDBConnect(
"org.sqlite.JDBC", 
"jdbc:sqlite:" + connection); 
 
  177             results = tempdbconnect.executeQry(query);
 
  178             tempdbconnect.closeConnection();
 
  179         } 
catch (Exception ex) {
 
  180             logger.log(Level.WARNING, 
"Error while trying to run sql query: " + query + 
" : " + connection, ex); 
 
  192     static long filetimeToMillis(
final long filetime) {
 
  193         return (filetime / FILETIME_ONE_MILLISECOND) - FILETIME_EPOCH_DIFF;