-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Implement SunOS FileSystemWatcher using portfs event ports #124716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gwr
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
gwr:illumos-fswatch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/libraries/Common/src/Interop/SunOS/portfs/Interop.portfs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // This implementation uses SunOS portfs (event ports) to watch directories. | ||
| // portfs can detect when a directory's modification time changes via port_associate, | ||
| // but cannot tell us WHAT changed in the directory. This makes it different from | ||
| // Linux inotify or Windows ReadDirectoryChangesW. | ||
| // | ||
| // The FileSystemWatcher implementation must: | ||
| // 1. Use port_associate to watch for FILE_MODIFIED events on directories | ||
| // 2. When an event occurs, re-read the directory contents | ||
| // 3. Compare with cached state to determine what actually changed | ||
| // | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class PortFs | ||
| { | ||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_PortCreate", | ||
| SetLastError = true)] | ||
| internal static partial SafeFileHandle PortCreate(); | ||
|
|
||
| // pFileObj must point to pinned memory with size >= sizeof(file_obj) | ||
| // because the address is used as an identifier in the kernel. | ||
| // dirPath string is used while making the association but is | ||
| // never referenced again after PortAssociate returns. | ||
| // mtime is the directory modification time before reading the directory | ||
| // cookie is returned by PortGet to identify which directory changed | ||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_PortAssociate", | ||
| SetLastError = true, StringMarshalling = StringMarshalling.Utf8)] | ||
| internal static unsafe partial int PortAssociate(SafeFileHandle fd, IntPtr pFileObj, string dirPath, Interop.Sys.TimeSpec* mtime, int evmask, nuint cookie); | ||
|
|
||
| // Returns the cookie value from PortAssociate in the cookie parameter | ||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_PortGet", | ||
| SetLastError = true)] | ||
| internal static unsafe partial int PortGet(SafeFileHandle fd, int* events, nuint* cookie, Interop.Sys.TimeSpec* tmo); | ||
|
|
||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_PortDissociate", | ||
| SetLastError = true)] | ||
| internal static partial int PortDissociate(SafeFileHandle fd, IntPtr pFileObj); | ||
|
|
||
| // Send a synthetic event to wake up a blocked PortGet call | ||
| // evflags can be any PortEvent value (e.g., FILE_NOFOLLOW for cancellation) | ||
| // cookie is returned to PortGet to identify the event | ||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_PortSend", | ||
| SetLastError = true)] | ||
| internal static partial int PortSend(SafeFileHandle fd, int evflags, nuint cookie); | ||
|
|
||
| [Flags] | ||
| internal enum PortEvent | ||
| { | ||
| FILE_ACCESS = 0x00000001, | ||
| FILE_MODIFIED = 0x00000002, | ||
| FILE_ATTRIB = 0x00000004, | ||
| FILE_TRUNC = 0x00100000, | ||
| FILE_NOFOLLOW = 0x10000000, | ||
| FILE_EXCEPTION = 0x20000000, | ||
|
|
||
| // Exception events | ||
| FILE_DELETE = 0x00000010, | ||
| FILE_RENAME_TO = 0x00000020, | ||
| FILE_RENAME_FROM = 0x00000040, | ||
| // These are unused, and the duplicate value causes warnings. | ||
| // UNMOUNTED = 0x20000000, | ||
| // MOUNTEDOVER = 0x40000000, | ||
| } | ||
|
|
||
| // sizeof(file_obj) = 3*sizeof(timespec) + 3*sizeof(uintptr_t) + sizeof(char*) | ||
| // On 64-bit: 3*16 + 3*8 + 8 = 80 bytes | ||
| internal const int FileObjSize = 80; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.