Mercurial > public > mercurial-scm > hg
annotate hgext/inotify/server.py @ 7350:c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
---|---|
date | Tue, 11 Nov 2008 23:16:59 +0100 |
parents | fd4bf5269733 |
children | 5ab0abf27dd9 |
rev | line source |
---|---|
6239 | 1 # server.py - inotify status server |
2 # | |
3 # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> | |
4 # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> | |
5 # | |
6 # This software may be used and distributed according to the terms | |
7 # of the GNU General Public License, incorporated herein by reference. | |
8 | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
9 from mercurial.i18n import _ |
6239 | 10 from mercurial import osutil, ui, util |
11 import common | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
12 import errno, os, select, socket, stat, struct, sys, tempfile, time |
6239 | 13 |
14 try: | |
6994
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
15 import linux as inotify |
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
16 from linux import watcher |
6239 | 17 except ImportError: |
18 raise | |
19 | |
20 class AlreadyStartedException(Exception): pass | |
21 | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
22 class statusdict(dict): |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
23 status = None |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
24 |
6239 | 25 def join(a, b): |
26 if a: | |
27 if a[-1] == '/': | |
28 return a + b | |
29 return a + '/' + b | |
30 return b | |
31 | |
32 walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) | |
33 | |
34 def walkrepodirs(repo): | |
35 '''Iterate over all subdirectories of this repo. | |
36 Exclude the .hg directory, any nested repos, and ignored dirs.''' | |
37 rootslash = repo.root + os.sep | |
38 def walkit(dirname, top): | |
39 hginside = False | |
40 try: | |
41 for name, kind in osutil.listdir(rootslash + dirname): | |
42 if kind == stat.S_IFDIR: | |
43 if name == '.hg': | |
44 hginside = True | |
45 if not top: break | |
46 else: | |
47 d = join(dirname, name) | |
48 if repo.dirstate._ignore(d): | |
49 continue | |
50 for subdir, hginsub in walkit(d, False): | |
51 if not hginsub: | |
52 yield subdir, False | |
53 except OSError, err: | |
54 if err.errno not in walk_ignored_errors: | |
55 raise | |
56 yield rootslash + dirname, hginside | |
57 for dirname, hginside in walkit('', True): | |
58 yield dirname | |
59 | |
60 def walk(repo, root): | |
61 '''Like os.walk, but only yields regular files.''' | |
62 | |
63 # This function is critical to performance during startup. | |
64 | |
65 reporoot = root == '' | |
66 rootslash = repo.root + os.sep | |
67 | |
68 def walkit(root, reporoot): | |
69 files, dirs = [], [] | |
70 hginside = False | |
71 | |
72 try: | |
73 fullpath = rootslash + root | |
74 for name, kind in osutil.listdir(fullpath): | |
75 if kind == stat.S_IFDIR: | |
76 if name == '.hg': | |
77 hginside = True | |
78 if reporoot: | |
79 continue | |
80 else: | |
81 break | |
82 dirs.append(name) | |
83 elif kind in (stat.S_IFREG, stat.S_IFLNK): | |
84 path = join(root, name) | |
85 files.append((name, kind)) | |
86 | |
87 yield hginside, fullpath, dirs, files | |
88 | |
89 for subdir in dirs: | |
90 path = join(root, subdir) | |
91 if repo.dirstate._ignore(path): | |
92 continue | |
93 for result in walkit(path, False): | |
94 if not result[0]: | |
95 yield result | |
96 except OSError, err: | |
97 if err.errno not in walk_ignored_errors: | |
98 raise | |
99 for result in walkit(root, reporoot): | |
100 yield result[1:] | |
101 | |
102 def _explain_watch_limit(ui, repo, count): | |
103 path = '/proc/sys/fs/inotify/max_user_watches' | |
104 try: | |
105 limit = int(file(path).read()) | |
106 except IOError, err: | |
107 if err.errno != errno.ENOENT: | |
108 raise | |
109 raise util.Abort(_('this system does not seem to ' | |
110 'support inotify')) | |
111 ui.warn(_('*** the current per-user limit on the number ' | |
112 'of inotify watches is %s\n') % limit) | |
113 ui.warn(_('*** this limit is too low to watch every ' | |
114 'directory in this repository\n')) | |
115 ui.warn(_('*** counting directories: ')) | |
116 ndirs = len(list(walkrepodirs(repo))) | |
117 ui.warn(_('found %d\n') % ndirs) | |
118 newlimit = min(limit, 1024) | |
119 while newlimit < ((limit + ndirs) * 1.1): | |
120 newlimit *= 2 | |
121 ui.warn(_('*** to raise the limit from %d to %d (run as root):\n') % | |
122 (limit, newlimit)) | |
123 ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) | |
124 raise util.Abort(_('cannot watch %s until inotify watch limit is raised') | |
125 % repo.root) | |
126 | |
127 class Watcher(object): | |
128 poll_events = select.POLLIN | |
129 statuskeys = 'almr!?' | |
130 | |
131 def __init__(self, ui, repo, master): | |
132 self.ui = ui | |
133 self.repo = repo | |
134 self.wprefix = self.repo.wjoin('') | |
135 self.timeout = None | |
136 self.master = master | |
137 self.mask = ( | |
138 inotify.IN_ATTRIB | | |
139 inotify.IN_CREATE | | |
140 inotify.IN_DELETE | | |
141 inotify.IN_DELETE_SELF | | |
142 inotify.IN_MODIFY | | |
143 inotify.IN_MOVED_FROM | | |
144 inotify.IN_MOVED_TO | | |
145 inotify.IN_MOVE_SELF | | |
146 inotify.IN_ONLYDIR | | |
147 inotify.IN_UNMOUNT | | |
148 0) | |
149 try: | |
150 self.watcher = watcher.Watcher() | |
151 except OSError, err: | |
152 raise util.Abort(_('inotify service not available: %s') % | |
153 err.strerror) | |
154 self.threshold = watcher.Threshold(self.watcher) | |
155 self.registered = True | |
156 self.fileno = self.watcher.fileno | |
157 | |
158 self.repo.dirstate.__class__.inotifyserver = True | |
159 | |
160 self.tree = {} | |
161 self.statcache = {} | |
162 self.statustrees = dict([(s, {}) for s in self.statuskeys]) | |
163 | |
164 self.watches = 0 | |
165 self.last_event = None | |
166 | |
167 self.eventq = {} | |
168 self.deferred = 0 | |
169 | |
170 self.ds_info = self.dirstate_info() | |
171 self.scan() | |
172 | |
173 def event_time(self): | |
174 last = self.last_event | |
175 now = time.time() | |
176 self.last_event = now | |
177 | |
178 if last is None: | |
179 return 'start' | |
180 delta = now - last | |
181 if delta < 5: | |
182 return '+%.3f' % delta | |
183 if delta < 50: | |
184 return '+%.2f' % delta | |
185 return '+%.1f' % delta | |
186 | |
187 def dirstate_info(self): | |
188 try: | |
189 st = os.lstat(self.repo.join('dirstate')) | |
190 return st.st_mtime, st.st_ino | |
191 except OSError, err: | |
192 if err.errno != errno.ENOENT: | |
193 raise | |
194 return 0, 0 | |
195 | |
196 def add_watch(self, path, mask): | |
197 if not path: | |
198 return | |
199 if self.watcher.path(path) is None: | |
200 if self.ui.debugflag: | |
201 self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) | |
202 try: | |
203 self.watcher.add(path, mask) | |
204 self.watches += 1 | |
205 except OSError, err: | |
206 if err.errno in (errno.ENOENT, errno.ENOTDIR): | |
207 return | |
208 if err.errno != errno.ENOSPC: | |
209 raise | |
210 _explain_watch_limit(self.ui, self.repo, self.watches) | |
211 | |
212 def setup(self): | |
213 self.ui.note(_('watching directories under %r\n') % self.repo.root) | |
214 self.add_watch(self.repo.path, inotify.IN_DELETE) | |
215 self.check_dirstate() | |
216 | |
217 def wpath(self, evt): | |
218 path = evt.fullpath | |
219 if path == self.repo.root: | |
220 return '' | |
221 if path.startswith(self.wprefix): | |
222 return path[len(self.wprefix):] | |
223 raise 'wtf? ' + path | |
224 | |
225 def dir(self, tree, path): | |
226 if path: | |
227 for name in path.split('/'): | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
228 tree.setdefault(name, statusdict()) |
6239 | 229 tree = tree[name] |
230 return tree | |
231 | |
232 def lookup(self, path, tree): | |
233 if path: | |
234 try: | |
235 for name in path.split('/'): | |
236 tree = tree[name] | |
237 except KeyError: | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
238 tree = statusdict() |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
239 tree.status = 'x' |
6239 | 240 except TypeError: |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
241 tree = statusdict() |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
242 tree.status = 'd' |
6239 | 243 return tree |
244 | |
245 def split(self, path): | |
246 c = path.rfind('/') | |
247 if c == -1: | |
248 return '', path | |
249 return path[:c], path[c+1:] | |
250 | |
251 def filestatus(self, fn, st): | |
252 try: | |
253 type_, mode, size, time = self.repo.dirstate._map[fn][:4] | |
254 except KeyError: | |
255 type_ = '?' | |
256 if type_ == 'n': | |
257 if not st: | |
258 return '!' | |
259 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
|
260 if size == -1: |
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
261 return 'l' |
6239 | 262 if size and (size != st_size or (mode ^ st_mode) & 0100): |
263 return 'm' | |
264 if time != int(st_mtime): | |
265 return 'l' | |
266 return 'n' | |
267 if type_ in 'ma' and not st: | |
268 return '!' | |
269 if type_ == '?' and self.repo.dirstate._ignore(fn): | |
270 return 'i' | |
271 return type_ | |
272 | |
7086
4033195d455b
inotify: avoid status getting out of sync
Matt Mackall <mpm@selenic.com>
parents:
7085
diff
changeset
|
273 def updatestatus(self, wfn, st=None, status=None): |
6239 | 274 if st: |
275 status = self.filestatus(wfn, st) | |
276 else: | |
277 self.statcache.pop(wfn, None) | |
278 root, fn = self.split(wfn) | |
279 d = self.dir(self.tree, root) | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
280 oldstatus = d.setdefault(fn, statusdict()).status |
6239 | 281 isdir = False |
282 if oldstatus: | |
283 try: | |
284 if not status: | |
285 if oldstatus in 'almn': | |
286 status = '!' | |
287 elif oldstatus == 'r': | |
288 status = 'r' | |
289 except TypeError: | |
290 # oldstatus may be a dict left behind by a deleted | |
291 # directory | |
292 isdir = True | |
293 else: | |
294 if oldstatus in self.statuskeys and oldstatus != status: | |
295 del self.dir(self.statustrees[oldstatus], root)[fn] | |
296 if self.ui.debugflag and oldstatus != status: | |
297 if isdir: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
298 self.ui.note(_('status: %r dir(%d) -> %s\n') % |
6239 | 299 (wfn, len(oldstatus), status)) |
300 else: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
301 self.ui.note(_('status: %r %s -> %s\n') % |
6239 | 302 (wfn, oldstatus, status)) |
303 if not isdir: | |
304 if status and status != 'i': | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
305 d[fn].status = status |
6239 | 306 if status in self.statuskeys: |
307 dd = self.dir(self.statustrees[status], root) | |
308 if oldstatus != status or fn not in dd: | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
309 dd.setdefault(fn, statusdict()).status = status |
6239 | 310 else: |
311 d.pop(fn, None) | |
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) | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
371 if val.status is not None and val.status in states: |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
372 yield path, val |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
373 for p in self.walk(states, val, path): |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
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) | |
479 else: | |
480 self.schedule_work(wpath, 'd') | |
481 | |
482 def process_modify(self, wpath, evt): | |
483 if self.ui.debugflag: | |
484 self.ui.note(_('%s event: modified %s\n') % | |
485 (self.event_time(), wpath)) | |
486 | |
487 if not (evt.mask & inotify.IN_ISDIR): | |
488 self.schedule_work(wpath, 'm') | |
489 | |
490 def process_unmount(self, evt): | |
491 self.ui.warn(_('filesystem containing %s was unmounted\n') % | |
492 evt.fullpath) | |
493 sys.exit(0) | |
494 | |
495 def handle_event(self, fd, event): | |
496 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
497 self.ui.note(_('%s readable: %d bytes\n') % |
6239 | 498 (self.event_time(), self.threshold.readable())) |
499 if not self.threshold(): | |
500 if self.registered: | |
501 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
502 self.ui.note(_('%s below threshold - unhooking\n') % |
6239 | 503 (self.event_time())) |
504 self.master.poll.unregister(fd) | |
505 self.registered = False | |
506 self.timeout = 250 | |
507 else: | |
508 self.read_events() | |
509 | |
510 def read_events(self, bufsize=None): | |
511 events = self.watcher.read(bufsize) | |
512 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
513 self.ui.note(_('%s reading %d events\n') % |
6239 | 514 (self.event_time(), len(events))) |
515 for evt in events: | |
516 wpath = self.wpath(evt) | |
517 if evt.mask & inotify.IN_UNMOUNT: | |
518 self.process_unmount(wpath, evt) | |
519 elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): | |
520 self.process_modify(wpath, evt) | |
521 elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | | |
522 inotify.IN_MOVED_FROM): | |
523 self.process_delete(wpath, evt) | |
524 elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): | |
525 self.process_create(wpath, evt) | |
526 | |
527 def handle_timeout(self): | |
528 if not self.registered: | |
529 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
530 self.ui.note(_('%s hooking back up with %d bytes readable\n') % |
6239 | 531 (self.event_time(), self.threshold.readable())) |
532 self.read_events(0) | |
533 self.master.poll.register(self, select.POLLIN) | |
534 self.registered = True | |
535 | |
536 if self.eventq: | |
537 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
538 self.ui.note(_('%s processing %d deferred events as %d\n') % |
6239 | 539 (self.event_time(), self.deferred, |
540 len(self.eventq))) | |
6762 | 541 for wpath, evts in util.sort(self.eventq.items()): |
6239 | 542 for evt in evts: |
543 self.deferred_event(wpath, evt) | |
544 self.eventq.clear() | |
545 self.deferred = 0 | |
546 self.timeout = None | |
547 | |
548 def shutdown(self): | |
549 self.watcher.close() | |
550 | |
551 class Server(object): | |
552 poll_events = select.POLLIN | |
553 | |
554 def __init__(self, ui, repo, watcher, timeout): | |
555 self.ui = ui | |
556 self.repo = repo | |
557 self.watcher = watcher | |
558 self.timeout = timeout | |
559 self.sock = socket.socket(socket.AF_UNIX) | |
560 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
|
561 self.realsockpath = None |
6239 | 562 try: |
563 self.sock.bind(self.sockpath) | |
564 except socket.error, err: | |
565 if err[0] == errno.EADDRINUSE: | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
566 raise AlreadyStartedException(_('could not start server: %s') |
6239 | 567 % err[1]) |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
568 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
|
569 tempdir = tempfile.mkdtemp(prefix="hg-inotify-") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
570 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
|
571 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
572 self.sock.bind(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
573 os.symlink(self.realsockpath, self.sockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
574 except (OSError, socket.error), inst: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
575 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
576 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
577 except: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
578 pass |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
579 os.rmdir(tempdir) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
580 if inst.errno == errno.EEXIST: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
581 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
|
582 % inst.strerror) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
583 raise |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
584 else: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
585 raise |
6239 | 586 self.sock.listen(5) |
587 self.fileno = self.sock.fileno | |
588 | |
589 def handle_timeout(self): | |
590 pass | |
591 | |
592 def handle_event(self, fd, event): | |
593 sock, addr = self.sock.accept() | |
594 | |
595 cs = common.recvcs(sock) | |
596 version = ord(cs.read(1)) | |
597 | |
598 sock.sendall(chr(common.version)) | |
599 | |
600 if version != common.version: | |
601 self.ui.warn(_('received query from incompatible client ' | |
602 'version %d\n') % version) | |
603 return | |
604 | |
605 names = cs.read().split('\0') | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
606 |
6239 | 607 states = names.pop() |
608 | |
609 self.ui.note(_('answering query for %r\n') % states) | |
610 | |
611 if self.watcher.timeout: | |
612 # We got a query while a rescan is pending. Make sure we | |
613 # rescan before responding, or we could give back a wrong | |
614 # answer. | |
615 self.watcher.handle_timeout() | |
616 | |
617 if not names: | |
618 def genresult(states, tree): | |
619 for fn, state in self.watcher.walk(states, tree): | |
620 yield fn | |
621 else: | |
622 def genresult(states, tree): | |
623 for fn in names: | |
624 l = self.watcher.lookup(fn, tree) | |
7350
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
625 if l.status is not None and l.status in states: |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
626 yield fn |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
diff
changeset
|
627 for f, s in self.watcher.walk(states, l, fn): |
c5dbe86b0fee
inotify: fix replacing a folder with a file (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7329
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): | |
712 m = Master(ui, repo) | |
713 sys.stdout.flush() | |
714 sys.stderr.flush() | |
715 | |
716 pid = os.fork() | |
717 if pid: | |
718 return pid | |
719 | |
720 os.setsid() | |
721 | |
722 fd = os.open('/dev/null', os.O_RDONLY) | |
723 os.dup2(fd, 0) | |
724 if fd > 0: | |
725 os.close(fd) | |
726 | |
727 fd = os.open(ui.config('inotify', 'log', '/dev/null'), | |
728 os.O_RDWR | os.O_CREAT | os.O_TRUNC) | |
729 os.dup2(fd, 1) | |
730 os.dup2(fd, 2) | |
731 if fd > 2: | |
732 os.close(fd) | |
733 | |
734 try: | |
735 m.run() | |
736 finally: | |
737 m.shutdown() | |
738 os._exit(0) |