From d34c8445ee0f7982b3d7a6b74b2d1fbf045d621b Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Thu, 11 Jan 2018 14:35:43 +0100 Subject: [PATCH] Move static methods to ATXCommon. --- ATXSerializables/ATXCommon.csproj | 2 ++ ATXSerializables/FsUtils.cs | 19 +++++++++++++++++++ ATXSerializables/TimeUtils.cs | 15 +++++++++++++++ AutoTx/AutoTx.cs | 30 ++++++------------------------ AutoTx/RoboCommand.cs | 3 ++- 5 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 ATXSerializables/FsUtils.cs create mode 100644 ATXSerializables/TimeUtils.cs diff --git a/ATXSerializables/ATXCommon.csproj b/ATXSerializables/ATXCommon.csproj index d6477bd..a282202 100644 --- a/ATXSerializables/ATXCommon.csproj +++ b/ATXSerializables/ATXCommon.csproj @@ -40,10 +40,12 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="FsUtils.cs" /> <Compile Include="Serializables\DriveToCheck.cs" /> <Compile Include="Serializables\ServiceConfig.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Serializables\ServiceStatus.cs" /> + <Compile Include="TimeUtils.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. diff --git a/ATXSerializables/FsUtils.cs b/ATXSerializables/FsUtils.cs new file mode 100644 index 0000000..c6dadb1 --- /dev/null +++ b/ATXSerializables/FsUtils.cs @@ -0,0 +1,19 @@ +using System.IO; +using System.Linq; + +namespace ATXCommon +{ + public static class FsUtils + { + /// <summary> + /// Recursively sum up size of all files under a given path. + /// </summary> + /// <param name="path">Full path of the directory.</param> + /// <returns>The total size in bytes.</returns> + public static long GetDirectorySize(string path) { + return new DirectoryInfo(path) + .GetFiles("*", SearchOption.AllDirectories) + .Sum(file => file.Length); + } + } +} diff --git a/ATXSerializables/TimeUtils.cs b/ATXSerializables/TimeUtils.cs new file mode 100644 index 0000000..04cc8fa --- /dev/null +++ b/ATXSerializables/TimeUtils.cs @@ -0,0 +1,15 @@ +using System; + +namespace ATXCommon +{ + public static class TimeUtils + { + /// <summary> + /// Helper method to create timestamp strings in a consistent fashion. + /// </summary> + /// <returns>A timestamp string of the current time.</returns> + public static string Timestamp() { + return DateTime.Now.ToString("yyyy-MM-dd__HH-mm-ss"); + } + } +} diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs index 4a9426e..281b432 100644 --- a/AutoTx/AutoTx.cs +++ b/AutoTx/AutoTx.cs @@ -10,6 +10,7 @@ using System.DirectoryServices.AccountManagement; using System.Globalization; using System.Management; using ATXCommon.Serializables; +using ATXCommon; using RoboSharp; namespace AutoTx @@ -413,14 +414,6 @@ namespace AutoTx } } - /// <summary> - /// Helper method to create timestamp strings in a consistent fashion. - /// </summary> - /// <returns>A timestamp string of the current time.</returns> - private static string CreateTimestamp() { - return DateTime.Now.ToString("yyyy-MM-dd__HH-mm-ss"); - } - #endregion #region ActiveDirectory, email address, user name, ... @@ -733,7 +726,7 @@ namespace AutoTx var targetDir = Path.Combine( _managedPath, target, - CreateTimestamp(), + TimeUtils.Timestamp(), userDir.Name); if (MoveAllSubDirs(userDir, targetDir)) return; @@ -758,7 +751,7 @@ namespace AutoTx _managedPath, "DONE", sourceDirectory.Name, // the username directory - CreateTimestamp()); + TimeUtils.Timestamp()); // writeLogDebug("MoveToGraceLocation: src(" + sourceDirectory.FullName + ") dst(" + dstPath + ")"); try { @@ -803,7 +796,7 @@ namespace AutoTx var target = Path.Combine(destPath, subDir.Name); // make sure NOT to overwrite the subdirectories: if (Directory.Exists(target)) - target += "_" + CreateTimestamp(); + target += "_" + TimeUtils.Timestamp(); writeLogDebug(" - " + subDir.Name + " > " + target); subDir.MoveTo(target); @@ -848,7 +841,7 @@ namespace AutoTx if (unique == false) return dirPath; - dirPath = dirPath + "_" + CreateTimestamp(); + dirPath = dirPath + "_" + TimeUtils.Timestamp(); } Directory.CreateDirectory(dirPath); writeLogDebug("Created directory: " + dirPath); @@ -915,17 +908,6 @@ namespace AutoTx _lastUserDirCheck = DateTime.Now; } - /// <summary> - /// Recursively sum up size of all files under a given path. - /// </summary> - /// <param name="path">Full path of the directory.</param> - /// <returns>The total size in bytes.</returns> - public static long GetDirectorySize(string path) { - return new DirectoryInfo(path) - .GetFiles("*", SearchOption.AllDirectories) - .Sum(file => file.Length); - } - /// <summary> /// Generate a report on expired folders in the grace location. /// @@ -977,7 +959,7 @@ namespace AutoTx continue; long size = -1; try { - size = GetDirectorySize(subdir.FullName) / MegaBytes; + size = FsUtils.GetDirectorySize(subdir.FullName) / MegaBytes; } catch (Exception ex) { writeLog("ERROR getting directory size of " + subdir.FullName + diff --git a/AutoTx/RoboCommand.cs b/AutoTx/RoboCommand.cs index fa0233e..9c04350 100644 --- a/AutoTx/RoboCommand.cs +++ b/AutoTx/RoboCommand.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Management; +using ATXCommon; using RoboSharp; namespace AutoTx @@ -22,7 +23,7 @@ namespace AutoTx return; _status.CurrentTransferSrc = sourcePath; - _status.CurrentTransferSize = GetDirectorySize(sourcePath); + _status.CurrentTransferSize = FsUtils.GetDirectorySize(sourcePath); // the user name is expected to be the last part of the path: _status.CurrentTargetTmp = new DirectoryInfo(sourcePath).Name; -- GitLab