From deec93fb6f21f21bfed13656443307cfe2a8a871 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Mon, 23 Mar 2015 14:19:22 -0700 Subject: [PATCH] Responding to PR feedback Commit migrated from https://github.com/dotnet/corefx/commit/9b129eccfc6f8bcb423b2658954975c15164d03b --- .../src/Interop/OSX/Interop.CoreFoundation.cs | 80 ++-- ...CoreServices.cs => Interop.EventStream.cs} | 77 ++-- .../src/Interop/OSX/Interop.Libraries.cs | 11 + .../Common/src/Interop/OSX/Interop.RunLoop.cs | 42 ++ .../src/Interop/Unix/Interop.Libraries.cs | 2 +- .../System.IO.FileSystem.Watcher.sln | 20 +- .../src/Resources/Strings.resx | 3 + .../src/System.IO.FileSystem.Watcher.csproj | 38 +- .../src/System/IO/FileSystemWatcher.Linux.cs | 3 +- .../src/System/IO/FileSystemWatcher.OSX.cs | 366 +++++++++--------- .../src/System/IO/FileSystemWatcher.Win32.cs | 2 - .../src/System/IO/FileSystemWatcher.cs | 4 + .../src/packages.config | 2 +- 13 files changed, 353 insertions(+), 297 deletions(-) rename src/libraries/Common/src/Interop/OSX/{Interop.CoreServices.cs => Interop.EventStream.cs} (73%) create mode 100644 src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs create mode 100644 src/libraries/Common/src/Interop/OSX/Interop.RunLoop.cs diff --git a/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.cs b/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.cs index 55cc324b3e9..f456cfb2c71 100644 --- a/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.cs +++ b/src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.cs @@ -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 { - /// - /// This constant specifies that we want to use the default Run mode for the thread's Run loop. - /// - /// - /// For more information, see the Apple documentation: https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html - /// - internal static IntPtr kCFRunLoopDefaultMode = CFStringCreateWithCString(IntPtr.Zero, "kCFRunLoopDefaultMode", 0); - - /// - /// This is the default system path for the CoreFoundation library that PInvoke will use - /// - private const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"; - /// /// Tells the OS what encoding the passed in String is in. These come from the CFString.h header file in the CoreFoundation framework. /// - 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 } /// @@ -58,11 +41,22 @@ internal static partial class Interop /// The string to get a CFStringRef for /// The encoding of the str variable. This should be UTF 8 for OS X /// Returns a pointer to a CFString on success; otherwise, returns IntPtr.Zero - [DllImport(CoreFoundationLibrary)] - internal static extern CFStringRef CFStringCreateWithCString( + /// For *nix systems, the CLR maps ANSI to UTF-8, so be explicit about that + [DllImport(Interop.Libraries.CoreFoundationLibrary, CharSet = CharSet.Ansi)] + private static extern CFStringRef CFStringCreateWithCString( IntPtr allocator, string str, CFStringBuiltInEncodings encoding); + + /// + /// Creates a CFStringRef from a 8-bit String object. Follows the "Create Rule" where if you create it, you delete it. + /// + /// The string to get a CFStringRef for + /// Returns a pointer to a CFString on success; otherwise, returns IntPtr.Zero + internal static CFStringRef CFStringCreateWithCString(string str) + { + return CFStringCreateWithCString(IntPtr.Zero, str, CFStringBuiltInEncodings.kCFStringEncodingUTF8); + } /// /// 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 /// The number of values in the array /// Should be IntPtr.Zero /// Returns a pointer to a CFArray on success; otherwise, returns IntPtr.Zero - [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); /// - /// 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. /// - /// Returns a pointer to a CFRunLoop on success; otherwise, returns IntPtr.Zero - [DllImport(CoreFoundationLibrary)] - internal static extern CFRunLoopRef CFRunLoopGetCurrent(); + /// The values to put in the array + /// The number of values in the array + /// Returns a pointer to a CFArray on success; otherwise, returns IntPtr.Zero + internal static CFArrayRef CFArrayCreate(IntPtr[] values, ulong numValues) + { + return CFArrayCreate(IntPtr.Zero, values, numValues, IntPtr.Zero); + } /// /// Decrements the reference count on the specified object and, if the ref count hits 0, cleans up the object. /// /// The pointer on which to decrement the reference count. - [DllImport(CoreFoundationLibrary)] + [DllImport(Interop.Libraries.CoreFoundationLibrary)] internal extern static void CFRelease(IntPtr ptr); - - /// - /// Starts the current thread's RunLoop. If the RunLoop is already running, creates a new, nested, RunLoop in the same stack. - /// - [DllImport(CoreFoundationLibrary)] - internal extern static void CFRunLoopRun(); - - /// - /// Notifies a RunLoop to stop and return control to the execution context that called CFRunLoopRun - /// - /// The RunLoop to notify to stop - [DllImport(CoreFoundationLibrary)] - internal extern static void CFRunLoopStop(CFRunLoopRef rl); } -} \ No newline at end of file +} diff --git a/src/libraries/Common/src/Interop/OSX/Interop.CoreServices.cs b/src/libraries/Common/src/Interop/OSX/Interop.EventStream.cs similarity index 73% rename from src/libraries/Common/src/Interop/OSX/Interop.CoreServices.cs rename to src/libraries/Common/src/Interop/OSX/Interop.EventStream.cs index 6e5cf8f113c..515be7c9174 100644 --- a/src/libraries/Common/src/Interop/OSX/Interop.CoreServices.cs +++ b/src/libraries/Common/src/Interop/OSX/Interop.EventStream.cs @@ -14,18 +14,13 @@ using CFRunLoopRef = System.IntPtr; internal static partial class Interop { - internal static partial class CoreServices + internal static partial class EventStream { /// /// This constant specifies that we don't want historical file system events, only new ones /// internal const ulong kFSEventStreamEventIdSinceNow = ulong.MaxValue; - /// - /// This is the default system path for the CoreServices library that PInvoke will use - /// - private const string CoreServicesLibrary = "/System/Library/Frameworks/CoreServices.framework/CoreServices"; - /// /// Flags that describe what happened in the event that was received. These come from the FSEvents.h header file in the CoreServices framework. /// @@ -92,7 +87,7 @@ internal static partial class Interop FSEventStreamEventId[] eventIds); /// - /// 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). /// /// Should be IntPtr.Zero /// A callback instance that will be called for every event batch. @@ -105,16 +100,40 @@ internal static partial class Interop /// /// Coalescing period to wait before sending events. /// Flags to say what kind of events should be sent through this stream. - /// - [DllImport(CoreServicesLibrary)] - internal static extern FSEventStreamRef FSEventStreamCreate( - IntPtr allocator, - FSEventStreamCallback cb, - IntPtr context, - CFArrayRef pathsToWatch, - FSEventStreamEventId sinceWhen, - CFTimeInterval latency, - FSEventStreamCreateFlags flags); + /// On success, returns a pointer to an FSEventStream object; otherwise, returns IntPtr.Zero + /// For *nix systems, the CLR maps ANSI to UTF-8, so be explicit about that + [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); + + /// + /// Creates a new EventStream to listen to events from the core OS (such as File System events). + /// + /// A callback instance that will be called for every event batch. + /// A CFArray of the path(s) to watch for events. + /// + /// 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. + /// + /// Coalescing period to wait before sending events. + /// Flags to say what kind of events should be sent through this stream. + /// On success, returns a pointer to an FSEventStream object; otherwise, returns IntPtr.Zero + 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); + } /// /// 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 /// The stream to attach to the RunLoop /// The RunLoop to attach the stream to /// The mode of the RunLoop; this should usually be kCFRunLoopDefaultMode. See the documentation for RunLoops for more info. - [DllImport(CoreServicesLibrary)] + [DllImport(Interop.Libraries.CoreServicesLibrary)] internal static extern void FSEventStreamScheduleWithRunLoop( - FSEventStreamRef streamRef, - CFRunLoopRef runLoop, - CFStringRef runLoopMode); + FSEventStreamRef streamRef, + CFRunLoopRef runLoop, + CFStringRef runLoopMode); /// /// Starts receiving events on the specified stream. /// /// The stream to receive events on. /// Returns true if the stream was started; otherwise, returns false and no events will be received. - [DllImport(CoreServicesLibrary)] + [DllImport(Interop.Libraries.CoreServicesLibrary)] internal static extern bool FSEventStreamStart(FSEventStreamRef streamRef); /// /// Stops receiving events on the specified stream. The stream can be restarted and not miss any events. /// /// The stream to stop receiving events on. - [DllImport(CoreServicesLibrary)] + [DllImport(Interop.Libraries.CoreServicesLibrary)] internal static extern void FSEventStreamStop(FSEventStreamRef streamRef); /// @@ -149,17 +168,17 @@ internal static partial class Interop /// The stream to remove from the RunLoop /// The RunLoop to remove the stream from. /// The mode of the RunLoop; this should usually be kCFRunLoopDefaultMode. See the documentation for RunLoops for more info. - [DllImport(CoreServicesLibrary)] + [DllImport(Interop.Libraries.CoreServicesLibrary)] internal static extern void FSEventStreamUnscheduleFromRunLoop( - FSEventStreamRef streamRef, - CFRunLoopRef runLoop, - CFStringRef runLoopMode); + FSEventStreamRef streamRef, + CFRunLoopRef runLoop, + CFStringRef runLoopMode); /// /// Releases a reference count on the specified EventStream and, if necessary, cleans the stream up. /// /// The stream on which to decrement the reference count. - [DllImport(CoreServicesLibrary)] + [DllImport(Interop.Libraries.CoreServicesLibrary)] internal static extern void FSEventStreamRelease(FSEventStreamRef streamRef); } -} \ No newline at end of file +} diff --git a/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs b/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs new file mode 100644 index 00000000000..bb0c6266002 --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs @@ -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"; + } +} diff --git a/src/libraries/Common/src/Interop/OSX/Interop.RunLoop.cs b/src/libraries/Common/src/Interop/OSX/Interop.RunLoop.cs new file mode 100644 index 00000000000..9ae7122a6ee --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/Interop.RunLoop.cs @@ -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 + { + /// + /// This constant specifies that we want to use the default Run mode for the thread's Run loop. + /// + /// + /// For more information, see the Apple documentation: https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html + /// + internal static IntPtr kCFRunLoopDefaultMode = Interop.CoreFoundation.CFStringCreateWithCString("kCFRunLoopDefaultMode"); + + /// + /// Starts the current thread's RunLoop. If the RunLoop is already running, creates a new, nested, RunLoop in the same stack. + /// + [DllImport(Interop.Libraries.CoreFoundationLibrary)] + internal extern static void CFRunLoopRun(); + + /// + /// Notifies a RunLoop to stop and return control to the execution context that called CFRunLoopRun + /// + /// The RunLoop to notify to stop + [DllImport(Interop.Libraries.CoreFoundationLibrary)] + internal extern static void CFRunLoopStop(CFRunLoopRef rl); + + /// + /// 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. + /// + /// Returns a pointer to a CFRunLoop on success; otherwise, returns IntPtr.Zero + [DllImport(Interop.Libraries.CoreFoundationLibrary)] + internal static extern CFRunLoopRef CFRunLoopGetCurrent(); + } +} diff --git a/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs b/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs index 6eafab93bb7..f833354e15a 100644 --- a/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs +++ b/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs @@ -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 diff --git a/src/libraries/System.IO.FileSystem.Watcher/System.IO.FileSystem.Watcher.sln b/src/libraries/System.IO.FileSystem.Watcher/System.IO.FileSystem.Watcher.sln index f9b1ce97d93..e43bc0a3029 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/System.IO.FileSystem.Watcher.sln +++ b/src/libraries/System.IO.FileSystem.Watcher/System.IO.FileSystem.Watcher.sln @@ -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 diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/Resources/Strings.resx b/src/libraries/System.IO.FileSystem.Watcher/src/Resources/Strings.resx index b63f9bcbc27..2a114c578fb 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/Resources/Strings.resx +++ b/src/libraries/System.IO.FileSystem.Watcher/src/Resources/Strings.resx @@ -168,4 +168,7 @@ The process cannot access the file because it is being used by another process. + + Failed to start the EventStream + \ No newline at end of file diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj b/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj index fe2f3a10d3b..0af95d0fb2d 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj @@ -8,18 +8,16 @@ {77E702D9-C6D8-4CE4-9941-D3056C3CCBED} true - - - - + + - Common\System\NotImplemented.cs + Common\System\NotImplemented.cs @@ -39,20 +37,19 @@ - + - + Common\Interop\Linux\Interop.inotify.cs + + Common\Interop\Unix\Interop.Libraries.cs Common\Interop\Unix\libc\Interop.read.cs - - Common\src\Interop\Unix\libc\Interop.gnu_get_libc_version.cs - Common\Interop\Unix\libc\Interop.strerror.cs @@ -62,18 +59,29 @@ Common\Interop\Unix\Interop.IOErrors.cs + + Common\src\Interop\Unix\libc\Interop.gnu_get_libc_version.cs + - - Common\Interop\OSX\Interop.CoreServices.cs + + Common\Interop\OSX\Interop.EventStream.cs - + Common\Interop\OSX\Interop.CoreFoundation.cs + + Common\Interop\OSX\Interop.Libraries.cs + + + Common\Interop\OSX\Interop.RunLoop.cs + - + + Designer + - + \ No newline at end of file diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs index 5d44e839036..cb2758b2e6d 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs @@ -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 diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs index 4a498c36f66..693199dd76f 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs @@ -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); - /// Called when FileSystemWatcher is finalized. 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 handledRenameEvents = new Collections.Generic.List(); + List 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(); } + 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; } } -} \ No newline at end of file +} diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs index 8360f3228fd..ded3462b473 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs @@ -92,8 +92,6 @@ namespace System.IO } } - internal const bool CaseSensitive = false; - // ----------------------------- // ---- PAL layer ends here ---- // ----------------------------- diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs index a89bf1b9f90..c18f9a1ebfc 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs @@ -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; } /// @@ -107,6 +110,7 @@ namespace System.IO _directory = path; _filter = filter; + CaseSensitive = false; } /// diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/packages.config b/src/libraries/System.IO.FileSystem.Watcher/src/packages.config index 6797583cb9d..6e3375ad652 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/packages.config +++ b/src/libraries/System.IO.FileSystem.Watcher/src/packages.config @@ -17,8 +17,8 @@ - + \ No newline at end of file