mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
Responding to PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/9b129eccfc6f8bcb423b2658954975c15164d03b
This commit is contained in:
1 parent
9ffae2c2aa
commit
deec93fb6f
13 files changed
+353
-297
No files matched your search
@@ -6,33 +6,16 @@ using System.Runtime.InteropServices;
|
||||
|
||||
using CFStringRef = System.IntPtr;
|
||||
using CFArrayRef = System.IntPtr;
|
||||
using FSEventStreamRef = System.IntPtr;
|
||||
using size_t = System.IntPtr;
|
||||
using FSEventStreamEventId = System.UInt64;
|
||||
using CFTimeInterval = System.Double;
|
||||
using CFRunLoopRef = System.IntPtr;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static partial class CoreFoundation
|
||||
{
|
||||
/// <summary>
|
||||
/// This constant specifies that we want to use the default Run mode for the thread's Run loop.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// For more information, see the Apple documentation: https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html
|
||||
/// </remarks>
|
||||
internal static IntPtr kCFRunLoopDefaultMode = CFStringCreateWithCString(IntPtr.Zero, "kCFRunLoopDefaultMode", 0);
|
||||
|
||||
/// <summary>
|
||||
/// This is the default system path for the CoreFoundation library that PInvoke will use
|
||||
/// </summary>
|
||||
private const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
|
||||
|
||||
/// <summary>
|
||||
/// Tells the OS what encoding the passed in String is in. These come from the CFString.h header file in the CoreFoundation framework.
|
||||
/// </summary>
|
||||
internal enum CFStringBuiltInEncodings : uint
|
||||
private enum CFStringBuiltInEncodings : uint
|
||||
{
|
||||
kCFStringEncodingMacRoman = 0,
|
||||
kCFStringEncodingWindowsLatin1 = 0x0500,
|
||||
@@ -43,12 +26,12 @@ internal static partial class Interop
|
||||
kCFStringEncodingUTF8 = 0x08000100,
|
||||
kCFStringEncodingNonLossyASCII = 0x0BFF,
|
||||
|
||||
kCFStringEncodingUTF16 = 0x0100,
|
||||
kCFStringEncodingUTF16BE = 0x10000100,
|
||||
kCFStringEncodingUTF16LE = 0x14000100,
|
||||
kCFStringEncodingUTF32 = 0x0c000100,
|
||||
kCFStringEncodingUTF32BE = 0x18000100,
|
||||
kCFStringEncodingUTF32LE = 0x1c000100
|
||||
kCFStringEncodingUTF16 = 0x0100,
|
||||
kCFStringEncodingUTF16BE = 0x10000100,
|
||||
kCFStringEncodingUTF16LE = 0x14000100,
|
||||
kCFStringEncodingUTF32 = 0x0c000100,
|
||||
kCFStringEncodingUTF32BE = 0x18000100,
|
||||
kCFStringEncodingUTF32LE = 0x1c000100
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -58,11 +41,22 @@ internal static partial class Interop
|
||||
/// <param name="str">The string to get a CFStringRef for</param>
|
||||
/// <param name="encoding">The encoding of the str variable. This should be UTF 8 for OS X</param>
|
||||
/// <returns>Returns a pointer to a CFString on success; otherwise, returns IntPtr.Zero</returns>
|
||||
[DllImport(CoreFoundationLibrary)]
|
||||
internal static extern CFStringRef CFStringCreateWithCString(
|
||||
/// <remarks>For *nix systems, the CLR maps ANSI to UTF-8, so be explicit about that</remarks>
|
||||
[DllImport(Interop.Libraries.CoreFoundationLibrary, CharSet = CharSet.Ansi)]
|
||||
private static extern CFStringRef CFStringCreateWithCString(
|
||||
IntPtr allocator,
|
||||
string str,
|
||||
CFStringBuiltInEncodings encoding);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a CFStringRef from a 8-bit String object. Follows the "Create Rule" where if you create it, you delete it.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to get a CFStringRef for</param>
|
||||
/// <returns>Returns a pointer to a CFString on success; otherwise, returns IntPtr.Zero</returns>
|
||||
internal static CFStringRef CFStringCreateWithCString(string str)
|
||||
{
|
||||
return CFStringCreateWithCString(IntPtr.Zero, str, CFStringBuiltInEncodings.kCFStringEncodingUTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a pointer to an unmanaged CFArray containing the input values. Folows the "Create Rule" where if you create it, you delete it.
|
||||
@@ -72,8 +66,8 @@ internal static partial class Interop
|
||||
/// <param name="numValues">The number of values in the array</param>
|
||||
/// <param name="callbacks">Should be IntPtr.Zero</param>
|
||||
/// <returns>Returns a pointer to a CFArray on success; otherwise, returns IntPtr.Zero</returns>
|
||||
[DllImport(CoreFoundationLibrary)]
|
||||
internal static extern CFArrayRef CFArrayCreate(
|
||||
[DllImport(Interop.Libraries.CoreFoundationLibrary)]
|
||||
private static extern CFArrayRef CFArrayCreate(
|
||||
IntPtr allocator,
|
||||
[MarshalAs(UnmanagedType.LPArray)]
|
||||
IntPtr[] values,
|
||||
@@ -81,31 +75,21 @@ internal static partial class Interop
|
||||
IntPtr callbacks);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the RunLoop associated with the current thread; all threads automatically have a RunLoop.
|
||||
/// Follows the "Get Rule" where you do not own the object unless you CFRetain it; in which case, you must also CFRelease it as well.
|
||||
/// Creates a pointer to an unmanaged CFArray containing the input values. Folows the "Create Rule" where if you create it, you delete it.
|
||||
/// </summary>
|
||||
/// <returns>Returns a pointer to a CFRunLoop on success; otherwise, returns IntPtr.Zero</returns>
|
||||
[DllImport(CoreFoundationLibrary)]
|
||||
internal static extern CFRunLoopRef CFRunLoopGetCurrent();
|
||||
/// <param name="values">The values to put in the array</param>
|
||||
/// <param name="numValues">The number of values in the array</param>
|
||||
/// <returns>Returns a pointer to a CFArray on success; otherwise, returns IntPtr.Zero</returns>
|
||||
internal static CFArrayRef CFArrayCreate(IntPtr[] values, ulong numValues)
|
||||
{
|
||||
return CFArrayCreate(IntPtr.Zero, values, numValues, IntPtr.Zero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the reference count on the specified object and, if the ref count hits 0, cleans up the object.
|
||||
/// </summary>
|
||||
/// <param name="ptr">The pointer on which to decrement the reference count.</param>
|
||||
[DllImport(CoreFoundationLibrary)]
|
||||
[DllImport(Interop.Libraries.CoreFoundationLibrary)]
|
||||
internal extern static void CFRelease(IntPtr ptr);
|
||||
|
||||
/// <summary>
|
||||
/// Starts the current thread's RunLoop. If the RunLoop is already running, creates a new, nested, RunLoop in the same stack.
|
||||
/// </summary>
|
||||
[DllImport(CoreFoundationLibrary)]
|
||||
internal extern static void CFRunLoopRun();
|
||||
|
||||
/// <summary>
|
||||
/// Notifies a RunLoop to stop and return control to the execution context that called CFRunLoopRun
|
||||
/// </summary>
|
||||
/// <param name="rl">The RunLoop to notify to stop</param>
|
||||
[DllImport(CoreFoundationLibrary)]
|
||||
internal extern static void CFRunLoopStop(CFRunLoopRef rl);
|
||||
}
|
||||
}
|
||||
}
|
||||
+48
-29
@@ -14,18 +14,13 @@ using CFRunLoopRef = System.IntPtr;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static partial class CoreServices
|
||||
internal static partial class EventStream
|
||||
{
|
||||
/// <summary>
|
||||
/// This constant specifies that we don't want historical file system events, only new ones
|
||||
/// </summary>
|
||||
internal const ulong kFSEventStreamEventIdSinceNow = ulong.MaxValue;
|
||||
|
||||
/// <summary>
|
||||
/// This is the default system path for the CoreServices library that PInvoke will use
|
||||
/// </summary>
|
||||
private const string CoreServicesLibrary = "/System/Library/Frameworks/CoreServices.framework/CoreServices";
|
||||
|
||||
/// <summary>
|
||||
/// Flags that describe what happened in the event that was received. These come from the FSEvents.h header file in the CoreServices framework.
|
||||
/// </summary>
|
||||
@@ -92,7 +87,7 @@ internal static partial class Interop
|
||||
FSEventStreamEventId[] eventIds);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new EventStream to listen to events from the core OS (such as File System events).
|
||||
/// Internal wrapper to create a new EventStream to listen to events from the core OS (such as File System events).
|
||||
/// </summary>
|
||||
/// <param name="allocator">Should be IntPtr.Zero</param>
|
||||
/// <param name="cb">A callback instance that will be called for every event batch.</param>
|
||||
@@ -105,16 +100,40 @@ internal static partial class Interop
|
||||
/// </param>
|
||||
/// <param name="latency">Coalescing period to wait before sending events.</param>
|
||||
/// <param name="flags">Flags to say what kind of events should be sent through this stream.</param>
|
||||
/// <returns></returns>
|
||||
[DllImport(CoreServicesLibrary)]
|
||||
internal static extern FSEventStreamRef FSEventStreamCreate(
|
||||
IntPtr allocator,
|
||||
FSEventStreamCallback cb,
|
||||
IntPtr context,
|
||||
CFArrayRef pathsToWatch,
|
||||
FSEventStreamEventId sinceWhen,
|
||||
CFTimeInterval latency,
|
||||
FSEventStreamCreateFlags flags);
|
||||
/// <returns>On success, returns a pointer to an FSEventStream object; otherwise, returns IntPtr.Zero</returns>
|
||||
/// <remarks>For *nix systems, the CLR maps ANSI to UTF-8, so be explicit about that</remarks>
|
||||
[DllImport(Interop.Libraries.CoreServicesLibrary, CharSet = CharSet.Ansi)]
|
||||
private static extern FSEventStreamRef FSEventStreamCreate(
|
||||
IntPtr allocator,
|
||||
FSEventStreamCallback cb,
|
||||
IntPtr context,
|
||||
CFArrayRef pathsToWatch,
|
||||
FSEventStreamEventId sinceWhen,
|
||||
CFTimeInterval latency,
|
||||
FSEventStreamCreateFlags flags);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new EventStream to listen to events from the core OS (such as File System events).
|
||||
/// </summary>
|
||||
/// <param name="cb">A callback instance that will be called for every event batch.</param>
|
||||
/// <param name="pathsToWatch">A CFArray of the path(s) to watch for events.</param>
|
||||
/// <param name="sinceWhen">
|
||||
/// The start point to receive events from. This can be to retrieve historical events or only new events.
|
||||
/// To get historical events, pass in the corresponding ID of the event you want to start from.
|
||||
/// To get only new events, pass in kFSEventStreamEventIdSinceNow.
|
||||
/// </param>
|
||||
/// <param name="latency">Coalescing period to wait before sending events.</param>
|
||||
/// <param name="flags">Flags to say what kind of events should be sent through this stream.</param>
|
||||
/// <returns>On success, returns a pointer to an FSEventStream object; otherwise, returns IntPtr.Zero</returns>
|
||||
public static FSEventStreamRef FSEventStreamCreate(
|
||||
FSEventStreamCallback cb,
|
||||
CFArrayRef pathsToWatch,
|
||||
FSEventStreamEventId sinceWhen,
|
||||
CFTimeInterval latency,
|
||||
FSEventStreamCreateFlags flags)
|
||||
{
|
||||
return FSEventStreamCreate(IntPtr.Zero, cb, IntPtr.Zero, pathsToWatch, sinceWhen, latency, flags);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attaches an EventStream to a RunLoop so events can be received. This should usually be the current thread's RunLoop.
|
||||
@@ -122,25 +141,25 @@ internal static partial class Interop
|
||||
/// <param name="streamRef">The stream to attach to the RunLoop</param>
|
||||
/// <param name="runLoop">The RunLoop to attach the stream to</param>
|
||||
/// <param name="runLoopMode">The mode of the RunLoop; this should usually be kCFRunLoopDefaultMode. See the documentation for RunLoops for more info.</param>
|
||||
[DllImport(CoreServicesLibrary)]
|
||||
[DllImport(Interop.Libraries.CoreServicesLibrary)]
|
||||
internal static extern void FSEventStreamScheduleWithRunLoop(
|
||||
FSEventStreamRef streamRef,
|
||||
CFRunLoopRef runLoop,
|
||||
CFStringRef runLoopMode);
|
||||
FSEventStreamRef streamRef,
|
||||
CFRunLoopRef runLoop,
|
||||
CFStringRef runLoopMode);
|
||||
|
||||
/// <summary>
|
||||
/// Starts receiving events on the specified stream.
|
||||
/// </summary>
|
||||
/// <param name="streamRef">The stream to receive events on.</param>
|
||||
/// <returns>Returns true if the stream was started; otherwise, returns false and no events will be received.</returns>
|
||||
[DllImport(CoreServicesLibrary)]
|
||||
[DllImport(Interop.Libraries.CoreServicesLibrary)]
|
||||
internal static extern bool FSEventStreamStart(FSEventStreamRef streamRef);
|
||||
|
||||
/// <summary>
|
||||
/// Stops receiving events on the specified stream. The stream can be restarted and not miss any events.
|
||||
/// </summary>
|
||||
/// <param name="streamRef">The stream to stop receiving events on.</param>
|
||||
[DllImport(CoreServicesLibrary)]
|
||||
[DllImport(Interop.Libraries.CoreServicesLibrary)]
|
||||
internal static extern void FSEventStreamStop(FSEventStreamRef streamRef);
|
||||
|
||||
/// <summary>
|
||||
@@ -149,17 +168,17 @@ internal static partial class Interop
|
||||
/// <param name="streamRef">The stream to remove from the RunLoop</param>
|
||||
/// <param name="runLoop">The RunLoop to remove the stream from.</param>
|
||||
/// <param name="runLoopMode">The mode of the RunLoop; this should usually be kCFRunLoopDefaultMode. See the documentation for RunLoops for more info.</param>
|
||||
[DllImport(CoreServicesLibrary)]
|
||||
[DllImport(Interop.Libraries.CoreServicesLibrary)]
|
||||
internal static extern void FSEventStreamUnscheduleFromRunLoop(
|
||||
FSEventStreamRef streamRef,
|
||||
CFRunLoopRef runLoop,
|
||||
CFStringRef runLoopMode);
|
||||
FSEventStreamRef streamRef,
|
||||
CFRunLoopRef runLoop,
|
||||
CFStringRef runLoopMode);
|
||||
|
||||
/// <summary>
|
||||
/// Releases a reference count on the specified EventStream and, if necessary, cleans the stream up.
|
||||
/// </summary>
|
||||
/// <param name="streamRef">The stream on which to decrement the reference count.</param>
|
||||
[DllImport(CoreServicesLibrary)]
|
||||
[DllImport(Interop.Libraries.CoreServicesLibrary)]
|
||||
internal static extern void FSEventStreamRelease(FSEventStreamRef streamRef);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
private static partial class Libraries
|
||||
{
|
||||
internal const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
|
||||
internal const string CoreServicesLibrary = "/System/Library/Frameworks/CoreServices.framework/CoreServices";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using CFRunLoopRef = System.IntPtr;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static partial class RunLoop
|
||||
{
|
||||
/// <summary>
|
||||
/// This constant specifies that we want to use the default Run mode for the thread's Run loop.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// For more information, see the Apple documentation: https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html
|
||||
/// </remarks>
|
||||
internal static IntPtr kCFRunLoopDefaultMode = Interop.CoreFoundation.CFStringCreateWithCString("kCFRunLoopDefaultMode");
|
||||
|
||||
/// <summary>
|
||||
/// Starts the current thread's RunLoop. If the RunLoop is already running, creates a new, nested, RunLoop in the same stack.
|
||||
/// </summary>
|
||||
[DllImport(Interop.Libraries.CoreFoundationLibrary)]
|
||||
internal extern static void CFRunLoopRun();
|
||||
|
||||
/// <summary>
|
||||
/// Notifies a RunLoop to stop and return control to the execution context that called CFRunLoopRun
|
||||
/// </summary>
|
||||
/// <param name="rl">The RunLoop to notify to stop</param>
|
||||
[DllImport(Interop.Libraries.CoreFoundationLibrary)]
|
||||
internal extern static void CFRunLoopStop(CFRunLoopRef rl);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the RunLoop associated with the current thread; all threads automatically have a RunLoop.
|
||||
/// Follows the "Get Rule" where you do not own the object unless you CFRetain it; in which case, you must also CFRelease it as well.
|
||||
/// </summary>
|
||||
/// <returns>Returns a pointer to a CFRunLoop on success; otherwise, returns IntPtr.Zero</returns>
|
||||
[DllImport(Interop.Libraries.CoreFoundationLibrary)]
|
||||
internal static extern CFRunLoopRef CFRunLoopGetCurrent();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
private static class Libraries
|
||||
private static partial class Libraries
|
||||
{
|
||||
internal const string Libc = "libc"; // C library
|
||||
internal const string LibCoreClr = "libcoreclr"; // CoreCLR runtime
|
||||
|
||||
@@ -16,8 +16,8 @@ Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Linux_Debug|Any CPU = Linux_Debug|Any CPU
|
||||
Linux_Release|Any CPU = Linux_Release|Any CPU
|
||||
Mac_Debug|Any CPU = Mac_Debug|Any CPU
|
||||
Mac_Release|Any CPU = Mac_Release|Any CPU
|
||||
OSX_Debug|Any CPU = OSX_Debug|Any CPU
|
||||
OSX_Release|Any CPU = OSX_Release|Any CPU
|
||||
Windows_Debug|Any CPU = Windows_Debug|Any CPU
|
||||
Windows_Release|Any CPU = Windows_Release|Any CPU
|
||||
EndGlobalSection
|
||||
@@ -26,26 +26,26 @@ Global
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Linux_Debug|Any CPU.Build.0 = Linux_Debug|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Linux_Release|Any CPU.ActiveCfg = Linux_Release|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Linux_Release|Any CPU.Build.0 = Linux_Release|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Mac_Debug|Any CPU.ActiveCfg = Mac_Debug|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Mac_Debug|Any CPU.Build.0 = Mac_Debug|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Mac_Release|Any CPU.ActiveCfg = Mac_Release|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Mac_Release|Any CPU.Build.0 = Mac_Release|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.OSX_Debug|Any CPU.ActiveCfg = OSX_Debug|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.OSX_Debug|Any CPU.Build.0 = OSX_Debug|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.OSX_Release|Any CPU.ActiveCfg = OSX_Release|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.OSX_Release|Any CPU.Build.0 = OSX_Release|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Windows_Debug|Any CPU.ActiveCfg = Windows_Debug|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Windows_Debug|Any CPU.Build.0 = Windows_Debug|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Windows_Release|Any CPU.ActiveCfg = Windows_Release|Any CPU
|
||||
{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}.Windows_Release|Any CPU.Build.0 = Windows_Release|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Linux_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Linux_Release|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Mac_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Mac_Release|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.OSX_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.OSX_Release|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Windows_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Windows_Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Windows_Release|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{20411A66-C7A4-4941-8FA2-66308365FD22}.Windows_Release|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.Linux_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.Linux_Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.Mac_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.Mac_Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.OSX_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.OSX_Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.Windows_Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.Windows_Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BE8ED8C1-C314-4C4E-A929-64C9C8B3552A}.Windows_Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
||||
@@ -168,4 +168,7 @@
|
||||
<data name="IO_SharingViolation_NoFileName" xml:space="preserve">
|
||||
<value>The process cannot access the file because it is being used by another process.</value>
|
||||
</data>
|
||||
<data name="EventStream_FailedToStart" xml:space="preserve">
|
||||
<value>Failed to start the EventStream</value>
|
||||
</data>
|
||||
</root>
|
||||
+23
-15
@@ -8,18 +8,16 @@
|
||||
<ProjectGuid>{77E702D9-C6D8-4CE4-9941-D3056C3CCBED}</ProjectGuid>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Help VS understand available configurations -->
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Windows_Debug|AnyCPU'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Windows_Release|AnyCPU'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Linux_Debug|AnyCPU'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Linux_Release|AnyCPU'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Mac_Debug|AnyCPU'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Mac_Release|AnyCPU'" />
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'OSX_Debug|AnyCPU'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'OSX_Release|AnyCPU'" />
|
||||
<ItemGroup>
|
||||
<Compile Include="$(CommonPath)\System\NotImplemented.cs">
|
||||
<Link>Common\System\NotImplemented.cs</Link>
|
||||
<Link>Common\System\NotImplemented.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="System\HResults.cs" />
|
||||
<Compile Include="System\IO\ErrorEventArgs.cs" />
|
||||
@@ -39,20 +37,19 @@
|
||||
<Compile Include="Microsoft\Win32\UnsafeNativeMethods.cs" />
|
||||
<Compile Include="System\IO\FileSystemWatcher.Win32.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
|
||||
<ItemGroup Condition=" '$(TargetsLinux)' == 'true'">
|
||||
<Compile Include="System\IO\FileSystemWatcher.Linux.cs" />
|
||||
<Compile Include="$(CommonPath)\Interop\Linux\libc\Interop.inotify.cs" >
|
||||
<Compile Include="$(CommonPath)\Interop\Linux\libc\Interop.inotify.cs">
|
||||
<Link>Common\Interop\Linux\Interop.inotify.cs</Link>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
|
||||
<Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs">
|
||||
<Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.read.cs">
|
||||
<Link>Common\Interop\Unix\libc\Interop.read.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.gnu_get_libc_version.cs">
|
||||
<Link>Common\src\Interop\Unix\libc\Interop.gnu_get_libc_version.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.strerror.cs">
|
||||
<Link>Common\Interop\Unix\libc\Interop.strerror.cs</Link>
|
||||
</Compile>
|
||||
@@ -62,18 +59,29 @@
|
||||
<Compile Include="$(CommonPath)\Interop\Unix\Interop.IOErrors.cs">
|
||||
<Link>Common\Interop\Unix\Interop.IOErrors.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.gnu_get_libc_version.cs">
|
||||
<Link>Common\src\Interop\Unix\libc\Interop.gnu_get_libc_version.cs</Link>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(TargetsOSX)' == 'true' ">
|
||||
<Compile Include="System\IO\FileSystemWatcher.OSX.cs" />
|
||||
<Compile Include="$(CommonPath)\Interop\OSX\Interop.CoreServices.cs" >
|
||||
<Link>Common\Interop\OSX\Interop.CoreServices.cs</Link>
|
||||
<Compile Include="$(CommonPath)\Interop\OSX\Interop.EventStream.cs">
|
||||
<Link>Common\Interop\OSX\Interop.EventStream.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\Interop\OSX\Interop.CoreFoundation.cs" >
|
||||
<Compile Include="$(CommonPath)\Interop\OSX\Interop.CoreFoundation.cs">
|
||||
<Link>Common\Interop\OSX\Interop.CoreFoundation.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\Interop\OSX\Interop.Libraries.cs">
|
||||
<Link>Common\Interop\OSX\Interop.Libraries.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\Interop\OSX\Interop.RunLoop.cs">
|
||||
<Link>Common\Interop\OSX\Interop.RunLoop.cs</Link>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
<None Include="packages.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
|
||||
</Project>
|
||||
</Project>
|
||||
@@ -86,8 +86,6 @@ namespace System.IO
|
||||
StopRaisingEvents();
|
||||
}
|
||||
|
||||
internal const bool CaseSensitive = true;
|
||||
|
||||
// -----------------------------
|
||||
// ---- PAL layer ends here ----
|
||||
// -----------------------------
|
||||
@@ -251,6 +249,7 @@ namespace System.IO
|
||||
_includeSubdirectories = includeSubdirectories;
|
||||
_notifyFilters = notifyFilters;
|
||||
_cancellationToken = cancellationToken;
|
||||
FileSystemWatcher.CaseSensitive = true;
|
||||
|
||||
// Add a watch for this starting directory. We keep track of the watch descriptor => directory information
|
||||
// mapping in a dictionary; this is needed in order to be able to determine the containing directory
|
||||
|
||||
+177
-189
@@ -1,9 +1,10 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
using CFStringRef = System.IntPtr;
|
||||
using CFArrayRef = System.IntPtr;
|
||||
@@ -15,41 +16,8 @@ using CFRunLoopRef = System.IntPtr;
|
||||
|
||||
namespace System.IO
|
||||
{
|
||||
// OSX implementation of FileSystemWatcher's PAL. The main partial definition of FileSystemWatcher expects:
|
||||
// - StartRaisingEvents
|
||||
// - StopRaisingEvents
|
||||
// - FinalizeDispose
|
||||
|
||||
public partial class FileSystemWatcher
|
||||
{
|
||||
// OS X has a case-sensitive File System
|
||||
internal const bool CaseSensitive = true;
|
||||
|
||||
// Flags used to create the event stream
|
||||
private const Interop.CoreServices.FSEventStreamCreateFlags EventStreamFlags = (Interop.CoreServices.FSEventStreamCreateFlags.kFSEventStreamCreateFlagFileEvents |
|
||||
Interop.CoreServices.FSEventStreamCreateFlags.kFSEventStreamCreateFlagWatchRoot);
|
||||
|
||||
// The last directory we were told to watch. This allows us to be smart about how we create streams
|
||||
// and to only teardown and recreate streams when we need to.
|
||||
private String _lastWatchedDirectory = String.Empty;
|
||||
|
||||
// The bitmask of events that we want to send to the user
|
||||
private Interop.CoreServices.FSEventStreamEventFlags _filterFlags;
|
||||
|
||||
// The EventStream to listen for events on
|
||||
private FSEventStreamRef _eventStream = IntPtr.Zero;
|
||||
|
||||
// The background thread to use to monitor events
|
||||
private Thread _watcherThread = null;
|
||||
|
||||
// A reference to the RunLoop that we can use to start or stop a Watcher
|
||||
private CFRunLoopRef _watcherRunLoop = IntPtr.Zero;
|
||||
|
||||
// Use an event to try to prevent StartRaisingEvents from returning before the
|
||||
// RunLoop actually begins. This will mitigate a race condition where the watcher
|
||||
// thread hasn't completed initialization and stop is called before the RunLoop even starts.
|
||||
private AutoResetEvent _runLoopStartedEvent = new AutoResetEvent(false);
|
||||
|
||||
/// <summary>Called when FileSystemWatcher is finalized.</summary>
|
||||
private void FinalizeDispose()
|
||||
{
|
||||
@@ -61,36 +29,36 @@ namespace System.IO
|
||||
private void StartRaisingEvents()
|
||||
{
|
||||
// Make sure the Start and Stop can be called from different threads and don't
|
||||
// stomp on the other's operation. We use the _lastWatchedDirectory instead of the
|
||||
// stomp on the other's operation. We use the _syncLock instead of the
|
||||
// RunLoop or StreamRef because IntPtrs are value types and can't be locked
|
||||
lock (_lastWatchedDirectory)
|
||||
lock (_syncLock)
|
||||
{
|
||||
// Always re-create the filter flags when start is called since they could have changed
|
||||
if (_notifyFilters.HasFlag(NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size))
|
||||
if ((_notifyFilters & (NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size)) != 0)
|
||||
{
|
||||
_filterFlags = Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemInodeMetaMod | Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemFinderInfoMod;
|
||||
_filterFlags = Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemInodeMetaMod | Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemFinderInfoMod;
|
||||
}
|
||||
if (_notifyFilters.HasFlag(NotifyFilters.Security))
|
||||
if ((_notifyFilters & NotifyFilters.Security) != 0)
|
||||
{
|
||||
_filterFlags |= Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemChangeOwner | Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemXattrMod;
|
||||
_filterFlags |= Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemChangeOwner | Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemXattrMod;
|
||||
}
|
||||
if (_notifyFilters.HasFlag(NotifyFilters.DirectoryName))
|
||||
if ((_notifyFilters & NotifyFilters.DirectoryName) != 0)
|
||||
{
|
||||
_filterFlags |= Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsDir |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsSymlink |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed;
|
||||
_filterFlags |= Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsDir |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsSymlink |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed;
|
||||
}
|
||||
if (_notifyFilters.HasFlag(NotifyFilters.FileName))
|
||||
if ((_notifyFilters & NotifyFilters.FileName) != 0)
|
||||
{
|
||||
_filterFlags |= Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsFile |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsSymlink |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved |
|
||||
Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed;
|
||||
_filterFlags |= Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsFile |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemIsSymlink |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved |
|
||||
Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed;
|
||||
}
|
||||
|
||||
// The directory has not changed then just restart receiving events. If the directory has changed
|
||||
@@ -108,6 +76,53 @@ namespace System.IO
|
||||
}
|
||||
}
|
||||
|
||||
private void StopRaisingEvents()
|
||||
{
|
||||
// Make sure the Start and Stop can be called from different threads and don't
|
||||
// stomp on the other's operation. We use the _syncLock instead of the
|
||||
// RunLoop or StreamRef because IntPtrs are value types and can't be locked
|
||||
lock (_syncLock)
|
||||
{
|
||||
// Be optimistic and only stop the stream, don't clean up unless we need to.
|
||||
StopStream();
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// ---- PAL layer ends here ----
|
||||
// -----------------------------
|
||||
|
||||
// Flags used to create the event stream
|
||||
private const Interop.EventStream.FSEventStreamCreateFlags EventStreamFlags = (Interop.EventStream.FSEventStreamCreateFlags.kFSEventStreamCreateFlagFileEvents |
|
||||
Interop.EventStream.FSEventStreamCreateFlags.kFSEventStreamCreateFlagWatchRoot);
|
||||
|
||||
// The last directory we were told to watch. This allows us to be smart about how we create streams
|
||||
// and to only teardown and recreate streams when we need to.
|
||||
private string _lastWatchedDirectory = String.Empty;
|
||||
|
||||
// The bitmask of events that we want to send to the user
|
||||
private Interop.EventStream.FSEventStreamEventFlags _filterFlags = 0;
|
||||
|
||||
// The EventStream to listen for events on
|
||||
private FSEventStreamRef _eventStream = IntPtr.Zero;
|
||||
|
||||
// The background thread to use to monitor events
|
||||
private Thread _watcherThread = null;
|
||||
|
||||
// A reference to the RunLoop that we can use to start or stop a Watcher
|
||||
private CFRunLoopRef _watcherRunLoop = IntPtr.Zero;
|
||||
|
||||
// Since all variables are IntPtr, Strings, or change references, using this in our locks
|
||||
private readonly object _syncLock = new object();
|
||||
|
||||
// Callback delegate for the EventStream events
|
||||
private Interop.EventStream.FSEventStreamCallback _callback = null;
|
||||
|
||||
// Use an event to try to prevent StartRaisingEvents from returning before the
|
||||
// RunLoop actually begins. This will mitigate a race condition where the watcher
|
||||
// thread hasn't completed initialization and stop is called before the RunLoop even starts.
|
||||
private readonly AutoResetEvent _runLoopStartedEvent = new AutoResetEvent(false);
|
||||
|
||||
private void CreateStreamAndStartWatcher()
|
||||
{
|
||||
Debug.Assert(_eventStream == IntPtr.Zero);
|
||||
@@ -116,20 +131,34 @@ namespace System.IO
|
||||
// Make sure we only do this if there is a valid directory
|
||||
if (String.IsNullOrEmpty(_directory) == false)
|
||||
{
|
||||
// Get the path to watch and put it into a CFArray.
|
||||
CFStringRef path = Interop.CoreFoundation.CFStringCreateWithCString(IntPtr.Zero, _directory, Interop.CoreFoundation.CFStringBuiltInEncodings.kCFStringEncodingUTF8);
|
||||
CFArrayRef arrPaths = Interop.CoreFoundation.CFArrayCreate(IntPtr.Zero, new CFStringRef[1] { path }, 1, IntPtr.Zero);
|
||||
// Get the path to watch and verify we created the CFStringRef
|
||||
CFStringRef path = Interop.CoreFoundation.CFStringCreateWithCString(_directory);
|
||||
if (path == IntPtr.Zero)
|
||||
{
|
||||
throw Interop.GetExceptionForIoErrno(Marshal.GetLastWin32Error(), _directory, true);
|
||||
}
|
||||
|
||||
// Take the CFStringRef and put it into an array to pass to the EventStream
|
||||
CFArrayRef arrPaths = Interop.CoreFoundation.CFArrayCreate(new CFStringRef[1] { path }, 1);
|
||||
if (arrPaths == IntPtr.Zero)
|
||||
{
|
||||
throw Interop.GetExceptionForIoErrno(Marshal.GetLastWin32Error(), _directory, true);
|
||||
}
|
||||
|
||||
// Create the callback for the EventStream
|
||||
_callback = new Interop.EventStream.FSEventStreamCallback(FileSystemEventCallback);
|
||||
|
||||
// Create the event stream for the path and tell the stream to watch for file system events.
|
||||
_eventStream = Interop.CoreServices.FSEventStreamCreate(
|
||||
IntPtr.Zero,
|
||||
FileSystemEventCallback,
|
||||
IntPtr.Zero,
|
||||
_eventStream = Interop.EventStream.FSEventStreamCreate(
|
||||
_callback,
|
||||
arrPaths,
|
||||
Interop.CoreServices.kFSEventStreamEventIdSinceNow,
|
||||
Interop.EventStream.kFSEventStreamEventIdSinceNow,
|
||||
0.2f,
|
||||
EventStreamFlags);
|
||||
Debug.Assert(_eventStream != IntPtr.Zero);
|
||||
if (_eventStream == IntPtr.Zero)
|
||||
{
|
||||
throw Interop.GetExceptionForIoErrno(Marshal.GetLastWin32Error(), _directory, true);
|
||||
}
|
||||
|
||||
// The paths are copied inside FSEventStreamCreate so delete our references
|
||||
Interop.CoreFoundation.CFRelease(path);
|
||||
@@ -151,33 +180,30 @@ namespace System.IO
|
||||
if (_watcherRunLoop != IntPtr.Zero)
|
||||
{
|
||||
// Stop the RunLoop and wait for the thread to exit gracefully
|
||||
Interop.CoreFoundation.CFRunLoopStop(_watcherRunLoop);
|
||||
Interop.RunLoop.CFRunLoopStop(_watcherRunLoop);
|
||||
_watcherThread.Join();
|
||||
_watcherRunLoop = IntPtr.Zero;
|
||||
}
|
||||
|
||||
// Clean up the EventStream, if it exists
|
||||
if (_eventStream != IntPtr.Zero)
|
||||
{
|
||||
Interop.CoreFoundation.CFRelease(_eventStream);
|
||||
_eventStream = IntPtr.Zero;
|
||||
}
|
||||
|
||||
// Reset back to zero
|
||||
_eventStream = IntPtr.Zero;
|
||||
_watcherRunLoop = IntPtr.Zero;
|
||||
}
|
||||
|
||||
private void RestartStream()
|
||||
{
|
||||
if (_eventStream != IntPtr.Zero)
|
||||
Debug.Assert(_eventStream != IntPtr.Zero);
|
||||
|
||||
// We don't need to rebuild the stream since the path is the same so just restart the stream.
|
||||
if (Interop.EventStream.FSEventStreamStart(_eventStream) == false)
|
||||
{
|
||||
// We don't need to rebuild the stream since the path is the same so just restart the stream.
|
||||
if (Interop.CoreServices.FSEventStreamStart(_eventStream) == false)
|
||||
{
|
||||
// Pass the error back to the user and set the last watched directory to null so that we'll
|
||||
// cleanup and recreate our streams next time Start is called.
|
||||
OnError(new ErrorEventArgs(new IOException("", Marshal.GetLastWin32Error())));
|
||||
_lastWatchedDirectory = String.Empty;
|
||||
}
|
||||
// Pass the error back to the user and set the last watched directory to null so that we'll
|
||||
// cleanup and recreate our streams next time Start is called.
|
||||
OnError(new ErrorEventArgs(new IOException("", Marshal.GetLastWin32Error())));
|
||||
_lastWatchedDirectory = String.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,36 +213,39 @@ namespace System.IO
|
||||
// and not have to teardown everything and rebuild.
|
||||
if (_eventStream != IntPtr.Zero)
|
||||
{
|
||||
Interop.CoreServices.FSEventStreamStop(_eventStream);
|
||||
Interop.EventStream.FSEventStreamStop(_eventStream);
|
||||
}
|
||||
}
|
||||
|
||||
private void WatchForFileSystemEventsThreadStart()
|
||||
{
|
||||
// Get this thread's RunLoop
|
||||
_watcherRunLoop = Interop.CoreFoundation.CFRunLoopGetCurrent();
|
||||
_watcherRunLoop = Interop.RunLoop.CFRunLoopGetCurrent();
|
||||
Debug.Assert(_watcherRunLoop != IntPtr.Zero);
|
||||
|
||||
// Schedule the EventStream to run on the thread's RunLoop
|
||||
Interop.CoreServices.FSEventStreamScheduleWithRunLoop(_eventStream, _watcherRunLoop, Interop.CoreFoundation.kCFRunLoopDefaultMode);
|
||||
if (Interop.CoreServices.FSEventStreamStart(_eventStream))
|
||||
Interop.EventStream.FSEventStreamScheduleWithRunLoop(_eventStream, _watcherRunLoop, Interop.RunLoop.kCFRunLoopDefaultMode);
|
||||
if (Interop.EventStream.FSEventStreamStart(_eventStream))
|
||||
{
|
||||
// Notify the StartRaisingEvents call that we are initialized and about to start
|
||||
// so that it can return and avoid a race-condition around multiple threads calling Stop and Start
|
||||
_runLoopStartedEvent.Set();
|
||||
|
||||
// Start the OS X RunLoop (a blocking call) that will pump file system changes into the callback function
|
||||
Interop.CoreFoundation.CFRunLoopRun();
|
||||
Interop.RunLoop.CFRunLoopRun();
|
||||
|
||||
// When we get here, we've requested to stop so cleanup the EventStream and unschedule from the RunLoop
|
||||
Interop.CoreServices.FSEventStreamStop(_eventStream);
|
||||
Interop.CoreServices.FSEventStreamUnscheduleFromRunLoop(_eventStream, _watcherRunLoop, Interop.CoreFoundation.kCFRunLoopDefaultMode);
|
||||
Interop.EventStream.FSEventStreamStop(_eventStream);
|
||||
Interop.EventStream.FSEventStreamUnscheduleFromRunLoop(_eventStream, _watcherRunLoop, Interop.RunLoop.kCFRunLoopDefaultMode);
|
||||
}
|
||||
else
|
||||
{
|
||||
// We failed and need to release the caller so the OnError event can be processed
|
||||
_runLoopStartedEvent.Set();
|
||||
|
||||
// An error occurred while trying to start the run loop so fail out
|
||||
Interop.CoreServices.FSEventStreamUnscheduleFromRunLoop(_eventStream, _watcherRunLoop, Interop.CoreFoundation.kCFRunLoopDefaultMode);
|
||||
OnError(new ErrorEventArgs(new IOException("", Marshal.GetLastWin32Error())));
|
||||
Interop.EventStream.FSEventStreamUnscheduleFromRunLoop(_eventStream, _watcherRunLoop, Interop.RunLoop.kCFRunLoopDefaultMode);
|
||||
OnError(new ErrorEventArgs(new IOException(SR.EventStream_FailedToStart, Marshal.GetLastWin32Error())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,119 +255,94 @@ namespace System.IO
|
||||
size_t numEvents,
|
||||
String[] eventPaths,
|
||||
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
|
||||
Interop.CoreServices.FSEventStreamEventFlags[] eventFlags,
|
||||
Interop.EventStream.FSEventStreamEventFlags[] eventFlags,
|
||||
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
|
||||
FSEventStreamEventId[] eventIds)
|
||||
{
|
||||
Debug.Assert((numEvents.ToInt32() == eventPaths.Length) && (numEvents.ToInt32() == eventFlags.Length) && (numEvents.ToInt32() == eventIds.Length));
|
||||
|
||||
|
||||
// Since renames come in pairs, when we find the first we need to search for the next one. Once we find it, we'll add it to this
|
||||
// list so when the for-loop comes across it, we'll skip it since it's already been processed as part of the original of the pair.
|
||||
System.Collections.Generic.List<FSEventStreamEventId> handledRenameEvents = new Collections.Generic.List<FSEventStreamEventId>();
|
||||
List<FSEventStreamEventId> handledRenameEvents = null;
|
||||
|
||||
for (long i = 0; i < numEvents.ToInt32(); i++)
|
||||
{
|
||||
// First, we should check if this event should kick off a re-scan since we can't really rely on anything after this point if that is true
|
||||
if (ShouldRescanOccur(eventFlags[i]))
|
||||
{
|
||||
OnError(new ErrorEventArgs(new IOException("", (int)eventFlags[i])));
|
||||
OnError(new ErrorEventArgs(new IOException(SR.FSW_BufferOverflow, (int)eventFlags[i])));
|
||||
break;
|
||||
}
|
||||
else if (handledRenameEvents.Contains(eventIds[i]))
|
||||
else if ((handledRenameEvents != null) && (handledRenameEvents.Contains(eventIds[i])))
|
||||
{
|
||||
// If this event is the second in a rename pair then skip it
|
||||
continue;
|
||||
}
|
||||
else
|
||||
else if ((DoesPathPassNameFilter(eventPaths[i])) && ((_filterFlags & eventFlags[i]) != 0))
|
||||
{
|
||||
// Check if the path passes the name filter and if the event passes the event filter.
|
||||
if ((DoesPathPassNameFilter(eventPaths[i])) && (_filterFlags.HasFlag(eventFlags[i])))
|
||||
{
|
||||
// Remove the base directory prefix (including trailing / that OS X adds)
|
||||
String relativePath = eventPaths[i].Remove(0, _directory.Length + 1);
|
||||
// Remove the base directory prefix (including trailing / that OS X adds)
|
||||
string relativePath = eventPaths[i].Remove(0, _directory.Length + 1);
|
||||
|
||||
// Check if this is a rename
|
||||
if ((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed)
|
||||
// Check if this is a rename
|
||||
if ((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed)
|
||||
{
|
||||
// Find the rename that is paired to this rename, which should be the next rename in the list
|
||||
long pairedId = FindRenameChangePairedChange(i, eventPaths, eventFlags, eventIds);
|
||||
if (pairedId == long.MinValue)
|
||||
{
|
||||
// Find the rename that is paired to this rename, which should be the next rename in the list
|
||||
long pairedId = FindRenameChangePairedChange(i, eventPaths, eventFlags, eventIds);
|
||||
if (pairedId == long.MinValue)
|
||||
{
|
||||
OnError(new ErrorEventArgs(new ArgumentOutOfRangeException("pairedId")));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Remove the base directory prefix (including trailing / that OS X adds) and
|
||||
// add the paired event to the list of events to skip and notify the user of the rename
|
||||
String newPathRelativeName = eventPaths[pairedId].Remove(0, _directory.Length + 1);
|
||||
NotifyRenameEventArgs(WatcherChangeTypes.Renamed, newPathRelativeName, relativePath);
|
||||
handledRenameEvents.Add(eventIds[pairedId]);
|
||||
}
|
||||
OnError(new ErrorEventArgs(new ArgumentOutOfRangeException("pairedId")));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Note: the order here matters due to coalescing since two events (such as a mod followed by a delete) could be coalesced into one notification
|
||||
// So look for deletes first since those will mean the listening app can't do anything with the item since it doesn't exist (such as stat
|
||||
// the file to determine what exactly changed, in this example).
|
||||
if ((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved)
|
||||
// Remove the base directory prefix (including trailing / that OS X adds) and
|
||||
// add the paired event to the list of events to skip and notify the user of the rename
|
||||
string newPathRelativeName = eventPaths[pairedId].Remove(0, _directory.Length + 1);
|
||||
NotifyRenameEventArgs(WatcherChangeTypes.Renamed, newPathRelativeName, relativePath);
|
||||
|
||||
// Create a new list, if necessary, and add the event
|
||||
if (handledRenameEvents == null)
|
||||
{
|
||||
NotifyFileSystemEventArgs(WatcherChangeTypes.Deleted, relativePath);
|
||||
}
|
||||
else if ((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated)
|
||||
{
|
||||
// Next look for creates since a create + modification coalesced could confuse apps since a file they haven't heard of yet would get a mod event
|
||||
NotifyFileSystemEventArgs(WatcherChangeTypes.Created, relativePath);
|
||||
}
|
||||
else if (((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemInodeMetaMod) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemInodeMetaMod) ||
|
||||
((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified) ||
|
||||
((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemFinderInfoMod) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemChangeOwner) ||
|
||||
((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemXattrMod) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemXattrMod))
|
||||
{
|
||||
// Everything else is a modification
|
||||
NotifyFileSystemEventArgs(WatcherChangeTypes.Changed, relativePath);
|
||||
handledRenameEvents = new List<FSEventStreamEventId>();
|
||||
}
|
||||
handledRenameEvents.Add(eventIds[pairedId]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Note: the order here matters due to coalescing since two events (such as a mod followed by a delete) could be coalesced into one notification
|
||||
// So look for deletes first since those will mean the listening app can't do anything with the item since it doesn't exist (such as stat
|
||||
// the file to determine what exactly changed, in this example).
|
||||
if ((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRemoved)
|
||||
{
|
||||
NotifyFileSystemEventArgs(WatcherChangeTypes.Deleted, relativePath);
|
||||
}
|
||||
else if ((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemCreated)
|
||||
{
|
||||
// Next look for creates since a create + modification coalesced could confuse apps since a file they haven't heard of yet would get a mod event
|
||||
NotifyFileSystemEventArgs(WatcherChangeTypes.Created, relativePath);
|
||||
}
|
||||
else if (((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemInodeMetaMod) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemInodeMetaMod) ||
|
||||
((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemModified) ||
|
||||
((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemFinderInfoMod) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemChangeOwner) ||
|
||||
((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemXattrMod) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemXattrMod))
|
||||
{
|
||||
// Everything else is a modification
|
||||
NotifyFileSystemEventArgs(WatcherChangeTypes.Changed, relativePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool ShouldRescanOccur(Interop.CoreServices.FSEventStreamEventFlags flags)
|
||||
private bool ShouldRescanOccur(Interop.EventStream.FSEventStreamEventFlags flags)
|
||||
{
|
||||
// Check if the MustScanSubDirs flag exists
|
||||
bool shouldRescan = ((flags & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagMustScanSubDirs) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagMustScanSubDirs);
|
||||
|
||||
// Check if the UserDropped flag exists; meaning the UserMode daemon dropped a change coming from the kernel
|
||||
if (shouldRescan == false)
|
||||
{
|
||||
shouldRescan = ((flags & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagUserDropped) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagUserDropped);
|
||||
}
|
||||
|
||||
// Check if the KernelDropped flag exists; meaning the Kernel dropped a flag before telling the UserMode daemon about it
|
||||
if (shouldRescan == false)
|
||||
{
|
||||
shouldRescan = ((flags & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagKernelDropped) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagKernelDropped);
|
||||
}
|
||||
|
||||
// Check if the RootChanged flag exists; meaning the Root folder changed in some way that could affect all child items and requires a rescan to rebuild an accurate snapshot.
|
||||
if (shouldRescan == false)
|
||||
{
|
||||
shouldRescan = ((flags & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagRootChanged) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagRootChanged);
|
||||
}
|
||||
|
||||
// Check if the Mount flag exists; meaning a whole directory has been added under the root, which would require a rescan to rebuild an accurate snapshot.
|
||||
if (shouldRescan == false)
|
||||
{
|
||||
shouldRescan = ((flags & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagMount) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagMount);
|
||||
}
|
||||
|
||||
// Check if the Unmount flag exists; meaning a whole directory has been removed from under the root, which would require a rescan to rebuild an accurate snapshot
|
||||
if (shouldRescan == false)
|
||||
{
|
||||
shouldRescan = ((flags & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagUnmount) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagUnmount);
|
||||
}
|
||||
|
||||
return shouldRescan;
|
||||
// Check if any bit is set that signals that the caller should rescan
|
||||
return (((flags & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagMustScanSubDirs) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagMustScanSubDirs) ||
|
||||
((flags & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagUserDropped) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagUserDropped) ||
|
||||
((flags & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagKernelDropped) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagKernelDropped) ||
|
||||
((flags & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagRootChanged) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagRootChanged) ||
|
||||
((flags & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagMount) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagMount) ||
|
||||
((flags & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagUnmount) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagUnmount));
|
||||
}
|
||||
|
||||
private bool DoesPathPassNameFilter(string eventPath)
|
||||
@@ -350,7 +354,7 @@ namespace System.IO
|
||||
{
|
||||
// Check if the parent is the root. If so, then we'll continue processing based on the name.
|
||||
// If it isn't, then this will be set to false and we'll skip the name processing since it's irrelevant.
|
||||
String parent = System.IO.Path.GetDirectoryName(eventPath);
|
||||
string parent = System.IO.Path.GetDirectoryName(eventPath);
|
||||
doesPathPass = (parent.Equals(_directory, StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
|
||||
@@ -365,39 +369,23 @@ namespace System.IO
|
||||
|
||||
private long FindRenameChangePairedChange(
|
||||
long currentIndex,
|
||||
String[] eventPaths,
|
||||
Interop.CoreServices.FSEventStreamEventFlags[] eventFlags,
|
||||
String[] eventPaths,
|
||||
Interop.EventStream.FSEventStreamEventFlags[] eventFlags,
|
||||
FSEventStreamEventId[] eventIds)
|
||||
{
|
||||
Debug.Assert((currentIndex + 1 < eventPaths.Length) && (currentIndex + 1 < eventFlags.Length) & (currentIndex + 1 < eventIds.Length));
|
||||
|
||||
// The ID of the corresponding rename
|
||||
long pairedRenameId = long.MinValue;
|
||||
|
||||
// Start at one past the current index and try to find the next Rename item, which should be the old path.
|
||||
for (long i = currentIndex + 1; i < eventPaths.Length; i++)
|
||||
{
|
||||
if ((eventFlags[i] & Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed) == Interop.CoreServices.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed)
|
||||
if ((eventFlags[i] & Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed) == Interop.EventStream.FSEventStreamEventFlags.kFSEventStreamEventFlagItemRenamed)
|
||||
{
|
||||
// We found a match, stop looking
|
||||
pairedRenameId = i;
|
||||
break;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return pairedRenameId;
|
||||
}
|
||||
|
||||
private void StopRaisingEvents()
|
||||
{
|
||||
// Make sure the Start and Stop can be called from different threads and don't
|
||||
// stomp on the other's operation. We use the _lastWatchedDirectory instead of the
|
||||
// RunLoop or StreamRef because IntPtrs are value types and can't be locked
|
||||
lock (_lastWatchedDirectory)
|
||||
{
|
||||
// Be optimistic and only stop the stream, don't clean up unless we need to.
|
||||
StopStream();
|
||||
}
|
||||
return long.MinValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,8 +92,6 @@ namespace System.IO
|
||||
}
|
||||
}
|
||||
|
||||
internal const bool CaseSensitive = false;
|
||||
|
||||
// -----------------------------
|
||||
// ---- PAL layer ends here ----
|
||||
// -----------------------------
|
||||
|
||||
@@ -57,6 +57,8 @@ namespace System.IO
|
||||
NotifyFilters.Security |
|
||||
NotifyFilters.Size);
|
||||
|
||||
internal static bool CaseSensitive { get; set; }
|
||||
|
||||
#if DEBUG
|
||||
static FileSystemWatcher()
|
||||
{
|
||||
@@ -79,6 +81,7 @@ namespace System.IO
|
||||
{
|
||||
_directory = string.Empty;
|
||||
_filter = "*.*";
|
||||
CaseSensitive = false;
|
||||
}
|
||||
|
||||
/// <devdoc>
|
||||
@@ -107,6 +110,7 @@ namespace System.IO
|
||||
|
||||
_directory = path;
|
||||
_filter = filter;
|
||||
CaseSensitive = false;
|
||||
}
|
||||
|
||||
/// <devdoc>
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
<package id="System.Runtime.InteropServices" version="4.0.20-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
<package id="System.Text.Encoding" version="4.0.10-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
<package id="System.Threading" version="4.0.0-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
<package id="System.Threading.Thread" version="4.0.0-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
<package id="System.Threading.Overlapped" version="4.0.0-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
<package id="System.Threading.Tasks" version="4.0.10-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
<package id="System.Threading.Thread" version="4.0.0-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
<package id="System.Threading.ThreadPool" version="4.0.10-beta-22412" targetFramework="portable-net45+win+wpa81" />
|
||||
</packages>
|
||||
Reference in new issue
Block a user