From a0abc299e98354028c70109ac6cd7976807e42f7 Mon Sep 17 00:00:00 2001 From: Jason Curl Date: Sat, 1 Feb 2020 23:19:23 +0100 Subject: [PATCH] CleanUpDump: Allow a path to be given No longer clean up during a dump if that dump received a filename explicitly. The user must use the new CleanUpDump(dumpDir) method. The dumpDir wasn't being cleaned prior in any case, but the central location was. When the user uses the CleanUpDump() method, we don't need to clean both locations. There are two new methods: * CleanUpDump(string dumpFolder): Remove all logs in the folder given based on default rules * CleanUpDump(string dumpFolder, string fileMatchRegEx): A regular expression which needs to match to remove the file. Include the .zip at the end. Issue: HELIOS-1351 --- CrashReporter/CrashReporter.cs | 78 ++++++++++++++++++++-------------- CrashReporter/Dump/Crash.cs | 2 +- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/CrashReporter/CrashReporter.cs b/CrashReporter/CrashReporter.cs index 4b01731..5db8b53 100644 --- a/CrashReporter/CrashReporter.cs +++ b/CrashReporter/CrashReporter.cs @@ -327,9 +327,11 @@ public static string CreateDump(CoreType coreType) /// public static string CreateDump(string fileName, CoreType coreType) { - try { - CleanUpDump(); - } catch { /* Ignore any errors while trying to clean up the dump, so we can continue to crash */ } + if (fileName == null) { + try { + CleanUpDump(); + } catch { /* Ignore any errors while trying to clean up the dump, so we can continue to crash */ } + } string crashDumpFile = null; if (fileName != null) { @@ -394,26 +396,43 @@ public static string CreateDump(string fileName, CoreType coreType) return dumpFileName; } - private static Regex s_CrashFileRegex; + private const long GbMultiplier = 1024 * 1024 * 1024; - private static Regex CrashFileRegEx + /// + /// Removes old dump files from the default dump directory created when using . + /// + /// + /// Over time, the number of files in the dump may take a significant amount of space. This method allows to + /// programmatically remove old dumps and large dumps as disk space is reduced. + /// + /// A log might be required to be deleted, but if there is a file system error, it will be skipped. + /// + /// + public static void CleanUpDump() { - get - { - if (s_CrashFileRegex == null) { - s_CrashFileRegex = new Regex(Crash.CrashPathRegEx); - } - return s_CrashFileRegex; - } + string dumpFolder = Crash.GetCrashFolder(); + CleanUpDump(dumpFolder, Crash.CrashPathRegEx); } - private const long GbMultiplier = 1024 * 1024 * 1024; + /// + /// Removes old dump files from the default dump directory created when using . + /// + /// The dump folder to clean. + /// + /// is . + /// + public static void CleanUpDump(string dumpFolder) + { + CleanUpDump(dumpFolder, null); + } /// /// Removes old dump files from the default dump directory created when using . /// - /// - /// The platform is not supported, so delete operations can't be performed. + /// The dump folder to clean. + /// The file match regular expression to only erase dumps that match. + /// + /// is . /// /// /// Over time, the number of files in the dump may take a significant amount of space. This method allows to @@ -426,17 +445,20 @@ private static Regex CrashFileRegEx Justification = "Kept in case ordering is changed to reduce possible bugs")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0059:Unnecessary assignment of a value", Justification = "Kept in case ordering is changed to reduce possible bugs")] - public static void CleanUpDump() + public static void CleanUpDump(string dumpFolder, string fileMatchRegEx) { - string dumpFolder; - try { - dumpFolder = Crash.GetCrashFolder(); - } catch (PlatformNotSupportedException) { - return; - } + if (dumpFolder == null) throw new ArgumentNullException(nameof(dumpFolder)); if (!Directory.Exists(dumpFolder)) return; - Regex crashFileRegex = CrashFileRegEx; + Regex crashFileRegex = null; + if (fileMatchRegEx != null) { + try { + crashFileRegex = new Regex(fileMatchRegEx); + } catch (ArgumentException) { + // Ignore invalid regex. We don't clean. + return; + } + } IList crashCandidates = new List(); DirectoryInfo directory; @@ -450,7 +472,7 @@ public static void CleanUpDump() DirectoryInfo[] subDirs = directory.GetDirectories(); if (subDirs != null) { foreach (DirectoryInfo subDir in subDirs) { - if (crashFileRegex.IsMatch(subDir.Name)) { + if (crashFileRegex == null || crashFileRegex.IsMatch(subDir.Name)) { crashCandidates.Add(subDir); } } @@ -459,20 +481,14 @@ public static void CleanUpDump() FileInfo[] files = directory.GetFiles(); if (files != null) { foreach (FileInfo file in files) { - string nameNoExt = Path.GetFileNameWithoutExtension(file.Name); - if (crashFileRegex.IsMatch(nameNoExt)) { + if (crashFileRegex == null || crashFileRegex.IsMatch(file.Name)) { crashCandidates.Add(file); } } } - // Delete everything more than 45 days old crashCandidates = CleanUpDumpOld(Config.CrashDumper.DumpDir.AgeDays, crashCandidates); - - // Keep the 40 newest sets of logs crashCandidates = CleanUpDumpKeepNewest(Config.CrashDumper.DumpDir.MaxLogs, crashCandidates); - - // 1000MB or 10% should be minimum free space, but keep the last 5 files always crashCandidates = CleanUpKeepSpace( drive, Config.CrashDumper.DumpDir.ReserveFree * GbMultiplier, Config.CrashDumper.DumpDir.ReserveFreePercent, diff --git a/CrashReporter/Dump/Crash.cs b/CrashReporter/Dump/Crash.cs index db062af..035d0d1 100644 --- a/CrashReporter/Dump/Crash.cs +++ b/CrashReporter/Dump/Crash.cs @@ -184,7 +184,7 @@ public string GetCrashDir(string prefix) return crashDir; } - internal const string CrashPathRegEx = @"-\d{14}\.[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"; + internal const string CrashPathRegEx = @"-\d{14}\.[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(\.zip)?$"; internal static string GetCrashFolder() {