Skip to content

Commit

Permalink
Add FileIdenfifier.FromFile
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Dec 16, 2024
1 parent e822782 commit 52ba93a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/Meziantou.Framework.Win32.ChangeJournal/FileIdentifier.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System.Globalization;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Microsoft.Win32.SafeHandles;
using Windows.Win32;
using Windows.Win32.Storage.FileSystem;

namespace Meziantou.Framework.Win32;
Expand Down Expand Up @@ -28,6 +32,26 @@ internal FileIdentifier(FILE_ID_128 parentFileReferenceNumber)
{
}

[SupportedOSPlatform("windows6.0.6000")]
public unsafe static FileIdentifier FromFile(string path)
{
using var handle = File.OpenHandle(path);
return FromFile(handle);
}

[SupportedOSPlatform("windows6.0.6000")]
public unsafe static FileIdentifier FromFile(SafeFileHandle handle)
{
var result = new FILE_ID_INFO();
var pointer = Unsafe.AsPointer(ref result);
if (PInvoke.GetFileInformationByHandleEx(handle, FILE_INFO_BY_HANDLE_CLASS.FileIdInfo, pointer, (uint)sizeof(FILE_ID_INFO)))
{
return new FileIdentifier(result.FileId);
}

throw new Win32Exception(Marshal.GetLastWin32Error());
}

public bool Equals(FileIdentifier other) => _value == other._value;
public override bool Equals([NotNullWhen(true)] object? obj) => obj is FileIdentifier identifier && Equals(identifier);
public override int GetHashCode() => _value.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>$(LatestTargetFrameworks)</TargetFrameworks>
<IsTrimmable>false</IsTrimmable>
<RootNamespace>Meziantou.Framework.Win32</RootNamespace>
<Version>3.0.0</Version>
<Version>3.1.0</Version>
<Description>Allow to access the Windows Change Journal to quickly detect changed files</Description>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ Windows.Win32.System.Ioctl.USN_RECORD_V2
Windows.Win32.System.Ioctl.USN_RECORD_V3
Windows.Win32.System.Ioctl.USN_RECORD_V4
Windows.Win32.System.Ioctl.USN_TRACK_MODIFIED_RANGES
FLAG_USN_TRACK_MODIFIED_RANGES_ENABLE
FLAG_USN_TRACK_MODIFIED_RANGES_ENABLE
GetFileInformationByHandleEx
FILE_ID_INFO
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public void NonAdministrator()
});
}

[RunIfFact(FactOperatingSystem.Windows)]
public void GetFileIdentifier()
{
var file = Path.GetTempFileName();
var identifier = FileIdentifier.FromFile(file);
Assert.NotEqual(default, identifier);
}

[Fact]
public void FileIdentifier128ToString()
{
Expand Down

0 comments on commit 52ba93a

Please sign in to comment.