\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#384: Add File Alteration Monitoring to the Tcl Core}
\date{December 02, 2010}
\author{Reinhard Max, Gerald W. Lester}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#384}&\textbf{Add File Alteration Monitoring to the Tcl Core}\\\hline
Author&

Reinhard Max \tipmail{max}{tclers.tk}
\par
Gerald W. Lester \tipmail{Gerald.Lester}{cox.net}
\\
Created&Thursday, \(\text{2}^{\text{nd}}\) December 2010\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.4 $\\
Keywords&kqueue, inotify, dnotify, gamin, FSevents, fam\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to add capabilities for monitoring changes in the file system to the Tcl core.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Most, if not all, platforms supported by Tcl today allow user space processes to monitor the file system for certain changes such as the creation, deletion, alteration or renaming of files.

Unfortunately there is no unique cross-platform API for this, so a Tcl core feature will have to be backed by a series of platform-dependent implementations and possibly a fallback for platforms that don't have these monitoring capabilities or instances on which they have been disabled.

Platform-specific APIs which should be supported by the final implementation (list to be extended):
\begin{itemize}
\item{}
inotify[\url{http://en.wikipedia.org/wiki/Inotify}] (Linux)
\item{}
kqueue[\url{http://en.wikipedia.org/wiki/Kqueue}] (BSD)
\item{}
FSEvents[\url{http://en.wikipedia.org/wiki/FSEvents}] (Mac OS X)
\item{}
TWAPI [\url{http://twapi.magicsplat.com/disk.html\#begin_filesystem_monitor}] (Windows)
\end{itemize}

\section{Proposal}

A new subcommand should get added to the \textbf{file} command with the following syntax:

\begin{quote}
\textbf{file monitor} \textit{path} \textit{callback} ?\textit{filter}?
\end{quote}

This will register \textit{callback} to be called from the event loop when one of the events specified by \textit{filter} occurs to the file or directory specified by \textit{path}. Calling \textbf{file monitor} with an empty callback argument removes an existing callback from the given path.

The calling conventions for \textit{callback} and the syntax of \textit{filter} are yet to be determined.

\section{Rejected Alternatives}

The \textbf{file} command was chosen over \textbf{fileevent} and \textbf{chan event}, because the object to be monitored is passed by name and not as an open channel.

As an API, FAM[\url{http://en.wikipedia.org/wiki/File_alteration_monitor}] would have been a more portable alternative, but it requres a daemon to be running on the local machine and it requires linking against a lib that is licensed under the LGPL. It might be possible to support FAM as a compile time alternative to the system's native notification method. There exists a serverless implementation of a subset of the FAM API, called Gamin[\url{http://people.gnome.org/~veillard/gamin/index.html}], but that's also licensed under the LGPL.

There exists a Tcl extension for inotify[\url{https://sourceforge.net/projects/tcl-inotify/}], but it is licensed under the GPL and it's script level API is too close to the inotify primitives to serve as the basis of a cross-platform abstraction for such notifications. But it might be a good base for a scripted prototype to sort out the remaining details of the proposed script level API.

\section{Further Ideas}

It might be an option to release the C code that unifies the various OS speciffic mechanisms under a single API as a separate library, so that projects other than Tcl can use it as well.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
