Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b4e803a

Browse files
author
Jonathan Miller
committed
Fixing bugs in OS X FileSystemWatcher to make all tests pass
1 parent 9b16041 commit b4e803a

File tree

7 files changed

+152
-107
lines changed

7 files changed

+152
-107
lines changed

src/Common/src/Interop/OSX/Interop.EventStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal static partial class EventStream
2121
/// <summary>
2222
/// This constant specifies that we don't want historical file system events, only new ones
2323
/// </summary>
24-
internal const ulong kFSEventStreamEventIdSinceNow = ulong.MaxValue;
24+
internal const ulong kFSEventStreamEventIdSinceNow = 0xFFFFFFFFFFFFFFFF;
2525

2626
/// <summary>
2727
/// Flags that describe what happened in the event that was received. These come from the FSEvents.h header file in the CoreServices framework.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class libc
10+
{
11+
/// <summary>
12+
/// Flushes the OS-wide file buffer(s)
13+
/// </summary>
14+
[DllImport(Interop.Libraries.Libc)]
15+
internal extern static void sync();
16+
}
17+
}

src/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
<Compile Include="$(CommonPath)\Interop\OSX\Interop.RunLoop.cs">
8585
<Link>Common\Interop\OSX\Interop.RunLoop.cs</Link>
8686
</Compile>
87+
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.sync.cs">
88+
<Link>Common\Interop\Unix\Interop.sync.cs</Link>
89+
</Compile>
8790
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeCreateHandle.OSX.cs">
8891
<Link>Common\Microsoft\Win32\SafeHandles\SafeCreateHandle.OSX.cs</Link>
8992
</Compile>

src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs

Lines changed: 104 additions & 104 deletions
Large diffs are not rendered by default.

src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ private void NotifyFileSystemEventArgs(WatcherChangeTypes changeType, string nam
456456
break;
457457
}
458458

459-
if (handler != null && MatchPattern(name))
459+
if (handler != null && MatchPattern(string.IsNullOrEmpty(name) ? _directory : name))
460460
{
461461
handler(this, new FileSystemEventArgs(changeType, _directory, name));
462462
}

src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Changed.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,22 @@ public static void FileSystemWatcher_Changed_Negative()
7575
Utility.ExpectNoEvent(eventOccured, "changed");
7676
}
7777
}
78+
79+
[Fact, ActiveIssue(2279)]
80+
public static void FileSystemWatcher_ChangeWatchedFolder()
81+
{
82+
using (var dir = Utility.CreateTestDirectory())
83+
using (var watcher = new FileSystemWatcher())
84+
{
85+
watcher.Path = Path.GetFullPath(dir.Path);
86+
watcher.Filter = "*";
87+
AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Changed);
88+
89+
watcher.EnableRaisingEvents = true;
90+
91+
Directory.SetLastAccessTime(watcher.Path, DateTime.Now);
92+
93+
Utility.ExpectEvent(eventOccured, "changed");
94+
}
95+
}
7896
}

src/System.IO.FileSystem/src/System/IO/UnixFileSystemObject.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,14 @@ public DateTimeOffset CreationTime
178178
DateTimeOffset.FromUnixTimeSeconds(_fileinfo.btime) :
179179
default(DateTimeOffset);
180180
}
181-
set { } // Not supported
181+
set
182+
{
183+
// The ctime in Unix can be interpreted differently by different formats so there isn't
184+
// a reliable way to set this; however, we can't just do nothing since the FileSystemWatcher
185+
// specifically looks for this call to make a Metatdata Change, so we should set the
186+
// LastAccessTime of the file to cause the metadata change we need.
187+
LastAccessTime = LastAccessTime;
188+
}
182189
}
183190

184191
public DateTimeOffset LastAccessTime

0 commit comments

Comments
 (0)