Exemple #1
0
        private FtpDataStream GetStream(long offset, TransferDirection dir)
        {
            m_parent.CheckSessionCurrentDirectory();

            FtpSessionConnected Session = m_parent.Session;

            if (offset != 0)
            {
                Session.ControlChannel.REST(offset);
            }

            FtpDataStream stream = Session.ControlChannel.GetDataStream(dir);

            try
            {
                if (dir == TransferDirection.Download)
                {
                    Session.ControlChannel.RETR(m_name);
                }
                else
                {
                    Session.ControlChannel.STOR(m_name);
                }
            }
            catch
            {
                stream.Close();
                throw;
            }

            return(stream);
        }
Exemple #2
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="FtpSessionConnected"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    // This will be done regardless of whether the object is finalized or disposed.

                    if (disposing)
                    {
                        m_host    = null;
                        m_root    = null;
                        m_current = null;

                        if ((object)m_ctrlChannel != null)
                        {
                            m_ctrlChannel.Close();
                        }

                        m_ctrlChannel = null;

                        if ((object)m_dataStream != null)
                        {
                            m_dataStream.Dispose();
                        }

                        m_dataStream = null;
                    }
                }
                finally
                {
                    m_disposed = true;  // Prevent duplicate dispose.
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates a new zero-length remote file in directory.
        /// </summary>
        /// <param name="newFileName">New remote file name.</param>
        /// <returns>File reference to new zero-length remote file.</returns>
        public FtpFile CreateFile(string newFileName)
        {
            FtpDataStream stream = CreateFileStream(newFileName);

            stream.Close();

            return(m_files[newFileName]);
        }
Exemple #4
0
        internal void StartTransfer()
        {
            FileStream    localStream  = null;
            FtpDataStream remoteStream = null;

            try
            {
                // Files just created may still have a file lock, we'll wait a few seconds for read access if needed...
                if (TransferDirection == TransferDirection.Upload)
                {
                    FilePath.WaitForReadLock(LocalFileName, m_session.Host.WaitLockTimeout);
                }

                m_session.Host.OnBeginFileTransfer(LocalFileName, RemoteFileName, TransferDirection);

                remoteStream = m_session.ControlChannel.GetDataStream(TransferDirection);
                m_ftpFileCommandRoutine(RemoteFileName);

                if (TransferDirection == TransferDirection.Download)
                {
                    localStream = new FileStream(LocalFileName, FileMode.Create);
                }
                else
                {
                    localStream = new FileStream(LocalFileName, FileMode.Open, FileAccess.Read);
                }

                m_streamCopyRoutine(remoteStream, localStream);

                // Dispose remote stream before testing file transfer result to ensure
                // we have received the server's response to the file transfer command
                remoteStream.Dispose();
                TestTransferResult();

                m_session.Host.OnEndFileTransfer(LocalFileName, RemoteFileName, TransferDirection);
            }
            catch
            {
                m_session.Host.OnEndFileTransfer(LocalFileName, RemoteFileName, TransferDirection);
                throw;
            }
            finally
            {
                // Need to make sure we end data transfer on the session, which would
                // normally happen automatically when the remote stream is closed
                if (remoteStream != null)
                {
                    remoteStream.Dispose();
                }
                else
                {
                    m_session.EndDataTransfer();
                }

                localStream?.Dispose();
            }
        }
Exemple #5
0
        internal void EndDataTransfer()
        {
            lock (this)
            {
                if ((object)m_dataStream == null)
                    throw new InvalidOperationException();

                m_dataStream = null;
            }
        }
Exemple #6
0
        internal void BeginDataTransfer(FtpDataStream stream)
        {
            lock (this)
            {
                if ((object)m_dataStream != null)
                    throw new FtpDataTransferException();

                m_dataStream = stream;
            }
        }
Exemple #7
0
        internal void StartTransfer()
        {
            FileStream    localStream  = null;
            FtpDataStream remoteStream = null;

            try
            {
                // Files just created may still have a file lock, we'll wait a few seconds for read access if needed...
                if (m_transferDirection == TransferDirection.Upload)
                {
                    FilePath.WaitForReadLock(m_localFile, m_session.Host.WaitLockTimeout);
                }

                m_session.Host.OnBeginFileTransfer(m_localFile, m_remoteFile, m_transferDirection);

                if (m_transferDirection == TransferDirection.Download)
                {
                    localStream = new FileStream(m_localFile, FileMode.OpenOrCreate);
                }
                else
                {
                    localStream = new FileStream(m_localFile, FileMode.Open, FileAccess.Read);
                }
                remoteStream = m_session.ControlChannel.GetPassiveDataStream(m_transferDirection);

                m_ftpFileCommandRoutine(m_remoteFile);
                m_streamCopyRoutine(remoteStream, localStream);

                remoteStream.Close();
                TestTransferResult();

                m_session.Host.OnEndFileTransfer(m_localFile, m_remoteFile, m_transferDirection);
            }
            catch
            {
                m_session.Host.OnEndFileTransfer(m_localFile, m_remoteFile, m_transferDirection);
                throw;
            }
            finally
            {
                if ((object)remoteStream != null)
                {
                    remoteStream.Close();
                }

                if ((object)localStream != null)
                {
                    localStream.Close();
                }
            }
        }
Exemple #8
0
        // You can only aborting file transfer started by
        // BeginPutFile and BeginGetFile
        public void AbortTransfer()
        {
            // Save a copy of m_dataStream since it will be set
            // to null when DataStream call EndDataTransfer
            FtpDataStream tempDataStream = m_dataStream;

            if ((object)tempDataStream != null)
            {
                tempDataStream.Abort();

                while (!tempDataStream.IsClosed)
                    Thread.Sleep(1);
            }
        }
Exemple #9
0
        internal void StartTransfer()
        {
            FileStream    localStream  = null;
            FtpDataStream remoteStream = null;

            try
            {
                // Files just created may still have a file lock, we'll wait a few seconds for read access if needed...
                if (m_transferDirection == TransferDirection.Upload)
                {
                    FilePath.WaitForReadLock(m_localFile, m_session.Host.WaitLockTimeout);
                }

                m_session.Host.OnBeginFileTransfer(m_localFile, m_remoteFile, m_transferDirection);

                remoteStream = m_session.ControlChannel.GetDataStream(m_transferDirection);
                m_ftpFileCommandRoutine(m_remoteFile);

                if (m_transferDirection == TransferDirection.Download)
                {
                    localStream = new FileStream(m_localFile, FileMode.Create);
                }
                else
                {
                    localStream = new FileStream(m_localFile, FileMode.Open, FileAccess.Read);
                }

                m_streamCopyRoutine(remoteStream, localStream);

                // Dispose remote stream before testing file transfer result to ensure
                // we have received the server's response to the file transfer command
                remoteStream.Dispose();
                TestTransferResult();

                m_session.Host.OnEndFileTransfer(m_localFile, m_remoteFile, m_transferDirection);
            }
            catch
            {
                m_session.Host.OnEndFileTransfer(m_localFile, m_remoteFile, m_transferDirection);
                throw;
            }
            finally
            {
                remoteStream?.Dispose();
                localStream?.Dispose();
            }
        }
Exemple #10
0
        internal Queue List(bool passive)
        {
            const string errorMsgListing = "Error when listing server directory.";

            try
            {
                Type(TransferMode.Ascii);
                Queue lineQueue = new Queue();

                using (FtpDataStream dataStream = GetDataStream())
                    using (StreamReader lineReader = new StreamReader(dataStream, Encoding.Default, true, 1024, true))
                    {
                        Command("LIST");

                        if (m_lastResponse.Code != FtpResponse.DataChannelOpenedTransferStart && m_lastResponse.Code != FtpResponse.FileOkBeginOpenDataChannel)
                        {
                            dataStream.Close(true);
                            throw new FtpCommandException(errorMsgListing, m_lastResponse);
                        }

                        string line = lineReader.ReadLine();

                        while ((object)line != null)
                        {
                            lineQueue.Enqueue(line);
                            line = lineReader.ReadLine();
                        }
                    }

                if (m_lastResponse.Code != FtpResponse.ClosingDataChannel)
                {
                    throw new FtpCommandException(errorMsgListing, m_lastResponse);
                }

                return(lineQueue);
            }
            catch (IOException ie)
            {
                throw new Exception(errorMsgListing, ie);
            }
            catch (SocketException se)
            {
                throw new Exception(errorMsgListing, se);
            }
        }
Exemple #11
0
        /// <summary>
        /// Creates a new data stream for remote file in directory.
        /// </summary>
        /// <param name="newFileName">New remote file name.</param>
        /// <returns>Output data stream for new remote file.</returns>
        public FtpOutputDataStream CreateFileStream(string newFileName)
        {
            InitHashtable();

            FtpDataStream stream = m_session.ControlChannel.GetPassiveDataStream(TransferDirection.Upload);

            try
            {
                m_session.ControlChannel.STOR(newFileName);

                FtpFile newFile = new FtpFile(this, newFileName);

                m_files[newFileName] = newFile;

                return((FtpOutputDataStream)stream);
            }
            catch
            {
                stream.Close();
                throw;
            }
        }
Exemple #12
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="FtpSessionConnected"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    // This will be done regardless of whether the object is finalized or disposed.

                    if (disposing)
                    {
                        m_host = null;
                        m_root = null;
                        m_current = null;

                        if ((object)m_ctrlChannel != null)
                            m_ctrlChannel.Close();

                        m_ctrlChannel = null;

                        if ((object)m_dataStream != null)
                            m_dataStream.Dispose();

                        m_dataStream = null;
                    }
                }
                finally
                {
                    m_disposed = true;  // Prevent duplicate dispose.
                }
            }
        }