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