Esempio n. 1
0
        internal static TransferEventArgs Read(CustomLogReader areader)
        {
            TransferEventArgs args = new TransferEventArgs();

            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                while (reader.Read(0))
                {
                    string value;
                    if (reader.GetEmptyElementValue("filename", out value))
                    {
                        args.FileName = value;
                    }
                    else if (reader.GetEmptyElementValue("destination", out value))
                    {
                        args.Destination = value;
                    }
                    else if (SessionRemoteException.IsResult(reader))
                    {
                        args.Error = SessionRemoteException.ReadResult(reader);
                    }
                }
            }

            return args;
        }
Esempio n. 2
0
        internal static TransferEventArgs Read(CustomLogReader areader)
        {
            TransferEventArgs args = new TransferEventArgs();

            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                while (reader.Read(0))
                {
                    string value;
                    if (reader.GetEmptyElementValue("filename", out value))
                    {
                        args.FileName = value;
                    }
                    else if (reader.GetEmptyElementValue("destination", out value))
                    {
                        args.Destination = value;
                    }
                    else if (SessionRemoteException.IsResult(reader))
                    {
                        args.Error = SessionRemoteException.ReadResult(reader);
                    }
                }
            }

            return(args);
        }
Esempio n. 3
0
        internal static TouchEventArgs Read(CustomLogReader areader)
        {
            TouchEventArgs args = new TouchEventArgs();

            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                while (reader.Read(0))
                {
                    string value;
                    if (reader.GetEmptyElementValue("filename", out value))
                    {
                        args.FileName = value;
                    }
                    else if (reader.GetEmptyElementValue("modification", out value))
                    {
                        args.LastWriteTime = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Local);
                    }
                    else if (SessionRemoteException.IsResult(reader))
                    {
                        args.Error = SessionRemoteException.ReadResult(reader);
                    }
                }
            }

            return args;
        }
Esempio n. 4
0
        internal static ChmodEventArgs Read(CustomLogReader areader)
        {
            ChmodEventArgs args = new ChmodEventArgs();

            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                while (reader.Read(0))
                {
                    if (reader.GetEmptyElementValue("filename", out string value))
                    {
                        args.FileName = value;
                    }
                    else if (reader.GetEmptyElementValue("permissions", out value))
                    {
                        args.FilePermissions = FilePermissions.CreateReadOnlyFromText(value);
                    }
                    else if (SessionRemoteException.IsResult(reader))
                    {
                        args.Error = SessionRemoteException.ReadResult(reader);
                    }
                }
            }

            return(args);
        }
        internal static TouchEventArgs Read(CustomLogReader areader)
        {
            TouchEventArgs args = new TouchEventArgs();

            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                while (reader.Read(0))
                {
                    string value;
                    if (reader.GetEmptyElementValue("filename", out value))
                    {
                        args.FileName = value;
                    }
                    else if (reader.GetEmptyElementValue("modification", out value))
                    {
                        args.LastWriteTime = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Local);
                    }
                    else if (SessionRemoteException.IsResult(reader))
                    {
                        args.Error = SessionRemoteException.ReadResult(reader);
                    }
                }
            }

            return(args);
        }
        private static SessionRemoteException ReadMessages(CustomLogReader areader)
        {
            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                string        message   = null;
                List <string> messages  = new List <string>();
                bool          inMessage = false;

                while (reader.Read(0))
                {
                    if (reader.IsNonEmptyElement("message"))
                    {
                        inMessage = true;
                        message   = null;
                    }
                    else if (inMessage &&
                             (reader.NodeType == XmlNodeType.Text))
                    {
                        message += reader.Value;
                    }
                    else if (inMessage &&
                             reader.IsEndElement("message"))
                    {
                        messages.Add(message);
                        message   = null;
                        inMessage = false;
                    }
                }

                string error = string.Join(Environment.NewLine, messages.ToArray());
                return(new SessionRemoteException(reader.Session, error));
            }
        }
Esempio n. 7
0
        internal static TransferEventArgs Read(ProgressSide side, CustomLogReader areader)
        {
            TransferEventArgs args = new TransferEventArgs()
            {
                Side = side
            };

            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                while (reader.Read(0))
                {
                    if (reader.GetEmptyElementValue("filename", out string value))
                    {
                        args.FileName = value;
                    }
                    else if (reader.GetEmptyElementValue("destination", out value))
                    {
                        args.Destination = value;
                    }
                    else if (reader.GetEmptyElementValue("size", out value))
                    {
                        args.Length = long.Parse(value, CultureInfo.InvariantCulture);
                    }
                    else if (SessionRemoteException.IsResult(reader))
                    {
                        args.Error = SessionRemoteException.ReadResult(reader);
                    }
                }
            }

            return(args);
        }
Esempio n. 8
0
        internal static ChmodEventArgs Read(CustomLogReader areader)
        {
            ChmodEventArgs args = new ChmodEventArgs();

            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                while (reader.Read(0))
                {
                    string value;
                    if (reader.GetEmptyElementValue("filename", out value))
                    {
                        args.FileName = value;
                    }
                    else if (reader.GetEmptyElementValue("permissions", out value))
                    {
                        args.FilePermissions = FilePermissions.CreateReadOnlyFromText(value);
                    }
                    else if (SessionRemoteException.IsResult(reader))
                    {
                        args.Error = SessionRemoteException.ReadResult(reader);
                    }
                }
            }

            return args;
        }
        private static SessionRemoteException ReadMessages(CustomLogReader areader)
        {
            using (ElementLogReader reader = new ElementLogReader(areader))
            {
                string error = null;
                string message = null;
                List<string> messages = new List<string>();
                bool inMessage = false;

                while (reader.Read(0))
                {
                    if (reader.IsNonEmptyElement("message"))
                    {
                        inMessage = true;
                        message = null;
                    }
                    else if (inMessage &&
                        (reader.NodeType == XmlNodeType.Text))
                    {
                        message += reader.Value;
                    }
                    else if (inMessage &&
                        reader.IsEndElement("message"))
                    {
                        if (error == null)
                        {
                            error = message;
                        }
                        else
                        {
                            messages.Add(message);
                        }
                        message = null;
                        inMessage = false;
                    }
                }

                Exception inner = null;
                if (messages.Count > 0)
                {
                    inner = new SessionRemoteException(reader.Session, string.Join(Environment.NewLine, messages.ToArray()));
                }

                return new SessionRemoteException(reader.Session, error, inner);
            }
        }
Esempio n. 10
0
        public void Open(SessionOptions sessionOptions)
        {
            using (Logger.CreateCallstackAndLock())
            {
                CheckNotDisposed();

                if (Opened)
                {
                    throw new InvalidOperationException("Session is already opened");
                }

                try
                {
                    SetupTempPath();

                    _process = new ExeSessionProcess(this);

                    _process.OutputDataReceived += ProcessOutputDataReceived;

                    _process.Start();

                    GotOutput();

                    // setup batch mode
                    WriteCommand("option batch on");
                    WriteCommand("option confirm off");

                    if (ReconnectTime != TimeSpan.MaxValue)
                    {
                        WriteCommand(string.Format(CultureInfo.InvariantCulture, "option reconnecttime {0}", (int)ReconnectTime.TotalSeconds));
                    }

                    WriteCommand("open " + SessionOptionsToOpenArguments(sessionOptions));

                    string logExplanation =
                        string.Format(CultureInfo.CurrentCulture,
                            "(response log file {0} was not created). This could indicate lack of write permissions to the log folder or problems starting WinSCP itself.",
                            XmlLogPath);

                    // Wait until the log file gets created or WinSCP terminates (in case of fatal error)
                    do
                    {
                        if (_process.HasExited && !File.Exists(XmlLogPath))
                        {
                            string[] output = new string[Output.Count];
                            Output.CopyTo(output, 0);
                            Logger.WriteCounters();
                            Logger.WriteProcesses();
                            _process.WriteStatus();
                            throw new SessionLocalException(this,
                                string.Format(CultureInfo.CurrentCulture,
                                    "WinSCP process terminated with exit code {0} and output \"{1}\", without responding {2}",
                                    _process.ExitCode, string.Join(Environment.NewLine, output), logExplanation));
                        }

                        Thread.Sleep(50);

                        CheckForTimeout(
                            string.Format(CultureInfo.CurrentCulture,
                                "WinSCP has not responded in time {0}",
                                logExplanation));

                    } while (!File.Exists(XmlLogPath));

                    _logReader = new SessionLogReader(this);

                    _reader = _logReader.WaitForNonEmptyElementAndCreateLogReader("session", LogReadFlags.ThrowFailures);

                    using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
                    {
                        ReadElement(groupReader, LogReadFlags.ThrowFailures);
                    }
                }
                catch(Exception e)
                {
                    Logger.WriteLine("Exception: {0}", e);
                    Cleanup();
                    throw;
                }
            }
        }
Esempio n. 11
0
        private void Cleanup()
        {
            using (Logger.CreateCallstack())
            {
                if (_process != null)
                {
                    Logger.WriteLine("Terminating process");
                    try
                    {
                        try
                        {
                            WriteCommand("exit");
                            _process.Close();
                        }
                        finally
                        {
                            _process.Dispose();
                            _process = null;
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.WriteLine("Process cleanup Exception: {0}", e);
                    }
                }

                Logger.WriteLine("Disposing log readers");

                if (_reader != null)
                {
                    _reader.Dispose();
                    _reader = null;
                }

                if (_logReader != null)
                {
                    _logReader.Dispose();
                    _logReader = null;
                }

                // Cleanup log file
                if ((XmlLogPath != null) && File.Exists(XmlLogPath))
                {
                    Logger.WriteLine("Deleting XML log file [{0}]", XmlLogPath);
                    try
                    {
                        File.Delete(XmlLogPath);
                    }
                    catch (DirectoryNotFoundException e)
                    {
                        Logger.WriteLine("XML log cleanup DirectoryNotFoundException: {0}", e);
                    }
                    catch (IOException e)
                    {
                        Logger.WriteLine("XML log cleanup IOException: {0}", e);
                    }
                    catch (UnauthorizedAccessException e)
                    {
                        Logger.WriteLine("XML log cleanup UnauthorizedAccessException: {0}", e);
                    }

                    _xmlLogPath = null;
                }
            }
        }
Esempio n. 12
0
        public void Open(SessionOptions sessionOptions)
        {
            using (Logger.CreateCallstackAndLock())
            {
                CheckNotDisposed();

                if (Opened)
                {
                    throw new InvalidOperationException("Session is already opened");
                }

                try
                {
                    SetupTempPath();

                    _process = new ExeSessionProcess(this);

                    _process.OutputDataReceived += ProcessOutputDataReceived;

                    _process.Start();

                    GotOutput();

                    // setup batch mode
                    WriteCommand("option batch on");
                    WriteCommand("option confirm off");

                    object reconnectTimeValue;
                    if (ReconnectTime != TimeSpan.MaxValue)
                    {
                        reconnectTimeValue = (int)ReconnectTime.TotalSeconds;
                    }
                    else
                    {
                        reconnectTimeValue = "off";
                    }
                    string reconnectTimeCommand =
                        string.Format(CultureInfo.InvariantCulture, "option reconnecttime {0}", reconnectTimeValue);
                    WriteCommand(reconnectTimeCommand);

                    string command;
                    string log;
                    SessionOptionsToOpenCommand(sessionOptions, out command, out log);
                    WriteCommand(command, log);

                    string logExplanation =
                        string.Format(CultureInfo.CurrentCulture,
                            "(response log file {0} was not created). This could indicate lack of write permissions to the log folder or problems starting WinSCP itself.",
                            XmlLogPath);

                    // Wait until the log file gets created or WinSCP terminates (in case of fatal error)
                    do
                    {
                        if (_process.HasExited && !File.Exists(XmlLogPath))
                        {
                            string[] output = new string[Output.Count];
                            Output.CopyTo(output, 0);
                            Logger.WriteCounters();
                            Logger.WriteProcesses();
                            _process.WriteStatus();
                            string exitCode = string.Format(CultureInfo.CurrentCulture, "{0}", _process.ExitCode);
                            if (_process.ExitCode < 0)
                            {
                                exitCode = string.Format(CultureInfo.CurrentCulture, "{0} ({1:X})", exitCode, _process.ExitCode);
                            }
                            throw new SessionLocalException(this,
                                string.Format(CultureInfo.CurrentCulture,
                                    "WinSCP process terminated with exit code {0} and output \"{1}\", without responding {2}",
                                    exitCode, string.Join(Environment.NewLine, output), logExplanation));
                        }

                        Thread.Sleep(50);

                        CheckForTimeout(
                            string.Format(CultureInfo.CurrentCulture,
                                "WinSCP has not responded in time {0}",
                                logExplanation));

                    } while (!File.Exists(XmlLogPath));

                    _logReader = new SessionLogReader(this);

                    _logReader.WaitForNonEmptyElement("session", LogReadFlags.ThrowFailures);

                    // special variant of ElementLogReader that throws when closing element (</session>) is encountered
                    _reader = new SessionElementLogReader(_logReader);

                    // Skip "open" command <group>
                    using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
                    {
                        ReadElement(groupReader, LogReadFlags.ThrowFailures);
                    }

                    WriteCommand("pwd");

                    using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
                    using (ElementLogReader cwdReader = groupReader.WaitForNonEmptyElementAndCreateLogReader("cwd", LogReadFlags.ThrowFailures))
                    {
                        while (cwdReader.Read(0))
                        {
                            string value;
                            if (cwdReader.GetEmptyElementValue("cwd", out value))
                            {
                                _homePath = value;
                            }
                        }

                        groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
                    }

                }
                catch(Exception e)
                {
                    Logger.WriteLine("Exception: {0}", e);
                    Cleanup();
                    throw;
                }
            }
        }