diff --git a/ATXSerializables/ATXCommon.csproj b/ATXSerializables/ATXCommon.csproj index d6477bd0bdea121eb0c26d211e27aef800018d1d..a282202c7235ec24cbd0da42d25422625a62eb58 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 0000000000000000000000000000000000000000..c6dadb1103b511605dce2bc7fd5673d085aa7c2f --- /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 0000000000000000000000000000000000000000..04cc8fa2817d8ea8504fd237c452e3e474ada3e6 --- /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 4a9426ec6665d1fe2229dac9a11c4a2e211ea4aa..281b43219cc569d280e51085277bbd05a7551ba2 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 fa0233e3e9adbbac0c4a57d0c365ace0c0c31ed6..9c04350250e4c07fbf203f383ede0cc2c0a187cb 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;