From 29483b2625e4d7a512788983bc6d01ea6568f0b0 Mon Sep 17 00:00:00 2001 From: David Sparer Date: Mon, 23 Jul 2018 12:53:03 -0500 Subject: [PATCH] attached the file backup pruner to listen to connection file save events resolves #1020 --- CHANGELOG.TXT | 1 + mRemoteV1/App/Runtime.cs | 7 ------ .../Config/DataProviders/FileBackupPruner.cs | 23 +++++++++++-------- mRemoteV1/UI/Forms/frmMain.cs | 13 ++++++++++- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index 6db27634..4fa1c566 100644 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -3,6 +3,7 @@ Fixes: ------ #1030: Exception thrown if importing from port scan and no tree node is selected +#1020: BackupFileKeepCount setting not limiting backup file count #1004: Duplicating root or PuTTy node through hotkey causes unhandled exception #1002: Disabling filtering without clearing keyword leaves filtered state #1001: Connection tree context menu hotkeys stop working and disappear in some cases diff --git a/mRemoteV1/App/Runtime.cs b/mRemoteV1/App/Runtime.cs index 8a46c6b2..b1921a8c 100644 --- a/mRemoteV1/App/Runtime.cs +++ b/mRemoteV1/App/Runtime.cs @@ -4,7 +4,6 @@ using System.Security; using System.Threading; using System.Windows.Forms; using mRemoteNG.App.Info; -using mRemoteNG.Config.DataProviders; using mRemoteNG.Config.Putty; using mRemoteNG.Connection; using mRemoteNG.Credential; @@ -78,12 +77,6 @@ namespace mRemoteNG.App { connectionFileName = ConnectionsService.GetStartupConnectionFileName(); } - - var backupFileCreator = new FileBackupCreator(); - backupFileCreator.CreateBackupFile(connectionFileName); - - var backupPruner = new FileBackupPruner(); - backupPruner.PruneBackupFiles(connectionFileName); } ConnectionsService.LoadConnections(Settings.Default.UseSQLServer, false, connectionFileName); diff --git a/mRemoteV1/Config/DataProviders/FileBackupPruner.cs b/mRemoteV1/Config/DataProviders/FileBackupPruner.cs index d371647d..5572a345 100644 --- a/mRemoteV1/Config/DataProviders/FileBackupPruner.cs +++ b/mRemoteV1/Config/DataProviders/FileBackupPruner.cs @@ -1,26 +1,29 @@ -using System; -using System.IO; +using System.IO; +using System.Linq; namespace mRemoteNG.Config.DataProviders { public class FileBackupPruner { - public void PruneBackupFiles(string baseName) + public void PruneBackupFiles(string filePath, int maxBackupsToKeep) { - var fileName = Path.GetFileName(baseName); - var directoryName = Path.GetDirectoryName(baseName); + var fileName = Path.GetFileName(filePath); + var directoryName = Path.GetDirectoryName(filePath); - if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(directoryName)) return; + if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(directoryName)) + return; var searchPattern = string.Format(mRemoteNG.Settings.Default.BackupFileNameFormat, fileName, "*"); var files = Directory.GetFiles(directoryName, searchPattern); - if (files.Length <= mRemoteNG.Settings.Default.BackupFileKeepCount) return; + if (files.Length <= maxBackupsToKeep) + return; - Array.Sort(files); - Array.Resize(ref files, files.Length - mRemoteNG.Settings.Default.BackupFileKeepCount); + var filesToDelete = files + .OrderByDescending(s => s) + .Skip(maxBackupsToKeep); - foreach (var file in files) + foreach (var file in filesToDelete) { File.Delete(file); } diff --git a/mRemoteV1/UI/Forms/frmMain.cs b/mRemoteV1/UI/Forms/frmMain.cs index 8c5f3821..e378ff66 100644 --- a/mRemoteV1/UI/Forms/frmMain.cs +++ b/mRemoteV1/UI/Forms/frmMain.cs @@ -14,6 +14,7 @@ using mRemoteNG.App.Info; using mRemoteNG.App.Initialization; using mRemoteNG.Config; using mRemoteNG.Config.Connections; +using mRemoteNG.Config.DataProviders; using mRemoteNG.Config.Putty; using mRemoteNG.Config.Settings; using mRemoteNG.Connection; @@ -31,7 +32,7 @@ using WeifenLuo.WinFormsUI.Docking; namespace mRemoteNG.UI.Forms { - public partial class FrmMain + public partial class FrmMain { public static FrmMain Default { get; } = new FrmMain(); @@ -46,6 +47,7 @@ namespace mRemoteNG.UI.Forms private ConnectionInfo _selectedConnection; private readonly IList _messageWriters = new List(); private readonly ThemeManager _themeManager; + private readonly FileBackupPruner _backupPruner = new FileBackupPruner(); internal FullscreenHandler Fullscreen { get; set; } @@ -159,6 +161,7 @@ namespace mRemoteNG.UI.Forms SetDefaultLayout(); Runtime.ConnectionsService.ConnectionsLoaded += ConnectionsServiceOnConnectionsLoaded; + Runtime.ConnectionsService.ConnectionsSaved += ConnectionsServiceOnConnectionsSaved; var credsAndConsSetup = new CredsAndConsSetup(); credsAndConsSetup.LoadCredsAndCons(); @@ -217,6 +220,14 @@ namespace mRemoteNG.UI.Forms UpdateWindowTitle(); } + private void ConnectionsServiceOnConnectionsSaved(object sender, ConnectionsSavedEventArgs connectionsSavedEventArgs) + { + if (connectionsSavedEventArgs.UsingDatabase) + return; + + _backupPruner.PruneBackupFiles(connectionsSavedEventArgs.ConnectionFileName, Settings.Default.BackupFileKeepCount); + } + private void SetMenuDependencies() { var connectionInitiator = new ConnectionInitiator();