Skip to content

Commit 123d581

Browse files
committed
refactor: catch exceptions thrown by file info queries
1 parent 7d49548 commit 123d581

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

deadlock-dotnet-sdk/Domain/SafeFileHandleEx.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ internal SafeFileHandleEx(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX sysHandleEx) : base(
3232
{
3333
try
3434
{
35-
if (sysHandleEx.IsFileHandle())
35+
if ((bool)(IsFileHandle = SysHandleEx.IsFileHandle()))
3636
{
37-
FileFullPath = TryGetFinalPath();
38-
if (FileFullPath != null)
37+
try
3938
{
39+
FileFullPath = TryGetFinalPath();
4040
FileName = Path.GetFileName(FileFullPath);
41-
FileIsDirectory = (File.GetAttributes(FileFullPath) & FileAttributes.Directory) == FileAttributes.Directory;
41+
IsDirectory = (File.GetAttributes(FileFullPath) & FileAttributes.Directory) == FileAttributes.Directory;
42+
}
43+
catch (Exception e)
44+
{
45+
ExceptionLog.Add(e);
4246
}
4347
}
4448
else
@@ -54,7 +58,8 @@ internal SafeFileHandleEx(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX sysHandleEx) : base(
5458

5559
public string? FileFullPath { get; }
5660
public string? FileName { get; }
57-
public bool? FileIsDirectory { get; private set; }
61+
public bool? IsDirectory { get; }
62+
public bool? IsFileHandle { get; }
5863

5964
/// <summary>
6065
/// Try to get the absolute path of the file. Traverses filesystem links (e.g. symbolic, junction) to get the 'real' path.
@@ -79,7 +84,7 @@ private unsafe string TryGetFinalPath()
7984
buffer = Marshal.ReAllocHGlobal(buffer, (IntPtr)length);
8085
fullName = new((char*)buffer);
8186

82-
bufLength = GetFinalPathNameByHandle(SysHandleEx.ToSafeFileHandle(), fullName, bufLength, FILE_NAME.FILE_NAME_NORMALIZED);
87+
bufLength = GetFinalPathNameByHandle(ToSafeFileHandle(), fullName, bufLength, FILE_NAME.FILE_NAME_NORMALIZED);
8388
}
8489
return fullName.ToString();
8590
}

deadlock-dotnet-sdk/Domain/SafeHandleEx.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,5 @@ protected override bool ReleaseHandle()
262262
return IsClosed;
263263
}
264264

265-
/// <summary>
266-
/// Serialize the current instance to JSON-formatted text
267-
/// </summary>
268-
public override string? ToString() => JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
265+
internal SafeHandle ToSafeFileHandle() => SysHandleEx.ToSafeFileHandle();
269266
}

0 commit comments

Comments
 (0)