Publish ISOs via FTP
authorCasper Hornstrup <chorns@users.sourceforge.net>
Thu, 24 Nov 2005 12:34:08 +0000 (12:34 +0000)
committerCasper Hornstrup <chorns@users.sourceforge.net>
Thu, 24 Nov 2005 12:34:08 +0000 (12:34 +0000)
svn path=/trunk/; revision=19523

cis/ReactOS.CustomRevisionAction/App.config
cis/ReactOS.CustomRevisionAction/FtpClient.cs [new file with mode: 0644]
cis/ReactOS.CustomRevisionAction/Main.cs

index b697b94..5c694a5 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8" ?> \r
 <configuration>\r
        <appSettings>\r
-               <add key="publishPath" value="c:\iso" />\r
+               <add key="publishPath" value="c:\iso" /> <!-- c:\iso or ftp://ftp.server.com/iso -->\r
                <add key="smtpServer" value="localhost" />\r
                <add key="errorEmail" value="mailbox@somewhere-on-the-net" />\r
                <add key="makeParameters" value="" />\r
diff --git a/cis/ReactOS.CustomRevisionAction/FtpClient.cs b/cis/ReactOS.CustomRevisionAction/FtpClient.cs
new file mode 100644 (file)
index 0000000..11b9634
--- /dev/null
@@ -0,0 +1,1035 @@
+using System;\r
+using System.Net;\r
+using System.IO;\r
+using System.Text;\r
+using System.Net.Sockets;\r
+using System.Diagnostics;\r
+using System.Runtime.Remoting;\r
+using System.Runtime.Remoting.Messaging;\r
+\r
+/*\r
+ * FTP Client library in C#\r
+ * Author: Jaimon Mathew\r
+ * mailto:jaimonmathew@rediffmail.com\r
+ * http://www.csharphelp.com/archives/archive9.html\r
+ * \r
+ * Addapted for use by Dan Glass 07/03/03\r
+ */\r
+\r
+\r
+namespace ReactOS.CustomRevisionAction\r
+{\r
+\r
+       public class FtpClient\r
+       {\r
+\r
+               public class FtpException : Exception\r
+               {\r
+                       public FtpException(string message) : base(message){}\r
+                       public FtpException(string message, Exception innerException) : base(message,innerException){}\r
+               }\r
+\r
+               private static int BUFFER_SIZE = 512;\r
+               private static Encoding ASCII = Encoding.ASCII;\r
+\r
+               private bool verboseDebugging = false;\r
+\r
+               // defaults\r
+               private string server = "localhost";\r
+               private string remotePath = ".";\r
+               private string username = "anonymous";\r
+               private string password = "anonymous@anonymous.net";\r
+               private string message = null;\r
+               private string result = null;\r
+\r
+               private int port = 21;\r
+               private int bytes = 0;\r
+               private int resultCode = 0;\r
+\r
+               private bool loggedin = false;\r
+               private bool binMode = false;\r
+\r
+               private Byte[] buffer = new Byte[BUFFER_SIZE];\r
+               private Socket clientSocket = null;\r
+\r
+               private int timeoutSeconds = 10;\r
+\r
+               /// <summary>\r
+               /// Default contructor\r
+               /// </summary>\r
+               public FtpClient()\r
+               {\r
+               }\r
+               /// <summary>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="server"></param>\r
+               /// <param name="username"></param>\r
+               /// <param name="password"></param>\r
+               public FtpClient(string server, string username, string password)\r
+               {\r
+                       this.server = server;\r
+                       this.username = username;\r
+                       this.password = password;\r
+               }\r
+               /// <summary>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="server"></param>\r
+               /// <param name="username"></param>\r
+               /// <param name="password"></param>\r
+               /// <param name="timeoutSeconds"></param>\r
+               /// <param name="port"></param>\r
+               public FtpClient(string server, string username, string password, int timeoutSeconds, int port)\r
+               {\r
+                       this.server = server;\r
+                       this.username = username;\r
+                       this.password = password;\r
+                       this.timeoutSeconds = timeoutSeconds;\r
+                       this.port = port;\r
+               }\r
+\r
+               /// <summary>\r
+               /// Display all communications to the debug log\r
+               /// </summary>\r
+               public bool VerboseDebugging\r
+               {\r
+                       get\r
+                       {\r
+                               return this.verboseDebugging;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.verboseDebugging = value;\r
+                       }\r
+               }\r
+               /// <summary>\r
+               /// Remote server port. Typically TCP 21\r
+               /// </summary>\r
+               public int Port\r
+               {\r
+                       get\r
+                       {\r
+                               return this.port;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.port = value;\r
+                       }\r
+               }\r
+               /// <summary>\r
+               /// Timeout waiting for a response from server, in seconds.\r
+               /// </summary>\r
+               public int Timeout\r
+               {\r
+                       get\r
+                       {\r
+                               return this.timeoutSeconds;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.timeoutSeconds = value;\r
+                       }\r
+               }\r
+               /// <summary>\r
+               /// Gets and Sets the name of the FTP server.\r
+               /// </summary>\r
+               /// <returns></returns>\r
+               public string Server\r
+               {\r
+                       get\r
+                       {\r
+                               return this.server;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.server = value;\r
+                       }\r
+               }\r
+               /// <summary>\r
+               /// Gets and Sets the port number.\r
+               /// </summary>\r
+               /// <returns></returns>\r
+               public int RemotePort\r
+               {\r
+                       get\r
+                       {\r
+                               return this.port;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.port = value;\r
+                       }\r
+               }\r
+               /// <summary>\r
+               /// GetS and Sets the remote directory.\r
+               /// </summary>\r
+               public string RemotePath\r
+               {\r
+                       get\r
+                       {\r
+                               return this.remotePath;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.remotePath = value;\r
+                       }\r
+\r
+               }\r
+               /// <summary>\r
+               /// Gets and Sets the username.\r
+               /// </summary>\r
+               public string Username\r
+               {\r
+                       get\r
+                       {\r
+                               return this.username;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.username = value;\r
+                       }\r
+               }\r
+               /// <summary>\r
+               /// Gets and Set the password.\r
+               /// </summary>\r
+               public string Password\r
+               {\r
+                       get\r
+                       {\r
+                               return this.password;\r
+                       }\r
+                       set\r
+                       {\r
+                               this.password = value;\r
+                       }\r
+               }\r
+\r
+               /// <summary>\r
+               /// If the value of mode is true, set binary mode for downloads, else, Ascii mode.\r
+               /// </summary>\r
+               public bool BinaryMode\r
+               {\r
+                       get\r
+                       {\r
+                               return this.binMode;\r
+                       }\r
+                       set\r
+                       {\r
+                               if ( this.binMode == value ) return;\r
+\r
+                               if ( value )\r
+                                       sendCommand("TYPE I");\r
+\r
+                               else\r
+                                       sendCommand("TYPE A");\r
+\r
+                               if ( this.resultCode != 200 ) throw new FtpException(result.Substring(4));\r
+                       }\r
+               }\r
+               /// <summary>\r
+               /// Login to the remote server.\r
+               /// </summary>\r
+               public void Login()\r
+               {\r
+                       if ( this.loggedin ) this.Close();\r
+\r
+                       Debug.WriteLine("Opening connection to " + this.server, "FtpClient" );\r
+\r
+                       IPAddress addr = null;\r
+                       IPEndPoint ep = null;\r
+\r
+                       try\r
+                       {\r
+                               this.clientSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );\r
+                               addr = Dns.Resolve(this.server).AddressList[0];\r
+                               ep = new IPEndPoint( addr, this.port );\r
+                               this.clientSocket.Connect(ep);\r
+                       }\r
+                       catch(Exception ex)\r
+                       {\r
+                               // doubtfull\r
+                               if ( this.clientSocket != null && this.clientSocket.Connected ) this.clientSocket.Close();\r
+\r
+                               throw new FtpException("Couldn't connect to remote server",ex);\r
+                       }\r
+\r
+                       this.readResponse();\r
+\r
+                       if(this.resultCode != 220)\r
+                       {\r
+                               this.Close();\r
+                               throw new FtpException(this.result.Substring(4));\r
+                       }\r
+\r
+                       this.sendCommand( "USER " + username );\r
+\r
+                       if( !(this.resultCode == 331 || this.resultCode == 230) )\r
+                       {\r
+                               this.cleanup();\r
+                               throw new FtpException(this.result.Substring(4));\r
+                       }\r
+\r
+                       if( this.resultCode != 230 )\r
+                       {\r
+                               this.sendCommand( "PASS " + password );\r
+\r
+                               if( !(this.resultCode == 230 || this.resultCode == 202) )\r
+                               {\r
+                                       this.cleanup();\r
+                                       throw new FtpException(this.result.Substring(4));\r
+                               }\r
+                       }\r
+\r
+                       this.loggedin = true;\r
+\r
+                       Debug.WriteLine( "Connected to " + this.server, "FtpClient" );\r
+\r
+                       this.ChangeDir(this.remotePath);\r
+               }\r
+               \r
+               /// <summary>\r
+               /// Close the FTP connection.\r
+               /// </summary>\r
+               public void Close()\r
+               {\r
+                       Debug.WriteLine("Closing connection to " + this.server, "FtpClient" );\r
+\r
+                       if( this.clientSocket != null )\r
+                       {\r
+                               this.sendCommand("QUIT");\r
+                       }\r
+\r
+                       this.cleanup();\r
+               }\r
+\r
+               /// <summary>\r
+               /// Return a string array containing the remote directory's file list.\r
+               /// </summary>\r
+               /// <returns></returns>\r
+               public string[] GetFileList()\r
+               {\r
+                       return this.GetFileList("*.*");\r
+               }\r
+\r
+               /// <summary>\r
+               /// Return a string array containing the remote directory's file list.\r
+               /// </summary>\r
+               /// <param name="mask"></param>\r
+               /// <returns></returns>\r
+               public string[] GetFileList(string mask)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       Socket cSocket = createDataSocket();\r
+\r
+                       this.sendCommand("NLST " + mask);\r
+\r
+                       if(!(this.resultCode == 150 || this.resultCode == 125)) throw new FtpException(this.result.Substring(4));\r
+\r
+                       this.message = "";\r
+\r
+                       DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);\r
+\r
+                       while( timeout > DateTime.Now )\r
+                       {\r
+                               int bytes = cSocket.Receive(buffer, buffer.Length, 0);\r
+                               this.message += ASCII.GetString(buffer, 0, bytes);\r
+\r
+                               if ( bytes < this.buffer.Length ) break;\r
+                       }\r
+\r
+                       string[] msg = this.message.Replace("\r","").Split('\n');\r
+\r
+                       cSocket.Close();\r
+\r
+                       if ( this.message.IndexOf( "No such file or directory" ) != -1 )\r
+                               msg = new string[]{};\r
+\r
+                       this.readResponse();\r
+\r
+                       if ( this.resultCode != 226 )\r
+                               msg = new string[]{};\r
+                       //      throw new FtpException(result.Substring(4));\r
+\r
+                       return msg;\r
+               }\r
+               \r
+               /// <summary>\r
+               /// Return the size of a file.\r
+               /// </summary>\r
+               /// <param name="fileName"></param>\r
+               /// <returns></returns>\r
+               public long GetFileSize(string fileName)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       this.sendCommand("SIZE " + fileName);\r
+                       long size=0;\r
+\r
+                       if ( this.resultCode == 213 )\r
+                               size = long.Parse(this.result.Substring(4));\r
+\r
+                       else\r
+                               throw new FtpException(this.result.Substring(4));\r
+\r
+                       return size;\r
+               }\r
+       \r
+               \r
+               /// <summary>\r
+               /// Download a file to the Assembly's local directory,\r
+               /// keeping the same file name.\r
+               /// </summary>\r
+               /// <param name="remFileName"></param>\r
+               public void Download(string remFileName)\r
+               {\r
+                       this.Download(remFileName,"",false);\r
+               }\r
+\r
+               /// <summary>\r
+               /// Download a remote file to the Assembly's local directory,\r
+               /// keeping the same file name, and set the resume flag.\r
+               /// </summary>\r
+               /// <param name="remFileName"></param>\r
+               /// <param name="resume"></param>\r
+               public void Download(string remFileName,Boolean resume)\r
+               {\r
+                       this.Download(remFileName,"",resume);\r
+               }\r
+               \r
+               /// <summary>\r
+               /// Download a remote file to a local file name which can include\r
+               /// a path. The local file name will be created or overwritten,\r
+               /// but the path must exist.\r
+               /// </summary>\r
+               /// <param name="remFileName"></param>\r
+               /// <param name="locFileName"></param>\r
+               public void Download(string remFileName,string locFileName)\r
+               {\r
+                       this.Download(remFileName,locFileName,false);\r
+               }\r
+\r
+               /// <summary>\r
+               /// Download a remote file to a local file name which can include\r
+               /// a path, and set the resume flag. The local file name will be\r
+               /// created or overwritten, but the path must exist.\r
+               /// </summary>\r
+               /// <param name="remFileName"></param>\r
+               /// <param name="locFileName"></param>\r
+               /// <param name="resume"></param>\r
+               public void Download(string remFileName,string locFileName,Boolean resume)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       this.BinaryMode = true;\r
+\r
+                       Debug.WriteLine("Downloading file " + remFileName + " from " + server + "/" + remotePath, "FtpClient" );\r
+\r
+                       if (locFileName.Equals(""))\r
+                       {\r
+                               locFileName = remFileName;\r
+                       }\r
+\r
+                       FileStream output = null;\r
+\r
+                       if ( !File.Exists(locFileName) )\r
+                               output = File.Create(locFileName);\r
+\r
+                       else\r
+                               output = new FileStream(locFileName,FileMode.Open);\r
+\r
+                       Socket cSocket = createDataSocket();\r
+\r
+                       long offset = 0;\r
+\r
+                       if ( resume )\r
+                       {\r
+                               offset = output.Length;\r
+\r
+                               if ( offset > 0 )\r
+                               {\r
+                                       this.sendCommand( "REST " + offset );\r
+                                       if ( this.resultCode != 350 )\r
+                                       {\r
+                                               //Server dosnt support resuming\r
+                                               offset = 0;\r
+                                               Debug.WriteLine("Resuming not supported:" + result.Substring(4), "FtpClient" );\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               Debug.WriteLine("Resuming at offset " + offset, "FtpClient" );\r
+                                               output.Seek( offset, SeekOrigin.Begin );\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       this.sendCommand("RETR " + remFileName);\r
+\r
+                       if ( this.resultCode != 150 && this.resultCode != 125 )\r
+                       {\r
+                               throw new FtpException(this.result.Substring(4));\r
+                       }\r
+\r
+                       DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);\r
+\r
+                       while ( timeout > DateTime.Now )\r
+                       {\r
+                               this.bytes = cSocket.Receive(buffer, buffer.Length, 0);\r
+                               output.Write(this.buffer,0,this.bytes);\r
+\r
+                               if ( this.bytes <= 0)\r
+                               {\r
+                                       break;\r
+                               }\r
+                       }\r
+\r
+                       output.Close();\r
+\r
+                       if ( cSocket.Connected ) cSocket.Close();\r
+\r
+                       this.readResponse();\r
+\r
+                       if( this.resultCode != 226 && this.resultCode != 250 )\r
+                               throw new FtpException(this.result.Substring(4));\r
+               }\r
+\r
+               \r
+               /// <summary>\r
+               /// Upload a file.\r
+               /// </summary>\r
+               /// <param name="fileName"></param>\r
+               public void Upload(string fileName)\r
+               {\r
+                       this.Upload(fileName,false);\r
+               }\r
+\r
+               \r
+               /// <summary>\r
+               /// Upload a file and set the resume flag.\r
+               /// </summary>\r
+               /// <param name="fileName"></param>\r
+               /// <param name="resume"></param>\r
+               public void Upload(string fileName, bool resume)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       Socket cSocket = null ;\r
+                       long offset = 0;\r
+\r
+                       if ( resume )\r
+                       {\r
+                               try\r
+                               {\r
+                                       this.BinaryMode = true;\r
+\r
+                                       offset = GetFileSize( Path.GetFileName(fileName) );\r
+                               }\r
+                               catch(Exception)\r
+                               {\r
+                                       // file not exist\r
+                                       offset = 0;\r
+                               }\r
+                       }\r
+\r
+                       // open stream to read file\r
+                       FileStream input = new FileStream(fileName,FileMode.Open);\r
+\r
+                       if ( resume && input.Length < offset )\r
+                       {\r
+                               // different file size\r
+                               Debug.WriteLine("Overwriting " + fileName, "FtpClient");\r
+                               offset = 0;\r
+                       }\r
+                       else if ( resume && input.Length == offset )\r
+                       {\r
+                               // file done\r
+                               input.Close();\r
+                               Debug.WriteLine("Skipping completed " + fileName + " - turn resume off to not detect.", "FtpClient");\r
+                               return;\r
+                       }\r
+\r
+                       // dont create untill we know that we need it\r
+                       cSocket = this.createDataSocket();\r
+\r
+                       if ( offset > 0 )\r
+                       {\r
+                               this.sendCommand( "REST " + offset );\r
+                               if ( this.resultCode != 350 )\r
+                               {\r
+                                       Debug.WriteLine("Resuming not supported", "FtpClient");\r
+                                       offset = 0;\r
+                               }\r
+                       }\r
+\r
+                       this.sendCommand( "STOR " + Path.GetFileName(fileName) );\r
+\r
+                       if ( this.resultCode != 125 && this.resultCode != 150 ) throw new FtpException(result.Substring(4));\r
+\r
+                       if ( offset != 0 )\r
+                       {\r
+                               Debug.WriteLine("Resuming at offset " + offset, "FtpClient" );\r
+\r
+                               input.Seek(offset,SeekOrigin.Begin);\r
+                       }\r
+\r
+                       Debug.WriteLine( "Uploading file " + fileName + " to " + remotePath, "FtpClient" );\r
+\r
+                       while ((bytes = input.Read(buffer,0,buffer.Length)) > 0)\r
+                       {\r
+                               cSocket.Send(buffer, bytes, 0);\r
+                       }\r
+                       \r
+                       input.Close();\r
+\r
+                       if (cSocket.Connected)\r
+                       {\r
+                               cSocket.Close();\r
+                       }\r
+\r
+                       this.readResponse();\r
+\r
+                       if( this.resultCode != 226 && this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));\r
+               }\r
+               \r
+               /// <summary>\r
+               /// Upload a directory and its file contents\r
+               /// </summary>\r
+               /// <param name="path"></param>\r
+               /// <param name="recurse">Whether to recurse sub directories</param>\r
+               public void UploadDirectory(string path, bool recurse)\r
+               {\r
+                       this.UploadDirectory(path,recurse,"*.*");\r
+               }\r
+               \r
+               /// <summary>\r
+               /// Upload a directory and its file contents\r
+               /// </summary>\r
+               /// <param name="path"></param>\r
+               /// <param name="recurse">Whether to recurse sub directories</param>\r
+               /// <param name="mask">Only upload files of the given mask - everything is '*.*'</param>\r
+               public void UploadDirectory(string path, bool recurse, string mask)\r
+               {\r
+                       string[] dirs = path.Replace("/",@"\").Split('\\');\r
+                       string rootDir = dirs[ dirs.Length - 1 ];\r
+\r
+                       // make the root dir if it doed not exist\r
+                       if ( this.GetFileList(rootDir).Length < 1 ) this.MakeDir(rootDir);\r
+\r
+                       this.ChangeDir(rootDir);\r
+\r
+                       foreach ( string file in Directory.GetFiles(path,mask) )\r
+                       {\r
+                               this.Upload(file,true);\r
+                       }\r
+                       if ( recurse )\r
+                       {\r
+                               foreach ( string directory in Directory.GetDirectories(path) )\r
+                               {\r
+                                       this.UploadDirectory(directory,recurse,mask);\r
+                               }\r
+                       }\r
+\r
+                       this.ChangeDir("..");\r
+               }\r
+\r
+               /// <summary>\r
+               /// Delete a file from the remote FTP server.\r
+               /// </summary>\r
+               /// <param name="fileName"></param>\r
+               public void DeleteFile(string fileName)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       this.sendCommand( "DELE " + fileName );\r
+\r
+                       if ( this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));\r
+\r
+                       Debug.WriteLine( "Deleted file " + fileName, "FtpClient" );\r
+               }\r
+\r
+               /// <summary>\r
+               /// Rename a file on the remote FTP server.\r
+               /// </summary>\r
+               /// <param name="oldFileName"></param>\r
+               /// <param name="newFileName"></param>\r
+               /// <param name="overwrite">setting to false will throw exception if it exists</param>\r
+               public void RenameFile(string oldFileName,string newFileName, bool overwrite)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       this.sendCommand( "RNFR " + oldFileName );\r
+\r
+                       if ( this.resultCode != 350 ) throw new FtpException(this.result.Substring(4));\r
+\r
+                       if ( !overwrite && this.GetFileList(newFileName).Length > 0 ) throw new FtpException("File already exists");\r
+\r
+                       this.sendCommand( "RNTO " + newFileName );\r
+\r
+                       if ( this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));\r
+\r
+                       Debug.WriteLine( "Renamed file " + oldFileName + " to " + newFileName, "FtpClient" );\r
+               }\r
+               \r
+               /// <summary>\r
+               /// Create a directory on the remote FTP server.\r
+               /// </summary>\r
+               /// <param name="dirName"></param>\r
+               public void MakeDir(string dirName)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       this.sendCommand( "MKD " + dirName );\r
+\r
+                       if ( this.resultCode != 250 && this.resultCode != 257 ) throw new FtpException(this.result.Substring(4));\r
+\r
+                       Debug.WriteLine( "Created directory " + dirName, "FtpClient" );\r
+               }\r
+\r
+               /// <summary>\r
+               /// Delete a directory on the remote FTP server.\r
+               /// </summary>\r
+               /// <param name="dirName"></param>\r
+               public void RemoveDir(string dirName)\r
+               {\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       this.sendCommand( "RMD " + dirName );\r
+\r
+                       if ( this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));\r
+\r
+                       Debug.WriteLine( "Removed directory " + dirName, "FtpClient" );\r
+               }\r
+\r
+               /// <summary>\r
+               /// Change the current working directory on the remote FTP server.\r
+               /// </summary>\r
+               /// <param name="dirName"></param>\r
+               public void ChangeDir(string dirName)\r
+               {\r
+                       if( dirName == null || dirName.Equals(".") || dirName.Length == 0 )\r
+                       {\r
+                               return;\r
+                       }\r
+\r
+                       if ( !this.loggedin ) this.Login();\r
+\r
+                       this.sendCommand( "CWD " + dirName );\r
+\r
+                       if ( this.resultCode != 250 ) throw new FtpException(result.Substring(4));\r
+\r
+                       this.sendCommand( "PWD" );\r
+\r
+                       if ( this.resultCode != 257 ) throw new FtpException(result.Substring(4));\r
+\r
+                       // gonna have to do better than this....\r
+                       this.remotePath = this.message.Split('"')[1];\r
+\r
+                       Debug.WriteLine( "Current directory is " + this.remotePath, "FtpClient" );\r
+               }\r
+\r
+               /// <summary>\r
+               /// \r
+               /// </summary>\r
+               private void readResponse()\r
+               {\r
+                       this.message = "";\r
+                       this.result = this.readLine();\r
+\r
+                       if ( this.result.Length > 3 )\r
+                               this.resultCode = int.Parse( this.result.Substring(0,3) );\r
+                       else\r
+                               this.result = null;\r
+               }\r
+\r
+               /// <summary>\r
+               /// \r
+               /// </summary>\r
+               /// <returns></returns>\r
+               private string readLine()\r
+               {\r
+                       while(true)\r
+                       {\r
+                               this.bytes = clientSocket.Receive( this.buffer, this.buffer.Length, 0 );\r
+                               this.message += ASCII.GetString( this.buffer, 0, this.bytes );\r
+\r
+                               if ( this.bytes < this.buffer.Length )\r
+                               {\r
+                                       break;\r
+                               }\r
+                       }\r
+\r
+                       string[] msg = this.message.Split('\n');\r
+\r
+                       if ( this.message.Length > 2 )\r
+                               this.message = msg[ msg.Length - 2 ];\r
+\r
+                       else\r
+                               this.message = msg[0];\r
+\r
+\r
+                       if ( this.message.Length > 4 && !this.message.Substring(3,1).Equals(" ") ) return this.readLine();\r
+\r
+                       if ( this.verboseDebugging )\r
+                       {\r
+                               for(int i = 0; i < msg.Length - 1; i++)\r
+                               {\r
+                                       Debug.Write( msg[i], "FtpClient" );\r
+                               }\r
+                       }\r
+\r
+                       return message;\r
+               }\r
+\r
+               /// <summary>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="command"></param>\r
+               private void sendCommand(String command)\r
+               {\r
+                       if ( this.verboseDebugging ) Debug.WriteLine(command,"FtpClient");\r
+\r
+                       Byte[] cmdBytes = Encoding.ASCII.GetBytes( ( command + "\r\n" ).ToCharArray() );\r
+                       clientSocket.Send( cmdBytes, cmdBytes.Length, 0);\r
+                       this.readResponse();\r
+               }\r
+\r
+               /// <summary>\r
+               /// when doing data transfers, we need to open another socket for it.\r
+               /// </summary>\r
+               /// <returns>Connected socket</returns>\r
+               private Socket createDataSocket()\r
+               {\r
+                       this.sendCommand("PASV");\r
+\r
+                       if ( this.resultCode != 227 ) throw new FtpException(this.result.Substring(4));\r
+\r
+                       int index1 = this.result.IndexOf('(');\r
+                       int index2 = this.result.IndexOf(')');\r
+\r
+                       string ipData = this.result.Substring(index1+1,index2-index1-1);\r
+\r
+                       int[] parts = new int[6];\r
+\r
+                       int len = ipData.Length;\r
+                       int partCount = 0;\r
+                       string buf="";\r
+\r
+                       for (int i = 0; i < len && partCount <= 6; i++)\r
+                       {\r
+                               char ch = char.Parse( ipData.Substring(i,1) );\r
+\r
+                               if ( char.IsDigit(ch) )\r
+                                       buf+=ch;\r
+\r
+                               else if (ch != ',')\r
+                                       throw new FtpException("Malformed PASV result: " + result);\r
+\r
+                               if ( ch == ',' || i+1 == len )\r
+                               {\r
+                                       try\r
+                                       {\r
+                                               parts[partCount++] = int.Parse(buf);\r
+                                               buf = "";\r
+                                       }\r
+                                       catch (Exception ex)\r
+                                       {\r
+                                               throw new FtpException("Malformed PASV result (not supported?): " + this.result, ex);\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       string ipAddress = parts[0] + "."+ parts[1]+ "." + parts[2] + "." + parts[3];\r
+\r
+                       int port = (parts[4] << 8) + parts[5];\r
+\r
+                       Socket socket = null;\r
+                       IPEndPoint ep = null;\r
+\r
+                       try\r
+                       {\r
+                               socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);\r
+                               ep = new IPEndPoint(Dns.Resolve(ipAddress).AddressList[0], port);\r
+                               socket.Connect(ep);\r
+                       }\r
+                       catch(Exception ex)\r
+                       {\r
+                               // doubtfull....\r
+                               if ( socket != null && socket.Connected ) socket.Close();\r
+\r
+                               throw new FtpException("Can't connect to remote server", ex);\r
+                       }\r
+\r
+                       return socket;\r
+               }\r
+               \r
+               /// <summary>\r
+               /// Always release those sockets.\r
+               /// </summary>\r
+               private void cleanup()\r
+               {\r
+                       if ( this.clientSocket!=null )\r
+                       {\r
+                               this.clientSocket.Close();\r
+                               this.clientSocket = null;\r
+                       }\r
+                       this.loggedin = false;\r
+               }\r
+\r
+               /// <summary>\r
+               /// Destuctor\r
+               /// </summary>\r
+               ~FtpClient()\r
+               {\r
+                       this.cleanup();\r
+               }\r
+\r
+\r
+               /**************************************************************************************************************/\r
+               #region Async methods (auto generated)\r
+\r
+/*\r
+                               WinInetApi.FtpClient ftp = new WinInetApi.FtpClient();\r
+\r
+                               MethodInfo[] methods = ftp.GetType().GetMethods(BindingFlags.DeclaredOnly|BindingFlags.Instance|BindingFlags.Public);\r
+\r
+                               foreach ( MethodInfo method in methods )\r
+                               {\r
+                                       string param = "";\r
+                                       string values = "";\r
+                                       foreach ( ParameterInfo i in  method.GetParameters() )\r
+                                       {\r
+                                               param += i.ParameterType.Name + " " + i.Name + ",";\r
+                                               values += i.Name + ",";\r
+                                       }\r
+                                       \r
+\r
+                                       Debug.WriteLine("private delegate " + method.ReturnType.Name + " " + method.Name + "Callback(" + param.TrimEnd(',') + ");");\r
+\r
+                                       Debug.WriteLine("public System.IAsyncResult Begin" + method.Name + "( " + param + " System.AsyncCallback callback )");\r
+                                       Debug.WriteLine("{");\r
+                                       Debug.WriteLine("" + method.Name + "Callback ftpCallback = new " + method.Name + "Callback(" + values + " this." + method.Name + ");");\r
+                                       Debug.WriteLine("return ftpCallback.BeginInvoke(callback, null);");\r
+                                       Debug.WriteLine("}");\r
+                                       Debug.WriteLine("public void End" + method.Name + "(System.IAsyncResult asyncResult)");\r
+                                       Debug.WriteLine("{");\r
+                                       Debug.WriteLine(method.Name + "Callback fc = (" + method.Name + "Callback) ((AsyncResult)asyncResult).AsyncDelegate;");\r
+                                       Debug.WriteLine("fc.EndInvoke(asyncResult);");\r
+                                       Debug.WriteLine("}");\r
+                                       //Debug.WriteLine(method);\r
+                               }\r
+*/\r
+\r
+\r
+               private delegate void LoginCallback();\r
+               public System.IAsyncResult BeginLogin(  System.AsyncCallback callback )\r
+               {\r
+                       LoginCallback ftpCallback = new LoginCallback( this.Login);\r
+                       return ftpCallback.BeginInvoke(callback, null);\r
+               }\r
+               private delegate void CloseCallback();\r
+               public System.IAsyncResult BeginClose(  System.AsyncCallback callback )\r
+               {\r
+                       CloseCallback ftpCallback = new CloseCallback( this.Close);\r
+                       return ftpCallback.BeginInvoke(callback, null);\r
+               }\r
+               private delegate String[] GetFileListCallback();\r
+               public System.IAsyncResult BeginGetFileList(  System.AsyncCallback callback )\r
+               {\r
+                       GetFileListCallback ftpCallback = new GetFileListCallback( this.GetFileList);\r
+                       return ftpCallback.BeginInvoke(callback, null);\r
+               }\r
+               private delegate String[] GetFileListMaskCallback(String mask);\r
+               public System.IAsyncResult BeginGetFileList( String mask, System.AsyncCallback callback )\r
+               {\r
+                       GetFileListMaskCallback ftpCallback = new GetFileListMaskCallback(this.GetFileList);\r
+                       return ftpCallback.BeginInvoke(mask, callback, null);\r
+               }\r
+               private delegate Int64 GetFileSizeCallback(String fileName);\r
+               public System.IAsyncResult BeginGetFileSize( String fileName, System.AsyncCallback callback )\r
+               {\r
+                       GetFileSizeCallback ftpCallback = new GetFileSizeCallback(this.GetFileSize);\r
+                       return ftpCallback.BeginInvoke(fileName, callback, null);\r
+               }\r
+               private delegate void DownloadCallback(String remFileName);\r
+               public System.IAsyncResult BeginDownload( String remFileName, System.AsyncCallback callback )\r
+               {\r
+                       DownloadCallback ftpCallback = new DownloadCallback(this.Download);\r
+                       return ftpCallback.BeginInvoke(remFileName, callback, null);\r
+               }\r
+               private delegate void DownloadFileNameResumeCallback(String remFileName,Boolean resume);\r
+               public System.IAsyncResult BeginDownload( String remFileName,Boolean resume, System.AsyncCallback callback )\r
+               {\r
+                       DownloadFileNameResumeCallback ftpCallback = new DownloadFileNameResumeCallback(this.Download);\r
+                       return ftpCallback.BeginInvoke(remFileName, resume, callback, null);\r
+               }\r
+               private delegate void DownloadFileNameFileNameCallback(String remFileName,String locFileName);\r
+               public System.IAsyncResult BeginDownload( String remFileName,String locFileName, System.AsyncCallback callback )\r
+               {\r
+                       DownloadFileNameFileNameCallback ftpCallback = new DownloadFileNameFileNameCallback(this.Download);\r
+                       return ftpCallback.BeginInvoke(remFileName, locFileName, callback, null);\r
+               }\r
+               private delegate void DownloadFileNameFileNameResumeCallback(String remFileName,String locFileName,Boolean resume);\r
+               public System.IAsyncResult BeginDownload( String remFileName,String locFileName,Boolean resume, System.AsyncCallback callback )\r
+               {\r
+                       DownloadFileNameFileNameResumeCallback ftpCallback = new DownloadFileNameFileNameResumeCallback(this.Download);\r
+                       return ftpCallback.BeginInvoke(remFileName, locFileName, resume, callback, null);\r
+               }\r
+               private delegate void UploadCallback(String fileName);\r
+               public System.IAsyncResult BeginUpload( String fileName, System.AsyncCallback callback )\r
+               {\r
+                       UploadCallback ftpCallback = new UploadCallback(this.Upload);\r
+                       return ftpCallback.BeginInvoke(fileName, callback, null);\r
+               }\r
+               private delegate void UploadFileNameResumeCallback(String fileName,Boolean resume);\r
+               public System.IAsyncResult BeginUpload( String fileName,Boolean resume, System.AsyncCallback callback )\r
+               {\r
+                       UploadFileNameResumeCallback ftpCallback = new UploadFileNameResumeCallback(this.Upload);\r
+                       return ftpCallback.BeginInvoke(fileName, resume, callback, null);\r
+               }\r
+               private delegate void UploadDirectoryCallback(String path,Boolean recurse);\r
+               public System.IAsyncResult BeginUploadDirectory( String path,Boolean recurse, System.AsyncCallback callback )\r
+               {\r
+                       UploadDirectoryCallback ftpCallback = new UploadDirectoryCallback(this.UploadDirectory);\r
+                       return ftpCallback.BeginInvoke(path, recurse, callback, null);\r
+               }\r
+               private delegate void UploadDirectoryPathRecurseMaskCallback(String path,Boolean recurse,String mask);\r
+               public System.IAsyncResult BeginUploadDirectory( String path,Boolean recurse,String mask, System.AsyncCallback callback )\r
+               {\r
+                       UploadDirectoryPathRecurseMaskCallback ftpCallback = new UploadDirectoryPathRecurseMaskCallback(this.UploadDirectory);\r
+                       return ftpCallback.BeginInvoke(path, recurse, mask, callback, null);\r
+               }\r
+               private delegate void DeleteFileCallback(String fileName);\r
+               public System.IAsyncResult BeginDeleteFile( String fileName, System.AsyncCallback callback )\r
+               {\r
+                       DeleteFileCallback ftpCallback = new DeleteFileCallback(this.DeleteFile);\r
+                       return ftpCallback.BeginInvoke(fileName, callback, null);\r
+               }\r
+               private delegate void RenameFileCallback(String oldFileName,String newFileName,Boolean overwrite);\r
+               public System.IAsyncResult BeginRenameFile( String oldFileName,String newFileName,Boolean overwrite, System.AsyncCallback callback )\r
+               {\r
+                       RenameFileCallback ftpCallback = new RenameFileCallback(this.RenameFile);\r
+                       return ftpCallback.BeginInvoke(oldFileName, newFileName, overwrite, callback, null);\r
+               }\r
+               private delegate void MakeDirCallback(String dirName);\r
+               public System.IAsyncResult BeginMakeDir( String dirName, System.AsyncCallback callback )\r
+               {\r
+                       MakeDirCallback ftpCallback = new MakeDirCallback(this.MakeDir);\r
+                       return ftpCallback.BeginInvoke(dirName, callback, null);\r
+               }\r
+               private delegate void RemoveDirCallback(String dirName);\r
+               public System.IAsyncResult BeginRemoveDir( String dirName, System.AsyncCallback callback )\r
+               {\r
+                       RemoveDirCallback ftpCallback = new RemoveDirCallback(this.RemoveDir);\r
+                       return ftpCallback.BeginInvoke(dirName, callback, null);\r
+               }\r
+               private delegate void ChangeDirCallback(String dirName);\r
+               public System.IAsyncResult BeginChangeDir( String dirName, System.AsyncCallback callback )\r
+               {\r
+                       ChangeDirCallback ftpCallback = new ChangeDirCallback(this.ChangeDir);\r
+                       return ftpCallback.BeginInvoke(dirName, callback, null);\r
+               }\r
+\r
+               #endregion\r
+       }\r
+}
\ No newline at end of file
index 3535671..319b102 100644 (file)
@@ -13,6 +13,17 @@ namespace ReactOS.CustomRevisionAction
                /// </summary>\r
                private static string publishPath;\r
 \r
+               /// <summary>\r
+               /// Whether or not to publish ISOs to a remote destination via FTP.\r
+               /// </summary>\r
+               private static bool PublishToRemoteFtpLocation\r
+               {\r
+                       get\r
+                       {\r
+                               return publishPath.StartsWith("ftp://");\r
+                       }\r
+               }\r
+\r
                /// <summary>\r
                /// Run the application.\r
                /// </summary>\r
@@ -138,8 +149,59 @@ namespace ReactOS.CustomRevisionAction
                                             revision);\r
                }\r
 \r
+               private static void SplitRemotePublishPath(string publishPath,\r
+                                                          out string server,\r
+                                                          out string directory)\r
+               {\r
+                       string searchString = "://";\r
+                       int index = publishPath.IndexOf(searchString);\r
+                       if (index == -1)\r
+                               throw new InvalidOperationException();\r
+                       int endOfProtocolIndex = index + searchString.Length;\r
+                       string withoutProtocol = publishPath.Remove(0, endOfProtocolIndex);\r
+                       index = withoutProtocol.IndexOf("/");\r
+                       if (index == -1)\r
+                       {\r
+                               server = withoutProtocol;\r
+                               directory = "";\r
+                       }\r
+                       else\r
+                       {\r
+                               server = withoutProtocol.Substring(0, index);\r
+                               directory = withoutProtocol.Remove(0, index + 1);\r
+                       }\r
+               }\r
+\r
                /// <summary>\r
-               /// Copy ISO to the destination.\r
+               /// Copy ISO to the (remote) destination.\r
+               /// </summary>\r
+               /// <param name="sourceFilename">Name of source ISO file to copy.</param>\r
+               /// <param name="branch">Branch.</param>\r
+               /// <param name="revision">Revision.</param>\r
+               /// <remarks>\r
+               /// Structure is ftp://ftp.server.com/whereever/<branch>/ReactOS-<branch>-r<revision>.iso.\r
+               /// </remarks>\r
+               private static void CopyISOToRemoteFtpDestination(string sourceFilename,\r
+                                                                 string branch,\r
+                                                                 int revision)\r
+               {\r
+                       string server;\r
+                       string directory;\r
+                       SplitRemotePublishPath(publishPath, out server, out directory);\r
+                       FtpClient ftpClient = new FtpClient(server, "anonymous", "sin@svn.reactos.com");\r
+                       ftpClient.Login();\r
+                       if (directory != "")\r
+                               ftpClient.ChangeDir(directory);\r
+                       /* Create destination directory if it does not already exist */\r
+                       if (ftpClient.GetFileList(branch).Length < 1)\r
+                               ftpClient.MakeDir(branch);\r
+                       ftpClient.ChangeDir(branch);\r
+                       ftpClient.Upload(sourceFilename);\r
+                       ftpClient.Close();\r
+               }\r
+\r
+               /// <summary>\r
+               /// Copy ISO to the (local) destination.\r
                /// </summary>\r
                /// <param name="sourceFilename">Name of source ISO file to copy.</param>\r
                /// <param name="branch">Branch.</param>\r
@@ -147,9 +209,9 @@ namespace ReactOS.CustomRevisionAction
                /// <remarks>\r
                /// Structure is <branch>\ReactOS-<branch>-r<revision>.iso.\r
                /// </remarks>\r
-               private static void CopyISOToDestination(string sourceFilename,\r
-                                                        string branch,\r
-                                                        int revision)\r
+               private static void CopyISOToLocalDestination(string sourceFilename,\r
+                                                             string branch,\r
+                                                             int revision)\r
                {\r
                        string distributionFilename = GetDistributionFilename(branch,\r
                                                                              revision);\r
@@ -163,6 +225,25 @@ namespace ReactOS.CustomRevisionAction
                                  destinationFilename);\r
                }\r
 \r
+               /// <summary>\r
+               /// Copy ISO to the destination.\r
+               /// </summary>\r
+               /// <param name="sourceFilename">Name of source ISO file to copy.</param>\r
+               /// <param name="branch">Branch.</param>\r
+               /// <param name="revision">Revision.</param>\r
+               /// <remarks>\r
+               /// Structure is <branch>\ReactOS-<branch>-r<revision>.iso.\r
+               /// </remarks>\r
+               private static void CopyISOToDestination(string sourceFilename,\r
+                                                        string branch,\r
+                                                        int revision)\r
+               {\r
+                       if (PublishToRemoteFtpLocation)\r
+                               CopyISOToRemoteFtpDestination(sourceFilename, branch, revision);\r
+                       else\r
+                               CopyISOToLocalDestination(sourceFilename, branch, revision);\r
+               }\r
+\r
                /// <summary>\r
                /// Publish a revision of ReactOS.\r
                /// </summary>\r