@@ -9,6 +9,7 @@ namespace Files.App.Server;
99internal sealed partial class DirectoryChangeMonitor : IDisposable
1010{
1111 private readonly Lock gate = new ( ) ;
12+ private readonly Lock configureGate = new ( ) ;
1213 private readonly string directory ;
1314 private readonly string filter ;
1415 private readonly bool recursive ;
@@ -17,19 +18,26 @@ internal sealed partial class DirectoryChangeMonitor : IDisposable
1718 private FileSystemWatcher ? parentWatcher ;
1819 private bool watchingTarget ;
1920 private bool disposed ;
21+ private long configurationVersion ;
2022
2123 internal DirectoryChangeMonitor ( string directory , string filter , bool recursive , Action < FileSystemEventArgs > changed )
2224 {
2325 this . directory = Path . TrimEndingDirectorySeparator ( Path . GetFullPath ( directory ) ) ;
2426 this . filter = filter ;
2527 this . recursive = recursive ;
2628 this . changed = changed ;
27- lock ( gate )
28- Configure ( ) ;
29+ Configure ( ) ;
2930 }
3031
3132 private void Configure ( )
3233 {
34+ long version ;
35+ lock ( gate )
36+ {
37+ if ( disposed )
38+ return ;
39+ version = ++ configurationVersion ;
40+ }
3341 var existing = directory ;
3442 var next = directory ;
3543 while ( ! Directory . Exists ( existing ) )
@@ -54,13 +62,29 @@ private void Configure()
5462 replacement . Dispose ( ) ;
5563 throw ;
5664 }
57- var previous = watcher ;
58- var previousParent = parentWatcher ;
59- watcher = replacement ;
60- parentWatcher = replacementParent ;
61- watchingTarget = targetExists ;
65+ FileSystemWatcher ? previous ;
66+ FileSystemWatcher ? previousParent ;
67+ lock ( gate )
68+ {
69+ if ( disposed || version != configurationVersion )
70+ {
71+ previous = replacement ;
72+ previousParent = replacementParent ;
73+ }
74+ else
75+ {
76+ previous = watcher ;
77+ previousParent = parentWatcher ;
78+ watcher = replacement ;
79+ parentWatcher = replacementParent ;
80+ watchingTarget = targetExists ;
81+ }
82+ }
6283 previous ? . Dispose ( ) ;
6384 previousParent ? . Dispose ( ) ;
85+ // The missing component may have appeared before its parent watch was enabled.
86+ if ( ! targetExists && Directory . Exists ( next ) )
87+ Configure ( ) ;
6488 }
6589
6690 private FileSystemWatcher CreateWatcher ( string path , string pattern , bool includeSubdirectories , NotifyFilters notifyFilter )
@@ -95,19 +119,23 @@ private bool Relevant(string path)
95119
96120 private void OnChanged ( object sender , FileSystemEventArgs args )
97121 {
122+ bool reconfigure ;
98123 lock ( gate )
99124 {
100125 if ( disposed || ( ! ReferenceEquals ( sender , watcher ) && ! ReferenceEquals ( sender , parentWatcher ) ) )
101126 return ;
102- if ( ReferenceEquals ( sender , parentWatcher ) || ! watchingTarget || ! Directory . Exists ( directory ) )
103- {
104- Reconfigure ( ) ;
105- args = new FileSystemEventArgs ( WatcherChangeTypes . All , directory , string . Empty ) ;
106- }
107- else if ( ! Relevant ( args . FullPath ) && ! ( args is RenamedEventArgs renamed && Relevant ( renamed . OldFullPath ) ) )
127+ reconfigure = ReferenceEquals ( sender , parentWatcher ) || ! watchingTarget ;
128+ }
129+ if ( reconfigure || ! Directory . Exists ( directory ) )
130+ {
131+ if ( ! Reconfigure ( sender ) )
108132 return ;
133+ args = new FileSystemEventArgs ( WatcherChangeTypes . All , directory , string . Empty ) ;
109134 }
110- changed ( args ) ;
135+ else if ( ! Relevant ( args . FullPath ) && ! ( args is RenamedEventArgs renamed && Relevant ( renamed . OldFullPath ) ) )
136+ return ;
137+ if ( ! Volatile . Read ( ref disposed ) )
138+ changed ( args ) ;
111139 }
112140
113141 private void OnError ( object sender , ErrorEventArgs args )
@@ -116,33 +144,48 @@ private void OnError(object sender, ErrorEventArgs args)
116144 {
117145 if ( disposed || ( ! ReferenceEquals ( sender , watcher ) && ! ReferenceEquals ( sender , parentWatcher ) ) )
118146 return ;
119- if ( args . GetException ( ) is not InternalBufferOverflowException )
120- Reconfigure ( ) ;
121147 }
122- changed ( new FileSystemEventArgs ( WatcherChangeTypes . All , directory , string . Empty ) ) ;
148+ if ( args . GetException ( ) is not InternalBufferOverflowException && ! Reconfigure ( sender ) )
149+ return ;
150+ if ( ! Volatile . Read ( ref disposed ) )
151+ changed ( new FileSystemEventArgs ( WatcherChangeTypes . All , directory , string . Empty ) ) ;
123152 }
124153
125- private void Reconfigure ( )
154+ private bool Reconfigure ( object sender )
126155 {
127- try
156+ lock ( configureGate )
128157 {
129- Configure ( ) ;
130- }
131- catch ( Exception ex )
132- {
133- Program . Logger . LogWarning ( ex , "Could not update a directory watch." ) ;
158+ lock ( gate )
159+ {
160+ if ( disposed || ( ! ReferenceEquals ( sender , watcher ) && ! ReferenceEquals ( sender , parentWatcher ) ) )
161+ return false ;
162+ }
163+ try
164+ {
165+ Configure ( ) ;
166+ }
167+ catch ( Exception ex )
168+ {
169+ Program . Logger . LogWarning ( ex , "Could not update a directory watch." ) ;
170+ }
171+ return ! Volatile . Read ( ref disposed ) ;
134172 }
135173 }
136174
137175 public void Dispose ( )
138176 {
177+ FileSystemWatcher ? previous ;
178+ FileSystemWatcher ? previousParent ;
139179 lock ( gate )
140180 {
141181 disposed = true ;
142- watcher ? . Dispose ( ) ;
182+ configurationVersion ++ ;
183+ previous = watcher ;
184+ previousParent = parentWatcher ;
143185 watcher = null ;
144- parentWatcher ? . Dispose ( ) ;
145186 parentWatcher = null ;
146187 }
188+ previous ? . Dispose ( ) ;
189+ previousParent ? . Dispose ( ) ;
147190 }
148191}
0 commit comments