Ejemplo n.º 1
0
        internal virtual void SerializeCache(string stateFile, TaskLoggingHelper log)
        {
            try
            {
                if (!string.IsNullOrEmpty(stateFile))
                {
                    if (FileSystems.Default.FileExists(stateFile))
                    {
                        File.Delete(stateFile);
                    }

                    using (var s = new FileStream(stateFile, FileMode.CreateNew))
                    {
                        var translator = BinaryTranslator.GetWriteTranslator(s);
                        translator.Translate(ref _serializedVersion);
                        Translate(translator);
                    }
                }
            }
            // If there was a problem writing the file (like it's read-only or locked on disk, for
            // example), then eat the exception and log a warning.  Otherwise, rethrow.
            catch (Exception e) when(!ExceptionHandling.NotExpectedSerializationException(e))
            {
                // Not being able to serialize the cache is not an error, but we let the user know anyway.
                // Don't want to hold up processing just because we couldn't read the file.
                log.LogWarningWithCodeFromResources("General.CouldNotWriteStateFile", stateFile, e.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes the contents of this object out to the specified file.
        /// TODO: once all derived classes from StateFileBase adopt new serialization, we shall consider to mode this into base class
        /// </summary>
        internal void SerializeCacheByTranslator(string stateFile, TaskLoggingHelper log)
        {
            try
            {
                if (!string.IsNullOrEmpty(stateFile))
                {
                    if (FileSystems.Default.FileExists(stateFile))
                    {
                        File.Delete(stateFile);
                    }

                    using var s = new FileStream(stateFile, FileMode.CreateNew);
                    var translator = BinaryTranslator.GetWriteTranslator(s);

                    // write file signature
                    translator.Writer.Write(TranslateContractSignature);
                    translator.Writer.Write(TranslateContractVersion);

                    Translate(translator);
                    isDirty = false;
                }
            }
            catch (Exception e) when(!ExceptionHandling.NotExpectedSerializationException(e))
            {
                // Not being able to serialize the cache is not an error, but we let the user know anyway.
                // Don't want to hold up processing just because we couldn't read the file.
                log.LogWarningWithCodeFromResources("General.CouldNotWriteStateFile", stateFile, e.Message);
            }
        }
Ejemplo n.º 3
0
        public static (IConfigCache ConfigCache, IResultsCache ResultsCache, Exception exception) DeserializeCaches(string inputCacheFile)
        {
            try
            {
                ConfigCache  configCache  = null;
                ResultsCache resultsCache = null;

                using (var fileStream = File.OpenRead(inputCacheFile))
                {
                    using var translator = BinaryTranslator.GetReadTranslator(fileStream, null);

                    translator.Translate(ref configCache);
                    translator.Translate(ref resultsCache);
                }

                ErrorUtilities.VerifyThrowInternalNull(configCache, nameof(configCache));
                ErrorUtilities.VerifyThrowInternalNull(resultsCache, nameof(resultsCache));

                return(configCache, resultsCache, null);
            }
            catch (Exception e)
            {
                return(null, null, e);
            }
        }
Ejemplo n.º 4
0
        public void VerifyAssemblyNameExSerializationWithRemappedFromByTranslator()
        {
            AssemblyNameExtension assemblyNameOriginal = new AssemblyNameExtension("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");
            AssemblyNameExtension assemblyRemappedFrom = new AssemblyNameExtension("System.Xml, Version=9.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");

            assemblyRemappedFrom.MarkImmutable();
            assemblyNameOriginal.AddRemappedAssemblyName(assemblyRemappedFrom);
            assemblyNameOriginal.RemappedFromEnumerator.Count().ShouldBe(1);

            AssemblyNameExtension assemblyNameDeserialized = null;

            MemoryStream serializationStream = new MemoryStream();
            ITranslator  writeTranslator     = BinaryTranslator.GetWriteTranslator(serializationStream);

            writeTranslator.Translate(ref assemblyNameOriginal, (ITranslator t) => new AssemblyNameExtension(t));

            serializationStream.Seek(0, SeekOrigin.Begin);
            ITranslator readTranslator = BinaryTranslator.GetReadTranslator(serializationStream, null);

            readTranslator.Translate(ref assemblyNameDeserialized, (ITranslator t) => new AssemblyNameExtension(t));

            assemblyNameDeserialized.Equals(assemblyNameOriginal).ShouldBeTrue();
            assemblyNameDeserialized.RemappedFromEnumerator.Count().ShouldBe(1);
            assemblyNameDeserialized.RemappedFromEnumerator.First().ShouldBe(assemblyRemappedFrom);
        }
Ejemplo n.º 5
0
        public void TestSerializationMode()
        {
            MemoryStream stream     = new MemoryStream();
            ITranslator  translator = BinaryTranslator.GetReadTranslator(stream, null);

            Assert.Equal(TranslationDirection.ReadFromStream, translator.Mode);

            translator = BinaryTranslator.GetWriteTranslator(stream);
            Assert.Equal(TranslationDirection.WriteToStream, translator.Mode);
        }
Ejemplo n.º 6
0
        private ITranslator GetReadTranslator()
        {
            if (_serializationStream == null)
            {
                throw new InvalidOperationException("GetWriteTranslator has to be called before GetReadTranslator");
            }

            _serializationStream.Seek(0, SeekOrigin.Begin);
            return(BinaryTranslator.GetReadTranslator(_serializationStream, null));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads the specified file from disk into a StateFileBase derived object.
        /// </summary>
        internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelper log, Type requiredReturnType)
        {
            StateFileBase retVal = null;

            // First, we read the cache from disk if one exists, or if one does not exist, we create one.
            try
            {
                if (!string.IsNullOrEmpty(stateFile) && FileSystems.Default.FileExists(stateFile))
                {
                    using (FileStream s = File.OpenRead(stateFile))
                    {
                        var  translator = BinaryTranslator.GetReadTranslator(s, buffer: null);
                        byte version    = 0;
                        translator.Translate(ref version);
                        var constructors = requiredReturnType.GetConstructors();
                        foreach (var constructor in constructors)
                        {
                            var parameters = constructor.GetParameters();
                            if (parameters.Length == 1 && parameters[0].ParameterType == typeof(ITranslator))
                            {
                                retVal = constructor.Invoke(new object[] { translator }) as StateFileBase;
                            }
                        }

                        // If retVal is still null or the version is wrong, log a message not a warning. This could be a valid cache with the wrong version preventing correct deserialization.
                        // For the latter case, internals may be unexpectedly null.
                        if (retVal == null || version != CurrentSerializationVersion)
                        {
                            // When upgrading to Visual Studio 2008 and running the build for the first time the resource cache files are replaced which causes a cast error due
                            // to a new version number on the tasks class. "Unable to cast object of type 'Microsoft.Build.Tasks.SystemState' to type 'Microsoft.Build.Tasks.StateFileBase".
                            // If there is an invalid cast, a message rather than a warning should be emitted.
                            log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType"));
                            return(null);
                        }
                        else if (!requiredReturnType.IsInstanceOfType(retVal))
                        {
                            log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile,
                                                        log.FormatResourceString("General.IncompatibleStateFileType"));
                            retVal = null;
                        }
                    }
                }
            }
            catch (Exception e) when(!ExceptionHandling.IsCriticalException(e))
            {
                // The deserialization process seems like it can throw just about
                // any exception imaginable.  Catch them all here.
                // Not being able to deserialize the cache is not an error, but we let the user know anyway.
                // Don't want to hold up processing just because we couldn't read the file.
                log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, e.Message);
            }

            return(retVal);
        }
Ejemplo n.º 8
0
        public void VerifyAssemblyNameExSerializationByTranslator(string assemblyName)
        {
            AssemblyNameExtension assemblyNameOriginal     = new AssemblyNameExtension(assemblyName);
            AssemblyNameExtension assemblyNameDeserialized = null;

            MemoryStream serializationStream = new MemoryStream();
            ITranslator  writeTranslator     = BinaryTranslator.GetWriteTranslator(serializationStream);

            writeTranslator.Translate(ref assemblyNameOriginal, (ITranslator t) => new AssemblyNameExtension(t));

            serializationStream.Seek(0, SeekOrigin.Begin);
            ITranslator readTranslator = BinaryTranslator.GetReadTranslator(serializationStream, null);

            readTranslator.Translate(ref assemblyNameDeserialized, (ITranslator t) => new AssemblyNameExtension(t));

            assemblyNameDeserialized.ShouldBe(assemblyNameOriginal);
        }
Ejemplo n.º 9
0
        public static void Accept(Stream input, Stream output)
        {
            var jsonStreamReader = new JsonStreamReader(input);

            using (var binaryStream = new MemoryStream())
                using (var binaryWriter = new BinaryWriter(binaryStream))
                {
                    var commandStreamWriter = new BinaryWriter(output);

                    var c = jsonStreamReader.ReadChar(); // '['
                    Assert.IsTrue(c == '[', $"FlatJson.FrontEnd.Accept expected '[' but found '{c}' as the first character of the stream. Line=[1]");

                    var fileInfo = new JsonObjectFileInfo();

                    // Read the next full json object from '{' to '}
                    JsonObjectReader objectReader;
                    while (jsonStreamReader.TryReadObject(out objectReader, ref fileInfo))
                    {
                        objectReader.ReadBeginObject();

                        // Unpack the typeId
                        var property = objectReader.ReadPropertyNameSegment();
                        Assert.IsTrue(property.Array[property.Offset] == '$');

                        var typeId = (UTinyTypeId)objectReader.ReadUInt16();

                        objectReader.Position = 0;

                        commandStreamWriter.Write(CommandType.GetCreateCommandType(typeId));

                        // Translate the json object to binary
                        BinaryTranslator.TranslateObject(ref objectReader, binaryWriter);

                        // Write the command payload as binary
                        commandStreamWriter.Write((uint)binaryStream.Position);
                        commandStreamWriter.Write(binaryStream.GetBuffer(), 0, (int)binaryStream.Position);
                        binaryStream.Position = 0;

                        c = jsonStreamReader.ReadChar(); // ',' or ']'
                        if (!(c == ',' || c == ']'))
                        {
                            throw new Exception($"FlatJson.FrontEnd.Accept expected ',' or ']' but found '{Escape(c)}' as the next character in the stream. Line=[{objectReader.Line}]");
                        }
                    }
                }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Read the contents of this object out to the specified file.
        /// TODO: once all classes derived from StateFileBase adopt the new serialization, we should consider moving this into the base class
        /// </summary>
        internal static SystemState DeserializeCacheByTranslator(string stateFile, TaskLoggingHelper log)
        {
            // First, we read the cache from disk if one exists, or if one does not exist, we create one.
            try
            {
                if (!string.IsNullOrEmpty(stateFile) && FileSystems.Default.FileExists(stateFile))
                {
                    using FileStream s = File.OpenRead(stateFile);
                    var translator = BinaryTranslator.GetReadTranslator(s, buffer: null); // TODO: shared buffering?

                    // verify file signature
                    var contractSignature = translator.Reader.ReadBytes(TranslateContractSignature.Length);
                    var contractVersion   = translator.Reader.ReadByte();

                    if (!contractSignature.SequenceEqual(TranslateContractSignature) || contractVersion != TranslateContractVersion)
                    {
                        log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType"));
                        return(null);
                    }

                    SystemState systemState = new SystemState();
                    systemState.Translate(translator);
                    systemState.isDirty = false;

                    return(systemState);
                }
            }
            catch (Exception e) when(!ExceptionHandling.IsCriticalException(e))
            {
                // The deserialization process seems like it can throw just about
                // any exception imaginable.  Catch them all here.
                // Not being able to deserialize the cache is not an error, but we let the user know anyway.
                // Don't want to hold up processing just because we couldn't read the file.
                log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, e.Message);
            }

            return(null);
        }
Ejemplo n.º 11
0
        public static string SerializeCaches(IConfigCache configCache, IResultsCache resultsCache, string outputCacheFile)
        {
            ErrorUtilities.VerifyThrowInternalNull(outputCacheFile, nameof(outputCacheFile));

            try
            {
                if (string.IsNullOrWhiteSpace(outputCacheFile))
                {
                    return(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("EmptyOutputCacheFile"));
                }

                var fullPath = FileUtilities.NormalizePath(outputCacheFile);

                Directory.CreateDirectory(Path.GetDirectoryName(fullPath));

                // Use FileStream constructor (File.OpenWrite should not be used as it doesn't reset the length of the file!)
                using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    var translator = BinaryTranslator.GetWriteTranslator(fileStream);

                    ConfigCache  configCacheToSerialize  = null;
                    ResultsCache resultsCacheToSerialize = null;

                    switch (configCache)
                    {
                    case ConfigCache asConfigCache:
                        configCacheToSerialize = asConfigCache;
                        break;

                    case ConfigCacheWithOverride configCacheWithOverride:
                        configCacheToSerialize = configCacheWithOverride.CurrentCache;
                        break;

                    default:
                        ErrorUtilities.ThrowInternalErrorUnreachable();
                        break;
                    }

                    switch (resultsCache)
                    {
                    case ResultsCache asResultsCache:
                        resultsCacheToSerialize = asResultsCache;
                        break;

                    case ResultsCacheWithOverride resultsCacheWithOverride:
                        resultsCacheToSerialize = resultsCacheWithOverride.CurrentCache;
                        break;

                    default:
                        ErrorUtilities.ThrowInternalErrorUnreachable();
                        break;
                    }

                    translator.Translate(ref configCacheToSerialize);
                    translator.Translate(ref resultsCacheToSerialize);
                }
            }
            catch (Exception e)
            {
                return(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("ErrorWritingCacheFile", outputCacheFile, e.Message));
            }

            return(null);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Gets a serializer used to read data.  Note that only one such serializer may be used from this class at a time,
 /// and this must be called after GetWriteTranslator() has been called.
 /// </summary>
 internal static ITranslator GetReadTranslator()
 {
     s_serializationStream.Seek(0, SeekOrigin.Begin);
     return(BinaryTranslator.GetReadTranslator(s_serializationStream, null));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Gets a serializer used to write data.  Note that only one such serializer may be used from this class at a time.
 /// </summary>
 internal static ITranslator GetWriteTranslator()
 {
     s_serializationStream = new MemoryStream();
     return(BinaryTranslator.GetWriteTranslator(s_serializationStream));
 }
Ejemplo n.º 14
0
 private ITranslator GetWriteTranslator()
 {
     _serializationStream = new MemoryStream();
     return(BinaryTranslator.GetWriteTranslator(_serializationStream));
 }