20 package org.sleuthkit.autopsy.coreutils;
22 import java.io.Closeable;
24 import java.io.IOException;
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.logging.Level;
72 AbstractFile getAbstractFile() {
83 this.dbAbstractFile = appSQLiteDBFileBundle.getAbstractFile();
85 Class.forName(
"org.sqlite.JDBC");
86 connection = DriverManager.getConnection(
"jdbc:sqlite:" + appSQLiteDBFileBundle.getFileCopy().getPath());
87 statement = connection.createStatement();
109 String dbName,
boolean matchExactName, String parentPathSubstr) {
111 List<AppSQLiteDB> appDbs =
new ArrayList<> ();
113 Collection<AppSQLiteDBFileBundle> dbFileBundles =
findAndCopySQLiteDB( dataSource, dbName, matchExactName, parentPathSubstr,
false);
114 dbFileBundles.forEach((dbFileBundle) -> {
117 appDbs.add(appSQLiteDB);
118 }
catch (ClassNotFoundException | SQLException ex) {
119 Logger.
getLogger(
AppSQLiteDB.class.getName()).log(Level.SEVERE, String.format(
"Failed to open a DB connection for file = '%s' and path = '%s'.", dbFileBundle.dbAbstractFile.getName(), dbFileBundle.getFileCopy().getPath()), ex);
122 }
catch (TskCoreException ex) {
123 Logger.
getLogger(
AppSQLiteDB.class.getName()).log(Level.SEVERE, String.format(
"Error finding App database files with name = '%s' and path = '%s'.", dbName, parentPathSubstr), ex);
151 String dbPath, String dbAlias)
throws SQLException {
154 Collection<AppSQLiteDBFileBundle> dbFileBundles =
findAndCopySQLiteDB(dataSource, dbName,
true, dbPath,
true);
155 if (!dbFileBundles.isEmpty()) {
157 String attachDbSql = String.format(
"ATTACH DATABASE '%s' AS '%s'", dbFileBundle.getFileCopy().getPath(), dbAlias);
158 statement.executeUpdate(attachDbSql);
160 return dbFileBundle.getAbstractFile();
162 }
catch (TskCoreException ex) {
163 Logger.
getLogger(
AppSQLiteDB.class.getName()).log(Level.SEVERE, String.format(
"Error attaching to App database files with name = '%s' and path = '%s'.", dbName, dbPath), ex);
183 private static Collection<AppSQLiteDBFileBundle>
findAndCopySQLiteDB(DataSource dataSource, String dbName,
184 boolean matchExactName, String dbPath,
boolean matchExactPath)
throws TskCoreException {
190 throw new TskCoreException(
"Failed to get current case.", ex);
193 List<AppSQLiteDBFileBundle> dbFileBundles =
new ArrayList<> ();
195 String localDiskPath =
"";
198 String parentPath = dbPath.replace(
"\\",
"/");
199 parentPath = SleuthkitCase.escapeSingleQuotes(parentPath);
202 if (matchExactName) {
203 whereClause = String.format(
"LOWER(name) = LOWER('%s')", dbName);
205 whereClause = String.format(
"LOWER(name) LIKE LOWER('%%%s%%') AND LOWER(name) NOT LIKE LOWER('%%journal%%')", dbName );
207 if (matchExactPath) {
208 whereClause += String.format(
" AND LOWER(parent_path) = LOWER('%s')", parentPath );
210 whereClause += String.format(
" AND LOWER(parent_path) LIKE LOWER('%%%s%%')", parentPath );
212 whereClause += String.format(
" AND data_source_obj_id = %s", dataSource.getId());
214 List<AbstractFile> absFiles = skCase.findAllFilesWhere(whereClause);
215 for (AbstractFile absFile : absFiles) {
218 + File.separator + absFile.getId() + absFile.getName();
219 File jFile =
new java.io.File(localDiskPath);
220 fileId = absFile.getId();
228 dbFileBundles.add(dbFileBundle);
230 }
catch (ReadContentInputStream.ReadContentInputStreamException ex) {
231 Logger.
getLogger(
AppSQLiteDB.class.getName()).log(Level.WARNING, String.format(
"Error reading content from file '%s' (id=%d).", absFile.getName(), fileId), ex);
233 Logger.
getLogger(
AppSQLiteDB.class.getName()).log(Level.SEVERE, String.format(
"Error creating AppSQLiteDB for file '%s' (id=%d) to copied to '%s'.", absFile.getName(), fileId, localDiskPath), ex);
237 return dbFileBundles;
248 String detachDbSql = String.format(
"DETACH DATABASE '%s'", dbAlias);
249 statement.executeUpdate(detachDbSql);
263 public ResultSet
runQuery(String queryStr)
throws SQLException {
264 ResultSet resultSet = null;
266 if (null != queryStr) {
267 resultSet = statement.executeQuery(queryStr);
278 public void close() throws IOException {
284 }
catch (SQLException e) {
285 logger.log(Level.SEVERE,
"Error closing the database", e);
311 List<AbstractFile> metaFiles = fileManager.
findFiles(
312 sqliteFile.getDataSource(), metaFileName,
313 sqliteFile.getParent().getName());
315 if (metaFiles != null) {
316 for (AbstractFile metaFile : metaFiles) {
318 + File.separator + sqliteFile.getId() + metaFile.getName();
319 File localMetaFile =
new File(localDiskPath);
320 if (!localMetaFile.exists()) {
FileManager getFileManager()
String getTempDirectory()
static void findAndCopySQLiteMetaFile(AbstractFile sqliteFile, String metaFileName)
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
void detachDatabase(String dbAlias)
AbstractFile attachDatabase(DataSource dataSource, String dbName, String dbPath, String dbAlias)
final Connection connection
AppSQLiteDB(AppSQLiteDBFileBundle appSQLiteDBFileBundle)
static Collection< AppSQLiteDBFileBundle > findAndCopySQLiteDB(DataSource dataSource, String dbName, boolean matchExactName, String dbPath, boolean matchExactPath)
SleuthkitCase getSleuthkitCase()
static Collection< AppSQLiteDB > findAppDatabases(DataSource dataSource, String dbName, boolean matchExactName, String parentPathSubstr)
final Statement statement
ResultSet runQuery(String queryStr)
synchronized List< AbstractFile > findFiles(String fileName)
synchronized static Logger getLogger(String name)
static Case getCurrentCaseThrows()
final AbstractFile dbAbstractFile
final AbstractFile dbAbstractFile