private void Load(FileInfo file)
{
_log.InfoFormat("Reading block store from {0}", file);
if (_channel != null)
{
_channel.Dispose();
}
_channel = file.OpenRead();
try
{
// Read a version byte.
var version = _channel.Read();
if (version == -1)
{
// No such file or the file was empty.
throw new FileNotFoundException(file.Name + " does not exist or is empty");
}
if (version != _fileFormatVersion)
{
throw new BlockStoreException("Bad version number: " + version);
}
// Chain head pointer is the first thing in the file.
var chainHeadHash = new byte[32];
if (_channel.Read(chainHeadHash) < chainHeadHash.Length)
throw new BlockStoreException("Truncated store: could not read chain head hash.");
_chainHead = new Sha256Hash(chainHeadHash);
_log.InfoFormat("Read chain head from disk: {0}", _chainHead);
_channel.Position = _channel.Length - Record.Size;
}
catch (IOException)
{
_channel.Close();
throw;
}
catch (BlockStoreException)
{
_channel.Close();
throw;
}
}