Mercurial > hg
annotate hgext/inotify/server.py @ 7220:60826e071ce2
inotify: avoid passing potentially stale stat to update
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Wed, 22 Oct 2008 19:40:32 -0500 |
parents | 4033195d455b |
children | 810ca383da9c |
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 # | |
6 # This software may be used and distributed according to the terms | |
7 # of the GNU General Public License, incorporated herein by reference. | |
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 _ |
6239 | 10 from mercurial import osutil, ui, util |
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 print >> sys.stderr, '*** native support is required for this extension' | |
19 raise | |
20 | |
21 class AlreadyStartedException(Exception): pass | |
22 | |
23 def join(a, b): | |
24 if a: | |
25 if a[-1] == '/': | |
26 return a + b | |
27 return a + '/' + b | |
28 return b | |
29 | |
30 walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) | |
31 | |
32 def walkrepodirs(repo): | |
33 '''Iterate over all subdirectories of this repo. | |
34 Exclude the .hg directory, any nested repos, and ignored dirs.''' | |
35 rootslash = repo.root + os.sep | |
36 def walkit(dirname, top): | |
37 hginside = False | |
38 try: | |
39 for name, kind in osutil.listdir(rootslash + dirname): | |
40 if kind == stat.S_IFDIR: | |
41 if name == '.hg': | |
42 hginside = True | |
43 if not top: break | |
44 else: | |
45 d = join(dirname, name) | |
46 if repo.dirstate._ignore(d): | |
47 continue | |
48 for subdir, hginsub in walkit(d, False): | |
49 if not hginsub: | |
50 yield subdir, False | |
51 except OSError, err: | |
52 if err.errno not in walk_ignored_errors: | |
53 raise | |
54 yield rootslash + dirname, hginside | |
55 for dirname, hginside in walkit('', True): | |
56 yield dirname | |
57 | |
58 def walk(repo, root): | |
59 '''Like os.walk, but only yields regular files.''' | |
60 | |
61 # This function is critical to performance during startup. | |
62 | |
63 reporoot = root == '' | |
64 rootslash = repo.root + os.sep | |
65 | |
66 def walkit(root, reporoot): | |
67 files, dirs = [], [] | |
68 hginside = False | |
69 | |
70 try: | |
71 fullpath = rootslash + root | |
72 for name, kind in osutil.listdir(fullpath): | |
73 if kind == stat.S_IFDIR: | |
74 if name == '.hg': | |
75 hginside = True | |
76 if reporoot: | |
77 continue | |
78 else: | |
79 break | |
80 dirs.append(name) | |
81 elif kind in (stat.S_IFREG, stat.S_IFLNK): | |
82 path = join(root, name) | |
83 files.append((name, kind)) | |
84 | |
85 yield hginside, fullpath, dirs, files | |
86 | |
87 for subdir in dirs: | |
88 path = join(root, subdir) | |
89 if repo.dirstate._ignore(path): | |
90 continue | |
91 for result in walkit(path, False): | |
92 if not result[0]: | |
93 yield result | |
94 except OSError, err: | |
95 if err.errno not in walk_ignored_errors: | |
96 raise | |
97 for result in walkit(root, reporoot): | |
98 yield result[1:] | |
99 | |
100 def _explain_watch_limit(ui, repo, count): | |
101 path = '/proc/sys/fs/inotify/max_user_watches' | |
102 try: | |
103 limit = int(file(path).read()) | |
104 except IOError, err: | |
105 if err.errno != errno.ENOENT: | |
106 raise | |
107 raise util.Abort(_('this system does not seem to ' | |
108 'support inotify')) | |
109 ui.warn(_('*** the current per-user limit on the number ' | |
110 'of inotify watches is %s\n') % limit) | |
111 ui.warn(_('*** this limit is too low to watch every ' | |
112 'directory in this repository\n')) | |
113 ui.warn(_('*** counting directories: ')) | |
114 ndirs = len(list(walkrepodirs(repo))) | |
115 ui.warn(_('found %d\n') % ndirs) | |
116 newlimit = min(limit, 1024) | |
117 while newlimit < ((limit + ndirs) * 1.1): | |
118 newlimit *= 2 | |
119 ui.warn(_('*** to raise the limit from %d to %d (run as root):\n') % | |
120 (limit, newlimit)) | |
121 ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) | |
122 raise util.Abort(_('cannot watch %s until inotify watch limit is raised') | |
123 % repo.root) | |
124 | |
125 class Watcher(object): | |
126 poll_events = select.POLLIN | |
127 statuskeys = 'almr!?' | |
128 | |
129 def __init__(self, ui, repo, master): | |
130 self.ui = ui | |
131 self.repo = repo | |
132 self.wprefix = self.repo.wjoin('') | |
133 self.timeout = None | |
134 self.master = master | |
135 self.mask = ( | |
136 inotify.IN_ATTRIB | | |
137 inotify.IN_CREATE | | |
138 inotify.IN_DELETE | | |
139 inotify.IN_DELETE_SELF | | |
140 inotify.IN_MODIFY | | |
141 inotify.IN_MOVED_FROM | | |
142 inotify.IN_MOVED_TO | | |
143 inotify.IN_MOVE_SELF | | |
144 inotify.IN_ONLYDIR | | |
145 inotify.IN_UNMOUNT | | |
146 0) | |
147 try: | |
148 self.watcher = watcher.Watcher() | |
149 except OSError, err: | |
150 raise util.Abort(_('inotify service not available: %s') % | |
151 err.strerror) | |
152 self.threshold = watcher.Threshold(self.watcher) | |
153 self.registered = True | |
154 self.fileno = self.watcher.fileno | |
155 | |
156 self.repo.dirstate.__class__.inotifyserver = True | |
157 | |
158 self.tree = {} | |
159 self.statcache = {} | |
160 self.statustrees = dict([(s, {}) for s in self.statuskeys]) | |
161 | |
162 self.watches = 0 | |
163 self.last_event = None | |
164 | |
165 self.eventq = {} | |
166 self.deferred = 0 | |
167 | |
168 self.ds_info = self.dirstate_info() | |
169 self.scan() | |
170 | |
171 def event_time(self): | |
172 last = self.last_event | |
173 now = time.time() | |
174 self.last_event = now | |
175 | |
176 if last is None: | |
177 return 'start' | |
178 delta = now - last | |
179 if delta < 5: | |
180 return '+%.3f' % delta | |
181 if delta < 50: | |
182 return '+%.2f' % delta | |
183 return '+%.1f' % delta | |
184 | |
185 def dirstate_info(self): | |
186 try: | |
187 st = os.lstat(self.repo.join('dirstate')) | |
188 return st.st_mtime, st.st_ino | |
189 except OSError, err: | |
190 if err.errno != errno.ENOENT: | |
191 raise | |
192 return 0, 0 | |
193 | |
194 def add_watch(self, path, mask): | |
195 if not path: | |
196 return | |
197 if self.watcher.path(path) is None: | |
198 if self.ui.debugflag: | |
199 self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) | |
200 try: | |
201 self.watcher.add(path, mask) | |
202 self.watches += 1 | |
203 except OSError, err: | |
204 if err.errno in (errno.ENOENT, errno.ENOTDIR): | |
205 return | |
206 if err.errno != errno.ENOSPC: | |
207 raise | |
208 _explain_watch_limit(self.ui, self.repo, self.watches) | |
209 | |
210 def setup(self): | |
211 self.ui.note(_('watching directories under %r\n') % self.repo.root) | |
212 self.add_watch(self.repo.path, inotify.IN_DELETE) | |
213 self.check_dirstate() | |
214 | |
215 def wpath(self, evt): | |
216 path = evt.fullpath | |
217 if path == self.repo.root: | |
218 return '' | |
219 if path.startswith(self.wprefix): | |
220 return path[len(self.wprefix):] | |
221 raise 'wtf? ' + path | |
222 | |
223 def dir(self, tree, path): | |
224 if path: | |
225 for name in path.split('/'): | |
226 tree.setdefault(name, {}) | |
227 tree = tree[name] | |
228 return tree | |
229 | |
230 def lookup(self, path, tree): | |
231 if path: | |
232 try: | |
233 for name in path.split('/'): | |
234 tree = tree[name] | |
235 except KeyError: | |
236 return 'x' | |
237 except TypeError: | |
238 return 'd' | |
239 return tree | |
240 | |
241 def split(self, path): | |
242 c = path.rfind('/') | |
243 if c == -1: | |
244 return '', path | |
245 return path[:c], path[c+1:] | |
246 | |
247 def filestatus(self, fn, st): | |
248 try: | |
249 type_, mode, size, time = self.repo.dirstate._map[fn][:4] | |
250 except KeyError: | |
251 type_ = '?' | |
252 if type_ == 'n': | |
253 if not st: | |
254 return '!' | |
255 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
|
256 if size == -1: |
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
257 return 'l' |
6239 | 258 if size and (size != st_size or (mode ^ st_mode) & 0100): |
259 return 'm' | |
260 if time != int(st_mtime): | |
261 return 'l' | |
262 return 'n' | |
263 if type_ in 'ma' and not st: | |
264 return '!' | |
265 if type_ == '?' and self.repo.dirstate._ignore(fn): | |
266 return 'i' | |
267 return type_ | |
268 | |
7086
4033195d455b
inotify: avoid status getting out of sync
Matt Mackall <mpm@selenic.com>
parents:
7085
diff
changeset
|
269 def updatestatus(self, wfn, st=None, status=None): |
6239 | 270 if st: |
271 status = self.filestatus(wfn, st) | |
272 else: | |
273 self.statcache.pop(wfn, None) | |
274 root, fn = self.split(wfn) | |
275 d = self.dir(self.tree, root) | |
7086
4033195d455b
inotify: avoid status getting out of sync
Matt Mackall <mpm@selenic.com>
parents:
7085
diff
changeset
|
276 oldstatus = d.get(fn) |
6239 | 277 isdir = False |
278 if oldstatus: | |
279 try: | |
280 if not status: | |
281 if oldstatus in 'almn': | |
282 status = '!' | |
283 elif oldstatus == 'r': | |
284 status = 'r' | |
285 except TypeError: | |
286 # oldstatus may be a dict left behind by a deleted | |
287 # directory | |
288 isdir = True | |
289 else: | |
290 if oldstatus in self.statuskeys and oldstatus != status: | |
291 del self.dir(self.statustrees[oldstatus], root)[fn] | |
292 if self.ui.debugflag and oldstatus != status: | |
293 if isdir: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
294 self.ui.note(_('status: %r dir(%d) -> %s\n') % |
6239 | 295 (wfn, len(oldstatus), status)) |
296 else: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
297 self.ui.note(_('status: %r %s -> %s\n') % |
6239 | 298 (wfn, oldstatus, status)) |
299 if not isdir: | |
300 if status and status != 'i': | |
301 d[fn] = status | |
302 if status in self.statuskeys: | |
303 dd = self.dir(self.statustrees[status], root) | |
304 if oldstatus != status or fn not in dd: | |
305 dd[fn] = status | |
306 else: | |
307 d.pop(fn, None) | |
308 | |
309 def check_deleted(self, key): | |
310 # Files that had been deleted but were present in the dirstate | |
311 # may have vanished from the dirstate; we must clean them up. | |
312 nuke = [] | |
313 for wfn, ignore in self.walk(key, self.statustrees[key]): | |
314 if wfn not in self.repo.dirstate: | |
315 nuke.append(wfn) | |
316 for wfn in nuke: | |
317 root, fn = self.split(wfn) | |
318 del self.dir(self.statustrees[key], root)[fn] | |
319 del self.dir(self.tree, root)[fn] | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
320 |
6239 | 321 def scan(self, topdir=''): |
322 self.handle_timeout() | |
323 ds = self.repo.dirstate._map.copy() | |
324 self.add_watch(join(self.repo.root, topdir), self.mask) | |
325 for root, dirs, entries in walk(self.repo, topdir): | |
326 for d in dirs: | |
327 self.add_watch(join(root, d), self.mask) | |
328 wroot = root[len(self.wprefix):] | |
329 d = self.dir(self.tree, wroot) | |
330 for fn, kind in entries: | |
331 wfn = join(wroot, fn) | |
332 self.updatestatus(wfn, self.getstat(wfn)) | |
333 ds.pop(wfn, None) | |
334 wtopdir = topdir | |
335 if wtopdir and wtopdir[-1] != '/': | |
336 wtopdir += '/' | |
337 for wfn, state in ds.iteritems(): | |
338 if not wfn.startswith(wtopdir): | |
339 continue | |
340 status = state[0] | |
7220
60826e071ce2
inotify: avoid passing potentially stale stat to update
Matt Mackall <mpm@selenic.com>
parents:
7086
diff
changeset
|
341 st = self.stat(wfn) |
6239 | 342 if status == 'r' and not st: |
343 self.updatestatus(wfn, st, status=status) | |
344 else: | |
7086
4033195d455b
inotify: avoid status getting out of sync
Matt Mackall <mpm@selenic.com>
parents:
7085
diff
changeset
|
345 self.updatestatus(wfn, st) |
6239 | 346 self.check_deleted('!') |
347 self.check_deleted('r') | |
348 | |
349 def check_dirstate(self): | |
350 ds_info = self.dirstate_info() | |
351 if ds_info == self.ds_info: | |
352 return | |
353 self.ds_info = ds_info | |
354 if not self.ui.debugflag: | |
355 self.last_event = None | |
356 self.ui.note(_('%s dirstate reload\n') % self.event_time()) | |
357 self.repo.dirstate.invalidate() | |
358 self.scan() | |
359 self.ui.note(_('%s end dirstate reload\n') % self.event_time()) | |
360 | |
361 def walk(self, states, tree, prefix=''): | |
362 # This is the "inner loop" when talking to the client. | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
363 |
6239 | 364 for name, val in tree.iteritems(): |
365 path = join(prefix, name) | |
366 try: | |
367 if val in states: | |
368 yield path, val | |
369 except TypeError: | |
370 for p in self.walk(states, val, path): | |
371 yield p | |
372 | |
373 def update_hgignore(self): | |
374 # An update of the ignore file can potentially change the | |
375 # states of all unknown and ignored files. | |
376 | |
377 # XXX If the user has other ignore files outside the repo, or | |
378 # changes their list of ignore files at run time, we'll | |
379 # potentially never see changes to them. We could get the | |
380 # client to report to us what ignore data they're using. | |
381 # But it's easier to do nothing than to open that can of | |
382 # worms. | |
383 | |
7085
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
384 if '_ignore' in self.repo.dirstate.__dict__: |
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
385 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
|
386 self.ui.note(_('rescanning due to .hgignore change\n')) |
6239 | 387 self.scan() |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
388 |
6239 | 389 def getstat(self, wpath): |
390 try: | |
391 return self.statcache[wpath] | |
392 except KeyError: | |
393 try: | |
394 return self.stat(wpath) | |
395 except OSError, err: | |
396 if err.errno != errno.ENOENT: | |
397 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
398 |
6239 | 399 def stat(self, wpath): |
400 try: | |
401 st = os.lstat(join(self.wprefix, wpath)) | |
402 ret = st.st_mode, st.st_size, st.st_mtime | |
403 self.statcache[wpath] = ret | |
404 return ret | |
405 except OSError, err: | |
406 self.statcache.pop(wpath, None) | |
407 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
408 |
6239 | 409 def created(self, wpath): |
410 if wpath == '.hgignore': | |
411 self.update_hgignore() | |
412 try: | |
413 st = self.stat(wpath) | |
414 if stat.S_ISREG(st[0]): | |
415 self.updatestatus(wpath, st) | |
416 except OSError, err: | |
417 pass | |
418 | |
419 def modified(self, wpath): | |
420 if wpath == '.hgignore': | |
421 self.update_hgignore() | |
422 try: | |
423 st = self.stat(wpath) | |
424 if stat.S_ISREG(st[0]): | |
425 if self.repo.dirstate[wpath] in 'lmn': | |
426 self.updatestatus(wpath, st) | |
427 except OSError: | |
428 pass | |
429 | |
430 def deleted(self, wpath): | |
431 if wpath == '.hgignore': | |
432 self.update_hgignore() | |
433 elif wpath.startswith('.hg/'): | |
434 if wpath == '.hg/wlock': | |
435 self.check_dirstate() | |
436 return | |
437 | |
438 self.updatestatus(wpath, None) | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
439 |
6239 | 440 def schedule_work(self, wpath, evt): |
441 self.eventq.setdefault(wpath, []) | |
442 prev = self.eventq[wpath] | |
443 try: | |
444 if prev and evt == 'm' and prev[-1] in 'cm': | |
445 return | |
446 self.eventq[wpath].append(evt) | |
447 finally: | |
448 self.deferred += 1 | |
449 self.timeout = 250 | |
450 | |
451 def deferred_event(self, wpath, evt): | |
452 if evt == 'c': | |
453 self.created(wpath) | |
454 elif evt == 'm': | |
455 self.modified(wpath) | |
456 elif evt == 'd': | |
457 self.deleted(wpath) | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
458 |
6239 | 459 def process_create(self, wpath, evt): |
460 if self.ui.debugflag: | |
461 self.ui.note(_('%s event: created %s\n') % | |
462 (self.event_time(), wpath)) | |
463 | |
464 if evt.mask & inotify.IN_ISDIR: | |
465 self.scan(wpath) | |
466 else: | |
467 self.schedule_work(wpath, 'c') | |
468 | |
469 def process_delete(self, wpath, evt): | |
470 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
471 self.ui.note(_('%s event: deleted %s\n') % |
6239 | 472 (self.event_time(), wpath)) |
473 | |
474 if evt.mask & inotify.IN_ISDIR: | |
475 self.scan(wpath) | |
476 else: | |
477 self.schedule_work(wpath, 'd') | |
478 | |
479 def process_modify(self, wpath, evt): | |
480 if self.ui.debugflag: | |
481 self.ui.note(_('%s event: modified %s\n') % | |
482 (self.event_time(), wpath)) | |
483 | |
484 if not (evt.mask & inotify.IN_ISDIR): | |
485 self.schedule_work(wpath, 'm') | |
486 | |
487 def process_unmount(self, evt): | |
488 self.ui.warn(_('filesystem containing %s was unmounted\n') % | |
489 evt.fullpath) | |
490 sys.exit(0) | |
491 | |
492 def handle_event(self, fd, event): | |
493 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
494 self.ui.note(_('%s readable: %d bytes\n') % |
6239 | 495 (self.event_time(), self.threshold.readable())) |
496 if not self.threshold(): | |
497 if self.registered: | |
498 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
499 self.ui.note(_('%s below threshold - unhooking\n') % |
6239 | 500 (self.event_time())) |
501 self.master.poll.unregister(fd) | |
502 self.registered = False | |
503 self.timeout = 250 | |
504 else: | |
505 self.read_events() | |
506 | |
507 def read_events(self, bufsize=None): | |
508 events = self.watcher.read(bufsize) | |
509 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
510 self.ui.note(_('%s reading %d events\n') % |
6239 | 511 (self.event_time(), len(events))) |
512 for evt in events: | |
513 wpath = self.wpath(evt) | |
514 if evt.mask & inotify.IN_UNMOUNT: | |
515 self.process_unmount(wpath, evt) | |
516 elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): | |
517 self.process_modify(wpath, evt) | |
518 elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | | |
519 inotify.IN_MOVED_FROM): | |
520 self.process_delete(wpath, evt) | |
521 elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): | |
522 self.process_create(wpath, evt) | |
523 | |
524 def handle_timeout(self): | |
525 if not self.registered: | |
526 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
527 self.ui.note(_('%s hooking back up with %d bytes readable\n') % |
6239 | 528 (self.event_time(), self.threshold.readable())) |
529 self.read_events(0) | |
530 self.master.poll.register(self, select.POLLIN) | |
531 self.registered = True | |
532 | |
533 if self.eventq: | |
534 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
535 self.ui.note(_('%s processing %d deferred events as %d\n') % |
6239 | 536 (self.event_time(), self.deferred, |
537 len(self.eventq))) | |
6762 | 538 for wpath, evts in util.sort(self.eventq.items()): |
6239 | 539 for evt in evts: |
540 self.deferred_event(wpath, evt) | |
541 self.eventq.clear() | |
542 self.deferred = 0 | |
543 self.timeout = None | |
544 | |
545 def shutdown(self): | |
546 self.watcher.close() | |
547 | |
548 class Server(object): | |
549 poll_events = select.POLLIN | |
550 | |
551 def __init__(self, ui, repo, watcher, timeout): | |
552 self.ui = ui | |
553 self.repo = repo | |
554 self.watcher = watcher | |
555 self.timeout = timeout | |
556 self.sock = socket.socket(socket.AF_UNIX) | |
557 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
|
558 self.realsockpath = None |
6239 | 559 try: |
560 self.sock.bind(self.sockpath) | |
561 except socket.error, err: | |
562 if err[0] == errno.EADDRINUSE: | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
563 raise AlreadyStartedException(_('could not start server: %s') |
6239 | 564 % err[1]) |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
565 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
|
566 tempdir = tempfile.mkdtemp(prefix="hg-inotify-") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
567 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
|
568 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
569 self.sock.bind(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
570 os.symlink(self.realsockpath, self.sockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
571 except (OSError, socket.error), inst: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
572 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
573 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
574 except: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
575 pass |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
576 os.rmdir(tempdir) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
577 if inst.errno == errno.EEXIST: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
578 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
|
579 % inst.strerror) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
580 raise |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
581 else: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
582 raise |
6239 | 583 self.sock.listen(5) |
584 self.fileno = self.sock.fileno | |
585 | |
586 def handle_timeout(self): | |
587 pass | |
588 | |
589 def handle_event(self, fd, event): | |
590 sock, addr = self.sock.accept() | |
591 | |
592 cs = common.recvcs(sock) | |
593 version = ord(cs.read(1)) | |
594 | |
595 sock.sendall(chr(common.version)) | |
596 | |
597 if version != common.version: | |
598 self.ui.warn(_('received query from incompatible client ' | |
599 'version %d\n') % version) | |
600 return | |
601 | |
602 names = cs.read().split('\0') | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
603 |
6239 | 604 states = names.pop() |
605 | |
606 self.ui.note(_('answering query for %r\n') % states) | |
607 | |
608 if self.watcher.timeout: | |
609 # We got a query while a rescan is pending. Make sure we | |
610 # rescan before responding, or we could give back a wrong | |
611 # answer. | |
612 self.watcher.handle_timeout() | |
613 | |
614 if not names: | |
615 def genresult(states, tree): | |
616 for fn, state in self.watcher.walk(states, tree): | |
617 yield fn | |
618 else: | |
619 def genresult(states, tree): | |
620 for fn in names: | |
621 l = self.watcher.lookup(fn, tree) | |
622 try: | |
623 if l in states: | |
624 yield fn | |
625 except TypeError: | |
626 for f, s in self.watcher.walk(states, l, fn): | |
627 yield f | |
628 | |
629 results = ['\0'.join(r) for r in [ | |
630 genresult('l', self.watcher.statustrees['l']), | |
631 genresult('m', self.watcher.statustrees['m']), | |
632 genresult('a', self.watcher.statustrees['a']), | |
633 genresult('r', self.watcher.statustrees['r']), | |
634 genresult('!', self.watcher.statustrees['!']), | |
635 '?' in states and genresult('?', self.watcher.statustrees['?']) or [], | |
636 [], | |
637 'c' in states and genresult('n', self.watcher.tree) or [], | |
638 ]] | |
639 | |
640 try: | |
641 try: | |
642 sock.sendall(struct.pack(common.resphdrfmt, | |
643 *map(len, results))) | |
644 sock.sendall(''.join(results)) | |
645 finally: | |
646 sock.shutdown(socket.SHUT_WR) | |
647 except socket.error, err: | |
648 if err[0] != errno.EPIPE: | |
649 raise | |
650 | |
651 def shutdown(self): | |
652 self.sock.close() | |
653 try: | |
654 os.unlink(self.sockpath) | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
655 if self.realsockpath: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
656 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
657 os.rmdir(os.path.dirname(self.realsockpath)) |
6239 | 658 except OSError, err: |
659 if err.errno != errno.ENOENT: | |
660 raise | |
661 | |
662 class Master(object): | |
663 def __init__(self, ui, repo, timeout=None): | |
664 self.ui = ui | |
665 self.repo = repo | |
666 self.poll = select.poll() | |
667 self.watcher = Watcher(ui, repo, self) | |
668 self.server = Server(ui, repo, self.watcher, timeout) | |
669 self.table = {} | |
670 for obj in (self.watcher, self.server): | |
671 fd = obj.fileno() | |
672 self.table[fd] = obj | |
673 self.poll.register(fd, obj.poll_events) | |
674 | |
675 def register(self, fd, mask): | |
676 self.poll.register(fd, mask) | |
677 | |
678 def shutdown(self): | |
679 for obj in self.table.itervalues(): | |
680 obj.shutdown() | |
681 | |
682 def run(self): | |
683 self.watcher.setup() | |
684 self.ui.note(_('finished setup\n')) | |
685 if os.getenv('TIME_STARTUP'): | |
686 sys.exit(0) | |
687 while True: | |
688 timeout = None | |
689 timeobj = None | |
690 for obj in self.table.itervalues(): | |
691 if obj.timeout is not None and (timeout is None or obj.timeout < timeout): | |
692 timeout, timeobj = obj.timeout, obj | |
693 try: | |
694 if self.ui.debugflag: | |
695 if timeout is None: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
696 self.ui.note(_('polling: no timeout\n')) |
6239 | 697 else: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
698 self.ui.note(_('polling: %sms timeout\n') % timeout) |
6239 | 699 events = self.poll.poll(timeout) |
700 except select.error, err: | |
701 if err[0] == errno.EINTR: | |
702 continue | |
703 raise | |
704 if events: | |
705 for fd, event in events: | |
706 self.table[fd].handle_event(fd, event) | |
707 elif timeobj: | |
708 timeobj.handle_timeout() | |
709 | |
710 def start(ui, repo): | |
711 m = Master(ui, repo) | |
712 sys.stdout.flush() | |
713 sys.stderr.flush() | |
714 | |
715 pid = os.fork() | |
716 if pid: | |
717 return pid | |
718 | |
719 os.setsid() | |
720 | |
721 fd = os.open('/dev/null', os.O_RDONLY) | |
722 os.dup2(fd, 0) | |
723 if fd > 0: | |
724 os.close(fd) | |
725 | |
726 fd = os.open(ui.config('inotify', 'log', '/dev/null'), | |
727 os.O_RDWR | os.O_CREAT | os.O_TRUNC) | |
728 os.dup2(fd, 1) | |
729 os.dup2(fd, 2) | |
730 if fd > 2: | |
731 os.close(fd) | |
732 | |
733 try: | |
734 m.run() | |
735 finally: | |
736 m.shutdown() | |
737 os._exit(0) |