Skip to content

Commit

Permalink
Add timeout to compressing junk backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Klocman committed Sep 28, 2023
1 parent 6115328 commit 947e9be
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void CreateBackup(string backupPath)

try
{
FilesystemTools.CompressDirectory(dir);
FilesystemTools.CompressDirectory(dir, TimeSpan.FromMinutes(2));
}
catch
{
Expand Down
5 changes: 3 additions & 2 deletions source/KlocTools/Tools/FilesystemTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ public static void MoveDirectory(DirectoryInfo source, DirectoryInfo target)
}
}

public static void CompressDirectory(string dirFullName)
public static void CompressDirectory(string dirFullName) => CompressDirectory(dirFullName, ManagementOptions.InfiniteTimeout);
public static void CompressDirectory(string dirFullName, TimeSpan timeout)
{
var objPath = "Win32_Directory.Name=" + "\"" + dirFullName.Replace(@"\", @"\\") + "\"";
using (var dir = new ManagementObject(objPath))
{
var outParams = dir.InvokeMethod("Compress", null, null);
var outParams = dir.InvokeMethod("Compress", null, new InvokeMethodOptions { Timeout = timeout });
if (outParams == null) throw new ArgumentNullException(nameof(outParams));
var ret = (uint)outParams.Properties["ReturnValue"].Value;
if (ret != 0)
Expand Down

0 comments on commit 947e9be

Please sign in to comment.