@Test public void testLockMissing_TempIndex() throws Exception { final File idx = new File(db.getDirectory(), "tmp_index"); final File lck = new File(db.getDirectory(), "tmp_index.lock"); assertFalse(idx.exists()); assertFalse(lck.exists()); final DirCache dc = DirCache.lock(idx, db.getFS()); assertNotNull(dc); assertFalse(idx.exists()); assertTrue(lck.exists()); assertEquals(0, dc.getEntryCount()); dc.unlock(); assertFalse(idx.exists()); assertFalse(lck.exists()); }
/** * Create a new in-core index representation, lock it, and read from disk. * * <p>The new index will be locked and then read before it is returned to the caller. Read * failures are reported as exceptions and therefore prevent the method from returning a partially * populated index. On read failure, the lock is released. * * @param indexLocation location of the index file on disk. * @param fs the file system abstraction which will be necessary to perform certain file system * operations. * @return a cache representing the contents of the specified index file (if it exists) or an * empty cache if the file does not exist. * @throws IOException the index file is present but could not be read, or the lock could not be * obtained. * @throws CorruptObjectException the index file is using a format or extension that this library * does not support. */ public static DirCache lock(final File indexLocation, final FS fs) throws CorruptObjectException, IOException { final DirCache c = new DirCache(indexLocation, fs); if (!c.lock()) throw new LockFailedException(indexLocation); try { c.read(); } catch (IOException e) { c.unlock(); throw e; } catch (RuntimeException e) { c.unlock(); throw e; } catch (Error e) { c.unlock(); throw e; } return c; }