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
  
#pike __REAL_VERSION__ 
 
// 
// Filesystem monitor with support for symbolic links. 
// 
// 
// 2010-01-25 Henrik Grubbström 
// 
//! Filesystem monitor with support for symbolic links. 
//! 
//! This module extends @[Filesystem.Monitor.basic] with 
//! support for symbolic links. 
//! 
//! @note 
//!   For operating systems where symbolic links aren't supported, 
//!   this module will behave exactly like @[Filesystem.Monitor.basic]. 
//! 
//! @seealso 
//!   @[Filesystem.Monitor.basic] 
 
//! @decl inherit Filesystem.Monitor.basic 
inherit "basic.pike" : basic; 
 
#define MON_WERR(X...)        report(NOTICE,  __func__, X) 
#define MON_WARN(X...)      report(WARNING, __func__, X) 
#define MON_ERROR(X...)     report(ERROR,   __func__, X) 
 
#if constant(readlink) 
 
//! @decl void attr_changed(string path, Stdio.Stat st) 
//! 
//! 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)@}. 
//! 
//! 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. 
//! 
//! @note 
//!   It differs from the @[Filesystem.Monitor.basic] version in that 
//!   symbolic links have the @[st] of their targets. 
//! 
//! Overload this to do something useful. 
 
//! @decl void file_exists(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)@}. 
//! 
//! 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. 
//! 
//! @note 
//!   It differs from the @[Filesystem.Monitor.basic] version in that 
//!   symbolic links have the @[st] of their targets. 
//! 
//! 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. 
 
//! @decl void file_created(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)@}. 
//! 
//! 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. 
//! 
//! @note 
//!   It differs from the @[Filesystem.Monitor.basic] version in that 
//!   symbolic links have the @[st] of their targets. 
//! 
//! Called by @[check()] and @[check_monitor()]. 
//! 
//! Overload this to do something useful. 
 
//! @decl void stable_data_change(string path, Stdio.Stat st) 
//! 
//! 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)@}. 
//! 
//! 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. 
//! 
//! @note 
//!   It differs from the @[Filesystem.Monitor.basic] version in that 
//!   symbolic links have the @[st] of their targets. 
//! 
//! Called by @[check()] and @[check_monitor()]. 
//! 
//! Overload this to do something useful. 
 
//! Monitoring information for a single filesystem path. 
//! 
//! With support for expansion of symbolic links. 
//! 
//! @seealso 
//!   @[monitor()] 
protected class DefaultMonitor 
{ 
  //! Based on @[Filesystem.Monitor.basic.DefaultMonitor]. 
  inherit basic::DefaultMonitor; 
 
  //! Mask of symlink ids that can reach this monitor. 
  int symlinks; 
 
  //! Call a notification callback and handle symlink expansion. 
  //! 
  //! @param cb 
  //!   Callback to call or @[UNDEFINED] for no operation. 
  //! 
  //! @param state 
  //!   State mapping to avoid multiple notification and infinite loops. 
  //!   Call with an empty mapping. 
  //! 
  //! @param symlinks 
  //!   Symlinks that have not been expanded yet. 
  //! 
  //! @param path 
  //!   Path to notify on. 
  //! 
  //! @param extras 
  //!   Extra arguments to @[cb]. 
  //! 
  //! @param symlink 
  //!   Symbolic link that must have been followed for the 
  //!   callback to be called. 
  void low_call_callback(function(string, mixed ...:void) cb, 
                         mapping(string:int) state, 
                         mapping(string:string) symlink_targets, 
                         string path, Stdio.Stat|void st, 
                         string|void symlink) 
  { 
    if (!cb || state[path] || (st && st->islnk)) return; 
    state[path] = 1; 
 
    MON_WERR("%O: path: %O, cb: %O, symlink_targets: %O\n", 
             this_function, path, cb, symlink_targets); 
 
    if (!symlink || !symlink_targets[symlink]) { 
      cb(path, st); 
    } 
    if (sizeof(symlink_targets)) { 
      // Check the list of symlink targets. 
      foreach(reverse(sort(indices(symlink_targets))), string src) { 
        string dest = symlink_targets[src]; 
        if (has_prefix(path, dest)) { 
          low_call_callback(cb, state, symlink_targets - ([ src : dest ]), 
                            src + path[sizeof(dest)..], st, symlink); 
        } 
      } 
    } 
  } 
 
  //! Call a notification callback and handle symlink expansion. 
  //! 
  //! @param cb 
  //!   Callback to call or @[UNDEFINED] for no operation. 
  //! 
  //! @param extras 
  //!   Extra arguments after the @[path] argument to @[cb]. 
  protected void call_callback(function(string, mixed ...:void) cb, 
                               string path, Stdio.Stat|void st) 
  { 
    if (!cb) return; 
    low_call_callback(cb, ([]), global::symlink_targets, path, st); 
  } 
 
  protected void notify_symlink(function(string, mixed ...:void) cb, 
                                string sym) 
  { 
    int sym_id = global::symlink_ids[sym]; 
    if (sym_id) { 
      // Depth-first. 
      foreach(reverse(sort(filter(values(monitors), 
                                  lambda(Monitor m, int sym_id) { 
                                    return m->symlinks & sym_id; 
                                  }, sym_id)->path)), 
              string m_path) { 
        Monitor m = monitors[m_path]; 
        m->low_call_callback(cb, ([]), global::symlink_targets, 
                             m_path, m->st, sym); 
      } 
    } 
  } 
 
  //! Called when the symlink @[path] is no more. 
  protected void zap_symlink(string path) 
  { 
    MON_WERR("%O(%O)\n", this_function, path); 
 
    string old_dest = global::symlink_targets[path]; 
 
    if (old_dest) { 
      int sym_id = global::symlink_ids[path]; 
      foreach(monitors; string m_path; Monitor m) { 
        if (m->symlinks & sym_id) { 
          MON_WERR("Found monitor %O reached through %O.\n", m, path); 
          m->low_call_callback(global::file_deleted, ([]), 
                               global::symlink_targets, 
                               m_path, UNDEFINED, path); 
          m->symlinks -= sym_id; 
          // Unregister the monitor if it is the last ref, 
          // and there are no hard links to the file. 
          m->check_for_release(MF_AUTO|MF_HARD, MF_AUTO); 
        } 
      } 
      global::available_ids |= m_delete(symlink_ids, path); 
      m_delete(global::symlink_targets, path); 
    } 
  } 
 
  //! Check whether a symlink has changed. 
  protected void check_symlink(string path, Stdio.Stat st, 
                               int|void inhibit_notify) 
  { 
    MON_WERR("%O(%O, %O, %O)...\n", this_function, path, st, inhibit_notify); 
 
    string dest; 
    if (st && st->islnk) { 
      dest = readlink(path); 
      if (dest) { 
        dest = canonic_path(combine_path(path, "..", dest)); 
        if (symlink_targets[path] == dest) return; 
      } 
    } 
    if (symlink_targets[path]) { 
      zap_symlink(path); 
    } 
    if (dest) { 
      // We have a new symbolic link. 
      MON_WERR("Found new symlink %O ==> %O.\n", path, dest); 
      symlink_targets[path] = dest; 
      int sym_id = allocate_symlink(path); 
      int sym_mask = sym_id | symlink_ids[dest]; 
      int sym_done = sym_id; 
      Monitor m; 
      if (!(m = monitors[dest])) { 
        MonitorFlags m_flags = (flags & ~MF_HARD) | MF_AUTO; 
        if (inhibit_notify) { 
          m_flags &= ~MF_INITED; 
        } 
        monitor(dest, m_flags, 
                max_dir_check_interval, 
                file_interval_factor, 
                stable_time); 
        m = monitors[dest]; 
      } 
      m->symlinks |= sym_id; 
      if (!has_suffix(dest, "/")) { 
        dest += "/"; 
      } 
      foreach(monitors; string mm_path; Monitor mm) { 
        if (has_prefix(mm_path, dest)) { 
          mm->symlinks |= sym_id; 
          sym_mask |= symlink_ids[mm_path]; 
        } 
      } 
      // Follow any found symlinks. 
      while (sym_mask != sym_done) { 
        int mask = sym_mask - sym_done; 
        foreach(monitors; string mm_path; Monitor mm) { 
          if ((mm->symlinks & mask) && !(mm->symlinks & sym_id)) { 
            mm->symlinks |= sym_id; 
            sym_mask |= symlink_ids[mm_path]; 
          } 
        } 
        sym_done |= mask; 
      } 
      if (!inhibit_notify) { 
        notify_symlink(global::file_created, path); 
      } 
    } 
  } 
 
  //! 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) 
  { 
    check_symlink(path, st); 
    if (st && st->islnk) { 
      return; 
    } 
    ::attr_changed(path, st); 
  } 
 
  protected void low_file_exists(string path, Stdio.Stat st) 
  { 
    // Note: May be called for symlink targets before they have 
    //       initialized properly, in which case st will be 0. 
    if (!st || !global::file_exists) return; 
    global::file_exists(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) 
  { 
    check_symlink(path, st, 1); 
    if (st && st->islnk) { 
      notify_symlink(low_file_exists, path); 
      return; 
    } 
    ::file_exists(path, st); 
  } 
 
  //! 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) 
  { 
    check_symlink(path, st); 
    if (st && st->islnk) { 
      // Note: check_symlink() above has already called the file_created() 
      // callback for us. 
      return; 
    } 
    ::file_created(path, 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()]. 
  protected void file_deleted(string path, Stdio.Stat old_st) 
  { 
    check_symlink(path, UNDEFINED); 
    if (old_st && old_st->islnk) { 
      return; 
    } 
    ::file_deleted(path, old_st); 
  } 
 
  //! 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) 
  { 
    if (st && st->islnk) { 
      int sym_id = global::symlink_ids[path]; 
      if (sym_id) { 
        foreach(monitors; string m_path; Monitor m) { 
          if ((m->symlinks & sym_id) && (m->last_change == 0x7fffffff)) { 
            m->low_call_callback(global::stable_data_change, ([]), 
                                 global::symlink_targets, m_path, m->st, path); 
          } 
        } 
      } 
      return; 
    } 
    ::stable_data_change(path, st); 
  } 
 
  //! Check if this monitor should be removed automatically. 
  void check_for_release(int mask, int flags) 
  { 
    if (symlinks) { 
      // We need to check if this is the direct target of a symlink. 
      foreach(symlink_targets;; string dest) { 
        if (path == dest) { 
          // The monitor still has a symlink pointing to it. 
          return; 
        } 
      } 
    } 
    ::check_for_release(mask, flags); 
  } 
 
  //! Called to create a sub monitor. 
  protected void monitor(string path, int flags, int max_dir_interval, 
                         int file_interval_factor, int stable_time) 
  { 
    object m; 
    ::monitor(path, flags, max_dir_check_interval, 
              file_interval_factor, stable_time); 
    if((m = monitors[path])) m->symlinks |= symlinks; 
  } 
 
  //! 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) 
  { 
    check_symlink(path, st); 
    if (st && st->islnk) { 
      return 1; 
    } 
    return ::status_change(old_st, st, orig_flags, flags); 
  } 
 
} 
 
#endif /* constant(readlink) */ 
 
//! Mapping from symlink name to symlink target. 
protected mapping(string:string) symlink_targets = ([]); 
 
//! Mapping from symlink name to symlink id. 
protected mapping(string:int) symlink_ids = ([]); 
 
//! Bitmask of all unallocated symlink ids. 
protected int available_ids = -1; 
 
//! Allocates a symlink id for the link @[sym]. 
protected int allocate_symlink(string sym) 
{ 
  int res = symlink_ids[sym]; 
  if (res) return res; 
  res = available_ids & ~(available_ids - 1); 
  available_ids -= res; 
  return symlink_ids[sym] = res; 
}