Vanstechelman.eu
   

Efficient file change notifications

Many applications rely on tracking filesystem changes internally, and until recently, the most popular library providing functionality like this was app-admin/fam. Packages which use FAM for file-monitoring include GNOME, KDE, PHP, various file managers, various mail clients and servers, and many more. FAM works by repeatedly polling directory contents and looking to see if things have changed. This is inefficient, but it did the job for a while.

More recently, ultra-efficient kernel-side support for monitoring file changes was merged into Linux 2.6. This functionality, called inotify, is on by default, and is probably already available on your system (assuming you are relatively up-to-date). app-admin/gamin is a direct replacement for FAM, even implementing an identical API. The biggest bonus about gamin is that where available, gamin monitors the filesystem using inotify, destroying the ugly overhead which FAM had.

Gamin will be the default for new Gentoo installs, but there is no automatic migration for existing users at this time. It is recommended that you make the switch manually:

+-------------------------------------------------------------------------+
| Code Listing 1:                                                         |
| Switching to gamin                                                      |
+-------------------------------------------------------------------------+
|                                                                         |
|# emerge -C app-admin/fam                                                |
|# emerge --oneshot app-admin/gamin                                       |
|It is not necessary to re-merge any software that is built on            |
|FAM, as gamin is literally a drop-in replacement.                        |
|                                                                         |
|                                                                         |
+-------------------------------------------------------------------------+

Note: Gamin also supports plain old filesystem polling, and seems to do a better job than FAM did. Even if you do not have an inotify-enabled system, it is still recommended that you change.

Given that the overhead for monitoring filesystem events is now so low, it is also suggested that you enable file-monitoring support for applications which optionally support it. To do so, enable the "fam" USE flag:

+-------------------------------------------------------------------------+
| Code Listing 2:                                                         |
| Add 'fam' USE flag and rebuild world                                    |
+-------------------------------------------------------------------------+
|                                                                         |
|# nano -w /etc/make.conf                                                 |
|Add "fam" to your USE variable and save the file                         |
|# emerge --newuse world                                                  |
|                                                                         |
+-------------------------------------------------------------------------+