private void Read() { string manifestFile = Config.ManifestFile(_baseFileName); if (!File.Exists(manifestFile)) { AddLastManifest(new ManifestImmutable(this)); return; } FileStream fs = new FileStream(manifestFile, FileMode.Open, FileAccess.Read, FileShare.None, 1024, false); BinaryReader reader = new BinaryReader(fs); try { var m = new ManifestImmutable(this); // Get the size of the last manifest block reader.BaseStream.Seek(-4, SeekOrigin.End); int size = reader.ReadInt32(); // Now seek to that position and read it reader.BaseStream.Seek(-size - 4, SeekOrigin.End); m.ReadManifestContents(reader); AddLastManifest(m); } catch (Exception ex) { LogMessage("Error reading manifest file: {0} - {1}", _baseFileName, ex.Message); } finally { reader.Close(); } }
public static IEnumerable <ManifestImmutable> ReadAllManifests(string baseFileName) { string manifestFile = Config.ManifestFile(baseFileName); if (!File.Exists(manifestFile)) { throw new FileNotFoundException("Could not find the manifest file.", manifestFile); } FileStream fs = new FileStream(manifestFile, FileMode.Open, FileAccess.Read, FileShare.None, 1024, false); BinaryReader reader = new BinaryReader(fs); try { do { var m = new ManifestImmutable(null); m.ReadManifestContents(reader); yield return(m); reader.ReadInt32(); // Consume the size encoded at the end of each manifest } while (true); } finally { reader.Close(); } }
private void Write(ManifestImmutable m) { // Get an in-memory copy of the all the bytes that will be written to the manifest var ms = new MemoryStream(); var writer = new BinaryWriter(ms); m.WriteManifestContents(writer); writer.Close(); var manifestBytes = ms.ToArray(); // Increment manifest versions, we're getting ready to write another copy _manifestVersion++; _altManifestVersion++; // Write primary manifest FileMode fileMode = FileMode.Append; if (_manifestVersion > Config.ManifestVersionCount) { fileMode = FileMode.Create; _manifestVersion = 0; } using (FileStream mfs = new FileStream(Config.ManifestFile(BaseFileName), fileMode, FileAccess.Write, FileShare.None, 8, FileOptions.WriteThrough)) { IAsyncResult manifestWrite = mfs.BeginWrite(manifestBytes, 0, manifestBytes.Length, null, null); // Write alternative manifest fileMode = FileMode.Append; if (_altManifestVersion > Config.ManifestVersionCount) { fileMode = FileMode.Create; _altManifestVersion = 0; } using (FileStream afs = new FileStream(Config.AltManifestFile(BaseFileName), fileMode, FileAccess.Write, FileShare.None, 8, FileOptions.WriteThrough)) { IAsyncResult altManifestWrite = afs.BeginWrite(manifestBytes, 0, manifestBytes.Length, null, null); // Wait for i/o's to complete mfs.EndWrite(manifestWrite); afs.EndWrite(altManifestWrite); } } }