Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FAMWatcher.cs « System.IO « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d0ee6526ee5013210c0fd46afee4c04958011719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// 
// System.IO.FAM.cs: interface with libfam
//
// Authors:
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2004 Novell, Inc. (http://www.novell.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace System.IO {
	struct FAMConnection {
		public int FD;
		public IntPtr opaque;
	}

	struct FAMRequest {
		public int ReqNum;
	}

	enum FAMCodes {
		Changed = 1,
		Deleted = 2,
		StartExecuting = 3, 
		StopExecuting = 4,
		Created = 5,
		Moved = 6, 
		Acknowledge = 7,
		Exists = 8,
		EndExist = 9
	};

	class FAMData {
		public FileSystemWatcher FSW;
		public string Directory;
		public string FileMask;
		public bool IncludeSubdirs;
		public bool Enabled;
		public FAMRequest Request;
		public Hashtable SubDirs;
	}

	class FAMWatcher : IFileWatcher
	{
		static bool failed;
		static FAMWatcher instance;
		static Hashtable watches;
		static Hashtable requests;
		static FAMConnection conn;
		static Thread thread;
		static bool stop;
		static bool use_gamin;
		
		private FAMWatcher ()
		{
		}
		
		// Locked by caller
		public static bool GetInstance (out IFileWatcher watcher, bool gamin)
		{
			if (failed == true) {
				watcher = null;
				return false;
			}

			if (instance != null) {
				watcher = instance;
				return true;
			}

			use_gamin = gamin;
			watches = Hashtable.Synchronized (new Hashtable ());
			requests = Hashtable.Synchronized (new Hashtable ());
			if (FAMOpen (out conn) == -1) {
				failed = true;
				watcher = null;
				return false;
			}

			instance = new FAMWatcher ();
			watcher = instance;
			return true;
		}
		
		public void StartDispatching (object handle)
		{
			var fsw = handle as FileSystemWatcher;
			FAMData data;
			lock (this) {
				if (thread == null) {
					thread = new Thread (new ThreadStart (Monitor));
					thread.IsBackground = true;
					thread.Start ();
				}

				data = (FAMData) watches [fsw];
			}

			if (data == null) {
				data = new FAMData ();
				data.FSW = fsw;
				data.Directory = fsw.FullPath;
				data.FileMask = fsw.MangledFilter;
				data.IncludeSubdirs = fsw.IncludeSubdirectories;
				if (data.IncludeSubdirs)
					data.SubDirs = new Hashtable ();

				data.Enabled = true;
				StartMonitoringDirectory (data, false);
				lock (this) {
					watches [fsw] = data;
					requests [data.Request.ReqNum] = data;
					stop = false;
				}
			}
		}

		static void StartMonitoringDirectory (FAMData data, bool justcreated)
		{
			FAMRequest fr;
			FileSystemWatcher fsw;
			if (FAMMonitorDirectory (ref conn, data.Directory, out fr, IntPtr.Zero) == -1)
				throw new Win32Exception ();

			fsw = data.FSW;
			data.Request = fr;

			if (data.IncludeSubdirs) {
				foreach (string directory in Directory.GetDirectories (data.Directory)) {
					FAMData fd = new FAMData ();
					fd.FSW = data.FSW;
					fd.Directory = directory;
					fd.FileMask = data.FSW.MangledFilter;
					fd.IncludeSubdirs = true;
					fd.SubDirs = new Hashtable ();
					fd.Enabled = true;

					if (justcreated) {
						lock (fsw) {
							RenamedEventArgs renamed = null;

							fsw.DispatchEvents (FileAction.Added, directory, ref renamed);

							if (fsw.Waiting) {
								fsw.Waiting = false;
								System.Threading.Monitor.PulseAll (fsw);
							}
						}
					}

					StartMonitoringDirectory (fd, justcreated);
					data.SubDirs [directory] = fd;
					requests [fd.Request.ReqNum] = fd;
				}
			}

			if (justcreated) {
				foreach (string filename in Directory.GetFiles(data.Directory)) {
					lock (fsw) {
						RenamedEventArgs renamed = null;

						fsw.DispatchEvents (FileAction.Added, filename, ref renamed);
						/* If a file has been created, then it has been written to */
						fsw.DispatchEvents (FileAction.Modified, filename, ref renamed);

						if (fsw.Waiting) {
							fsw.Waiting = false;
							System.Threading.Monitor.PulseAll(fsw);
						}
					}
				}
			}
		}

		public void StopDispatching (object handle)
		{
			var fsw = handle as FileSystemWatcher;
			FAMData data;
			lock (this) {
				data = (FAMData) watches [fsw];
				if (data == null)
					return;

				StopMonitoringDirectory (data);
				watches.Remove (fsw);
				requests.Remove (data.Request.ReqNum);
				if (watches.Count == 0)
					stop = true;

				if (!data.IncludeSubdirs)
					return;

				foreach (FAMData fd in data.SubDirs.Values) {
					StopMonitoringDirectory (fd);
					requests.Remove (fd.Request.ReqNum);
				}
			}
		}

		static void StopMonitoringDirectory (FAMData data)
		{
			if (FAMCancelMonitor (ref conn, ref data.Request) == -1)
				throw new Win32Exception ();
		}

		void Monitor ()
		{
			while (!stop) {
				int haveEvents;
				lock (this) {
					haveEvents = FAMPending (ref conn);
				}

				if (haveEvents > 0) {
					ProcessEvents ();
				} else {
					Thread.Sleep (500);
				}
			}

			lock (this) {
				thread = null;
				stop = false;
			}
		}

		const NotifyFilters changed = 	NotifyFilters.Attributes |
						NotifyFilters.LastAccess |
						NotifyFilters.Size	|
						NotifyFilters.LastWrite;

		void ProcessEvents ()
		{
			ArrayList newdirs = null;
			lock (this) {
				do {
					int code;
					string filename;
					int requestNumber;
					FileSystemWatcher fsw;

					if (InternalFAMNextEvent (ref conn, out filename,
								  out code, out requestNumber) != 1)
						return;

					bool found = false;
					switch ((FAMCodes) code) {
					case FAMCodes.Changed:
					case FAMCodes.Deleted:
					case FAMCodes.Created:
						found = requests.ContainsKey (requestNumber);
						break;
					case FAMCodes.Moved:
					case FAMCodes.StartExecuting:
					case FAMCodes.StopExecuting:
					case FAMCodes.Acknowledge:
					case FAMCodes.Exists:
					case FAMCodes.EndExist:
					default:
						found = false;
						break;
					}

					if (!found)
						continue;
					
					FAMData data = (FAMData) requests [requestNumber];
					if (!data.Enabled)
						continue;

					fsw = data.FSW;
					NotifyFilters flt = fsw.NotifyFilter;
					RenamedEventArgs renamed = null;
					FileAction fa = 0;
					if (code == (int) FAMCodes.Changed && (flt & changed) != 0)
						fa = FileAction.Modified;
					else if (code == (int) FAMCodes.Deleted)
						fa = FileAction.Removed;
					else if (code == (int) FAMCodes.Created)
						fa = FileAction.Added;

					if (fa == 0)
						continue;

					if (fsw.IncludeSubdirectories) {
						string full = fsw.FullPath;
						string datadir = data.Directory;
						if (datadir != full) {
							int len = full.Length;
							int slash = 1;
							if (len > 1 && full [len - 1] == Path.DirectorySeparatorChar)
								slash = 0;
							string reldir = datadir.Substring (full.Length + slash);
							datadir = Path.Combine (datadir, filename);
							filename = Path.Combine (reldir, filename);
						} else {
							datadir = Path.Combine (full, filename);
						}

						if (fa == FileAction.Added && Directory.Exists (datadir)) {
							if (newdirs == null)
								newdirs = new ArrayList (4);

							FAMData fd = new FAMData ();
							fd.FSW = fsw;
							fd.Directory = datadir;
							fd.FileMask = fsw.MangledFilter;
							fd.IncludeSubdirs = true;
							fd.SubDirs = new Hashtable ();
							fd.Enabled = true;
							newdirs.Add (fd);
							newdirs.Add (data);
						}
					}

					if (filename != data.Directory && !fsw.Pattern.IsMatch (filename))
						continue;

					lock (fsw) {
						fsw.DispatchEvents (fa, filename, ref renamed);
						if (fsw.Waiting) {
							fsw.Waiting = false;
							System.Threading.Monitor.PulseAll (fsw);
						}
					}
				} while (FAMPending (ref conn) > 0);
			}


			if (newdirs != null) {
				int count = newdirs.Count;
				for (int n = 0; n < count; n += 2) {
					FAMData newdir = (FAMData) newdirs [n];
					FAMData parent = (FAMData) newdirs [n + 1];
					StartMonitoringDirectory (newdir, true);
					requests [newdir.Request.ReqNum] = newdir;
					lock (parent) {
						parent.SubDirs [newdir.Directory] = newdir;
					}
				}
				newdirs.Clear ();
			}
		}

		~FAMWatcher ()
		{
			FAMClose (ref conn);
		}

		static int FAMOpen (out FAMConnection fc)
		{
			if (use_gamin)
				return gamin_Open (out fc);
			return fam_Open (out fc);
		}

		static int FAMClose (ref FAMConnection fc)
		{
			if (use_gamin)
				return gamin_Close (ref fc);
			return fam_Close (ref fc);
		}

		static int FAMMonitorDirectory (ref FAMConnection fc, string filename, out FAMRequest fr, IntPtr user_data)
		{
			if (use_gamin)
				return gamin_MonitorDirectory (ref fc, filename, out fr, user_data);
			return fam_MonitorDirectory (ref fc, filename, out fr, user_data);
		}

		static int FAMCancelMonitor (ref FAMConnection fc, ref FAMRequest fr)
		{
			if (use_gamin)
				return gamin_CancelMonitor (ref fc, ref fr);
			return fam_CancelMonitor (ref fc, ref fr);
		}

		static int FAMPending (ref FAMConnection fc)
		{
			if (use_gamin)
				return gamin_Pending (ref fc);
			return fam_Pending (ref fc);
		}

		public void Dispose (object handle)
		{
			// does nothing
		}

		
		[DllImport ("libfam.so.0", EntryPoint="FAMOpen")]
		extern static int fam_Open (out FAMConnection fc);

		[DllImport ("libfam.so.0", EntryPoint="FAMClose")]
		extern static int fam_Close (ref FAMConnection fc);

		[DllImport ("libfam.so.0", EntryPoint="FAMMonitorDirectory")]
		extern static int fam_MonitorDirectory (ref FAMConnection fc, string filename,
							out FAMRequest fr, IntPtr user_data);

		[DllImport ("libfam.so.0", EntryPoint="FAMCancelMonitor")]
		extern static int fam_CancelMonitor (ref FAMConnection fc, ref FAMRequest fr);

		[DllImport ("libfam.so.0", EntryPoint="FAMPending")]
		extern static int fam_Pending (ref FAMConnection fc);

		[DllImport ("libgamin-1.so.0", EntryPoint="FAMOpen")]
		extern static int gamin_Open (out FAMConnection fc);

		[DllImport ("libgamin-1.so.0", EntryPoint="FAMClose")]
		extern static int gamin_Close (ref FAMConnection fc);

		[DllImport ("libgamin-1.so.0", EntryPoint="FAMMonitorDirectory")]
		extern static int gamin_MonitorDirectory (ref FAMConnection fc, string filename,
							out FAMRequest fr, IntPtr user_data);

		[DllImport ("libgamin-1.so.0", EntryPoint="FAMCancelMonitor")]
		extern static int gamin_CancelMonitor (ref FAMConnection fc, ref FAMRequest fr);

		[DllImport ("libgamin-1.so.0", EntryPoint="FAMPending")]
		extern static int gamin_Pending (ref FAMConnection fc);

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		extern static int InternalFAMNextEvent (ref FAMConnection fc, out string filename,
							out int code, out int reqnum);

	}
}