internal void Save(Stream stream) { ExceptionUtilities.HandleRecoverableIOException( () => { // We don't have anything in particular to correlate this file to, // so we are simply creating a unique correlation id that is used as part // of the header consistency check. FileEnvelopeId correlationId = FileEnvelopeId.Create(); FileEnvelope.WriteHeader(stream, correlationId); using (BuildXLWriter writer = new BuildXLWriter(debug: false, stream: stream, leaveOpen: true, logStats: false)) { writer.Write(m_runtimeData.Count); foreach (KeyValuePair<long, PipHistoricPerfData> kvp in m_runtimeData) { writer.Write(kvp.Key); kvp.Value.Serialize(writer); } } FileEnvelope.FixUpHeader(stream, correlationId); return (object)null; }, ex => { throw new BuildXLException("Writing of file failed", ex); }); }
private void Save(FileEnvelopeId atomicSaveToken, string path) { Contract.Requires(!string.IsNullOrWhiteSpace(path)); FileUtilities.DeleteFile(path); FileUtilities.CreateDirectory(Path.GetDirectoryName(path)); using (var stream = FileUtilities.CreateFileStream( path, FileMode.Create, FileAccess.Write, FileShare.Delete, // Do not write the file with SequentialScan since it will be reread in the subsequent build FileOptions.None)) { ExceptionUtilities.HandleRecoverableIOException( () => { using (var pm = BuildXL.Tracing.PerformanceMeasurement.StartWithoutStatistic( m_loggingContext, loggingContext => Logger.Log.StartSavingChangeTracker(loggingContext, path), loggingContext => Logger.Log.EndSavingChangeTracker(loggingContext))) { Stopwatch sw = Stopwatch.StartNew(); FileEnvelope.WriteHeader(stream, atomicSaveToken); using (var writer = new BuildXLWriter(debug: false, stream: stream, leaveOpen: true, logStats: false)) { if (IsDisabledOrNullTrackingSet) { writer.Write(true); } else { writer.Write(false); writer.Write(m_buildEngineFingerprint, (w, s) => w.Write(s)); m_changeTrackingSet.Save(writer); } } FileEnvelope.FixUpHeader(stream, atomicSaveToken); Logger.Log.SavingChangeTracker( pm.LoggingContext, path, atomicSaveToken.ToString(), m_changeTrackingSet == null ? "Null" : TrackingState.ToString(), m_changeTrackingSet == null ? 0 : m_changeTrackingSet.TrackedVolumes.Count(), sw.ElapsedMilliseconds); } }, ex => { throw new BuildXLException("Failed to save file change tracker", ex); }); } }
public void Success() { var fe = new FileEnvelope("Dummy", 0); using (var stream = new MemoryStream()) { FileEnvelopeId id = FileEnvelopeId.Create(); fe.WriteHeader(stream, id); fe.FixUpHeader(stream, id); stream.Position = 0; fe.ReadHeader(stream); } }
public void MissingFixup() { var fe = new FileEnvelope("Dummy", 0); using (var stream = new MemoryStream()) { FileEnvelopeId id = FileEnvelopeId.Create(); fe.WriteHeader(stream, id); // fe.FixUpHeader(stream, id); stream.Position = 0; Assert.Throws <BuildXLException>( () => { fe.ReadHeader(stream); }); } }
public void WrongEnvelopeVersion() { var fe0 = new FileEnvelope("Dummy0", 0); var fe1 = new FileEnvelope("Dummy0", 1); using (var stream = new MemoryStream()) { FileEnvelopeId id = FileEnvelopeId.Create(); fe0.WriteHeader(stream, id); fe0.FixUpHeader(stream, id); stream.Position = 0; Assert.Throws <BuildXLException>( () => { fe1.ReadHeader(stream); }); } }
public void DetectFileLengthCorruption() { var fe = new FileEnvelope("Dummy", 0); using (var stream = new MemoryStream()) { FileEnvelopeId id = FileEnvelopeId.Create(); fe.WriteHeader(stream, id); fe.FixUpHeader(stream, id); stream.WriteByte(0); // not taken into account in fixed up header magic stream.Position = 0; Assert.Throws <BuildXLException>( () => { fe.ReadHeader(stream); }); } }
/// <summary> /// Creates a new output logger. /// /// The underlying file is created only upon first write. /// </summary> /// <param name="metadata">Metadata</param> /// <param name="sidebandLogFile">File to which to save the log.</param> /// <param name="rootDirectories">Only paths under one of these root directories will be recorded.</param> public SidebandWriter(SidebandMetadata metadata, string sidebandLogFile, [CanBeNull] IReadOnlyList <string> rootDirectories) { Metadata = metadata; SidebandLogFile = sidebandLogFile; RootDirectories = rootDirectories; m_recordedPathsCache = new HashSet <AbsolutePath>(); m_envelopeId = FileEnvelopeId.Create(); m_lazyBxlWriter = Lazy.Create(() => { Directory.CreateDirectory(Path.GetDirectoryName(SidebandLogFile)); var writer = new BuildXLWriter( stream: new FileStream(SidebandLogFile, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Delete), debug: false, logStats: false, leaveOpen: false); // write header and metadata before anything else FileEnvelope.WriteHeader(writer.BaseStream, m_envelopeId); Metadata.Serialize(writer); return(writer); }); }
/// <summary> /// Creates a new output logger. /// /// The underlying file is created only upon first write. /// </summary> /// <param name="sidebandLogFile">File to which to save the log.</param> /// <param name="rootDirectories">Only paths under one of the root directories are recorded in <see cref="RecordFileWrite(PathTable, AbsolutePath)"/>.</param> public SharedOpaqueOutputLogger(string sidebandLogFile, [CanBeNull] IReadOnlyList <string> rootDirectories) { SidebandLogFile = sidebandLogFile; RootDirectories = rootDirectories; m_recordedPathsCache = new HashSet <AbsolutePath>(); m_envelopeId = FileEnvelopeId.Create(); m_lazyBxlWriter = Lazy.Create(() => { Directory.CreateDirectory(Path.GetDirectoryName(SidebandLogFile)); return(new BuildXLWriter( stream: new FileStream(SidebandLogFile, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Delete), debug: false, logStats: false, leaveOpen: false)); }); m_lazyWriteHeader = Lazy.Create(() => { FileEnvelope.WriteHeader(m_lazyBxlWriter.Value.BaseStream, m_envelopeId); return(Unit.Void); }); }
public void DetectHeaderCorruption() { var r = new Random(0); var fe = new FileEnvelope("Dummy", 0); for (int i = 0; i < 10000; i++) { using (var stream = new MemoryStream()) { FileEnvelopeId id = FileEnvelopeId.Create(); fe.WriteHeader(stream, id); fe.FixUpHeader(stream, id); stream.Position = r.Next((int)stream.Length - 1); int b = stream.ReadByte(); stream.Position = stream.Position - 1; stream.WriteByte((byte)(b ^ (1 << r.Next(8)))); stream.Position = 0; Assert.Throws <BuildXLException>( () => { fe.ReadHeader(stream); }); } } }
/// <summary> /// Writes a file containing versioning information to the provided store directory. /// </summary> private static void WriteVersionFile(string storeDirectory, int storeVersion) { var versionFile = GetVersionFile(storeDirectory); using (var stream = FileUtilities.CreateFileStream( versionFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Delete)) { // We don't have anything in particular to correlate this file to, // so we are simply creating a unique correlation id that is used as part // of the header consistency check. var correlationId = FileEnvelopeId.Create(); s_fileEnvelope.WriteHeader(stream, correlationId); using (var writer = new BuildXLWriter(debug: false, stream: stream, leaveOpen: true, logStats: false)) { writer.Write(storeVersion); } s_fileEnvelope.FixUpHeader(stream, correlationId); } }