File: basic.pike

package info (click to toggle)
pike8.0 8.0.1956-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,580 kB
  • sloc: ansic: 259,734; xml: 36,320; makefile: 3,748; sh: 1,713; cpp: 1,349; awk: 1,036; lisp: 655; javascript: 468; asm: 242; objc: 240; pascal: 157; sed: 34
file content (1786 lines) | stat: -rw-r--r-- 50,160 bytes parent folder | download | duplicates (5)
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
#pike __REAL_VERSION__

//
// Basic filesystem monitor.
//
//
// 2009-07-09 Henrik Grubbstrm
//
//! Basic filesystem monitor.
//!
//! This module is intended to be used for incremental scanning of
//! a filesystem.
//!
//! Supports FSEvents on MacOS X and Inotify on Linux to provide low
//! overhead monitoring; other systems use a less efficient polling approach.
//!
//! @seealso
//!  @[Filesystem.Monitor.symlinks], @[System.FSEvents], @[System.Inotify]

//! The default maximum number of seconds between checks of directories
//! in seconds.
//!
//! This value is multiplied with @[default_file_interval_factor] to
//! get the corresponding default maximum number of seconds for files.
//!
//! The value can be changed by calling @[create()].
//!
//! The value can be overridden for individual files or directories
//! by calling @[monitor()].
//!
//! Overload this constant to change the default.
protected constant default_max_dir_check_interval = 60;

//! The default factor to multiply @[default_max_dir_check_interval]
//! with to get the maximum number of seconds between checks of files.
//!
//! The value can be changed by calling @[create()].
//!
//! The value can be overridden for individual files or directories
//! by calling @[monitor()].
//!
//! Overload this constant to change the default.
protected constant default_file_interval_factor = 5;

//! The default minimum number of seconds without changes for a change
//! to be regarded as stable (see @[stable_data_change()].
protected constant default_stable_time = 5;

protected int max_dir_check_interval = default_max_dir_check_interval;
protected int file_interval_factor = default_file_interval_factor;
protected int stable_time = default_stable_time;

protected inline constant SeverityLevel = DefaultCompilerEnvironment.SeverityLevel;
protected inline constant NOTICE = DefaultCompilerEnvironment.NOTICE;
protected inline constant WARNING = DefaultCompilerEnvironment.WARNING;
protected inline constant ERROR = DefaultCompilerEnvironment.ERROR;
protected inline constant FATAL = DefaultCompilerEnvironment.FATAL;

// Callbacks

//! Event tracing callback.
//!
//! @param level
//!   Severity level of the event.
//!
//! @param fun
//!   Name of the function that called @[report()].
//!
//! @param format
//!   @[sprintf()] formatting string describing the event.
//!
//! @param args
//!   Optional extra arguments for the @[format] string.
//!
//! This function is called in various places to provide
//! granular tracing of the monitor state.
//!
//! The default implementation calls @[werror()] with
//! @[format] and @[args] if @[level] is @[ERROR] or higher,
//! or if @tt{FILESYSTEM_MONITOR_DEBUG@} has been defined.
protected void report(SeverityLevel level, string(7bit) fun,
		      sprintf_format format, sprintf_args ... args)
{
#ifndef FILESYSTEM_MONITOR_DEBUG
  if (level < ERROR) return;
#endif
  werror(format, @args);
}

#define MON_WERR(X...)	report(NOTICE,	__func__, X)
#define MON_WARN(X...)	report(WARNING,	__func__, X)
#define MON_ERROR(X...)	report(ERROR,	__func__, X)

//! File content changed callback.
//!
//! @param path
//!   Path of the file which has had content changed.
//!
//! This function is called when a change has been detected for a
//! monitored file.
//!
//! Called by @[check()] and @[check_monitor()].
//!
//! Overload this to do something useful.
void data_changed(string path);

//! File attribute changed callback.
//!
//! @param path
//!   Path of the file or directory which has changed attributes.
//!
//! @param st
//!   Status information for @[path] as obtained by @expr{file_stat(path, 1)@}.
//!
//! This function is called when a change has been detected for an
//! attribute for a monitored file or directory.
//!
//! Called by @[check()] and @[check_monitor()].
//!
//! @note
//!   If there is a @[data_changed()] callback, it may supersede this
//!   callback if the file content also has changed.
//!
//! Overload this to do something useful.
void attr_changed(string path, Stdio.Stat st);

//! File existance callback.
//!
//! @param path
//!   Path of the file or directory.
//!
//! @param st
//!   Status information for @[path] as obtained by @expr{file_stat(path, 1)@}.
//!
//! This function is called during initialization for all monitored paths,
//! and subpaths for monitored directories. It represents the initial state
//! for the monitor.
//!
//! @note
//!   For directories, @[file_created()] will be called for the subpaths
//!   before the call for the directory itself. This can be used to detect
//!   when the initialization for a directory is finished.
//!
//! Called by @[check()] and @[check_monitor()] the first time a monitored
//! path is checked (and only if it exists).
//!
//! Overload this to do something useful.
void file_exists(string path, Stdio.Stat st);

//! File creation callback.
//!
//! @param path
//!   Path of the new file or directory.
//!
//! @param st
//!   Status information for @[path] as obtained by @expr{file_stat(path, 1)@}.
//!
//! This function is called when either a monitored path has started
//! existing, or when a new file or directory has been added to a
//! monitored directory.
//!
//! Called by @[check()] and @[check_monitor()].
//!
//! Overload this to do something useful.
void file_created(string path, Stdio.Stat st);

//! File deletion callback.
//!
//! @param path
//!   Path of the new file or directory that has been deleted.
//!
//! This function is called when either a monitored path has stopped
//! to exist, or when a file or directory has been deleted from a
//! monitored directory.
//!
//! Called by @[check()] and @[check_monitor()].
//!
//! Overload this to do something useful.
void file_deleted(string path);

//! Stable change callback.
//!
//! @param path
//!   Path of the file or directory that has stopped changing.
//!
//! @param st
//!   Status information for @[path] as obtained by @expr{file_stat(path, 1)@}.
//!
//! This function is called when previous changes to @[path] are
//! considered "stable".
//!
//! "Stable" in this case means that there have been no detected
//! changes for at lease @[stable_time] seconds.
//!
//! Called by @[check()] and @[check_monitor()].
//!
//! Overload this to do something useful.
void stable_data_change(string path, Stdio.Stat st);

//! Flags for @[Monitor]s.
enum MonitorFlags {
  MF_RECURSE = 1,
  MF_AUTO = 2,
  MF_INITED = 4,
  MF_HARD = 8,
};

protected constant S_IFMT = 0x7ffff000;

//! Monitoring information for a single filesystem path.
//!
//! @seealso
//!   @[monitor()]
protected class Monitor(string path,
			MonitorFlags flags,
			int max_dir_check_interval,
			int file_interval_factor,
			int stable_time)
{
  inherit ADT.Heap.Element;

  int next_poll;
  Stdio.Stat st;
  int last_change = 0x7fffffff;	// Future... Can be set to -0x7fffffff
				// to indicate immediate stabilization
				// (avoid an extra check() round to
				// let the stat stabilize).
  private bool initialized = false;
  array(string) files;

  //! Event tracing callback.
  //!
  //! @param level
  //!   Severity level of the event.
  //!
  //! @param fun
  //!   Name of the function that called @[report()].
  //!
  //! @param format
  //!   @[sprintf()] formatting string describing the event.
  //!
  //! @param args
  //!   Optional extra arguments for the @[format] string.
  //!
  //! This function is called in various places to provide
  //! granular tracing of the monitor state.
  //!
  //! The default implementation just calls @[global::report()]
  //! with the same arguments.
  protected void report(SeverityLevel level, string(7bit) fun,
			sprintf_format format, sprintf_args ... args)
  {
    global::report(level, fun, format, @args);
  }

  //! Register the @[Monitor] with the monitoring system.
  //!
  //! @param initial
  //!   Indicates that the @[Monitor] is newly created.
  protected void register_path(int|void initial)
  {
    if (initial) {
      // We need to be polled...
      MON_WERR("Registering %O for polling.\n", path);
      mixed key = monitor_mutex->lock();
      monitor_queue->push(this);
      if (monitor_queue->peek() == this) {
	if (co_id) {
	  reschedule_backend_check();
	}
      }
    }
  }

  //! Unregister the @[Monitor] from the monitoring system.
  //!
  //! @param dying
  //!   Indicates that the @[Monitor] is being destructed.
  //!   It is the destruction cause value offset by one.
  protected void unregister_path(int|void dying)
  {
    if (dying == 1) {
      // We are going away permanently due to explicit destruct(),
      // so remove ourselves from from the monitor_queue.
      mixed key = monitor_mutex->lock();
      MON_WERR("Unregistering %O from polling.\n", path);
      monitor_queue->remove(this);
    }
  }

  int `<(mixed m) { return next_poll < m; }
  int `>(mixed m) { return next_poll > m; }

  void create()
  {
    MON_WERR("Creating monitor for %O.\n", path);
    Element::create(this);
    register_path(1);
  }

  //! Returns the parent monitor, or UNDEFINED if no such monitor exists.
  this_program parent()
  {
    string parent_path = canonic_path (dirname (path));
    return monitors[parent_path];
  }

  //! To be called when a (direct) submonitor is released.
  void submonitor_released (this_program submon)
  {
    if (files) {
      string filename = basename (submon->path);
      MON_WERR("%O->submonitor_released(%O): Removing list state for %O.\n",
	       this, submon, filename);
      files -= ({ filename });
    }
  }

  //! Call a notification callback.
  //!
  //! @param cb
  //!   Callback to call or @[UNDEFINED] for no operation.
  //!
  //! @param path
  //!   Path to notify on.
  //!
  //! @param st
  //!   Stat for the @[path].
  protected void call_callback(function(string, Stdio.Stat|void:void) cb,
			       string path, Stdio.Stat|void st)
  {
    if (!cb) return;
    cb(path, st);
  }

  //! File attribute or content changed callback.
  //!
  //! @param st
  //!   Status information for @[path] as obtained by
  //!   @expr{file_stat(path, 1)@}.
  //!
  //! This function is called when a change has been detected for an
  //! attribute for a monitored file or directory.
  //!
  //! Called by @[check()] and @[check_monitor()].
  //!
  //! @note
  //!   If there is a @[data_changed()] callback, it may supersede this
  //!   callback if the file content also has changed.
  protected void attr_changed(string path, Stdio.Stat st)
  {
    MON_WERR("attr_changed(%O, %O)\n", path, st);
    if (global::data_changed) {
      call_callback(global::data_changed, path);
    } else {
      call_callback(global::attr_changed, path, st);
    }
  }

  //! File existance callback.
  //!
  //! @param st
  //!   Status information for @[path] as obtained by
  //!   @expr{file_stat(path, 1)@}.
  //!
  //! This function is called during initialization for all monitored paths,
  //! and subpaths for monitored directories. It represents the initial state
  //! for the monitor.
  //!
  //! @note
  //!   For directories, @[file_created()] will be called for the subpaths
  //!   before the call for the directory itself. This can be used to detect
  //!   when the initialization for a directory is finished.
  //!
  //! Called by @[check()] and @[check_monitor()] the first time a monitored
  //! path is checked (and only if it exists).
  protected void file_exists(string path, Stdio.Stat st)
  {
    MON_WERR("file_exists(%O, %O)\n", path, st);
    register_path();
    int t = time(1);
    call_callback(global::file_exists, path, st);
    if (st->mtime + (stable_time || global::stable_time) >= t) {
      // Not stable yet! We guess that the mtime is a
      // fair indication of when the file last changed.
      last_change = st->mtime;
    }
  }

  //! File creation callback.
  //!
  //! @param st
  //!   Status information for @[path] as obtained by
  //!   @expr{file_stat(path, 1)@}.
  //!
  //! This function is called when either a monitored path has started
  //! existing, or when a new file or directory has been added to a
  //! monitored directory.
  //!
  //! Called by @[check()] and @[check_monitor()].
  protected void file_created(string path, Stdio.Stat st)
  {
    MON_WERR("file_created(%O, %O)\n", path, st);
    register_path();
    call_callback(global::file_created, path, st);
  }

  //! File deletion callback.
  //!
  //! @param path
  //!   Path of the new file or directory that has been deleted.
  //!
  //! @param old_st
  //!   Stat for the file prior to deletion (if known). Note that
  //!   this argument is not passed along to top level function.
  //!
  //! This function is called when either a monitored path has stopped
  //! to exist, or when a file or directory has been deleted from a
  //! monitored directory.
  //!
  //! Called by @[check()] and @[check_monitor()].
  protected void file_deleted(string path, Stdio.Stat|void old_st)
  {
    MON_WERR("file_deleted(%O, %O)\n", path, st);
    unregister_path();
    call_callback(global::file_deleted, path);
  }

  //! Stable change callback.
  //!
  //! @param st
  //!   Status information for @[path] as obtained by
  //!   @expr{file_stat(path, 1)@}.
  //!
  //! This function is called when previous changes to @[path] are
  //! considered "stable".
  //!
  //! "Stable" in this case means that there have been no detected
  //! changes for at lease @[stable_time] seconds.
  //!
  //! Called by @[check()] and @[check_monitor()].
  protected void stable_data_change(string path, Stdio.Stat st)
  {
    MON_WERR("stable_data_change(%O, %O)\n", path, st);
    call_callback(global::stable_data_change, path, st);
  }

  protected string _sprintf(int c)
  {
    return sprintf("Monitor(%O, %O, next: %s, st: %O)",
		   path, flags, ctime(next_poll) - "\n", st);
  }

  //! Bump the monitor to an earlier scan time.
  //!
  //! @param flags
  //!   @int
  //!     @value 0
  //!       Don't recurse.
  //!     @value 1
  //!       Check all monitors for the entire subtree.
  //!   @endint
  //!
  //! @param seconds
  //!   Number of seconds from now to run next scan. Defaults to
  //!   half of the remaining interval.
  void bump(MonitorFlags|void flags, int|void seconds)
  {
    int now = time(1);
    if (seconds)
      next_poll = now + seconds;
    else if (next_poll > now)
      next_poll -= (next_poll - now) / 2;
    adjust_monitor(this);

    if ((flags & MF_RECURSE) && st && st->isdir && files) {
      // Bump the files in the directory as well.
      foreach(files, string file) {
	file = canonic_path(Stdio.append_path(path, file));
	Monitor m2 = monitors[file];
	if (m2) {
	  m2->bump(flags, seconds);
	}
      }
    }
  }

  //! Calculate and set a suitable time for the next poll of this monitor.
  //!
  //! @param st
  //!   New stat for the monitor.
  //!
  //! This function is called by @[check()] to schedule the
  //! next check.
  protected void update(Stdio.Stat st)
  {
    int delta = max_dir_check_interval || global::max_dir_check_interval;
    this::st = st;

    if (st && !st->isdir) {
      delta *= file_interval_factor || global::file_interval_factor;
    }

    if (st) {
      //  Start with a delta proportional to the time since mtime/ctime,
      //  but bound this to the max setting. A stat in the future will be
      //  adjusted to the max interval.
      int mtime = max(st->mtime, st->ctime);
      int d = ((time(1) - mtime) >> 2);
      if (!initialized && (d >= 0)) {
	// Assume that mtime is reasonable at startup.
	last_change = mtime;
      }
      if ((d >= 0) && (d < delta)) delta = d;
    }
    if (last_change <= time(1)) {
      // Time until stable.
      int d = last_change + (stable_time || global::stable_time) - time(1);
      d >>= 1;
      if (d < 0) d = 1;
      if (d < delta) delta = d;
    }
    if (!initialized) {
      // Attempt to distribute polls evenly at startup, and to
      // make sure that the full set of directory contents is
      // found reasonably fast.
      delta = 1 + random(delta >> 2);
      initialized = true;
    }

    MON_WERR("Next poll in %d seconds.\n", (delta || 1));
    next_poll = time(1) + (delta || 1);
    adjust_monitor(this);
  }

  //! Check if this monitor should be removed automatically.
  void check_for_release(int mask, int flags)
  {
    if ((this::flags & mask) == flags) {
      MON_WERR("Releasing in %O->check_for_release(%O, %O)\n",
	       this, mask, flags);
      m_delete(monitors, path);
      release_monitor(this);
      if (this_program par = parent()) {
	par->submonitor_released (this);
      } else {
	MON_WERR("%O has no parent monitor.\n", this);
      }
    }
  }

  //! Called to create a sub monitor.
  protected void monitor(string path, int flags, int max_dir_interval,
			 int file_interval_factor, int stable_time)
  {
    global::monitor(path, flags, max_dir_check_interval,
		    file_interval_factor, stable_time);
  }

  //! Called when the status has changed for an existing file.
  protected int(0..1) status_change(Stdio.Stat old_st, Stdio.Stat st,
				    int orig_flags, int flags)
  {
    MON_WERR("status_change(%O, %O, 0x%04x, 0x%04x)\n",
	     old_st, st, orig_flags, flags);
    if (st->isdir) {
      int res = 0;
      array(string) files = get_dir(path) || ({});
      array(string) new_files = files;
      array(string) deleted_files = ({});
      if (this::files) {
	new_files -= this::files;
	deleted_files = this::files - files;
      }
      MON_WERR("%d files created, %d files deleted.\n",
	       sizeof(new_files), sizeof(deleted_files));
      this::files = files;
      foreach(new_files, string file) {
	res = 1;
	file = canonic_path(Stdio.append_path(path, file));
	if(filter_file(file)) continue;
	Monitor m2 = monitors[file];
	mixed err = catch {
	    if (m2) {
	      // We have a separate monitor on the created file.
	      // Let it handle the notification.
	      m2->check(flags);
	    }
	  };
	if (this::flags & MF_RECURSE) {
	  monitor(file, orig_flags | MF_AUTO | MF_HARD,
		  max_dir_check_interval,
		  file_interval_factor,
		  stable_time);
	  monitors[file]->check();
	} else if (!m2) {
	  file_created(file, file_stat(file, 1));
	}
	if (err) {
	  master()->handle_error(err);
	}
      }
      foreach(deleted_files, string file) {
	res = 1;
	file = canonic_path(Stdio.append_path(path, file));
	if(filter_file(file)) continue;
	Monitor m2 = monitors[file];
	mixed err = catch {
	    if (m2) {
	      // We have a separate monitor on the deleted file.
	      // Let it handle the notification.
	      m2->check(flags);
	    }
	  };
	if (this::flags & MF_RECURSE) {
	  // The monitor for the file has probably removed itself,
	  // or the user has done it by hand, in either case we
	  // don't need to do anything more here.
	} else if (!m2) {
	  file_deleted(file);
	}
	if (err) {
	  master()->handle_error(err);
	}
      }
      if (flags & MF_RECURSE) {
	// Check the remaining files in the directory soon.
	foreach(((files - new_files) - deleted_files), string file) {
	  file = canonic_path(Stdio.append_path(path, file));
	  if(filter_file(file)) continue;
	  Monitor m2 = monitors[file];
	  if (m2) {
	    m2->bump(flags);
	  } else {
	    // Lost update due to race-condition:
	    //
	    //   Exist ==> Deleted ==> Exists
	    //
	    // with no update of directory inbetween.
	    //
	    // Create the lost submonitor again.
	    res = 1;
	    monitor(file, orig_flags | MF_AUTO | MF_HARD,
		    max_dir_check_interval,
		    file_interval_factor,
		    stable_time);
	    monitors[file]->check();
	  }
	}
      }
      return res;
    } else {
      attr_changed(path, st);
      return 1;
    }
    return 0;
  }

  //! Check for changes.
  //!
  //! @param flags
  //!   @int
  //!     @value 0
  //!       Don't recurse.
  //!     @value 1
  //!       Check all monitors for the entire subtree rooted in @[m].
  //!   @endint
  //!
  //! This function is called by @[check()] for the @[Monitor]s
  //! it considers need checking. If it detects any changes an
  //! appropriate callback will be called.
  //!
  //! @returns
  //!   Returns @expr{1@} if a change was detected and @expr{0@} (zero)
  //!   otherwise.
  //!
  //! @note
  //!   Any callbacks will be called from the same thread as the one
  //!   calling @[check_monitor()].
  //!
  //! @note
  //!   The return value can not be trusted to return @expr{1@} for all
  //!   detected changes in recursive mode.
  //!
  //! @seealso
  //!   @[check()], @[data_changed()], @[attr_changed()], @[file_created()],
  //!   @[file_deleted()], @[stable_data_change()]
  int(0..1) check(MonitorFlags|void flags)
  {
    MON_WERR("Checking monitor %O...\n", this);
    Stdio.Stat st = file_stat(path, 1);
    Stdio.Stat old_st = this::st;
    int orig_flags = this::flags;
    this::flags |= MF_INITED;
    update(st);
    if (!(orig_flags & MF_INITED)) {
      // Initialize.
      if (st) {
	if (st->isdir) {
	  array(string) files = get_dir(path) || ({});
	  this::files = files;
	  foreach(files, string file) {
	    file = canonic_path(Stdio.append_path(path, file));
	    if(filter_file(file)) continue;
	    if (monitors[file]) {
	      // There's already a monitor for the file.
	      // Assume it has already notified about existance.
	      continue;
	    }
	    if (this::flags & MF_RECURSE) {
	      monitor(file, orig_flags | MF_AUTO | MF_HARD,
		      max_dir_check_interval,
		      file_interval_factor,
		      stable_time);
	      check_monitor(monitors[file]);
	    } else {
	      file_exists(file, file_stat(file, 1));
	    }
	  }
	}
	// Signal file_exists for path as an end marker.
	file_exists(path, st);
      } else  {
	// The path we're supposed to monitor is already gone.
	check_for_release(MF_AUTO, MF_AUTO);
      }
      return 1;
    }
    if (old_st) {
      if (!st || ((old_st->mode & S_IFMT) != (st->mode & S_IFMT))) {
	// File deleted or changed type.

	int delay;
	// Propagate deletions to any submonitors.
	if (files) {
	  foreach(files, string file) {
	    file = canonic_path(Stdio.append_path(path, file));
	    if (Monitor submon = monitors[file]) {
	      // Adjust next_poll, so that the monitor will be checked soon.
	      submon->next_poll = time(1)-1;
	      adjust_monitor(submon);
	      delay = 1;
	    }
	  }
	}
	if (delay) {
	  // Delay the notification until the submonitors have notified.
	  this::st = old_st;
	  next_poll = time(1);
	  adjust_monitor(this);
	} else {
	  if (st) {
	    // Avoid race when a file has been replaced with a directory
	    // or vice versa or similar.
	    st = UNDEFINED;

	    // We will catch the new file at the next poll.
	    next_poll = time(1);
	    adjust_monitor(this);
	  } else {
	    // The monitor no longer has a link from its parent directory.
	    this::flags &= ~MF_HARD;

	    // Check if we should remove the monitor.
	    check_for_release(MF_AUTO, MF_AUTO);
	  }

	  file_deleted(path, old_st);
	  return 1;
	}
	return 0;
      }
    } else if (st) {
      // File created.

      last_change = time(1);
      file_created(path, st);
      if (st->isdir) {
	array(string) files = get_dir(path) || ({});
	this::files = files;
	foreach(files, string file) {
	  file = canonic_path(Stdio.append_path(path, file));
	  if (filter_file(file)) continue;
	  if (monitors[file]) {
	    // There's already a monitor for the file.
	    // Assume it has already notified about existance.
	    continue;
	  }
	  if (this::flags & MF_RECURSE) {
	    monitor(file, orig_flags | MF_AUTO | MF_HARD,
		    max_dir_check_interval,
		    file_interval_factor,
		    stable_time);
	    check_monitor(monitors[file]);
	  } else {
	    file_created(file, file_stat(file, 1));
	  }
	}
      }
      update(st);
      return 1;
    } else {
      return 0;
    }

    //  Note: ctime seems to change unexpectedly when running ImageMagick
    //        on NFS disk so we disable it for the moment [bug 5587].
    if (last_change != -0x7fffffff &&
	((st->mtime != old_st->mtime) ||
	 /* (st->ctime != old_st->ctime) || */
	 (st->size != old_st->size))) {
      MON_WERR("Inode changed. New st: %O.\n", st);
      last_change = time(1);
      update(st);
      if (status_change(old_st, st, orig_flags, flags)) return 1;
    } else if (last_change < time(1) - (stable_time || global::stable_time)) {
      MON_WERR("Inode stable now.\n");
      last_change = 0x7fffffff;
      stable_data_change(path, st);
      return 1;
    } else if (last_change != 0x7fffffff &&
	       st->isdir && status_change(old_st, st, orig_flags, flags)) {
      MON_WERR("Directory not stable yet.\n");
      // Directory not stable yet.
      last_change = time(1);
      update(st);
      return 1;
    } else if (last_change != 0x7fffffff) {
      MON_WERR("Not stable yet. Age: %d seconds. Path: %O\n",
	       time(1) - last_change, path);
    } else {
      MON_WERR("Inode stable still.\n");
    }
    return 0;
  }

  protected void destroy(int cause)
  {
    // NB: Cause #0 == DESTRUCT_EXPLICIT.
    //     Any other cause and unregistering is irrelevant.
    unregister_path(1 + cause);
  }
}

//! @class DefaultMonitor
//! This symbol evaluates to the @[Monitor] class used by
//! the default implementation of @[monitor_factory()].
//!
//! It is currently one of the values @[Monitor], @[EventStreamMonitor]
//! or @[InotifyMonitor].
//!
//! @seealso
//!   @[monitor_factory()]

// NB: See further below for the actual definitions.

//! @decl inherit Monitor

//! @endclass

//
// Some necessary setup activities for systems that provide
// filesystem event monitoring
//
#if constant(System.FSEvents.EventStream)
#define HAVE_EVENTSTREAM 1
#endif

#if constant(System.Inotify)
#define HAVE_INOTIFY 1
#endif

#if HAVE_EVENTSTREAM
protected System.FSEvents.EventStream eventstream;
protected array(string) eventstream_paths = ({});

//! This function is called when the FSEvents EventStream detects a change
//! in one of the monitored directories.
protected void low_eventstream_callback(string path, int flags, int event_id)
{
  MON_WERR("eventstream_callback(%O, 0x%08x, %O)\n", path, flags, event_id);
  if(path[-1] == '/') path = path[0..<1];
  MON_WERR("Normalized path: %O\n", path);

  int monitor_flags;
  if (flags & System.FSEvents.kFSEventStreamEventFlagMustScanSubDirs)
    monitor_flags |= MF_RECURSE;

  int found;
  string checkpath = path;
  for (int i = 0; i <= 1; i++) {
    MON_WERR("Looking up monitor for path %O.\n", checkpath);
    if(Monitor m = monitors[checkpath]) {
      MON_WERR("Found monitor %O for path %O.\n", m, checkpath);
      m->check(monitor_flags);
      found = 1;
      break;
    }
    checkpath = dirname (checkpath);
  }

  if (!found)
    MON_WARN("No monitor found for path %O.\n", path);
}

protected void eventstream_callback(string path, int flags, int event_id)
{
  if (backend)
    backend->call_out(low_eventstream_callback, 0, path, flags, event_id);
  else
    low_eventstream_callback(path, flags, event_id);
}

protected void start_accelerator()
{
  // Make sure that the main backend is in CF-mode.
  Pike.DefaultBackend.enable_core_foundation(1);

  MON_WERR("Creating event stream.\n");
#if constant (System.FSEvents.kFSEventStreamCreateFlagFileEvents)
  int flags = System.FSEvents.kFSEventStreamCreateFlagFileEvents;
#else
  int flags = System.FSEvents.kFSEventStreamCreateFlagNone;
#endif

  eventstream =
    System.FSEvents.EventStream(({}), 1.0,
				System.FSEvents.kFSEventStreamEventIdSinceNow,
				flags);
  eventstream->callback_func = eventstream_callback;
}

//! FSEvents EventStream-accelerated @[Monitor].
protected class EventStreamMonitor
{
  inherit Monitor;

  protected void register_path(int|void initial)
  {
#ifndef INHIBIT_EVENTSTREAM_MONITOR
    if (initial) {
      if (Pike.DefaultBackend.executing_thread() != Thread.this_thread()) {
	// eventstream stuff (especially start()) must be called from
	// the backend thread, otherwise events will be fired in
	// CFRunLoop contexts where noone listens.
	MON_WERR("%O: Switching to backend thread.\n", this_function);
	call_out(register_path, 0, initial);
	return;
      }

      // We're now in the main backend.

      if (!eventstream) {
	start_accelerator();
      }

      string found;
      foreach(eventstream_paths;;string p) {
	if((path == p) || has_prefix(path, p + "/")) {
	  MON_WERR("Path %O already monitored via path %O.\n", path, p);
	  found = p;
	  break;
	}
      }
      if (found) {
	MON_WERR("Path %O is accelerated via %O.\n", path, found);
      } else {
	// NB: Eventstream doesn't notify on the monitored path;
	//     only on its contents.
	mixed err = catch {
	    MON_WERR("Adding %O to the set of monitored paths.\n", path);
	    eventstream_paths += ({path});
	    if(eventstream->is_started())
	      eventstream->stop();
	    eventstream->add_path(path);
	    eventstream->start();
	  };

	if (err) {
	  MON_ERROR("%O: Failed to register path %O.\n", this_function, path);
	  master()->handle_error(err);
	}
      }
      // Note: Falling through to ::register_path() below.
      //       This is needed to handle paths mounted on eg network
      //       filesystems that are modified on other machines.
    }
#endif /* !INHIBIT_EVENTSTREAM_MONITOR */
    ::register_path(initial);
  }
}

constant DefaultMonitor = EventStreamMonitor;

#elseif HAVE_INOTIFY

protected System.Inotify._Instance instance;

protected string(8bit) inotify_cookie(int wd)
{
  // NB: Prefix with a NUL to make sure not to conflict with real paths.
  return sprintf("\0%8c", wd);
}

//! Event callback for Inotify.
protected void inotify_event(int wd, int event, int cookie, string(8bit) path)
{
  MON_WERR("inotify_event(%O, %s, %O, %O)...\n",
	   wd, System.Inotify.describe_mask(event), cookie, path);
  string(8bit) icookie = inotify_cookie(wd);
  Monitor m;
  if((m = monitors[icookie])) {
    if (sizeof (path)) {
      string full_path = canonic_path(Stdio.append_path(m->path, path));
      // We're interested in the sub monitor, if it exists.
      if (Monitor submon = monitors[full_path]) {
	MON_WERR ("inotify_event: Got submonitor %O.\n", submon);

	m = submon;
      } else {
	MON_WERR ("inotify_event: Forcing check of %O.\n", m);
	// No monitor exists for the path yet (typically happens for
	// IN_CREATE events). Force a check on the directory monitor.
	m->check(m->flags);

	// Try again after directory check.
	if (Monitor submon2 = monitors[full_path]) {
	  MON_WERR ("inotify_event: Got submonitor %O on retry.\n", submon2);
	  m = submon2;
	} else {
	  MON_WERR ("inotify_event: Failed to get monitor for file %s "
		    "in %O.\n", path, m);
	}
      }
    }
  }

  if (m) {
    if (event == System.Inotify.IN_IGNORED) {
      // This Inotify watch has been removed
      // (either by us or automatically).
      MON_WERR("### Monitor watch descriptor %d is no more.\n", wd);
      m_delete(monitors, icookie);
    }
    mixed err = catch {
	if (event & System.Inotify.IN_CLOSE_WRITE)
	  // File marked as stable immediately.
	  m->last_change = -0x7fffffff;
	m->check(0);
      };
    if (err) {
      master()->handle_error(err);
    }
  } else {
    // Most likely not reached.
    MON_WARN("Monitor not found for cookie %O, path %O.\n", icookie, path);
  }
}

protected void start_accelerator()
{
  MON_WERR("Creating Inotify monitor instance.\n");
  instance = System.Inotify._Instance();
  if (backend) instance->set_backend(backend);
  instance->set_event_callback(inotify_event);
  if (co_id) {
    MON_WERR("Turning on nonblocking mode for Inotify.\n");
    instance->set_nonblocking();
  }
}

//! Inotify-accelerated @[Monitor].
protected class InotifyMonitor
{
  inherit Monitor;

  protected int wd = -1;
  protected int(0..) out_of_inotify_space;

  protected void register_path(int|void initial)
  {
#ifndef INHIBIT_INOTIFY_MONITOR
    if (wd == -1) {
      if (!instance) {
	start_accelerator();
      }

      // NB: We need to follow symlinks here.
      // Currently we only support changing symlinks and symlinks to directories.
      // FIXME: Handle broken symlinks where the target later shows up and
      //        symlinks to changing files.
      Stdio.Stat st = file_stat(path, 1);
      mixed err;
      if (st && (!(flags & MF_AUTO) || st->isdir)) {
	// Note: We only want to add watchers on directories. File
	// notifications will take place on the directory watch
	// descriptors. Expansion of the path to cover notifications
	// on individual files is handled in the inotify_event
	// callback.

	if (err = catch {
	    int new_wd = instance->add_watch(path,
					     System.Inotify.IN_MOVED_FROM |
					     System.Inotify.IN_UNMOUNT |
					     System.Inotify.IN_MOVED_TO |
					     System.Inotify.IN_MASK_ADD |
					     System.Inotify.IN_MOVE_SELF |
					     System.Inotify.IN_DELETE |
					     System.Inotify.IN_MOVE |
					     System.Inotify.IN_MODIFY |
					     System.Inotify.IN_ATTRIB |
					     System.Inotify.IN_DELETE_SELF |
					     System.Inotify.IN_CREATE |
					     System.Inotify.IN_CLOSE_WRITE);

	    if (new_wd != -1) {
	      MON_WERR("Registered %O with %O ==> %d.\n", path, instance, new_wd);
	      out_of_inotify_space = 0;
	      wd = new_wd;
	      monitors[inotify_cookie(wd)] = this;
	    }
	  }) {
	  if (!has_value(lower_case(describe_error(err)), "no space left")) {
	    master()->handle_error(err);
	  } else if (!(out_of_inotify_space++ % 100)) {
	    werror("%O: Out of inotify space (%d attempts):\n",
		   this_function, out_of_inotify_space);
	    master()->handle_error(err);
	    werror("Consider increasing '/proc/sys/fs/inotify/max_user_watches'.\n");
	  }
	}
      }
    }

#endif /* !INHIBIT_INOTIFY_MONITOR */
    ::register_path(initial);
  }

  protected void unregister_path(int|void dying)
  {
    if (wd != -1) {
      // NB: instance may be null if the main object has been destructed
      //     and we've been called via a destroy().
      if (instance && dying) {
	MON_WERR("### Unregistering from inotify.\n");
	// NB: Inotify automatically removes watches for deleted files,
	//     and will complain if we attempt to remove them too.
	//
	//     Since we have no idea if there's already a queued ID_IGNORED
	//     pending we just throw away any error here.
	mixed err = catch {
	    instance->rm_watch(wd);
	  };
	if (err) {
	  MON_WARN("### Failed to unregister %O: %s\n",
		   path, describe_backtrace(err));
	}
      }
      wd = -1;
    }
    ::unregister_path(dying);
  }
}

constant DefaultMonitor = InotifyMonitor;

#else

constant DefaultMonitor = Monitor;

#endif /* HAVE_EVENTSTREAM || HAVE_INOTIFY */

//! Canonicalize a path.
//!
//! @param path
//!   Path to canonicalize.
//!
//! @returns
//!   The default implementation returns @expr{combine_path(path, ".")@},
//!   i.e. no trailing slashes.
protected string canonic_path(string path)
{
#if HAVE_EVENTSTREAM
  if (!backend) {
    catch {
      path = System.resolvepath(path);
    };
  }
#endif
  return combine_path(path, ".");
}

//! Mapping from monitored path to corresponding @[Monitor].
//!
//! The paths are normalized to @expr{canonic_path(path)@},
//!
//! @note
//!   All filesystems are handled as if case-sensitive. This should
//!   not be a problem for case-insensitive filesystems as long as
//!   case is maintained.
protected mapping(string:Monitor) monitors = ([]);

//! Heap containing active @[Monitor]s that need polling.
//!
//! The heap is sorted on @[Monitor()->next_poll].
protected ADT.Heap monitor_queue = ADT.Heap();

//! Mutex controlling access to @[monitor_queue].
protected Thread.Mutex monitor_mutex = Thread.Mutex();

//! Create a new monitor.
//!
//! @param max_dir_check_interval
//!   Override of @[default_max_dir_check_interval].
//!
//! @param file_interval_factor
//!   Override of @[default_file_interval_factor].
//!
//! @param stable_time
//!   Override of @[default_stable_time].
protected void create(int|void max_dir_check_interval,
		      int|void file_interval_factor,
		      int|void stable_time)
{
  if (max_dir_check_interval > 0) {
    this::max_dir_check_interval = max_dir_check_interval;
  }
  if (file_interval_factor > 0) {
    this::file_interval_factor = file_interval_factor;
  }
  if (stable_time > 0) {
    this::stable_time = stable_time;
  }
  clear();
}

protected void destroy()
{
  // Destruct monitors before we're destructed ourselves, since they
  // will attempt to unregister with us.
  foreach (monitors;; Monitor m)
    destruct (m);
}

//! Clear the set of monitored files and directories.
//!
//! @note
//!   Due to circular datastructures, it's recomended
//!   to call this function prior to discarding the object.
void clear()
{
  mixed key = monitor_mutex->lock();
  monitors = ([]);
  monitor_queue = ADT.Heap();
#if HAVE_EVENTSTREAM
  eventstream = 0;
#elseif HAVE_INOTIFY
  instance = 0;
#endif
}

//! Release a single @[Monitor] from monitoring.
//!
//! @seealso
//!   @[release()]
protected void release_monitor(Monitor m)
{
  mixed key = monitor_mutex->lock();
  monitor_queue->remove(m);
}

//! Update the position in the @[monitor_queue] for the monitor @[m]
//! to account for an updated next_poll value.
protected void adjust_monitor(Monitor m)
{
  // NB: May be called with monitors not on the monitor_queue due
  //     to double checks when using acceleration.
  if (m->pos < 0) return;	// Not on the monitor_queue.

  mixed key = monitor_mutex->lock();
  if (m->pos < 0) return;	// Not on the monitor_queue any more (race).

  monitor_queue->adjust(m);
  if (monitor_queue->peek() != m) {
    return;
  }

  if (co_id) {
    // Nonblocking mode and we need to poll earlier,
    // so reschedule the call_out.
    reschedule_backend_check();
  }
}

//! Create a new @[Monitor] for a @[path].
//!
//! This function is called by @[monitor()] to create a new @[Monitor]
//! object.
//!
//! The default implementation just calls @[DefaultMonitor] with the
//! same arguments.
//!
//! @seealso
//!   @[monitor()], @[DefaultMonitor]
protected DefaultMonitor monitor_factory(string path, MonitorFlags|void flags,
					 int(0..)|void max_dir_check_interval,
					 int(0..)|void file_interval_factor,
					 int(0..)|void stable_time)
{
  return DefaultMonitor(path, flags, max_dir_check_interval,
			file_interval_factor, stable_time);
}


//! Register a @[path] for monitoring.
//!
//! @param path
//!   Path to monitor.
//!
//! @param flags
//!   @int
//!     @value 0
//!       Don't recurse.
//!     @value 1
//!       Monitor the entire subtree, and any directories
//!       or files that may appear later.
//!     @value 3
//!       Monitor the entire subtree, and any directories
//!       or files that may appear later. Remove the monitor
//!       automatically when @[path] is deleted.
//!   @endint
//!
//! @param max_dir_check_interval
//!   Override of @[default_max_dir_check_interval] for this path
//!   or subtree.
//!
//! @param file_interval_factor
//!   Override of @[default_file_interval_factor] for this path
//!   or subtree.
//!
//! @param stable_time
//!   Override of @[default_stable_time] for this path
//!   or subtree.
//!
//! @seealso
//!   @[release()]
Monitor|void monitor(string path, MonitorFlags|void flags,
	     int(0..)|void max_dir_check_interval,
	     int(0..)|void file_interval_factor,
	     int(0..)|void stable_time)
{
  path = canonic_path(path);
  if(filter_file(path)) return;
  Monitor m = monitors[path];
  if (m) {
    if (!(flags & MF_AUTO)) {
      // The new monitor is added by hand.
      // Adjust the monitor.
      m->flags = flags;
      m->max_dir_check_interval = max_dir_check_interval;
      m->file_interval_factor = file_interval_factor;
      m->stable_time = stable_time;
      m->next_poll = 0;
      adjust_monitor(m);
    }
    if (flags & MF_HARD) {
      m->flags |= MF_HARD;
    }
    // For the other cases there's no need to do anything,
    // since we can keep the monitor as-is.
  } else {
    m = monitor_factory(path, flags, max_dir_check_interval,
			file_interval_factor, stable_time);
    monitors[path] = m;
    // NB: Registering with the monitor_queue is done as
    //     needed by register_path() as called by create().
  }
  return m;
}

int filter_file(string path)
{
  array x = path/"/";
  foreach(x;; string pc)
    if(pc && strlen(pc) && pc[0]=='.' && pc != "..") {
      MON_WERR("skipping %O\n", path);
      return 1;
    }

  return 0;
}

//! Release a @[path] from monitoring.
//!
//! @param path
//!   Path to stop monitoring.
//!
//! @param flags
//!   @int
//!     @value 0
//!       Don't recurse.
//!     @value 1
//!       Release the entire subtree.
//!     @value 3
//!       Release the entire subtree, but only those paths that were
//!       added automatically by a recursive monitor.
//!   @endint
//!
//! @seealso
//!   @[monitor()]
void release(string path, MonitorFlags|void flags)
{
  path = canonic_path(path);
  Monitor m = m_delete(monitors, path);
  if (!m) return;

  release_monitor(m);
  if (flags && m->st && m->st->isdir) {
    if (!sizeof(path) || path[-1] != '/') {
      path += "/";
    }
    foreach(monitors; string mpath; m) {
      if (has_prefix(mpath, path)) {
	m->check_for_release(flags, flags);
      }
    }
  }
}

//! Check whether a path is monitored or not.
//!
//! @param path
//!   Path to check.
//!
//! @returns
//!   Returns @expr{1@} if there is a monitor on @[path],
//!   and @expr{0@} (zero) otherwise.
//!
//! @seealso
//!   @[monitor()], @[release()]
int(0..1) is_monitored(string path)
{
  return !!monitors[canonic_path(path)];
}

//! Check a single @[Monitor] for changes.
//!
//! @param m
//!   @[Monitor] to check.
//!
//! @param flags
//!   @int
//!     @value 0
//!       Don't recurse.
//!     @value 1
//!       Check all monitors for the entire subtree rooted in @[m].
//!   @endint
//!
//! This function is called by @[check()] for the @[Monitor]s
//! it considers need checking. If it detects any changes an
//! appropriate callback will be called.
//!
//! @returns
//!   Returns @expr{1@} if a change was detected and @expr{0@} (zero)
//!   otherwise.
//!
//! @note
//!   Any callbacks will be called from the same thread as the one
//!   calling @[check_monitor()].
//!
//! @note
//!   The return value can not be trusted to return @expr{1@} for all
//!   detected changes in recursive mode.
//!
//! @seealso
//!   @[check()], @[data_changed()], @[attr_changed()], @[file_created()],
//!   @[file_deleted()], @[stable_data_change()]
protected int(0..1) check_monitor(Monitor m, MonitorFlags|void flags)
{
  return m && m->check(flags);
}

//! Check all monitors for changes.
//!
//! @param ret_stats
//!   Optional mapping that will be filled with statistics (see below).
//!
//! All monitored paths will be checked for changes.
//!
//! @note
//!   You typically don't want to call this function, but instead
//!   @[check()].
//!
//! @note
//!   Any callbacks will be called from the same thread as the one
//!   calling @[check()].
//!
//! @seealso
//!   @[check()], @[monitor()]
void check_all(mapping(string:int)|void ret_stats)
{
  int cnt;
  int scanned_cnt;
  foreach(monitors; string path; Monitor m) {
    scanned_cnt++;
    mixed err = catch {
	cnt += check_monitor(m);
      };
    if (err) {
      master()->handle_error(err);
    }
  }
  if (ret_stats) {
    ret_stats->num_monitors = sizeof(monitors);
    ret_stats->scanned_monitors = scanned_cnt;
    ret_stats->updated_monitors = cnt;
    ret_stats->idle_time = 0;
  }
}

//! Check for changes.
//!
//! @param max_wait
//!   Maximum time in seconds to wait for changes. @expr{-1@}
//!   for infinite wait.
//!
//! @param max_cnt
//!   Maximum number of paths to check in this call. @expr{0@}
//!   (zero) for unlimited.
//!
//! @param ret_stats
//!   Optional mapping that will be filled with statistics (see below).
//!
//! A suitable subset of the monitored files will be checked
//! for changes.
//!
//! @returns
//!   The function returns when either a change has been detected
//!   or when @[max_wait] has expired. The returned value indicates
//!   the number of seconds until the next call of @[check()].
//!
//!   If @[ret_stats] has been provided, it will be filled with
//!   the following entries:
//!   @mapping
//!     @member int "num_monitors"
//!       The total number of active monitors when the scan completed.
//!     @member int "scanned_monitors"
//!       The number of monitors that were scanned for updates during the call.
//!     @member int "updated_monitors"
//!       The number of monitors that were updated during the call.
//!     @member int "idle_time"
//!       The number of seconds that the call slept.
//!   @endmapping
//!
//! @note
//!   Any callbacks will be called from the same thread as the one
//!   calling @[check()].
//!
//! @seealso
//!   @[check_all()], @[monitor()]
int check(int|void max_wait, int|void max_cnt,
	  mapping(string:int)|void ret_stats)
{
#if HAVE_INOTIFY
  if (instance) {
    /* FIXME: No statistics currently available. */
    instance->poll();
  }
#endif
  int scan_cnt = max_cnt;
  int scan_wait = max_wait;
  while(1) {
    int ret = max_dir_check_interval;
    int cnt;
    int t = time();
    if (sizeof(monitor_queue)) {
      // NB: peek() can apparently in some circumstances throw errors.
      //     cf [bug 7644]. The likely cause being that a different
      //     thread removed the last element during the call. Make
      //     sure not to propagate the error to the caller.
      mixed err = catch {
	  Monitor m;
	  while ((m = monitor_queue->peek()) && (m->next_poll <= t)) {
	    cnt += check_monitor(m);
	    if (!(--scan_cnt)) {
	      m = monitor_queue->peek();
	      break;
	    }
	  }
	  if (m) {
	    ret = m->next_poll - t;
	    if (ret <= 0) ret = 1;
	  } else {
	    scan_cnt--;
	  }
	};
      if (err) {
	master()->handle_error(err);
      }
    }
    if (cnt || !scan_wait || !scan_cnt) {
      if (ret_stats) {
	ret_stats->num_monitors = sizeof(monitors);
	ret_stats->scanned_monitors = max_cnt - scan_cnt;
	ret_stats->updated_monitors = cnt;
	ret_stats->idle_time = max_wait - scan_wait;
      }
      return ret;
    }
    if (ret < scan_wait) {
      scan_wait -= ret;
      sleep(ret);
    } else {
      if (scan_wait > 0) scan_wait--;
      sleep(1);
    }
  }
}

//! Backend to use.
//!
//! If @expr{0@} (zero) - use the default backend.
protected Pike.Backend backend;

//! Call-out identifier for @[backend_check()] if in
//! nonblocking mode.
//!
//! @seealso
//!   @[set_nonblocking()], @[set_blocking()]
protected mixed co_id;

//! Change backend.
//!
//! @param backend
//!   Backend to use. @expr{0@} (zero) for the default backend.
void set_backend(Pike.Backend|void backend)
{
  int was_nonblocking = !!co_id;
  set_blocking();
  this::backend = backend;
#if HAVE_EVENTSTREAM
#elif HAVE_INOTIFY
  if (instance) {
    instance->set_backend(backend || Pike.DefaultBackend);
  }
#endif
  if (was_nonblocking) {
    set_nonblocking();
  }
}

//! Turn off nonblocking mode.
//!
//! @seealso
//!   @[set_nonblocking()]
void set_blocking()
{
  if (co_id) {
    if (backend) backend->remove_call_out(co_id);
    else remove_call_out(co_id);
    co_id = 0;
  }
#if HAVE_INOTIFY
  if (instance) {
    instance->set_blocking();
  }
#endif
}

//! Backend check callback function.
//!
//! This function is intended to be called from a backend,
//! and performs a @[check()] followed by rescheduling
//! itself via a call to @[set_nonblocking()].
//!
//! @seealso
//!   @[check()], @[set_nonblocking()]
protected void backend_check()
{
  co_id = 0;

  int t;
  mixed err = catch {
      t = check(0);
    };
  set_nonblocking(t);
  if (err) throw(err);
}

//! Reschedule beckend check.
//!
//! @param suggested_t
//!   Suggested time in seconds until next call of @[check()].
//!
//! Register suitable callbacks with the backend to automatically
//! call @[check()].
//!
//! @[check()] and thus all the callbacks will be called from the
//! backend thread.
protected void reschedule_backend_check(int|void suggested_t)
{
  // NB: Other stuff than plain files may be used with the monitoring
  //     system, so the call_out may be needed even with accelerators.
  //
  // NB: Also note that Inotify doesn't support monitoring of non-existing
  //     paths, so it still needs the call_out-loop.
  MON_WERR("Rescheduling call_out.\n");

  int t = max_dir_check_interval;
  if (sizeof(monitor_queue)) {
    Monitor m = monitor_queue->peek();
    if (m) {
      t = m->next_poll - time(1);
    }
    if (t > max_dir_check_interval) t = max_dir_check_interval;
  }
  if (!undefinedp(suggested_t) && (suggested_t < t)) {
    t = suggested_t;
  }
  if (t < 0) t = 0;

  if (co_id) {
    if (backend) backend->remove_call_out(co_id);
    else remove_call_out(co_id);
  }
  if (backend) co_id = backend->call_out(backend_check, t);
  else co_id = call_out(backend_check, t);
}

//! Turn on nonblocking mode.
//!
//! @param suggested_t
//!   Suggested time in seconds until next call of @[check()].
//!
//! Register suitable callbacks with the backend to automatically
//! call @[check()].
//!
//! @[check()] and thus all the callbacks will be called from the
//! backend thread.
//!
//! @note
//!   If nonblocking mode is already active, this function will
//!   be a noop.
//!
//! @seealso
//!   @[set_blocking()], @[check()].
void set_nonblocking(int|void suggested_t)
{
#if HAVE_INOTIFY
  if (instance) {
    instance->set_nonblocking();
  }
#endif
  if (co_id) return;
  reschedule_backend_check(suggested_t);
}

//! Set the @[max_dir_check_interval].
void set_max_dir_check_interval(int max_dir_check_interval)
{
  if (max_dir_check_interval > 0) {
    this::max_dir_check_interval = max_dir_check_interval;
  } else {
    this::max_dir_check_interval = default_max_dir_check_interval;
  }
}

//! Set the @[file_interval_factor].
void set_file_interval_factor(int file_interval_factor)
{
  if (file_interval_factor > 0) {
    this::file_interval_factor = file_interval_factor;
  } else {
    this::file_interval_factor = default_file_interval_factor;
  }
}

//! Set the @[stable_time].
void set_stable_time (int stable_time)
{
  if (stable_time > 0) {
    this::stable_time = stable_time;
  } else {
    this::stable_time = default_stable_time;
  }
}