-
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
25
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.
+1,788
−3
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f3887fd
Implement SunOS FileSystemWatcher using portfs event ports
gwr 1a096a1
Fix Copilot feedback
gwr 7538ce6
In Cleanup clear watcher._cancellation
gwr 63fcbc7
Selective exception handling in HandleDirectoryEvent
gwr 378566b
Fix inotify comments
gwr e6d2e86
Selective exception handling in HandleFileEvent
gwr 43b475c
Revert the change to Cleanup for now -- not quite right
gwr 4c7a642
Fix relative paths for rename
gwr d6a569e
Do PortDissociate before deleting elements from the cookieMap
gwr 00d55c7
Run ProcessEvents with captured execution context
gwr d1f6886
If HandleDirectoryEvent finds the watched directory gone, terminate
gwr fe230ca
Do PortAssociate before map insertion etc. in case that throws
gwr be37d3f
Don't need s_enableDebugLog condition for Debug.WriteLine
gwr 03a3292
Enable leak test helper for SunOS
gwr a353dad
Avoid duplicate OnError in HandleDirectoryEvent
gwr 5e8b062
Make CancellationCallback handle PortSend error
gwr bfe13e3
Improve efficiency in AssociateDirectoryContents
gwr 3291f02
Add FILE_NOFOLLOW to PortAssociate calls
gwr 1da2f50
Factor out AssociateNode
gwr 1884b5c
Use try/catch some more places
gwr f68a021
Silence EBADF in ProcessEvents, and fix some comments
gwr 078a3b7
More efficient CompareSnapshotsAndNotify
gwr 927aa87
fix record-syle updats
gwr 493bd15
Do (re)AssociateNode in finally block for relability
gwr c3307ba
Add design comments to CompareSnapshotsAndNotify
gwr 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.