Mercurial > hg
annotate hgext/inotify/server.py @ 8952:7c7a672e9db7
inotify: return version to client even when not matching
Old clients are expecting a version string even in case of mismatch, otherwise
they get stuck.
author | Simon Heimberg <simohe@besonet.ch> |
---|---|
date | Sun, 21 Jun 2009 18:11:19 +0200 |
parents | 1c610db4a897 |
children | e1d119f450f0 |
rev | line source |
---|---|
6239 | 1 # server.py - inotify status server |
2 # | |
3 # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> | |
4 # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> | |
5 # | |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8209
diff
changeset
|
6 # This software may be used and distributed according to the terms of the |
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8209
diff
changeset
|
7 # GNU General Public License version 2, incorporated herein by reference. |
6239 | 8 |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
9 from mercurial.i18n import _ |
7420
b4ac1e2cd38c
inotify: remove unused imports (thanks pyflakes)
Brendan Cully <brendan@kublai.com>
parents:
7351
diff
changeset
|
10 from mercurial import osutil, util |
6239 | 11 import common |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
12 import errno, os, select, socket, stat, struct, sys, tempfile, time |
6239 | 13 |
14 try: | |
6994
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
15 import linux as inotify |
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
16 from linux import watcher |
6239 | 17 except ImportError: |
18 raise | |
19 | |
20 class AlreadyStartedException(Exception): pass | |
21 | |
22 def join(a, b): | |
23 if a: | |
24 if a[-1] == '/': | |
25 return a + b | |
26 return a + '/' + b | |
27 return b | |
28 | |
8787
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
29 def split(path): |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
30 c = path.rfind('/') |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
31 if c == -1: |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
32 return '', path |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
33 return path[:c], path[c+1:] |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
34 |
6239 | 35 walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) |
36 | |
37 def walkrepodirs(repo): | |
38 '''Iterate over all subdirectories of this repo. | |
39 Exclude the .hg directory, any nested repos, and ignored dirs.''' | |
40 rootslash = repo.root + os.sep | |
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
41 |
6239 | 42 def walkit(dirname, top): |
8321
ec985dcfd7da
inotify: inotify.server.walkrepodirs() simplify
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8320
diff
changeset
|
43 fullpath = rootslash + dirname |
6239 | 44 try: |
8321
ec985dcfd7da
inotify: inotify.server.walkrepodirs() simplify
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8320
diff
changeset
|
45 for name, kind in osutil.listdir(fullpath): |
6239 | 46 if kind == stat.S_IFDIR: |
47 if name == '.hg': | |
8323
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
48 if not top: |
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
49 return |
6239 | 50 else: |
51 d = join(dirname, name) | |
52 if repo.dirstate._ignore(d): | |
53 continue | |
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
54 for subdir in walkit(d, False): |
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
55 yield subdir |
6239 | 56 except OSError, err: |
57 if err.errno not in walk_ignored_errors: | |
58 raise | |
8324
b923d599c309
inotify: inotify.server.walk*() remove unnecessary var
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8323
diff
changeset
|
59 yield fullpath |
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
60 |
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
61 return walkit('', True) |
6239 | 62 |
63 def walk(repo, root): | |
64 '''Like os.walk, but only yields regular files.''' | |
65 | |
66 # This function is critical to performance during startup. | |
67 | |
68 rootslash = repo.root + os.sep | |
69 | |
70 def walkit(root, reporoot): | |
71 files, dirs = [], [] | |
72 | |
73 try: | |
74 fullpath = rootslash + root | |
75 for name, kind in osutil.listdir(fullpath): | |
76 if kind == stat.S_IFDIR: | |
77 if name == '.hg': | |
8325
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
78 if not reporoot: |
8323
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
79 return |
8325
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
80 else: |
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
81 dirs.append(name) |
8381
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
82 path = join(root, name) |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
83 if repo.dirstate._ignore(path): |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
84 continue |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
85 for result in walkit(path, False): |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
86 yield result |
6239 | 87 elif kind in (stat.S_IFREG, stat.S_IFLNK): |
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
88 files.append(name) |
8324
b923d599c309
inotify: inotify.server.walk*() remove unnecessary var
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8323
diff
changeset
|
89 yield fullpath, dirs, files |
6239 | 90 |
91 except OSError, err: | |
92 if err.errno not in walk_ignored_errors: | |
93 raise | |
8320
a1305c1c8d8e
inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8319
diff
changeset
|
94 |
a1305c1c8d8e
inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8319
diff
changeset
|
95 return walkit(root, root == '') |
6239 | 96 |
8786
55af9be4efac
inotify: remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8610
diff
changeset
|
97 def _explain_watch_limit(ui, repo): |
6239 | 98 path = '/proc/sys/fs/inotify/max_user_watches' |
99 try: | |
100 limit = int(file(path).read()) | |
101 except IOError, err: | |
102 if err.errno != errno.ENOENT: | |
103 raise | |
104 raise util.Abort(_('this system does not seem to ' | |
105 'support inotify')) | |
106 ui.warn(_('*** the current per-user limit on the number ' | |
107 'of inotify watches is %s\n') % limit) | |
108 ui.warn(_('*** this limit is too low to watch every ' | |
109 'directory in this repository\n')) | |
110 ui.warn(_('*** counting directories: ')) | |
111 ndirs = len(list(walkrepodirs(repo))) | |
112 ui.warn(_('found %d\n') % ndirs) | |
113 newlimit = min(limit, 1024) | |
114 while newlimit < ((limit + ndirs) * 1.1): | |
115 newlimit *= 2 | |
116 ui.warn(_('*** to raise the limit from %d to %d (run as root):\n') % | |
117 (limit, newlimit)) | |
118 ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) | |
119 raise util.Abort(_('cannot watch %s until inotify watch limit is raised') | |
120 % repo.root) | |
121 | |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
122 class pollable(object): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
123 """ |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
124 Interface to support polling. |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
125 The file descriptor returned by fileno() is registered to a polling |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
126 object. |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
127 Usage: |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
128 Every tick, check if an event has happened since the last tick: |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
129 * If yes, call handle_events |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
130 * If no, call handle_timeout |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
131 """ |
6239 | 132 poll_events = select.POLLIN |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
133 instances = {} |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
134 poll = select.poll() |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
135 |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
136 def fileno(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
137 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
138 |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
139 def handle_events(self, events): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
140 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
141 |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
142 def handle_timeout(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
143 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
144 |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
145 def shutdown(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
146 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
147 |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
148 def register(self, timeout): |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
149 fd = self.fileno() |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
150 |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
151 pollable.poll.register(fd, pollable.poll_events) |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
152 pollable.instances[fd] = self |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
153 |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
154 self.registered = True |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
155 self.timeout = timeout |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
156 |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
157 def unregister(self): |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
158 pollable.poll.unregister(self) |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
159 self.registered = False |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
160 |
8793
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
161 @classmethod |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
162 def run(cls): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
163 while True: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
164 timeout = None |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
165 timeobj = None |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
166 for obj in cls.instances.itervalues(): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
167 if obj.timeout is not None and (timeout is None or obj.timeout < timeout): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
168 timeout, timeobj = obj.timeout, obj |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
169 try: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
170 events = cls.poll.poll(timeout) |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
171 except select.error, err: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
172 if err[0] == errno.EINTR: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
173 continue |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
174 raise |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
175 if events: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
176 by_fd = {} |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
177 for fd, event in events: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
178 by_fd.setdefault(fd, []).append(event) |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
179 |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
180 for fd, events in by_fd.iteritems(): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
181 cls.instances[fd].handle_pollevents(events) |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
182 |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
183 elif timeobj: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
184 timeobj.handle_timeout() |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
185 |
8791
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
186 def eventaction(code): |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
187 """ |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
188 Decorator to help handle events in repowatcher |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
189 """ |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
190 def decorator(f): |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
191 def wrapper(self, wpath): |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
192 if code == 'm' and wpath in self.lastevent and \ |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
193 self.lastevent[wpath] in 'cm': |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
194 return |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
195 self.lastevent[wpath] = code |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
196 self.timeout = 250 |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
197 |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
198 f(self, wpath) |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
199 |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
200 wrapper.func_name = f.func_name |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
201 return wrapper |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
202 return decorator |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
203 |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
204 class repowatcher(pollable): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
205 """ |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
206 Watches inotify events |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
207 """ |
6239 | 208 statuskeys = 'almr!?' |
8383
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
209 mask = ( |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
210 inotify.IN_ATTRIB | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
211 inotify.IN_CREATE | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
212 inotify.IN_DELETE | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
213 inotify.IN_DELETE_SELF | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
214 inotify.IN_MODIFY | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
215 inotify.IN_MOVED_FROM | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
216 inotify.IN_MOVED_TO | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
217 inotify.IN_MOVE_SELF | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
218 inotify.IN_ONLYDIR | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
219 inotify.IN_UNMOUNT | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
220 0) |
6239 | 221 |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
222 def __init__(self, ui, repo): |
6239 | 223 self.ui = ui |
224 self.repo = repo | |
225 self.wprefix = self.repo.wjoin('') | |
226 try: | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
227 self.watcher = watcher.watcher() |
6239 | 228 except OSError, err: |
229 raise util.Abort(_('inotify service not available: %s') % | |
230 err.strerror) | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
231 self.threshold = watcher.threshold(self.watcher) |
6239 | 232 self.fileno = self.watcher.fileno |
233 | |
234 self.tree = {} | |
235 self.statcache = {} | |
236 self.statustrees = dict([(s, {}) for s in self.statuskeys]) | |
237 | |
238 self.last_event = None | |
239 | |
8605
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
240 self.lastevent = {} |
6239 | 241 |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
242 self.register(timeout=None) |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
243 |
6239 | 244 self.ds_info = self.dirstate_info() |
8604
578f2a0049cd
inotify: do not recurse in handle_timeout(): call it explicitely, not in scan()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8600
diff
changeset
|
245 self.handle_timeout() |
6239 | 246 self.scan() |
247 | |
248 def event_time(self): | |
249 last = self.last_event | |
250 now = time.time() | |
251 self.last_event = now | |
252 | |
253 if last is None: | |
254 return 'start' | |
255 delta = now - last | |
256 if delta < 5: | |
257 return '+%.3f' % delta | |
258 if delta < 50: | |
259 return '+%.2f' % delta | |
260 return '+%.1f' % delta | |
261 | |
262 def dirstate_info(self): | |
263 try: | |
264 st = os.lstat(self.repo.join('dirstate')) | |
265 return st.st_mtime, st.st_ino | |
266 except OSError, err: | |
267 if err.errno != errno.ENOENT: | |
268 raise | |
269 return 0, 0 | |
270 | |
271 def add_watch(self, path, mask): | |
272 if not path: | |
273 return | |
274 if self.watcher.path(path) is None: | |
275 if self.ui.debugflag: | |
276 self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) | |
277 try: | |
278 self.watcher.add(path, mask) | |
279 except OSError, err: | |
280 if err.errno in (errno.ENOENT, errno.ENOTDIR): | |
281 return | |
282 if err.errno != errno.ENOSPC: | |
283 raise | |
8786
55af9be4efac
inotify: remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8610
diff
changeset
|
284 _explain_watch_limit(self.ui, self.repo) |
6239 | 285 |
286 def setup(self): | |
287 self.ui.note(_('watching directories under %r\n') % self.repo.root) | |
288 self.add_watch(self.repo.path, inotify.IN_DELETE) | |
289 self.check_dirstate() | |
290 | |
291 def wpath(self, evt): | |
292 path = evt.fullpath | |
293 if path == self.repo.root: | |
294 return '' | |
295 if path.startswith(self.wprefix): | |
296 return path[len(self.wprefix):] | |
297 raise 'wtf? ' + path | |
298 | |
299 def dir(self, tree, path): | |
300 if path: | |
301 for name in path.split('/'): | |
8384
483d9b2103da
inotify: dic.setdefault(k, d) ; v = dic[k] --> v = dic.setdefault(k, d)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8383
diff
changeset
|
302 tree = tree.setdefault(name, {}) |
6239 | 303 return tree |
304 | |
305 def lookup(self, path, tree): | |
306 if path: | |
307 try: | |
308 for name in path.split('/'): | |
309 tree = tree[name] | |
310 except KeyError: | |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
311 return 'x' |
6239 | 312 except TypeError: |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
313 return 'd' |
6239 | 314 return tree |
315 | |
316 def filestatus(self, fn, st): | |
317 try: | |
318 type_, mode, size, time = self.repo.dirstate._map[fn][:4] | |
319 except KeyError: | |
320 type_ = '?' | |
321 if type_ == 'n': | |
322 st_mode, st_size, st_mtime = st | |
7082
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
323 if size == -1: |
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
324 return 'l' |
6239 | 325 if size and (size != st_size or (mode ^ st_mode) & 0100): |
326 return 'm' | |
327 if time != int(st_mtime): | |
328 return 'l' | |
329 return 'n' | |
330 if type_ == '?' and self.repo.dirstate._ignore(fn): | |
331 return 'i' | |
332 return type_ | |
333 | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
334 def updatefile(self, wfn, osstat): |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
335 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
336 update the file entry of an existing file. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
337 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
338 osstat: (mode, size, time) tuple, as returned by os.lstat(wfn) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
339 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
340 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
341 self._updatestatus(wfn, self.filestatus(wfn, osstat)) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
342 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
343 def deletefile(self, wfn, oldstatus): |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
344 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
345 update the entry of a file which has been deleted. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
346 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
347 oldstatus: char in statuskeys, status of the file before deletion |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
348 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
349 if oldstatus == 'r': |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
350 newstatus = 'r' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
351 elif oldstatus in 'almn': |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
352 newstatus = '!' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
353 else: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
354 newstatus = None |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
355 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
356 self.statcache.pop(wfn, None) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
357 self._updatestatus(wfn, newstatus) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
358 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
359 def _updatestatus(self, wfn, newstatus): |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
360 ''' |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
361 Update the stored status of a file or directory. |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
362 |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
363 newstatus: - char in (statuskeys + 'ni'), new status to apply. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
364 - or None, to stop tracking wfn |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
365 ''' |
8787
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
366 root, fn = split(wfn) |
6239 | 367 d = self.dir(self.tree, root) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
368 |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
369 oldstatus = d.get(fn) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
370 # oldstatus can be either: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
371 # - None : fn is new |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
372 # - a char in statuskeys: fn is a (tracked) file |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
373 # - a dict: fn is a directory |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
374 isdir = isinstance(oldstatus, dict) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
375 |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
376 if self.ui.debugflag and oldstatus != newstatus: |
6239 | 377 if isdir: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
378 self.ui.note(_('status: %r dir(%d) -> %s\n') % |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
379 (wfn, len(oldstatus), newstatus)) |
6239 | 380 else: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
381 self.ui.note(_('status: %r %s -> %s\n') % |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
382 (wfn, oldstatus, newstatus)) |
6239 | 383 if not isdir: |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
384 if oldstatus and oldstatus in self.statuskeys \ |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
385 and oldstatus != newstatus: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
386 del self.dir(self.statustrees[oldstatus], root)[fn] |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
387 if newstatus and newstatus != 'i': |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
388 d[fn] = newstatus |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
389 if newstatus in self.statuskeys: |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
390 dd = self.dir(self.statustrees[newstatus], root) |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
391 if oldstatus != newstatus or fn not in dd: |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
392 dd[fn] = newstatus |
6239 | 393 else: |
394 d.pop(fn, None) | |
7892
67e59a9886d5
Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
7451
diff
changeset
|
395 |
6239 | 396 |
397 def check_deleted(self, key): | |
398 # Files that had been deleted but were present in the dirstate | |
399 # may have vanished from the dirstate; we must clean them up. | |
400 nuke = [] | |
401 for wfn, ignore in self.walk(key, self.statustrees[key]): | |
402 if wfn not in self.repo.dirstate: | |
403 nuke.append(wfn) | |
404 for wfn in nuke: | |
8787
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
405 root, fn = split(wfn) |
6239 | 406 del self.dir(self.statustrees[key], root)[fn] |
407 del self.dir(self.tree, root)[fn] | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
408 |
6239 | 409 def scan(self, topdir=''): |
410 ds = self.repo.dirstate._map.copy() | |
411 self.add_watch(join(self.repo.root, topdir), self.mask) | |
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
412 for root, dirs, files in walk(self.repo, topdir): |
6239 | 413 for d in dirs: |
414 self.add_watch(join(root, d), self.mask) | |
415 wroot = root[len(self.wprefix):] | |
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
416 for fn in files: |
6239 | 417 wfn = join(wroot, fn) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
418 self.updatefile(wfn, self.getstat(wfn)) |
6239 | 419 ds.pop(wfn, None) |
420 wtopdir = topdir | |
421 if wtopdir and wtopdir[-1] != '/': | |
422 wtopdir += '/' | |
423 for wfn, state in ds.iteritems(): | |
424 if not wfn.startswith(wtopdir): | |
425 continue | |
7302
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
426 try: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
427 st = self.stat(wfn) |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
428 except OSError: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
429 status = state[0] |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
430 self.deletefile(wfn, status) |
6239 | 431 else: |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
432 self.updatefile(wfn, st) |
6239 | 433 self.check_deleted('!') |
434 self.check_deleted('r') | |
435 | |
436 def check_dirstate(self): | |
437 ds_info = self.dirstate_info() | |
438 if ds_info == self.ds_info: | |
439 return | |
440 self.ds_info = ds_info | |
441 if not self.ui.debugflag: | |
442 self.last_event = None | |
443 self.ui.note(_('%s dirstate reload\n') % self.event_time()) | |
444 self.repo.dirstate.invalidate() | |
8604
578f2a0049cd
inotify: do not recurse in handle_timeout(): call it explicitely, not in scan()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8600
diff
changeset
|
445 self.handle_timeout() |
6239 | 446 self.scan() |
447 self.ui.note(_('%s end dirstate reload\n') % self.event_time()) | |
448 | |
449 def walk(self, states, tree, prefix=''): | |
450 # This is the "inner loop" when talking to the client. | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
451 |
6239 | 452 for name, val in tree.iteritems(): |
453 path = join(prefix, name) | |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
454 try: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
455 if val in states: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
456 yield path, val |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
457 except TypeError: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
458 for p in self.walk(states, val, path): |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
459 yield p |
6239 | 460 |
461 def update_hgignore(self): | |
462 # An update of the ignore file can potentially change the | |
463 # states of all unknown and ignored files. | |
464 | |
465 # XXX If the user has other ignore files outside the repo, or | |
466 # changes their list of ignore files at run time, we'll | |
467 # potentially never see changes to them. We could get the | |
468 # client to report to us what ignore data they're using. | |
469 # But it's easier to do nothing than to open that can of | |
470 # worms. | |
471 | |
7085
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
472 if '_ignore' in self.repo.dirstate.__dict__: |
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
473 delattr(self.repo.dirstate, '_ignore') |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
474 self.ui.note(_('rescanning due to .hgignore change\n')) |
8604
578f2a0049cd
inotify: do not recurse in handle_timeout(): call it explicitely, not in scan()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8600
diff
changeset
|
475 self.handle_timeout() |
6239 | 476 self.scan() |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
477 |
6239 | 478 def getstat(self, wpath): |
479 try: | |
480 return self.statcache[wpath] | |
481 except KeyError: | |
482 try: | |
483 return self.stat(wpath) | |
484 except OSError, err: | |
485 if err.errno != errno.ENOENT: | |
486 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
487 |
6239 | 488 def stat(self, wpath): |
489 try: | |
490 st = os.lstat(join(self.wprefix, wpath)) | |
491 ret = st.st_mode, st.st_size, st.st_mtime | |
492 self.statcache[wpath] = ret | |
493 return ret | |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
494 except OSError: |
6239 | 495 self.statcache.pop(wpath, None) |
496 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
497 |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
498 @eventaction('c') |
6239 | 499 def created(self, wpath): |
500 if wpath == '.hgignore': | |
501 self.update_hgignore() | |
502 try: | |
503 st = self.stat(wpath) | |
504 if stat.S_ISREG(st[0]): | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
505 self.updatefile(wpath, st) |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
506 except OSError: |
6239 | 507 pass |
508 | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
509 @eventaction('m') |
6239 | 510 def modified(self, wpath): |
511 if wpath == '.hgignore': | |
512 self.update_hgignore() | |
513 try: | |
514 st = self.stat(wpath) | |
515 if stat.S_ISREG(st[0]): | |
516 if self.repo.dirstate[wpath] in 'lmn': | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
517 self.updatefile(wpath, st) |
6239 | 518 except OSError: |
519 pass | |
520 | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
521 @eventaction('d') |
6239 | 522 def deleted(self, wpath): |
523 if wpath == '.hgignore': | |
524 self.update_hgignore() | |
525 elif wpath.startswith('.hg/'): | |
526 if wpath == '.hg/wlock': | |
527 self.check_dirstate() | |
528 return | |
529 | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
530 self.deletefile(wpath, self.repo.dirstate[wpath]) |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
531 |
6239 | 532 def process_create(self, wpath, evt): |
533 if self.ui.debugflag: | |
534 self.ui.note(_('%s event: created %s\n') % | |
535 (self.event_time(), wpath)) | |
536 | |
537 if evt.mask & inotify.IN_ISDIR: | |
538 self.scan(wpath) | |
539 else: | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
540 self.created(wpath) |
6239 | 541 |
542 def process_delete(self, wpath, evt): | |
543 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
544 self.ui.note(_('%s event: deleted %s\n') % |
6239 | 545 (self.event_time(), wpath)) |
546 | |
547 if evt.mask & inotify.IN_ISDIR: | |
8600
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
548 tree = self.dir(self.tree, wpath).copy() |
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
549 for wfn, ignore in self.walk('?', tree): |
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
550 self.deletefile(join(wpath, wfn), '?') |
6239 | 551 self.scan(wpath) |
8600
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
552 else: |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
553 self.deleted(wpath) |
6239 | 554 |
555 def process_modify(self, wpath, evt): | |
556 if self.ui.debugflag: | |
557 self.ui.note(_('%s event: modified %s\n') % | |
558 (self.event_time(), wpath)) | |
559 | |
560 if not (evt.mask & inotify.IN_ISDIR): | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
561 self.modified(wpath) |
6239 | 562 |
563 def process_unmount(self, evt): | |
564 self.ui.warn(_('filesystem containing %s was unmounted\n') % | |
565 evt.fullpath) | |
566 sys.exit(0) | |
567 | |
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
568 def handle_pollevents(self, events): |
6239 | 569 if self.ui.debugflag: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
570 self.ui.note(_('%s readable: %d bytes\n') % |
6239 | 571 (self.event_time(), self.threshold.readable())) |
572 if not self.threshold(): | |
573 if self.registered: | |
574 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
575 self.ui.note(_('%s below threshold - unhooking\n') % |
6239 | 576 (self.event_time())) |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
577 self.unregister() |
6239 | 578 self.timeout = 250 |
579 else: | |
580 self.read_events() | |
581 | |
582 def read_events(self, bufsize=None): | |
583 events = self.watcher.read(bufsize) | |
584 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
585 self.ui.note(_('%s reading %d events\n') % |
6239 | 586 (self.event_time(), len(events))) |
587 for evt in events: | |
588 wpath = self.wpath(evt) | |
589 if evt.mask & inotify.IN_UNMOUNT: | |
590 self.process_unmount(wpath, evt) | |
591 elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): | |
592 self.process_modify(wpath, evt) | |
593 elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | | |
594 inotify.IN_MOVED_FROM): | |
595 self.process_delete(wpath, evt) | |
596 elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): | |
597 self.process_create(wpath, evt) | |
598 | |
8605
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
599 self.lastevent.clear() |
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
600 |
6239 | 601 def handle_timeout(self): |
602 if not self.registered: | |
603 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
604 self.ui.note(_('%s hooking back up with %d bytes readable\n') % |
6239 | 605 (self.event_time(), self.threshold.readable())) |
606 self.read_events(0) | |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
607 self.register(timeout=None) |
6239 | 608 |
609 self.timeout = None | |
610 | |
611 def shutdown(self): | |
612 self.watcher.close() | |
613 | |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
614 def debug(self): |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
615 """ |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
616 Returns a sorted list of relatives paths currently watched, |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
617 for debugging purposes. |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
618 """ |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
619 return sorted(tuple[0][len(self.wprefix):] for tuple in self.watcher) |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
620 |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
621 class server(pollable): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
622 """ |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
623 Listens for client queries on unix socket inotify.sock |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
624 """ |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
625 def __init__(self, ui, repo, repowatcher, timeout): |
6239 | 626 self.ui = ui |
627 self.repo = repo | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
628 self.repowatcher = repowatcher |
6239 | 629 self.sock = socket.socket(socket.AF_UNIX) |
630 self.sockpath = self.repo.join('inotify.sock') | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
631 self.realsockpath = None |
6239 | 632 try: |
633 self.sock.bind(self.sockpath) | |
634 except socket.error, err: | |
635 if err[0] == errno.EADDRINUSE: | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
636 raise AlreadyStartedException(_('could not start server: %s') |
6239 | 637 % err[1]) |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
638 if err[0] == "AF_UNIX path too long": |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
639 tempdir = tempfile.mkdtemp(prefix="hg-inotify-") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
640 self.realsockpath = os.path.join(tempdir, "inotify.sock") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
641 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
642 self.sock.bind(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
643 os.symlink(self.realsockpath, self.sockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
644 except (OSError, socket.error), inst: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
645 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
646 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
647 except: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
648 pass |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
649 os.rmdir(tempdir) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
650 if inst.errno == errno.EEXIST: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
651 raise AlreadyStartedException(_('could not start server: %s') |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
652 % inst.strerror) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
653 raise |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
654 else: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
655 raise |
6239 | 656 self.sock.listen(5) |
657 self.fileno = self.sock.fileno | |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
658 self.register(timeout=timeout) |
6239 | 659 |
660 def handle_timeout(self): | |
661 pass | |
662 | |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
663 def answer_stat_query(self, cs): |
6239 | 664 names = cs.read().split('\0') |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
665 |
6239 | 666 states = names.pop() |
667 | |
668 self.ui.note(_('answering query for %r\n') % states) | |
669 | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
670 if self.repowatcher.timeout: |
6239 | 671 # We got a query while a rescan is pending. Make sure we |
672 # rescan before responding, or we could give back a wrong | |
673 # answer. | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
674 self.repowatcher.handle_timeout() |
6239 | 675 |
676 if not names: | |
677 def genresult(states, tree): | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
678 for fn, state in self.repowatcher.walk(states, tree): |
6239 | 679 yield fn |
680 else: | |
681 def genresult(states, tree): | |
682 for fn in names: | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
683 l = self.repowatcher.lookup(fn, tree) |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
684 try: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
685 if l in states: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
686 yield fn |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
687 except TypeError: |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
688 for f, s in self.repowatcher.walk(states, l, fn): |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
689 yield f |
6239 | 690 |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
691 return ['\0'.join(r) for r in [ |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
692 genresult('l', self.repowatcher.statustrees['l']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
693 genresult('m', self.repowatcher.statustrees['m']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
694 genresult('a', self.repowatcher.statustrees['a']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
695 genresult('r', self.repowatcher.statustrees['r']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
696 genresult('!', self.repowatcher.statustrees['!']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
697 '?' in states |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
698 and genresult('?', self.repowatcher.statustrees['?']) |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
699 or [], |
6239 | 700 [], |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
701 'c' in states and genresult('n', self.repowatcher.tree) or [], |
6239 | 702 ]] |
703 | |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
704 def answer_dbug_query(self): |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
705 return ['\0'.join(self.repowatcher.debug())] |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
706 |
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
707 def handle_pollevents(self, events): |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
708 for e in events: |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
709 self.handle_pollevent() |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
710 |
8608
228db070bfc4
inotify: rename handle_event to handle_pollevent to avoid confusion
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8607
diff
changeset
|
711 def handle_pollevent(self): |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
712 sock, addr = self.sock.accept() |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
713 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
714 cs = common.recvcs(sock) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
715 version = ord(cs.read(1)) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
716 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
717 if version != common.version: |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
718 self.ui.warn(_('received query from incompatible client ' |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
719 'version %d\n') % version) |
8952
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
720 try: |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
721 # try to send back our version to the client |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
722 # this way, the client too is informed of the mismatch |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
723 sock.sendall(chr(common.version)) |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
724 except: |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
725 pass |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
726 return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
727 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
728 type = cs.read(4) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
729 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
730 if type == 'STAT': |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
731 results = self.answer_stat_query(cs) |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
732 elif type == 'DBUG': |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
733 results = self.answer_dbug_query() |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
734 else: |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
735 self.ui.warn(_('unrecognized query type: %s\n') % type) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
736 return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
737 |
6239 | 738 try: |
739 try: | |
8386
4aad982111b6
inotify: Abstract the layer format and sizes to a inotify.common dictionary
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8385
diff
changeset
|
740 v = chr(common.version) |
4aad982111b6
inotify: Abstract the layer format and sizes to a inotify.common dictionary
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8385
diff
changeset
|
741 |
8553
e387ecd7a6ed
inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8386
diff
changeset
|
742 sock.sendall(v + type + struct.pack(common.resphdrfmts[type], |
e387ecd7a6ed
inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8386
diff
changeset
|
743 *map(len, results))) |
6239 | 744 sock.sendall(''.join(results)) |
745 finally: | |
746 sock.shutdown(socket.SHUT_WR) | |
747 except socket.error, err: | |
748 if err[0] != errno.EPIPE: | |
749 raise | |
750 | |
751 def shutdown(self): | |
752 self.sock.close() | |
753 try: | |
754 os.unlink(self.sockpath) | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
755 if self.realsockpath: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
756 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
757 os.rmdir(os.path.dirname(self.realsockpath)) |
6239 | 758 except OSError, err: |
759 if err.errno != errno.ENOENT: | |
760 raise | |
761 | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
762 class master(object): |
6239 | 763 def __init__(self, ui, repo, timeout=None): |
764 self.ui = ui | |
765 self.repo = repo | |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
766 self.repowatcher = repowatcher(ui, repo) |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
767 self.server = server(ui, repo, self.repowatcher, timeout) |
6239 | 768 |
769 def shutdown(self): | |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
770 for obj in pollable.instances.itervalues(): |
6239 | 771 obj.shutdown() |
772 | |
773 def run(self): | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
774 self.repowatcher.setup() |
6239 | 775 self.ui.note(_('finished setup\n')) |
776 if os.getenv('TIME_STARTUP'): | |
777 sys.exit(0) | |
8793
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
778 pollable.run() |
6239 | 779 |
780 def start(ui, repo): | |
7451
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
781 def closefds(ignore): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
782 # (from python bug #1177468) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
783 # close all inherited file descriptors |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
784 # Python 2.4.1 and later use /dev/urandom to seed the random module's RNG |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
785 # a file descriptor is kept internally as os._urandomfd (created on demand |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
786 # the first time os.urandom() is called), and should not be closed |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
787 try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
788 os.urandom(4) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
789 urandom_fd = getattr(os, '_urandomfd', None) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
790 except AttributeError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
791 urandom_fd = None |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
792 ignore.append(urandom_fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
793 for fd in range(3, 256): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
794 if fd in ignore: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
795 continue |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
796 try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
797 os.close(fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
798 except OSError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
799 pass |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
800 |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
801 m = master(ui, repo) |
6239 | 802 sys.stdout.flush() |
803 sys.stderr.flush() | |
804 | |
805 pid = os.fork() | |
806 if pid: | |
807 return pid | |
808 | |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
809 closefds(pollable.instances.keys()) |
6239 | 810 os.setsid() |
811 | |
812 fd = os.open('/dev/null', os.O_RDONLY) | |
813 os.dup2(fd, 0) | |
814 if fd > 0: | |
815 os.close(fd) | |
816 | |
817 fd = os.open(ui.config('inotify', 'log', '/dev/null'), | |
818 os.O_RDWR | os.O_CREAT | os.O_TRUNC) | |
819 os.dup2(fd, 1) | |
820 os.dup2(fd, 2) | |
821 if fd > 2: | |
822 os.close(fd) | |
823 | |
824 try: | |
825 m.run() | |
826 finally: | |
827 m.shutdown() | |
828 os._exit(0) |