/// <summary>
/// Create a new in-core index representation, lock it, and read from disk.
/// <para />
/// 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.
/// </summary>
/// <param name="indexLocation">
/// location of the index file on disk.
/// </param>
/// <returns>
/// A cache representing the contents of the specified index file (if
/// it exists) or an empty cache if the file does not exist.
/// </returns>
/// <exception cref="IOException">
/// The index file is present but could not be read, or the lock
/// could not be obtained.
/// </exception>
/// <exception cref="CorruptObjectException">
/// the index file is using a format or extension that this
/// library does not support.
/// </exception>
public static DirCache Lock(FileInfo indexLocation)
{
var c = new DirCache(indexLocation);
if (!c.Lock())
{
throw new IOException("Cannot lock " + indexLocation);
}
try
{
c.read();
}
catch (Exception)
{
c.unlock();
throw;
}
return(c);
}