From a06887ecc836e889a62a9bf886bf1cf5a79d2e8f Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Wed, 17 Jan 2018 23:14:21 +0100
Subject: [PATCH] Move ExpiredDirs to ATXCommon.FsUtils.

---
 ATXSerializables/FsUtils.cs | 36 ++++++++++++++++++++++++++++++++++++
 AutoTx/AutoTx.cs            | 36 ++----------------------------------
 2 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/ATXSerializables/FsUtils.cs b/ATXSerializables/FsUtils.cs
index 8dadf4d..c21eaa5 100644
--- a/ATXSerializables/FsUtils.cs
+++ b/ATXSerializables/FsUtils.cs
@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Linq;
@@ -40,5 +41,40 @@ namespace ATXCommon
             }
             return (baseTime - dirTimestamp).Days;
         }
+
+        /// <summary>
+        /// Assemble a dictionary with information about expired directories.
+        /// </summary>
+        /// <param name="baseDir">The base directory to scan for subdirectories.</param>
+        /// <param name="thresh">The number of days used as expiration threshold.</param>
+        /// <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>
+        public static Dictionary<string, List<Tuple<DirectoryInfo, long, int>>> ExpiredDirs(
+            DirectoryInfo baseDir,int thresh) {
+
+            var collection = new Dictionary<string, List<Tuple<DirectoryInfo, long, int>>>();
+            var now = DateTime.Now;
+            foreach (var userdir in baseDir.GetDirectories()) {
+                var expired = new List<Tuple<DirectoryInfo, long, int>>();
+                foreach (var subdir in userdir.GetDirectories()) {
+                    var age = DirNameToAge(subdir, now);
+                    if (age < thresh)
+                        continue;
+                    long size = -1;
+                    try {
+                        size = GetDirectorySize(subdir.FullName) / Conv.MegaBytes;
+                    }
+                    catch (Exception ex) {
+                        Log.Error("ERROR getting directory size of [{0}]: {1}",
+                            subdir.FullName, ex.Message);
+                    }
+                    expired.Add(new Tuple<DirectoryInfo, long, int>(subdir, size, age));
+                }
+                if (expired.Count > 0)
+                    collection.Add(userdir.Name, expired);
+            }
+            return collection;
+        }
     }
 }
diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index e3ee62c..ae708e5 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -961,7 +961,8 @@ namespace AutoTx
         /// </summary>
         /// <param name="threshold">The number of days used as expiration threshold.</param>
         public string GraceLocationSummary(int threshold) {
-            var expired = ExpiredDirs(threshold);
+            var doneDir = new DirectoryInfo(Path.Combine(_managedPath, "DONE"));
+            var expired = FsUtils.ExpiredDirs(doneDir, threshold);
             var report = "";
             foreach (var userdir in expired.Keys) {
                 report += "\n - user '" + userdir + "'\n";
@@ -983,39 +984,6 @@ namespace AutoTx
             return report;
         }
 
-        /// <summary>
-        /// Assemble a dictionary with information about expired directories.
-        /// </summary>
-        /// <param name="thresh">The number of days used as expiration threshold.</param>
-        /// <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.Now;
-            foreach (var userdir in graceDir.GetDirectories()) {
-                var expired = new List<Tuple<DirectoryInfo, long, int>>();
-                foreach (var subdir in userdir.GetDirectories()) {
-                    var age = FsUtils.DirNameToAge(subdir, now);
-                    if (age < thresh)
-                        continue;
-                    long size = -1;
-                    try {
-                        size = FsUtils.GetDirectorySize(subdir.FullName) / MegaBytes;
-                    }
-                    catch (Exception ex) {
-                        Log.Error("ERROR getting directory size of [{0}]: {1}",
-                            subdir.FullName, ex.Message);
-                    }
-                    expired.Add(new Tuple<DirectoryInfo, long, int>(subdir, size, age));
-                }
-                if (expired.Count > 0)
-                    collection.Add(userdir.Name, expired);
-            }
-            return collection;
-        }
-
         #endregion
 
     }
-- 
GitLab