pike.git / lib / modules / Filesystem.pmod / Monitor.pmod / basic.pike

version» Context lines:

pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:1:   //   // Basic filesystem monitor.   // - // $Id: basic.pike,v 1.8 2009/07/15 16:28:52 grubba Exp $ + // $Id: basic.pike,v 1.9 2009/07/16 14:13:44 grubba Exp $   //   // 2009-07-09 Henrik Grubbström   //      //! Basic filesystem monitor.   //!   //! This module is intended to be used for incremental scanning of   //! a filesystem.      //! The default maximum number of seconds between checks of directories
pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:31:   //! 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 minimum number of seconds without changes for a change to be   //! regarded as stable (see @[stable_data_change()]. - protected constant stable_time = 5; + 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;      // Callbacks      //! 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.
pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:155:   enum MonitorFlags {    MF_RECURSE = 1,    MF_AUTO = 2,    MF_INITED = 4,   };      //! Monitoring information.   protected class Monitor(string path,    MonitorFlags flags,    int max_dir_check_interval, -  int file_interval_factor) +  int file_interval_factor, +  int stable_time)   {    int next_poll;    Stdio.Stat st;    int last_change = 0x7fffffff; // Future...    array(string) files;       int `<(mixed m) { return next_poll < m; }    int `>(mixed m) { return next_poll > m; }    int `==(mixed m) { return next_poll == m; }   
pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:196:   //! The heap is sorted on @[Monitor()->next_poll].   protected ADT.Heap monitor_queue = ADT.Heap();      //! 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 file_interval_factor, +  int|void stable_time)   {    if (max_dir_check_interval > 0) {    this_program::max_dir_check_interval = max_dir_check_interval;    }    if (file_interval_factor > 0) {    this_program::file_interval_factor = file_interval_factor;    } -  +  if (stable_time > 0) { +  this_program::stable_time = stable_time; +  }    clear();   }      //! Clear the set of monitored files and directories.   void clear()   {    monitors = ([]);    monitor_queue = ADT.Heap();   }   
pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:283:   //! @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()]   void monitor(string path, MonitorFlags|void flags,    int(0..)|void max_dir_check_interval, -  int(0..)|void file_interval_factor) +  int(0..)|void file_interval_factor, +  int(0..)|void stable_time)   {    path = combine_path(path, ".");    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;    monitor_queue->adjust(m);    }    // For the other cases there's no need to do anything,    // since we can keep the monitor as-is.    } else { -  m = Monitor(path, flags, max_dir_check_interval, file_interval_factor); +  m = Monitor(path, flags, +  max_dir_check_interval, file_interval_factor, stable_time);    monitors[path] = m;    monitor_queue->push(m);    }   }      //! Release a @[path] from monitoring.   //!   //! @param path   //! Path to stop monitoring.   //!
pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:400:    m->files = files;    foreach(files, string file) {    file = Stdio.append_path(m->path, file);    if (monitors[file]) {    // There's already a monitor for the file.    // Assume it has already notified about existance.    continue;    }    if (m->flags & MF_RECURSE) {    monitor(file, orig_flags | MF_AUTO, -  m->max_dir_check_interval, m->file_interval_factor); +  m->max_dir_check_interval, +  m->file_interval_factor, +  m->stable_time);    check_monitor(monitors[file]);    } else if (file_exists) {    file_exists(file, file_stat(file, 1));    }    }    }    // Signal file_exists for path as an end marker.    if (file_exists) {    file_exists(m->path, st);    }
pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:453:    Monitor m2 = monitors[file];    mixed err = catch {    if (m2) {    // We have a separate monitor on the created file.    // Let it handle the notification.    check_monitor(m2, flags);    }    };    if (m->flags & MF_RECURSE) {    monitor(file, orig_flags | MF_AUTO, -  m->max_dir_check_interval, m->file_interval_factor); +  m->max_dir_check_interval, +  m->file_interval_factor, +  m->stable_time);    check_monitor(monitors[file]);    } else if (!m2 && file_created) {    file_created(file, file_stat(file, 1));    }    }    foreach(deleted_files, string file) {    file = Stdio.append_path(m->path, file);    Monitor m2 = monitors[file];    mixed err = catch {    if (m2) {
pike.git/lib/modules/Filesystem.pmod/Monitor.pmod/basic.pike:510:    if ((flags & MF_RECURSE) && (st->isdir)) {    // Check the files in the directory.    foreach(m->files, string file) {    file = Stdio.append_path(m->path, file);    Monitor m2 = monitors[file];    if (m2) {    check_monitor(m2, flags);    }    }    } -  if (m->last_change < time(1) - stable_time) { +  if (m->last_change < time(1) - m->stable_time) {    m->last_change = 0x7fffffff;    if (stable_data_change) {    stable_data_change(m->path, st);    }    return 1;    }    return 0;   }      //! Check for changes.