public static void Pull(ref FtpLib ftp) { // return if disabled if (Config.settings.SyncFolders == "") { return; } // download changes on server to local foreach (string folder in Config.settings.SyncFolders.Split(',')) { Console.WriteLine(Strings.Get("SyncDown") + " " + folder + " ... "); FTPSyncSettings ftpsyncsettings = new FTPSyncSettings(); if (folder == "config") { // special case for config folder ftpsyncsettings.Recurse = false; ftpsyncsettings.WhenMissingLocal = FTPAction.Copy; ftpsyncsettings.WhenMissingRemote = FTPAction.Noop; ftpsyncsettings.WhenNewerLocal = FTPAction.Noop; ftpsyncsettings.WhenNewerRemote = FTPAction.Copy; ftpsyncsettings.WhenLargerLocal = FTPAction.Noop; ftpsyncsettings.WhenLargerRemote = FTPAction.Noop; } else { // other folders ftpsyncsettings.Recurse = false; ftpsyncsettings.WhenMissingLocal = FTPAction.Copy; ftpsyncsettings.WhenMissingRemote = FTPAction.Delete; ftpsyncsettings.WhenNewerLocal = FTPAction.Noop; ftpsyncsettings.WhenNewerRemote = FTPAction.Noop; ftpsyncsettings.WhenLargerLocal = FTPAction.Noop; ftpsyncsettings.WhenLargerRemote = FTPAction.Noop; } Sync(ref ftp, folder, ftpsyncsettings); } }
public static void Push(ref FtpLib ftp) { // return if disabled if (Config.settings.SyncFolders == "") { return; } if (Config.settings.SyncAllowUpload != "true") { Console.WriteLine(Strings.Get("SyncUpNotAllowed")); Console.ReadKey(); return; } // upload changes in local folders to server foreach (string folder in Config.settings.SyncFolders.Split(',')) { Console.WriteLine(Strings.Get("SyncUp") + " " + folder + " ... "); FTPSyncSettings ftpsyncsettings = new FTPSyncSettings(); if (folder == "config") { // skip push for config folder continue; } else { // other folders ftpsyncsettings.Recurse = false; ftpsyncsettings.WhenMissingLocal = FTPAction.Delete; ftpsyncsettings.WhenMissingRemote = FTPAction.Copy; ftpsyncsettings.WhenNewerLocal = FTPAction.Noop; ftpsyncsettings.WhenNewerRemote = FTPAction.Noop; ftpsyncsettings.WhenLargerLocal = FTPAction.Noop; ftpsyncsettings.WhenLargerRemote = FTPAction.Noop; } Sync(ref ftp, folder, ftpsyncsettings); } }
static bool Sync(ref FtpLib ftp, string folder, FTPSyncSettings ftpsyncsettings) { try { // make list of changes string localFolder = Locations.LocalFolderName_Minecraft + "\\" + folder; string remoteFolder = Config.ftpsettings.FtpServerFolder + "/" + folder; List <FTPSyncModification> changes = ftp.CompareDirectory(localFolder, remoteFolder, ftpsyncsettings); if (changes.Count > 0) { Console.WriteLine(Strings.Get("Syncing") + " " + folder + " ... "); ftp.SynchronizeFiles(changes, false); } } catch (Exception ex) { Console.WriteLine(Strings.Get("SyncError") + ex.Message); Console.WriteLine(Strings.Get("PressKey")); Console.ReadKey(); return(false); } return(true); }
/// <summary> /// Compare local directory with remote directory /// </summary> /// <param name="localFolder">folder @ client</param> /// <param name="remoteFolder">folder @ server</param> /// <param name="recursive">Will we go into directories?</param> /// <returns>List of modifications to make</returns> public List <FTPSyncModification> CompareDirectory(string localFolder, string remoteFolder, FTPSyncSettings settings) { BytesTotal = 0; BytesDone = 0; BytesLast = 0; LastFile = ""; // make list of local files List <string> locals = new List <string>(); GetLocalFiles(ref locals, localFolder, settings.Recurse); // check remote files List <FTPSyncModification> mods = new List <FTPSyncModification>(); CheckRemoteFiles(ref locals, ref mods, localFolder, remoteFolder, settings); if (settings.WhenMissingRemote != FTPAction.Noop) { // local files still in list have been added FileInfo fi; foreach (string LocalFile in locals) { fi = new FileInfo(LocalFile); FTPSyncModification m = new FTPSyncModification(); m.Exists = false; m.Upload = true; m.Action = settings.WhenMissingRemote; m.LocalFile = LocalFile; m.ModifiedTime = fi.LastWriteTime; m.RemoteFile = LocalFile.Replace(localFolder, remoteFolder).Replace('\\', '/'); if (settings.WhenMissingRemote == FTPAction.Copy) { BytesTotal += fi.Length; } mods.Add(m); ShowMod(m); } } return(mods); }
/// <summary> /// Check all remote files for: /// </summary> /// <param name="locals"></param> /// <param name="mods">List of modifications. Items are added when syncing is required</param> /// <param name="localFolder">folder @ client</param> /// <param name="remoteFolder">folder @ server</param> /// <param name="recursive">if true, directories are also checked by recalling this function </param> private void CheckRemoteFiles(ref List <string> locals, ref List <FTPSyncModification> mods, string localFolder, string remoteFolder, FTPSyncSettings settings) { this.FtpServer.ChangeWorkingDirectory(remoteFolder); FTPFile[] FtpServerFileInfo = this.FtpServer.GetFileInfos(); string LocalTargetFolder = null; string FtpTargetFolder = null; bool AddModification = false; FileInfo fi = default(FileInfo); foreach (FTPFile CurrentFileOrDirectory in FtpServerFileInfo) { if (settings.Recurse) { if (CurrentFileOrDirectory.Dir && CurrentFileOrDirectory.Name != "." && CurrentFileOrDirectory.Name != "..") { LocalTargetFolder = Path.Combine(localFolder, CurrentFileOrDirectory.Name); FtpTargetFolder = string.Format("{0}/{1}", remoteFolder, CurrentFileOrDirectory.Name); CheckRemoteFiles(ref locals, ref mods, LocalTargetFolder, FtpTargetFolder, settings); // set the ftp working folder back to the correct value this.FtpServer.ChangeWorkingDirectory(remoteFolder); } } if (!CurrentFileOrDirectory.Dir) { FTPSyncModification m = new FTPSyncModification(); AddModification = false; DateTime RemoteStamp = TimeZone.CurrentTimeZone.ToLocalTime(CurrentFileOrDirectory.LastModified); m.LocalFile = Path.Combine(localFolder, CurrentFileOrDirectory.Name); m.RemoteFile = string.Format("{0}/{1}", remoteFolder, CurrentFileOrDirectory.Name); // check file existence if (!File.Exists(m.LocalFile)) { if (settings.WhenMissingLocal != FTPAction.Noop) { AddModification = true; m.Exists = false; m.ModifiedTime = RemoteStamp; m.Upload = false; m.Action = settings.WhenMissingLocal; if (settings.WhenMissingLocal == FTPAction.Copy) { BytesTotal += CurrentFileOrDirectory.Size; } } } else { // read file info fi = new FileInfo(m.LocalFile); DateTime LocalStamp = TimeZone.CurrentTimeZone.ToLocalTime(fi.LastWriteTime); // check file size if (CurrentFileOrDirectory.Size != fi.Length) { m.Exists = true; if (CurrentFileOrDirectory.Size < fi.Length) { if (settings.WhenLargerLocal == FTPAction.Copy) { AddModification = true; m.ModifiedTime = LocalStamp; m.Upload = true; m.Action = settings.WhenLargerLocal; BytesTotal += fi.Length; } } else { if (settings.WhenLargerRemote == FTPAction.Copy) { AddModification = true; m.ModifiedTime = RemoteStamp; m.Upload = false; m.Action = settings.WhenLargerRemote; BytesTotal += CurrentFileOrDirectory.Size; } } } // check modification time if (RemoteStamp != LocalStamp) { m.Exists = true; if (RemoteStamp < LocalStamp) { if (settings.WhenNewerLocal == FTPAction.Copy) { AddModification = true; m.ModifiedTime = LocalStamp; m.Upload = true; m.Action = settings.WhenNewerLocal; BytesTotal += fi.Length; } } else { if (settings.WhenNewerRemote == FTPAction.Copy) { AddModification = true; m.ModifiedTime = RemoteStamp; m.Upload = false; m.Action = settings.WhenNewerRemote; BytesTotal += CurrentFileOrDirectory.Size; } } } // remove from local list locals.Remove(m.LocalFile); } if (AddModification) { mods.Add(m); ShowMod(m); } } } }