/// <summary> /// Reread index data from disk if the index file has been changed /// </summary> /// <exception cref="IOException"> </exception> public void RereadIfNecessary() { if (_cacheFile.Exists && _cacheFile.LastWriteTime.Ticks != _lastCacheTime) { Read(); Repository.fireIndexChanged(); } }
/// <summary> /// Reread index data from disk if the index file has been changed /// </summary> /// <exception cref="IOException"> </exception> public void RereadIfNecessary() { _cacheFile.Refresh(); if (_cacheFile.Exists && _cacheFile.lastModified() != _lastCacheTime) { Read(); Repository.fireIndexChanged(); } }
/// <summary> /// Write content of index to disk. /// </summary> /// <exception cref="IOException"> </exception> public void write() { CheckWriteOk(); var tmpIndex = new FileInfo(_cacheFile.FullName + ".tmp"); var @lock = new FileInfo(_cacheFile.FullName + ".lock"); try { using (@lock.Create()) { } } catch (IOException) { throw new IOException("Index file is in use"); } try { using (var fileOutputStream = new FileStream(tmpIndex.FullName, System.IO.FileMode.CreateNew)) using (var writer = new EndianBinaryWriter(new BigEndianBitConverter(), fileOutputStream)) { MessageDigest newMessageDigest = Constants.newMessageDigest(); var ms = new MemoryStream(); _header = new Header(_entries.Values as ICollection); _header.Write(ms); newMessageDigest.Update(ms.ToArray()); writer.Write(ms.ToArray()); foreach (Entry entry in _entries.Values) { ms = new MemoryStream(); entry.Write(ms); newMessageDigest.Update(ms.ToArray()); writer.Write(ms.ToArray()); } ms = new MemoryStream(); byte[] digestBuffer = newMessageDigest.Digest(); ms.Write(digestBuffer, 0, digestBuffer.Length); writer.Write(ms.ToArray(), 0, digestBuffer.Length); } _cacheFile.Refresh(); if (_cacheFile.Exists) { try { _cacheFile.Delete(); } catch (IOException) { throw new IOException("Could not rename delete old index"); } } if (!tmpIndex.RenameTo(_cacheFile.FullName)) { throw new IOException("Could not rename temporary index file to index"); } _changed = false; _statDirty = false; _cacheFile.Refresh(); _lastCacheTime = _cacheFile.lastModified(); Repository.fireIndexChanged(); } finally { try { @lock.Delete(); } catch (IOException) { throw new IOException("Could not delete lock file. Should not happen"); } try { tmpIndex = new FileInfo(_cacheFile.FullName + ".tmp"); if (tmpIndex.Exists) { tmpIndex.Delete(); } } catch (Exception) { throw new IOException("Could not delete temporary index file. Should not happen"); } } }