Updated 2014-02-25 11:07:57 by RLE

I (Colin McCormack) have written a quick package to take advantage of the Linux directory modification facility.

What this means is that you can open a directory, register your interest, and have an asynchronous script invoked when a file is added to, modified in, deleted from that directory.

I have it in mind to use this under tclhttpd.

You can get the package here: http://sourceforge.net/projects/libtclpq/

Documentation available here: http://sourceforge.net/docman/display_doc.php?docid=11924&group_id=18733

Written with Critcl.

AK - Now this is a useful thing. Note that the documentation about directory notification [1] mentions that this can trigger when files in a directory change. In other words, this can be used to write a more efficient tail. (you could also use it in conjunction with VFS to lazily mirror real directories)

Also note that this is restricted to Linux kernel 2.4 and higher.

Regarding the described interface of dnotify I have one nit: That the caller is burdened with the task to extract the fd to watch out of a channel handle. A better interface (IMHO) would be to accept channel handles and have the C code determine the file handle (There are functions in the Tcl C API for this: Tcl_GetChannelHandle IIRC). Channel handles for non-files (like sockets) would simply cause the command to throw an error.

CMcC Good suggestion, I've made the change you suggested. Had to add something to extract fd from channel name, too, since tcl doesn't provide a fd->chan mapping anymore.

AK, query: Where do you need a mapping from fd's to channels, and why does that mean that the fd has to be extracted from the channel name (which is an implicit mapping from channel to fd, and not the other way around) ? I am confused.

CL has a still-too-vague memory that much of this is possible in Windows, also. More on this, in time.

Darren New I can't help too much, but the place to start with this on Windows is here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/dirref_9hgu.asp [Davy's been threatening to put some of this in winutils.]

Davy did today. winutils 0.8. are out.

That's now a dead link -- MSDN regularly is rearranged such that links into MSDN tend to become invalid over time.

TWAPI has the begin_filesystem_monitor/cancel_filesystem_monitor functions to allow you to monitor file system changes by registering a callback. Like the rest of TWAPI, WIndoes NT 4.0 and later only. See [2] for an example.

CmCc The idea for tclhttpd is to use directory notification to maintain a cache. .tml files can be transformed on-the-fly to .html by inspection at fetch-time by tclhttpd. However, I would like to experiment with something like Linux's kernel httpd, which doesn't transfer control to tclhttpd if the URL can be satisfied by static inspection of the directory tree. One possible solution, then, is to have tclhttpd monitor a directory for changes, and simply delete more-derived files. Thus kernel httpd would provoke tclhttpd to regenerate content.

Another idea is for look-aside (if that's the right word) caching for tclhttpd, where several directories could contain variant caches of a single source directory (perhaps specialised for different browsers?) Such a facility needs to be able to invalidate the caches on changes to the source - I think it would be more effective to maintain records of the relationship of source-to-cache in the source directory than the cached directories, and propagate changes (by simply removing the derived/cached content.)

It finally dawned on CL that there's a better way to handle all the people who ask for directory-change events, tail-write events, and so on. Until now, we've gravely responded: "thou must [poll], with certain platform-dependent exceptions."

Here's what questioners really want to hear: "use XXX; it synthesizes a virtual event that perfectly matches your description." Now all that's left is to write up the XXX implementations. With me traveling, and a weekend approaching, will RS or someone else beat me to it?

Bah! It's all trivial until you start looking at NFS, and then at NFS mounts under NFS mounts... then it get's too scary.

Dangers I have known...

I think libfam handles NFS mounts. Not sure about NFS mounts under NFS mounts though.

RS contributes this little poller, that works ok for local files, but changes in directories are only visible (on Win2k) when a new file is created (or renamed)... Needs more work, but it's a starting point:
 proc watchfile {name body {interval 1000}} {
   set t [file mtime $name]
   if [catch {expr {$t - [set watchfile::$name]}} dt] { 
       namespace eval watchfile [list variable $name $t]
   } elseif $dt {
       eval $body
       set watchfile::$name $t
   }
   after $interval [info level 0]
 }

Usage example:
 watchfile t.txt {puts "Now we have it!"}

"Filemon" [3] is freeware for WinNT (and so on) and Linux that will interest many of the same people.

See also here: Matthias Hoffmann - Tcl-Code-Snippets

[Is there also something called "famd", perhaps available only for Unix, that's pertinent in this discussion?]

AMG: Check out inotify, an important feature of Linux 2.6. Here's a README explaining its advantages over dnotify: [4] . "Rumor is that the 'd' in 'dnotify' does not stand for 'directory' but for 'suck.'"

A Tcl interface to the inotify kernel facility can be found here tcl-inotify.