diff --git a/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java b/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java index dbd4554a645886bf600e154cce8db3ff7a1b2d9c..bff959e7d30326ab245103b38fbc77f3fe8e1c0f 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java +++ b/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java @@ -92,6 +92,7 @@ public class SleuthkitCase { private PreparedStatement getFileSt; private PreparedStatement getFileWithParentSt; private PreparedStatement updateMd5St; + private PreparedStatement getLastContentIdSt; private static final Logger logger = Logger.getLogger(SleuthkitCase.class.getName()); /** @@ -194,6 +195,8 @@ private void initStatements() throws SQLException { getFileWithParentSt = con.prepareStatement("SELECT * FROM tsk_files WHERE LOWER(name) LIKE ? AND LOWER(name) NOT LIKE '%journal%' AND LOWER(parent_path) LIKE ? AND fs_obj_id = ?"); updateMd5St = con.prepareStatement("UPDATE tsk_files SET md5 = ? WHERE obj_id = ?"); + + getLastContentIdSt = con.prepareStatement("SELECT MAX(obj_id) from tsk_objects"); } private void closeStatements() { @@ -295,6 +298,11 @@ private void closeStatements() { updateMd5St.close(); updateMd5St = null; } + + if (getLastContentIdSt != null) { + getLastContentIdSt.close(); + getLastContentIdSt = null; + } } catch (SQLException e) { logger.log(Level.WARNING, @@ -3366,6 +3374,44 @@ public int countFsContentType(TskData.TSK_FS_META_TYPE_ENUM contentType) throws } return count; } + + /** + * Get last (max) object id of content object in tsk_objects. + * + * Note, if you are using this id to create a new object, make sure you are + * getting and using it in the same write lock/transaction to avoid + * potential concurrency issues with other writes + * + * @return currently max id + * @throws TskCoreException exception thrown when database error occurs and + * last object id could not be queried + */ + public long getLastObjectId() throws TskCoreException { + long id = -1; + ResultSet rs = null; + dbReadLock(); + try { + rs = getLastContentIdSt.executeQuery(); + if (rs.next()) { + id = rs.getLong(1); + } + } catch (SQLException e) { + final String msg = "Error getting last object id."; + logger.log(Level.SEVERE, msg, e); + throw new TskCoreException(msg, e); + } finally { + if (rs != null) { + try { + rs.close(); + } catch (SQLException ex) { + logger.log(Level.SEVERE, "Error closing result set after getting last object id.", ex); + } + } + dbReadUnlock(); + } + + return id; + } /** * Escape the single quotes in the given string so they can be added to the