Skip to content

Commit 1418c94

Browse files
committed
Added non-throwing filename and normalized filename query.
1 parent decb80b commit 1418c94

1 file changed

Lines changed: 47 additions & 9 deletions

File tree

NtApiDotNet/NtFile.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,18 @@ private static SafeFileHandle DuplicateAsFile(SafeKernelObjectHandle handle)
297297
}
298298
}
299299

300-
private string TryGetName(FileInformationClass info_class)
300+
private NtResult<string> TryGetName(FileInformationClass info_class, bool throw_on_error)
301301
{
302302
using (var buffer = new SafeStructureInOutBuffer<FileNameInformation>(32 * 1024, true))
303303
{
304304
IoStatus status = new IoStatus();
305-
NtSystemCalls.NtQueryInformationFile(Handle,
306-
status, buffer, buffer.Length, info_class).ToNtException();
305+
NtStatus nt_status = NtSystemCalls.NtQueryInformationFile(Handle,
306+
status, buffer, buffer.Length, info_class);
307+
if (!nt_status.IsSuccess())
308+
return nt_status.CreateResultFromError<string>(throw_on_error);
307309
char[] result = new char[buffer.Result.NameLength / 2];
308310
buffer.Data.ReadArray(0, result, 0, result.Length);
309-
return new string(result);
311+
return new string(result).CreateResult();
310312
}
311313
}
312314

@@ -1736,17 +1738,33 @@ public byte[] FsControl(NtIoControlCode control_code, byte[] input_buffer, int m
17361738
/// <param name="desired_access">The desired access for the file handle</param>
17371739
/// <param name="share_access">The file share access</param>
17381740
/// <param name="open_options">File open options</param>
1741+
/// <param name="attributes">Flags for the object attributes.</param>
17391742
/// <param name="throw_on_error">True to throw an exception on error.</param>
17401743
/// <returns>The NT status code and object result.</returns>
17411744
/// <exception cref="NtException">Thrown on error.</exception>
1742-
public NtResult<NtFile> ReOpen(FileAccessRights desired_access, FileShareMode share_access, FileOpenOptions open_options, bool throw_on_error)
1745+
public NtResult<NtFile> ReOpen(FileAccessRights desired_access, FileShareMode share_access,
1746+
FileOpenOptions open_options, AttributeFlags attributes, bool throw_on_error)
17431747
{
1744-
using (ObjectAttributes obj_attributes = new ObjectAttributes(string.Empty, AttributeFlags.CaseInsensitive, this))
1748+
using (ObjectAttributes obj_attributes = new ObjectAttributes(string.Empty, attributes, this))
17451749
{
17461750
return Open(obj_attributes, desired_access, share_access, open_options, throw_on_error);
17471751
}
17481752
}
17491753

1754+
/// <summary>
1755+
/// Re-open an existing file for different access.
1756+
/// </summary>
1757+
/// <param name="desired_access">The desired access for the file handle</param>
1758+
/// <param name="share_access">The file share access</param>
1759+
/// <param name="open_options">File open options</param>
1760+
/// <param name="throw_on_error">True to throw an exception on error.</param>
1761+
/// <returns>The NT status code and object result.</returns>
1762+
/// <exception cref="NtException">Thrown on error.</exception>
1763+
public NtResult<NtFile> ReOpen(FileAccessRights desired_access, FileShareMode share_access, FileOpenOptions open_options, bool throw_on_error)
1764+
{
1765+
return ReOpen(desired_access, share_access, open_options, AttributeFlags.CaseInsensitive, throw_on_error);
1766+
}
1767+
17501768
/// <summary>
17511769
/// Re-open an exsiting file for different access.
17521770
/// </summary>
@@ -4802,6 +4820,26 @@ public void SetQuota(Sid sid, long threshold, long limit)
48024820
SetQuota(new FileQuotaEntry(sid, threshold, limit));
48034821
}
48044822

4823+
/// <summary>
4824+
/// Get the file's full path.
4825+
/// </summary>
4826+
/// <param name="throw_on_error">True to throw on error.</param>
4827+
/// <returns>The file name.</returns>
4828+
public NtResult<string> GetFileName(bool throw_on_error)
4829+
{
4830+
return TryGetName(FileInformationClass.FileNameInformation, throw_on_error);
4831+
}
4832+
4833+
/// <summary>
4834+
/// Get the file's normalized path.
4835+
/// </summary>
4836+
/// <param name="throw_on_error">True to throw on error.</param>
4837+
/// <returns>The file name.</returns>
4838+
public NtResult<string> GetNormalizedFileName(bool throw_on_error)
4839+
{
4840+
return TryGetName(FileInformationClass.FileNormalizedNameInformation, throw_on_error);
4841+
}
4842+
48054843
/// <summary>
48064844
/// Method to query information for this object type.
48074845
/// </summary>
@@ -5117,12 +5155,12 @@ public FileCaseSensitiveFlags CaseSensitiveFlags
51175155
/// <summary>
51185156
/// Get the filename with the volume path.
51195157
/// </summary>
5120-
public string FileName => TryGetName(FileInformationClass.FileNameInformation);
5158+
public string FileName => TryGetName(FileInformationClass.FileNameInformation, true).Result;
51215159

51225160
/// <summary>
51235161
/// Get the normalized filename with the volume path.
51245162
/// </summary>
5125-
public string NormalizedFileName => TryGetName(FileInformationClass.FileNormalizedNameInformation);
5163+
public string NormalizedFileName => TryGetName(FileInformationClass.FileNormalizedNameInformation, true).Result;
51265164

51275165
/// <summary>
51285166
/// Get the associated short filename
@@ -5139,7 +5177,7 @@ public string FileShortName
51395177
/// </summary>
51405178
public string ShortName
51415179
{
5142-
get => TryGetName(FileInformationClass.FileAlternateNameInformation);
5180+
get => TryGetName(FileInformationClass.FileAlternateNameInformation, true).Result;
51435181
set => SetName(FileInformationClass.FileShortNameInformation, value);
51445182
}
51455183

0 commit comments

Comments
 (0)