/// <summary>
        /// Deserializes an instance of <see cref="SandboxedProcessOutput"/>.
        /// </summary>
        public static SandboxedProcessOutput Deserialize(BuildXLReader reader)
        {
            long     length   = reader.ReadInt64();
            string   value    = reader.ReadNullableString();
            string   fileName = reader.ReadNullableString();
            Encoding encoding = reader.ReadEncoding();
            SandboxedProcessStandardFiles standardFiles = reader.ReadNullable(r => SandboxedProcessStandardFiles.Deserialize(r));
            ISandboxedProcessFileStorage  fileStorage   = null;

            if (standardFiles != null)
            {
                fileStorage = new StandardFileStorage(standardFiles);
            }
            SandboxedProcessFile file      = (SandboxedProcessFile)reader.ReadUInt32Compact();
            BuildXLException     exception = reader.ReadNullable(r => new BuildXLException(r.ReadNullableString(), (ExceptionRootCause)r.ReadUInt32Compact()));

            return(new SandboxedProcessOutput(
                       length,
                       value,
                       fileName,
                       encoding,
                       fileStorage,
                       file,
                       exception));
        }
        /// <summary>
        /// Create generic result for failure.
        /// </summary>
        protected SandboxedProcessResult CreateResultForFailure(
            int exitCode,
            bool killed,
            bool timedOut,
            string output,
            string error,
            string hint)
        {
            var standardFiles = new SandboxedProcessStandardFiles(GetStdOutPath(hint), GetStdErrPath(hint));
            var storage       = new StandardFileStorage(standardFiles);

            return(new SandboxedProcessResult
            {
                ExitCode = exitCode,
                Killed = killed,
                TimedOut = timedOut,
                HasDetoursInjectionFailures = false,
                StandardOutput = new SandboxedProcessOutput(output.Length, output, null, Console.OutputEncoding, storage, SandboxedProcessFile.StandardOutput, null),
                StandardError = new SandboxedProcessOutput(error.Length, error, null, Console.OutputEncoding, storage, SandboxedProcessFile.StandardError, null),
                HasReadWriteToReadFileAccessRequest = false,
                AllUnexpectedFileAccesses = EmptyFileAccessesSet,
                FileAccesses = EmptyFileAccessesSet,
                DetouringStatuses = new ProcessDetouringStatusData[0],
                ExplicitlyReportedFileAccesses = EmptyFileAccessesSet,
                Processes = new ReportedProcess[0],
                MessageProcessingFailure = null,
                DumpCreationException = DumpCreationException,
                DumpFileDirectory = WorkingDirectory,
                PrimaryProcessTimes = new ProcessTimes(0, 0, 0, 0),
                SurvivingChildProcesses = new ReportedProcess[0],
            });
        }
        private SandboxedProcessResult CreateResultForFailure()
        {
            string output        = m_output.ToString();
            string error         = m_error.ToString();
            string hint          = Path.GetFileNameWithoutExtension(m_toolPath);
            var    standardFiles = new SandboxedProcessStandardFiles(GetStdOutPath(hint), GetStdErrPath(hint));
            var    storage       = new StandardFileStorage(standardFiles);

            return(new SandboxedProcessResult
            {
                ExitCode = m_processExecutor.TimedOut ? ExitCodes.Timeout : Process.ExitCode,
                Killed = m_processExecutor.Killed,
                TimedOut = m_processExecutor.TimedOut,
                HasDetoursInjectionFailures = false,
                StandardOutput = new SandboxedProcessOutput(output.Length, output, null, Console.OutputEncoding, storage, SandboxedProcessFile.StandardOutput, null),
                StandardError = new SandboxedProcessOutput(error.Length, error, null, Console.OutputEncoding, storage, SandboxedProcessFile.StandardError, null),
                HasReadWriteToReadFileAccessRequest = false,
                AllUnexpectedFileAccesses = s_emptyFileAccessesSet,
                FileAccesses = s_emptyFileAccessesSet,
                DetouringStatuses = new ProcessDetouringStatusData[0],
                ExplicitlyReportedFileAccesses = s_emptyFileAccessesSet,
                Processes = new ReportedProcess[0],
                MessageProcessingFailure = null,
                DumpCreationException = m_dumpCreationException,
                DumpFileDirectory = GetOutputDirectory(),
                PrimaryProcessTimes = new ProcessTimes(0, 0, 0, 0),
                SurvivingChildProcesses = new ReportedProcess[0],
            });
        }
 /// <summary>
 /// Serializes this instance to a given <paramref name="writer"/>.
 /// </summary>
 public void Serialize(BuildXLWriter writer)
 {
     writer.Write(m_length);
     writer.WriteNullableString(m_value);
     writer.WriteNullableString(m_fileName);
     writer.Write(m_encoding);
     writer.Write(m_fileStorage, (w, v) => SandboxedProcessStandardFiles.From(v).Serialize(w));
     writer.WriteCompact((uint)m_file);
     writer.Write(m_exception, (w, v) =>
     {
         w.WriteNullableString(v.Message);
         w.WriteCompact((uint)v.RootCause);
     });
 }
        /// <nodoc />
        public void Serialize(Stream stream)
        {
            using (var writer = new BuildXLWriter(false, stream, true, true))
            {
                writer.WriteNullableString(m_arguments);
                writer.WriteNullableString(m_commandLine);
                writer.Write(DisableConHostSharing);
                writer.WriteNullableString(FileName);
                writer.Write(StandardInputEncoding, (w, v) => w.Write(v));
                writer.Write(StandardOutputEncoding, (w, v) => w.Write(v));
                writer.Write(StandardErrorEncoding, (w, v) => w.Write(v));
                writer.WriteNullableString(WorkingDirectory);
                writer.Write(
                    EnvironmentVariables,
                    (w, v) => w.WriteReadOnlyList(
                        v.ToDictionary().ToList(),
                        (w2, kvp) =>
                {
                    w2.Write(kvp.Key);
                    w2.Write(kvp.Value);
                }));
                writer.Write(
                    AllowedSurvivingChildProcessNames,
                    (w, v) => w.WriteReadOnlyList(v, (w2, v2) => w2.Write(v2)));
                writer.Write(MaxLengthInMemory);
                writer.Write(Timeout, (w, v) => w.Write(v));
                writer.Write(NestedProcessTerminationTimeout);
                writer.Write(PipSemiStableHash);
                writer.WriteNullableString(TimeoutDumpDirectory);
                writer.Write((byte)SandboxKind);
                writer.WriteNullableString(PipDescription);

                if (SandboxedProcessStandardFiles == null)
                {
                    SandboxedProcessStandardFiles.From(FileStorage).Serialize(writer);
                }
                else
                {
                    SandboxedProcessStandardFiles.Serialize(writer);
                }

                writer.Write(StandardInputSourceInfo, (w, v) => v.Serialize(w));
                writer.Write(StandardObserverDescriptor, (w, v) => v.Serialize(w));

                // File access manifest should be serialize the last.
                writer.Write(FileAccessManifest, (w, v) => FileAccessManifest.Serialize(stream));
            }
        }
        /// <nodoc />
        public static SandboxedProcessInfo Deserialize(Stream stream, LoggingContext loggingContext, IDetoursEventListener detoursEventListener)
        {
            using (var reader = new BuildXLReader(false, stream, true))
            {
                string           arguments             = reader.ReadNullableString();
                string           commandLine           = reader.ReadNullableString();
                bool             disableConHostSharing = reader.ReadBoolean();
                string           fileName = reader.ReadNullableString();
                Encoding         standardInputEncoding  = reader.ReadNullable(r => r.ReadEncoding());
                Encoding         standardOutputEncoding = reader.ReadNullable(r => r.ReadEncoding());
                Encoding         standardErrorEncoding  = reader.ReadNullable(r => r.ReadEncoding());
                string           workingDirectory       = reader.ReadNullableString();
                IBuildParameters buildParameters        = null;
                var envVars = reader.ReadNullable(r => r.ReadReadOnlyList(r2 => new KeyValuePair <string, string>(r2.ReadString(), r2.ReadString())));
                if (envVars != null)
                {
                    buildParameters = BuildParameters.GetFactory().PopulateFromDictionary(envVars.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
                }

                string[]    allowedSurvivingChildNames = reader.ReadNullable(r => r.ReadReadOnlyList(r2 => r2.ReadString()))?.ToArray();
                int         maxLengthInMemory          = reader.ReadInt32();
                TimeSpan?   timeout = reader.ReadNullableStruct(r => r.ReadTimeSpan());
                TimeSpan    nestedProcessTerminationTimeout = reader.ReadTimeSpan();
                long        pipSemiStableHash    = reader.ReadInt64();
                string      timeoutDumpDirectory = reader.ReadNullableString();
                SandboxKind sandboxKind          = (SandboxKind)reader.ReadByte();
                string      pipDescription       = reader.ReadNullableString();
                SandboxedProcessStandardFiles sandboxedProcessStandardFiles = SandboxedProcessStandardFiles.Deserialize(reader);
                StandardInputInfo             standardInputSourceInfo       = reader.ReadNullable(r => StandardInputInfo.Deserialize(r));
                SandboxObserverDescriptor     standardObserverDescriptor    = reader.ReadNullable(r => SandboxObserverDescriptor.Deserialize(r));

                FileAccessManifest fam = reader.ReadNullable(r => FileAccessManifest.Deserialize(stream));

                return(new SandboxedProcessInfo(
                           new PathTable(),
                           new StandardFileStorage(sandboxedProcessStandardFiles),
                           fileName,
                           fam,
                           disableConHostSharing,
                           // TODO: serialize/deserialize container configuration.
                           containerConfiguration: ContainerConfiguration.DisabledIsolation,
                           loggingContext: loggingContext,
                           detoursEventListener: detoursEventListener)
                {
                    m_arguments = arguments,
                    m_commandLine = commandLine,
                    StandardInputEncoding = standardInputEncoding,
                    StandardOutputEncoding = standardOutputEncoding,
                    StandardErrorEncoding = standardErrorEncoding,
                    WorkingDirectory = workingDirectory,
                    EnvironmentVariables = buildParameters,
                    AllowedSurvivingChildProcessNames = allowedSurvivingChildNames,
                    MaxLengthInMemory = maxLengthInMemory,
                    Timeout = timeout,
                    NestedProcessTerminationTimeout = nestedProcessTerminationTimeout,
                    PipSemiStableHash = pipSemiStableHash,
                    TimeoutDumpDirectory = timeoutDumpDirectory,
                    SandboxKind = sandboxKind,
                    PipDescription = pipDescription,
                    SandboxedProcessStandardFiles = sandboxedProcessStandardFiles,
                    StandardInputSourceInfo = standardInputSourceInfo,
                    StandardObserverDescriptor = standardObserverDescriptor
                });
            }
        }
 /// <summary>
 /// Create an instance of <see cref="StandardFileStorage"/>.
 /// </summary>
 public StandardFileStorage(SandboxedProcessStandardFiles sandboxedProcessStandardFiles)
 {
     Contract.Requires(sandboxedProcessStandardFiles != null);
     m_sandboxedProcessStandardFiles = sandboxedProcessStandardFiles;
 }