[PATCH 5 of 5] inotify: introduce debuginotify, which lists which paths are under watch

Nicolas Dumazet nicdumz at gmail.com
Fri May 15 01:15:38 CDT 2009


# HG changeset patch
# User Nicolas Dumazet <nicdumz.commits at gmail.com>
# Date 1240328255 -32400
# Node ID 7eebc5ac886ec96f52ec724c17343d2598be8f99
# Parent  ed1205e47569186705300b7cfe73a6ab3ecaa6ae
inotify: introduce debuginotify, which lists which paths are under watch

diff --git a/hgext/inotify/__init__.py b/hgext/inotify/__init__.py
--- a/hgext/inotify/__init__.py
+++ b/hgext/inotify/__init__.py
@@ -39,6 +39,18 @@
     service = service()
     cmdutil.service(opts, initfn=service.init, runfn=service.run)
 
+def debuginotify(ui, repo, **opts):
+    '''Debugging information for inotify extension
+
+    Prints the list of directories being watched by the inotify server.
+    '''
+    cli = client(ui, repo)
+    response = cli.debugquery()
+
+    ui.write(_('directories being watched:\n'))
+    for path in response:
+        ui.write(('  %s/\n') % path)
+
 def reposetup(ui, repo):
     if not hasattr(repo, 'dirstate'):
         return
@@ -82,11 +94,13 @@
     repo.dirstate.__class__ = inotifydirstate
 
 cmdtable = {
+    'debuginotify':
+        (debuginotify, [], ('hg debuginotify')),
     '^inserve':
-    (serve,
-     [('d', 'daemon', None, _('run server in background')),
-      ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
-      ('t', 'idle-timeout', '', _('minutes to sit idle before exiting')),
-      ('', 'pid-file', '', _('name of file to write process ID to'))],
-     _('hg inserve [OPT]...')),
+        (serve,
+         [('d', 'daemon', None, _('run server in background')),
+          ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
+          ('t', 'idle-timeout', '', _('minutes to sit idle before exiting')),
+          ('', 'pid-file', '', _('name of file to write process ID to'))],
+         _('hg inserve [OPT]...')),
     }
diff --git a/hgext/inotify/client.py b/hgext/inotify/client.py
--- a/hgext/inotify/client.py
+++ b/hgext/inotify/client.py
@@ -142,5 +142,12 @@
                 if names:
                     return filter(match, names.split('\0'))
             return []
+        return map(readnames, resphdr)
 
-        return map(readnames, resphdr)
+    @start_server
+    def debugquery(self):
+        cs, resphdr = self.query('DBUG', '')
+
+        nbytes = resphdr[0]
+        names = cs.read(nbytes)
+        return names.split('\0')
diff --git a/hgext/inotify/common.py b/hgext/inotify/common.py
--- a/hgext/inotify/common.py
+++ b/hgext/inotify/common.py
@@ -18,6 +18,7 @@
      - For STAT, N+1 \0-separated strings:
         1) N different names that need checking
         2) 1 string containing all the status types to match
+     - No parameter needed for DBUG
 
   Server sending query answer:
   1) send protocol version number
@@ -31,7 +32,8 @@
 version = 2
 
 resphdrfmts = {
-    'STAT': '>llllllll' # status requests
+    'STAT': '>llllllll', # status requests
+    'DBUG': '>l'         # debugging queries
 }
 resphdrsizes = dict((k, struct.calcsize(v))
                     for k, v in resphdrfmts.iteritems())
diff --git a/hgext/inotify/server.py b/hgext/inotify/server.py
--- a/hgext/inotify/server.py
+++ b/hgext/inotify/server.py
@@ -542,6 +542,13 @@
     def shutdown(self):
         self.watcher.close()
 
+    def debug(self):
+        """
+        Returns a sorted list of relatives paths currently watched,
+        for debugging purposes.
+        """
+        return sorted(tuple[0][len(self.wprefix):] for tuple in self.watcher)
+
 class server(object):
     poll_events = select.POLLIN
 
@@ -624,6 +631,9 @@
             'c' in states and genresult('n', self.repowatcher.tree) or [],
             ]]
 
+    def answer_dbug_query(self):
+        return ['\0'.join(self.repowatcher.debug())]
+
     def handle_event(self, fd, event):
         sock, addr = self.sock.accept()
 
@@ -639,6 +649,8 @@
 
         if type == 'STAT':
             results = self.answer_stat_query(cs)
+        elif type == 'DBUG':
+            results = self.answer_dbug_query()
         else:
             self.ui.warn(_('unrecognized query type: %s\n') % type)
             return
diff --git a/tests/test-inotify-debuginotify b/tests/test-inotify-debuginotify
new file mode 100755
--- /dev/null
+++ b/tests/test-inotify-debuginotify
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+"$TESTDIR/hghave" inotify || exit 80
+
+hg init
+
+echo "[extensions]" >> $HGRCPATH
+echo "inotify=" >> $HGRCPATH
+
+echo % inserve
+hg inserve -d --pid-file=hg.pid
+cat hg.pid >> "$DAEMON_PIDS"
+
+# let the daemon finish its stuff
+sleep 1
+
+echo % empty
+hg debuginotify
+
+mkdir a
+sleep 1
+
+echo % only 'a'
+hg debuginotify
+
+rmdir a
+sleep 1
+
+echo % empty again
+hg debuginotify
+
+kill `cat hg.pid`
diff --git a/tests/test-inotify-debuginotify.out b/tests/test-inotify-debuginotify.out
new file mode 100644
--- /dev/null
+++ b/tests/test-inotify-debuginotify.out
@@ -0,0 +1,14 @@
+% inserve
+% empty
+directories being watched:
+  /
+  .hg/
+% only a
+directories being watched:
+  /
+  .hg/
+  a/
+% empty again
+directories being watched:
+  /
+  .hg/


More information about the Mercurial-devel mailing list