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