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