/// <summary>Request to the server that the get is set up.</summary> /// <param name="remoteFile">Name of remote file in current directory.</param> private void InitGet(string remoteFile) { CheckConnection(true); // reset the cancel flag cancelTransfer = false; bool close = false; data = null; try { // set up data channel data = control.CreateDataSocket(connectMode); data.Timeout = timeout; // if resume is requested, we must issue REST if (resume) { if (transferType.Equals(FTPTransferType.ASCII)) throw new FTPException("Resume only supported for BINARY transfers"); try { Restart(resumeMarker); } catch (FTPException ex) { resumeMarker = 0; resume = false; log.Warn("REST failed - resume will not be used (" + ex.Message + ")"); } } else resumeMarker = 0; // send the retrieve command FTPReply reply = control.SendCommand("RETR " + remoteFile); // Can get a 125 or a 150 lastValidReply = control.ValidateReply(reply, "125", "150"); } catch (SystemException) { close = true; throw; } catch (FTPException) { close = true; throw; } finally { if (close) { resume = false; resumeMarker = 0; CloseDataSocket(); } } }
private string[] Dir(string dirname, bool full, LineCallback lineCallback, object state) { CheckConnection(true); try { // set up data channel data = control.CreateDataSocket(connectMode); data.Timeout = timeout; // send the retrieve command string command = full ? "LIST ":"NLST "; if (showHiddenFiles) command += "-a "; if (dirname != null) command += dirname; // some FTP servers bomb out if NLST has whitespace appended command = command.Trim(); FTPReply reply = control.SendCommand(command); // check the control response. wu-ftp returns 550 if the // directory is empty, so we handle 550 appropriately. Similarly // proFTPD returns 450. If dir is empty, some servers return 226 Transfer complete lastValidReply = control.ValidateReply(reply, "125", "226", "150", "450", "550"); // an empty array of files for 450/550 string[] result = new string[0]; // a normal reply ... extract the file list string replyCode = lastValidReply.ReplyCode; if (!replyCode.Equals("450") && !replyCode.Equals("550") && !replyCode.Equals("226")) { // get a character input stream to read data from Encoding enc = controlEncoding == null ? Encoding.ASCII : controlEncoding; ArrayList lines = null; // reset the cancel flag cancelTransfer = false; try { if (enc.Equals(Encoding.ASCII)) { lines = ReadASCIIListingData(dirname, lineCallback, state); } else { lines = ReadListingData(dirname, enc, lineCallback, state); } // check the control response reply = control.ReadReply(); lastValidReply = control.ValidateReply(reply, "226", "250"); } catch (SystemException ex) { ValidateTransferOnError(); log.Error("SystemException in directory listing", ex); throw; } // empty array is default if (!(lines.Count == 0)) { log.Debug("Found " + lines.Count + " listing lines"); result = new string[lines.Count]; lines.CopyTo(result); } else log.Debug("No listing data found"); } else { // throw exception if not a "no files" message or transfer complete string replyText = lastValidReply.ReplyText.ToUpper(); if (!dirEmptyStrings.Matches(replyText) && !transferCompleteStrings.Matches(replyText)) throw new FTPException(reply); } return result; } finally { CloseDataSocket(); } }
/// <summary>Close the data socket</summary> private void CloseDataSocket() { if (data != null) { try { data.Close(); } catch (SystemException ex) { log.Warn("Caught exception closing data socket", ex); } data = null; } }
/// <summary>Request the server to set up the put.</summary> /// <param name="remoteFile">Name of remote file in current directory.</param> /// <param name="append"><c>true</c> if appending, <c>false</c> otherwise.</param> private void InitPut(string remoteFile, bool append) { CheckConnection(true); // reset the cancel flag cancelTransfer = false; bool close = false; data = null; try { resumeMarker = 0; // if resume is requested, we must obtain the size of the // remote file if (resume) { if (transferType.Equals(FTPTransferType.ASCII)) throw new FTPException("Resume only supported for BINARY transfers"); try { resumeMarker = Size(remoteFile); } catch (FTPException ex) { resumeMarker = 0; resume = false; log.Warn("SIZE failed '" + remoteFile + "' - resume will not be used (" + ex.Message + ")"); } } // set up data channel data = control.CreateDataSocket(connectMode); data.Timeout = timeout; // issue REST if (resume) { try { Restart(resumeMarker); } catch (FTPException ex) { resumeMarker = 0; resume = false; log.Warn("REST failed - resume will not be used (" + ex.Message + ")"); } } // send the command to store string cmd = append?"APPE ":"STOR "; FTPReply reply = control.SendCommand(cmd + remoteFile); // Can get a 125 or a 150, also allow 350 (for Global eXchange Services server) // JScape returns 151 lastValidReply = control.ValidateReply(reply, "125", "150", "151", "350"); } catch (SystemException) { close = true; throw; } catch (FTPException) { close = true; throw; } finally { if (close) { resume = false; resumeMarker = 0; CloseDataSocket(); } } }
/// <summary> /// Request the server to set up the put /// </summary> /// <param name="remoteFile"> name of remote file in /// current directory /// </param> /// <param name="append"> true if appending, false otherwise /// </param> private void InitPut(string remoteFile, bool append) { CheckConnection(true); // reset the cancel flag cancelTransfer = false; bool close = false; data = null; try { // set up data channel data = control.CreateDataSocket(connectMode); data.Timeout = timeout; // if resume is requested, we must obtain the size of the // remote file and issue REST if (resume) { if (transferType.Equals(FTPTransferType.ASCII)) throw new FTPException("Resume only supported for BINARY transfers"); resumeMarker = Size(remoteFile); Restart(resumeMarker); } // send the command to store string cmd = append?"APPE ":"STOR "; FTPReply reply = control.SendCommand(cmd + remoteFile); // Can get a 125 or a 150 string[] validCodes = new string[]{"125", "150"}; lastValidReply = control.ValidateReply(reply, validCodes); } catch (SystemException ex) { close = true; throw ex; } catch (FTPException ex) { close = true; throw ex; } finally { if (close) { resume = false; CloseDataSocket(); } } }
/// <summary> /// List a directory's contents as an array of strings. A detailed /// listing is available, otherwise just filenames are provided. /// The detailed listing varies in details depending on OS and /// FTP server. Note that a full listing can be used on a file /// name to obtain information about a file /// </summary> /// <param name="dirname"> name of directory OR filemask /// </param> /// <param name="full"> true if detailed listing required /// false otherwise /// </param> /// <returns> an array of directory listing strings /// </returns> public virtual string[] Dir(string dirname, bool full) { CheckConnection(true); // set up data channel data = control.CreateDataSocket(connectMode); data.Timeout = timeout; // send the retrieve command string command = full?"LIST ":"NLST "; if (dirname != null) command += dirname; // some FTP servers bomb out if NLST has whitespace appended command = command.Trim(); FTPReply reply = control.SendCommand(command); // check the control response. wu-ftp returns 550 if the // directory is empty, so we handle 550 appropriately. Similarly // proFTPD returns 450 string[] validCodes1 = new string[]{"125", "150", "450", "550"}; lastValidReply = control.ValidateReply(reply, validCodes1); // an empty array of files for 450/550 string[] result = new string[0]; // a normal reply ... extract the file list string replyCode = lastValidReply.ReplyCode; if (!replyCode.Equals("450") && !replyCode.Equals("550")) { // get a character input stream to read data from . StreamReader input = new StreamReader(data.DataStream); // read a line at a time ArrayList lines = new ArrayList(10); string line = null; while ((line = ReadLine(input)) != null) { lines.Add(line); } try { input.Close(); } catch (SystemException ex) { log.Warn("Caught exception closing data socket", ex); } CloseDataSocket(); // check the control response string[] validCodes2 = new string[]{"226", "250"}; reply = control.ReadReply(); lastValidReply = control.ValidateReply(reply, validCodes2); // empty array is default if (!(lines.Count == 0)) { result = new string[lines.Count]; lines.CopyTo(result); } } else { // 450 or 550 - still need to close data socket CloseDataSocket(); } return result; }
public virtual string[] Dir(string dirname, bool full) { string[] strArray3; this.CheckConnection(true); try { this.data = this.control.CreateDataSocket(this.connectMode); this.data.Timeout = this.timeout; string command = full ? "LIST " : "NLST "; if (this.showHiddenFiles) { command = command + "-a "; } if (dirname != null) { command = command + dirname; } command = command.Trim(); FTPReply reply = this.control.SendCommand(command); this.lastValidReply = this.control.ValidateReply(reply, new string[] { "125", "226", "150", "450", "550" }); string[] array = new string[0]; string replyCode = this.lastValidReply.ReplyCode; if ((!replyCode.Equals("450") && !replyCode.Equals("550")) && !replyCode.Equals("226")) { Encoding enc = (this.controlEncoding == null) ? Encoding.ASCII : this.controlEncoding; ArrayList list = null; this.cancelTransfer = false; try { if (enc.Equals(Encoding.ASCII)) { list = this.ReadASCIIListingData(dirname); } else { list = this.ReadListingData(dirname, enc); } reply = this.control.ReadReply(); this.lastValidReply = this.control.ValidateReply(reply, new string[] { "226", "250" }); } catch (SystemException exception) { this.ValidateTransferOnError(); this.log.Error("SystemException in directory listing", exception); throw; } if (list.Count != 0) { this.log.Debug("Found " + list.Count + " listing lines"); array = new string[list.Count]; list.CopyTo(array); } else { this.log.Debug("No listing data found"); } } else { string str3 = this.lastValidReply.ReplyText.ToUpper(); if (!this.dirEmptyStrings.Matches(str3) && !this.transferCompleteStrings.Matches(str3)) { throw new FTPException(reply); } } strArray3 = array; } finally { this.CloseDataSocket(); } return strArray3; }
public FTPClient() { this.dirEmptyStrings = new DirectoryEmptyStrings(); this.transferCompleteStrings = new TransferCompleteStrings(); this.fileNotFoundStrings = new FileNotFoundStrings(); this.modtimeFormats = new string[] { "yyyyMMddHHmmss", "yyyyMMddHHmmss'.'f", "yyyyMMddHHmmss'.'ff", "yyyyMMddHHmmss'.'fff" }; this.control = null; this.data = null; this.timeout = 0x1d4c0; this.noOperationInterval = 0; this.strictReturnCodes = false; this.cancelTransfer = false; this.transferNotifyListings = false; this.resume = false; this.deleteOnFailure = true; this.mdtmSupported = true; this.sizeSupported = true; this.resumeMarker = 0L; this.showHiddenFiles = false; this.monitorInterval = 0x1000L; this.transferBufferSize = 0x1000; this.parserCulture = CultureInfo.InvariantCulture; this.fileFactory = new EnterpriseDT.Net.Ftp.FTPFileFactory(); this.transferType = FTPTransferType.ASCII; this.connectMode = FTPConnectMode.PASV; this.synchronizePassiveConnections = false; this.activePortRange = new PortRange(); this.activeIPAddress = null; this.controlPort = -1; this.remoteHost = null; this.autoPassiveIPSubstitution = false; this.closeStreamsAfterTransfer = true; this.controlEncoding = null; this.dataEncoding = null; this.throttler = null; this.InitBlock(); }
private void CloseDataSocket() { if (this.data != null) { try { this.data.Close(); this.data = null; } catch (SystemException exception) { this.log.Warn("Caught exception closing data socket", exception); } } }
public virtual void QuitImmediately() { if (this.fileFactory != null) { this.fileFactory.System = null; } try { if (this.data != null) { this.data.Close(); } } finally { if (this.control != null) { this.control.Kill(); } this.control = null; this.data = null; } }
public virtual void Quit() { this.CheckConnection(true); if (this.fileFactory != null) { this.fileFactory.System = null; } try { FTPReply reply = this.control.SendCommand("QUIT"); this.lastValidReply = this.control.ValidateReply(reply, new string[] { "221", "226" }); } finally { if (this.data != null) { this.data.Close(); } this.data = null; this.control.Logout(); this.control = null; } }
private void InitPut(string remoteFile, bool append) { this.CheckConnection(true); this.cancelTransfer = false; bool flag = false; this.data = null; try { this.resumeMarker = 0L; if (this.resume) { if (this.transferType.Equals(FTPTransferType.ASCII)) { throw new FTPException("Resume only supported for BINARY transfers"); } try { this.resumeMarker = this.Size(remoteFile); } catch (FTPException exception) { this.log.Warn("Failed to find size of file '" + remoteFile + "' for resuming (" + exception.Message + ")"); } } this.data = this.control.CreateDataSocket(this.connectMode); this.data.Timeout = this.timeout; if (this.resume) { this.Restart(this.resumeMarker); } FTPReply reply = this.control.SendCommand((append ? "APPE " : "STOR ") + remoteFile); this.lastValidReply = this.control.ValidateReply(reply, new string[] { "125", "150", "151", "350" }); } catch (SystemException) { flag = true; throw; } catch (FTPException) { flag = true; throw; } finally { if (flag) { this.resume = false; this.CloseDataSocket(); } } }
private void InitGet(string remoteFile) { this.CheckConnection(true); this.cancelTransfer = false; bool flag = false; this.data = null; try { this.data = this.control.CreateDataSocket(this.connectMode); this.data.Timeout = this.timeout; if (this.resume) { if (this.transferType.Equals(FTPTransferType.ASCII)) { throw new FTPException("Resume only supported for BINARY transfers"); } this.Restart(this.resumeMarker); } else { this.resumeMarker = 0L; } FTPReply reply = this.control.SendCommand("RETR " + remoteFile); this.lastValidReply = this.control.ValidateReply(reply, new string[] { "125", "150" }); } catch (SystemException) { flag = true; throw; } catch (FTPException) { flag = true; throw; } finally { if (flag) { this.resume = false; this.CloseDataSocket(); } } }