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

Move DirNameToAge to FsUtils.

parent e5632220
No related branches found
No related tags found
No related merge requests found
using System.IO; using System;
using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using NLog;
namespace ATXCommon namespace ATXCommon
{ {
public static class FsUtils public static class FsUtils
{ {
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
/// <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>
...@@ -15,5 +20,25 @@ namespace ATXCommon ...@@ -15,5 +20,25 @@ namespace ATXCommon
.GetFiles("*", SearchOption.AllDirectories) .GetFiles("*", SearchOption.AllDirectories)
.Sum(file => file.Length); .Sum(file => file.Length);
} }
/// <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>
public static 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) {
Log.Warn("Unable to parse time from name [{0}], skipping: {1}",
dir.Name, ex.Message);
return -1;
}
return (baseTime - dirTimestamp).Days;
}
} }
} }
...@@ -1069,7 +1069,7 @@ namespace AutoTx ...@@ -1069,7 +1069,7 @@ namespace AutoTx
foreach (var userdir in graceDir.GetDirectories()) { foreach (var userdir in graceDir.GetDirectories()) {
var expired = new List<Tuple<DirectoryInfo, long, int>>(); var expired = new List<Tuple<DirectoryInfo, long, int>>();
foreach (var subdir in userdir.GetDirectories()) { foreach (var subdir in userdir.GetDirectories()) {
var age = DirNameToAge(subdir, now); var age = FsUtils.DirNameToAge(subdir, now);
if (age < thresh) if (age < thresh)
continue; continue;
long size = -1; long size = -1;
...@@ -1088,26 +1088,6 @@ namespace AutoTx ...@@ -1088,26 +1088,6 @@ namespace AutoTx
return collection; return collection;
} }
/// <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) {
Log.Warn("ERROR parsing timestamp from directory name [{0}], skipping: {1}",
dir.Name, 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