Skip to content

Commit beeb6b9

Browse files
committed
refactor: also check FileNameInfo for FileName processing
refactor: return exception to FileName when a path's file/directory name cannot be obtained
1 parent 95d9eb0 commit beeb6b9

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

deadlock-dotnet-sdk/Domain/SafeFileHandleEx.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ public unsafe (string? v, Exception? ex) FileFullPath
312312
}
313313
}
314314

315-
// TODO: leverage GetFileInformationByHandleEx
316315
public (string? v, Exception? ex) FileName
317316
{
318317
get
@@ -322,15 +321,37 @@ public unsafe (string? v, Exception? ex) FileFullPath
322321
const string errUnableMsg = "Unable to query " + nameof(FileName) + "; ";
323322
if (FileFullPath.v is not null)
324323
{
325-
return fileName = (Path.GetFileName(FileFullPath.v), null);
324+
getFileOrDirectoryName(FileFullPath.v);
325+
return fileName;
326+
}
327+
else if (FileNameInfo.v is not null)
328+
{
329+
getFileOrDirectoryName(FileNameInfo.v);
330+
return fileName;
326331
}
327332
else if (ObjectName.v is not null)
328333
{
329-
return fileName = (Path.GetFileName(ObjectName.v), null);
334+
getFileOrDirectoryName(ObjectName.v);
335+
return fileName;
330336
}
331337
else
332338
{
333-
return fileName = (null, new InvalidOperationException(errUnableMsg + "This operation requires FileFullPath or ObjectName."));
339+
return fileName = (null, new InvalidOperationException(errUnableMsg + "This operation requires FileFullPath, FileNameInfo, or ObjectName."));
340+
}
341+
342+
void getFileOrDirectoryName(string path)
343+
{
344+
string? tmp = Path.GetFileName(path);
345+
if (tmp.Length is 0)
346+
{
347+
fileName = (tmp = Path.GetDirectoryName(path)) is null
348+
? (null, new InvalidOperationException(errUnableMsg + $"'{path}' could not be processed for a file or directory name."))
349+
: (tmp, null);
350+
}
351+
else
352+
{
353+
fileName = (tmp, null);
354+
}
334355
}
335356
}
336357
else

0 commit comments

Comments
 (0)