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