Skip to content
Snippets Groups Projects
Commit edd17c7f authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Split CheckGraceLocation() into ExpiredDirs() and DirNameToAge().

Refers to #1
parent 3a1b6b1b
No related branches found
No related tags found
No related merge requests found
...@@ -914,7 +914,7 @@ namespace AutoTx ...@@ -914,7 +914,7 @@ namespace AutoTx
/// <summary> /// <summary>
/// Recursively sum up size of all files under a given path. /// Recursively sum up size of all files under a given path.
/// </summary> /// </summary>
/// <param name="path"></param> /// <param name="path">Full path of the directory.</param>
/// <returns>The total size in bytes.</returns> /// <returns>The total size in bytes.</returns>
public static long GetDirectorySize(string path) { public static long GetDirectorySize(string path) {
return new DirectoryInfo(path) return new DirectoryInfo(path)
...@@ -930,36 +930,68 @@ namespace AutoTx ...@@ -930,36 +930,68 @@ namespace AutoTx
/// containing the age and size of those directories. /// containing the age and size of those directories.
/// </summary> /// </summary>
public void CheckGraceLocation() { public void CheckGraceLocation() {
var graceDir = new DirectoryInfo(Path.Combine(_managedPath, "DONE")); var expired = ExpiredDirs(_config.GracePeriod);
var report = ""; var report = "";
foreach (var userdir in expired.Keys) {
report += "\n - user '" + userdir + "'\n";
foreach (var subdir in expired[userdir]) {
report += string.Format(" - {0} [age: {2} days, size: {1} MB]\n",
subdir.Item1, subdir.Item2, subdir.Item3);
}
}
writeLogDebug("Expired folders in grace location:\n" + report);
}
/// <summary>
/// Assemble a dictionary with information about expired directories.
/// </summary>
/// <returns>A dictionary having usernames as keys (of those users that actually do have
/// expired directories), where the values are lists of tuples with the DirInfo objects,
/// size and age (in days) of the expired directories.</returns>
private Dictionary<string, List<Tuple<DirectoryInfo, long, int>>> ExpiredDirs(int thresh) {
var collection = new Dictionary<string, List<Tuple<DirectoryInfo, long, int>>>();
var graceDir = new DirectoryInfo(Path.Combine(_managedPath, "DONE"));
var now = DateTime.UtcNow;
foreach (var userdir in graceDir.GetDirectories()) { foreach (var userdir in graceDir.GetDirectories()) {
var expired = ""; var expired = new List<Tuple<DirectoryInfo, long, int>>();
foreach (var subdir in userdir.GetDirectories()) { foreach (var subdir in userdir.GetDirectories()) {
DateTime timestamp; var age = DirNameToAge(subdir, now);
if (age < thresh)
continue;
long size = -1;
try { try {
timestamp = DateTime.ParseExact(subdir.Name, size = GetDirectorySize(subdir.FullName);
"yyyy-MM-dd__HH-mm-ss", CultureInfo.InvariantCulture);
} }
catch (Exception ex) { catch (Exception ex) {
writeLogDebug("ERROR parsing timestamp from directory name '" + writeLog("ERROR getting directory size of " + subdir.FullName +
subdir.Name + "', skipping: " + ex.Message); " - " + ex.Message);
continue;
} }
var delta = DateTime.UtcNow - timestamp; expired.Add(new Tuple<DirectoryInfo, long, int>(subdir, size, age));
if (delta.Days < _config.GracePeriod)
continue;
var size = GetDirectorySize(subdir.FullName) / MegaBytes;
expired += " - " + subdir + ": " + size + " MB (age: "
+ delta.Days + " days)\n";
} }
if (string.IsNullOrWhiteSpace(expired)) if (expired.Count > 0)
continue; collection.Add(userdir.Name, expired);
report += "\n - user '" + userdir + "':\n" + expired;
} }
if (string.IsNullOrWhiteSpace(report)) return collection;
return; }
writeLogDebug("Expired folders in grace location (" + graceDir.FullName + "):\n"
+ report); /// <summary>
/// Convert the timestamp given by the NAME of a directory into the age in days.
/// </summary>
/// <param name="dir">The DirectoryInfo object to check for its name-age.</param>
/// <param name="baseTime">The DateTime object to compare to.</param>
/// <returns>The age in days, or -1 in case of an error.</returns>
private int DirNameToAge(DirectoryInfo dir, DateTime baseTime) {
DateTime dirTimestamp;
try {
dirTimestamp = DateTime.ParseExact(dir.Name, "yyyy-MM-dd__HH-mm-ss",
CultureInfo.InvariantCulture);
}
catch (Exception ex) {
writeLogDebug("ERROR parsing timestamp from directory name '" +
dir.Name + "', skipping: " + ex.Message);
return -1;
}
return (baseTime - dirTimestamp).Days;
} }
#endregion #endregion
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment